Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions java/ql/src/semmle/code/java/PrintAst.qll
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ private newtype TPrintAstNode =
TJavadocNode(Javadoc jd) { exists(Documentable d | d.getJavadoc() = jd | shouldPrint(d, _)) } or
TJavadocElementNode(JavadocElement jd) {
exists(Documentable d | d.getJavadoc() = jd.getParent*() | shouldPrint(d, _))
} or
TImportsNode(CompilationUnit cu) {
shouldPrint(cu, _) and exists(Import i | i.getCompilationUnit() = cu)
}

/**
Expand Down Expand Up @@ -395,13 +398,12 @@ final class CompilationUnitNode extends ElementNode {

CompilationUnitNode() { cu = element }

private Element getADeclaration() {
cu.hasChildElement(result)
or
result.(Import).getCompilationUnit() = cu
}
private Element getADeclaration() { cu.hasChildElement(result) }

override PrintAstNode getChild(int childIndex) {
childIndex = -1 and
result.(ImportsNode).getCompilationUnit() = cu
or
childIndex >= 0 and
result.(ElementNode).getElement() =
rank[childIndex](Element e, string file, int line, int column |
Expand Down Expand Up @@ -622,6 +624,32 @@ final class JavadocElementNode extends PrintAstNode, TJavadocElementNode {
JavadocElement getJavadocElement() { result = jd }
}

/**
* A node representing the `Import`s of a `CompilationUnit`.
* Only rendered if there is at least one import.
*/
final class ImportsNode extends PrintAstNode, TImportsNode {
CompilationUnit cu;

ImportsNode() { this = TImportsNode(cu) }

override string toString() { result = "(Imports)" }

override ElementNode getChild(int childIndex) {
result.getElement() =
rank[childIndex](Import im, string file, int line, int column |
im.getCompilationUnit() = cu and locationSortKeys(im, file, line, column)
|
im order by file, line, column
)
}

/**
* Gets the underlying CompilationUnit.
*/
CompilationUnit getCompilationUnit() { result = cu }
}

/** Holds if `node` belongs to the output tree, and its property `key` has the given `value`. */
query predicate nodes(PrintAstNode node, string key, string value) { value = node.getProperty(key) }

Expand Down
5 changes: 3 additions & 2 deletions java/ql/test/library-tests/collections/PrintAst.expected
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
collections/Test.java:
# 0| [CompilationUnit] Test
# 3| 1: [ImportOnDemandFromPackage] import java.util.*
# 5| 2: [Class] Test
#-----| -1: (Imports)
# 3| 1: [ImportOnDemandFromPackage] import java.util.*
# 5| 1: [Class] Test
# 6| 3: [FieldDeclaration] Map<String,Integer> m, ...;
# 6| -1: [TypeAccess] Map<String,Integer>
# 6| 0: [TypeAccess] String
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
Example.java:
# 0| [CompilationUnit] Example
# 1| 1: [ImportType] import Set
# 2| 2: [ImportType] import List
# 4| 3: [Interface] Example
#-----| -1: (Imports)
# 1| 1: [ImportType] import Set
# 2| 2: [ImportType] import List
# 4| 1: [Interface] Example
#-----| -2: (Generic Parameters)
# 4| 0: [TypeVariable] A
#-----| -1: (Base Types)
Expand Down
11 changes: 6 additions & 5 deletions java/ql/test/library-tests/generics/PrintAst.expected
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
generics/A.java:
# 0| [CompilationUnit] A
# 3| 1: [ImportType] import HashMap
# 4| 2: [ImportType] import Map
# 6| 3: [Class] A
#-----| -1: (Imports)
# 3| 1: [ImportType] import HashMap
# 4| 2: [ImportType] import Map
# 6| 1: [Class] A
#-----| -2: (Generic Parameters)
# 6| 0: [TypeVariable] T
# 7| 2: [Class] B
# 10| 4: [Class] C
# 10| 2: [Class] C
# 11| 3: [FieldDeclaration] A<String> f, ...;
# 11| -1: [TypeAccess] A<String>
# 11| 0: [TypeAccess] String
Expand All @@ -22,7 +23,7 @@ generics/A.java:
# 13| -3: [TypeAccess] HashMap<String,Object>
# 13| 0: [TypeAccess] String
# 13| 1: [TypeAccess] Object
# 16| 5: [Class] D
# 16| 3: [Class] D
#-----| -2: (Generic Parameters)
# 16| 0: [TypeVariable] V
# 16| 0: [TypeAccess] Number
Expand Down
11 changes: 6 additions & 5 deletions java/ql/test/library-tests/java7/Diamond/PrintAst.expected
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
Diamond.java:
# 0| [CompilationUnit] Diamond
# 3| 1: [ImportType] import ArrayList
# 4| 2: [ImportType] import HashMap
# 5| 3: [ImportType] import List
# 6| 4: [ImportType] import Map
# 8| 5: [Class] Diamond
#-----| -1: (Imports)
# 3| 1: [ImportType] import ArrayList
# 4| 2: [ImportType] import HashMap
# 5| 3: [ImportType] import List
# 6| 4: [ImportType] import Map
# 8| 1: [Class] Diamond
# 10| 3: [FieldDeclaration] List<Integer> list, ...;
# 10| -1: [TypeAccess] List<Integer>
# 10| 0: [TypeAccess] Integer
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
MultiCatch.java:
# 0| [CompilationUnit] MultiCatch
# 3| 1: [ImportType] import IOException
# 4| 2: [ImportType] import SQLException
# 6| 3: [Class] MultiCatch
#-----| -1: (Imports)
# 3| 1: [ImportType] import IOException
# 4| 2: [ImportType] import SQLException
# 6| 1: [Class] MultiCatch
# 7| 2: [Method] multiCatch
# 7| 3: [TypeAccess] void
#-----| 4: (Parameters)
Expand Down
5 changes: 3 additions & 2 deletions java/ql/test/library-tests/reflection/PrintAst.expected
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
reflection/ReflectiveAccess.java:
# 0| [CompilationUnit] ReflectiveAccess
# 3| 1: [ImportType] import Annotation
# 5| 2: [Class] ReflectiveAccess
#-----| -1: (Imports)
# 3| 1: [ImportType] import Annotation
# 5| 1: [Class] ReflectiveAccess
# 6| 2: [Interface] TestAnnotation
# 10| 3: [Class] TestClass
#-----| -3: (Annotations)
Expand Down
5 changes: 3 additions & 2 deletions java/ql/test/library-tests/typeaccesses/PrintAst.expected
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ typeaccesses/Outer.java:
# 7| 0: [IntegerLiteral] 1
typeaccesses/TA.java:
# 0| [CompilationUnit] TA
# 3| 1: [ImportType] import ArrayList
# 5| 2: [Class] TA
#-----| -1: (Imports)
# 3| 1: [ImportType] import ArrayList
# 5| 1: [Class] TA
#-----| -1: (Base Types)
# 5| -1: [TypeAccess] ArrayList<TA>
# 5| 0: [TypeAccess] TA
Expand Down