CodeQL for Java
.. rst-class:: agenda
- Abstract syntax trees
- Database representation
- Program elements
- AST CodeQL classes
- The CodeQL class
Elementrepresents program elements with a name. - This includes: packages (
Package), compilation units (CompilationUnit), types (Type), methods (Method), constructors (Constructor), and variables (Variable). - It is often convenient to refer to an element that might either be a method or a constructor; the class
Callable, which is a common superclass ofMethodandConstructor, can be used for this purpose.
There are two primary AST CodeQL classes, used within Callables:
Expr: expressions such as assignments, variable references, function calls, ...Stmt: statements such as conditionals, loops, try statements, ...
Operations are provided for exploring the AST:
Expr.getAChildExprreturns a sub-expression of a given expression.Stmt.getAChildreturns a statement or expression that is nested directly inside a given statement.Expr.getParentandStmt.getParentreturn the parent node of an AST node.
The database also includes information about the types used in a program:
PrimitiveTyperepresents a primitive type, that is, one ofboolean,byte,char,double,float,int,long,short. CodeQL also classifiesvoidand<nulltype>(the type of thenullliteral) as primitive types.RefTyperepresents a reference type; it has several subclasses:Classrepresents a Java class.Interfacerepresents a Java interface.EnumTyperepresents a Java enum type.Arrayrepresents a Java array type.
Variable represents program variables, including locally scoped variables (LocalScopeVariable), fields (Fields), and parameters (Parameters):
string Variable.getName()Type Variable.getType()
Access represents references to declared entities such as methods (MethodAccess) and variables (VariableAccess), including fields (FieldAccess).
Declaration Access.getTarget()
VariableDeclarationEntry represents declarations or definitions of a variable.
Variable VariableDeclarationEntry.getVariable()
Callables are represented by the Callable CodeQL class.
Calls to callables are modeled by the CodeQL class Call and its subclasses:
Call.getCallee()gets the declared target of the callCall.getAReference()gets a call to this function
Typically, callables are identified by name:
string Callable.getName()string Callable.getQualifiedName()
.. rst-class:: java-expression-ast