Skip to content

Help: Query for Java methods with and without bodies using CodeQL and chaining queries #14626

Answered by aibaars
oriana19993926782 asked this question in Q&A
Discussion options

You must be logged in to vote

With CodeQL you cannot select things that do not exist, and CodeQL does not have a NULL value like SQL does. You could run two queries, one selecting all methods that have a body and another selecting all methods that do not have a body where not exists(m.getBody()).

Alternatively, you could select a dummy value for methods that do not have a body (for example an empty Location):

import java

class EmptyLocation extends Location {
  EmptyLocation() { this.hasLocationInfo("", 0, 0, 0, 0) }
}

from Method m, Location body
where
  body = m.getBody().getLocation()
  or
  not exists(m.getBody()) and body instanceof EmptyLocation and m.fromSource()
select m, body

Note that I used m.fromSource()

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by oriana19993926782
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants