Skip to content

Commit 4f9d2f1

Browse files
authored
Merge pull request #4288 from joefarebrother/printAST-java
Java: Add a container node for Imports in the PrintAst view
2 parents 6463a94 + 3258134 commit 4f9d2f1

File tree

8 files changed

+62
-27
lines changed

8 files changed

+62
-27
lines changed

java/ql/src/semmle/code/java/PrintAst.qll

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ private newtype TPrintAstNode =
124124
TJavadocNode(Javadoc jd) { exists(Documentable d | d.getJavadoc() = jd | shouldPrint(d, _)) } or
125125
TJavadocElementNode(JavadocElement jd) {
126126
exists(Documentable d | d.getJavadoc() = jd.getParent*() | shouldPrint(d, _))
127+
} or
128+
TImportsNode(CompilationUnit cu) {
129+
shouldPrint(cu, _) and exists(Import i | i.getCompilationUnit() = cu)
127130
}
128131

129132
/**
@@ -395,13 +398,12 @@ final class CompilationUnitNode extends ElementNode {
395398

396399
CompilationUnitNode() { cu = element }
397400

398-
private Element getADeclaration() {
399-
cu.hasChildElement(result)
400-
or
401-
result.(Import).getCompilationUnit() = cu
402-
}
401+
private Element getADeclaration() { cu.hasChildElement(result) }
403402

404403
override PrintAstNode getChild(int childIndex) {
404+
childIndex = -1 and
405+
result.(ImportsNode).getCompilationUnit() = cu
406+
or
405407
childIndex >= 0 and
406408
result.(ElementNode).getElement() =
407409
rank[childIndex](Element e, string file, int line, int column |
@@ -622,6 +624,32 @@ final class JavadocElementNode extends PrintAstNode, TJavadocElementNode {
622624
JavadocElement getJavadocElement() { result = jd }
623625
}
624626

627+
/**
628+
* A node representing the `Import`s of a `CompilationUnit`.
629+
* Only rendered if there is at least one import.
630+
*/
631+
final class ImportsNode extends PrintAstNode, TImportsNode {
632+
CompilationUnit cu;
633+
634+
ImportsNode() { this = TImportsNode(cu) }
635+
636+
override string toString() { result = "(Imports)" }
637+
638+
override ElementNode getChild(int childIndex) {
639+
result.getElement() =
640+
rank[childIndex](Import im, string file, int line, int column |
641+
im.getCompilationUnit() = cu and locationSortKeys(im, file, line, column)
642+
|
643+
im order by file, line, column
644+
)
645+
}
646+
647+
/**
648+
* Gets the underlying CompilationUnit.
649+
*/
650+
CompilationUnit getCompilationUnit() { result = cu }
651+
}
652+
625653
/** Holds if `node` belongs to the output tree, and its property `key` has the given `value`. */
626654
query predicate nodes(PrintAstNode node, string key, string value) { value = node.getProperty(key) }
627655

java/ql/test/library-tests/collections/PrintAst.expected

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
collections/Test.java:
22
# 0| [CompilationUnit] Test
3-
# 3| 1: [ImportOnDemandFromPackage] import java.util.*
4-
# 5| 2: [Class] Test
3+
#-----| -1: (Imports)
4+
# 3| 1: [ImportOnDemandFromPackage] import java.util.*
5+
# 5| 1: [Class] Test
56
# 6| 3: [FieldDeclaration] Map<String,Integer> m, ...;
67
# 6| -1: [TypeAccess] Map<String,Integer>
78
# 6| 0: [TypeAccess] String

java/ql/test/library-tests/dependency-counts/PrintAst.expected

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
Example.java:
22
# 0| [CompilationUnit] Example
3-
# 1| 1: [ImportType] import Set
4-
# 2| 2: [ImportType] import List
5-
# 4| 3: [Interface] Example
3+
#-----| -1: (Imports)
4+
# 1| 1: [ImportType] import Set
5+
# 2| 2: [ImportType] import List
6+
# 4| 1: [Interface] Example
67
#-----| -2: (Generic Parameters)
78
# 4| 0: [TypeVariable] A
89
#-----| -1: (Base Types)

java/ql/test/library-tests/generics/PrintAst.expected

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
generics/A.java:
22
# 0| [CompilationUnit] A
3-
# 3| 1: [ImportType] import HashMap
4-
# 4| 2: [ImportType] import Map
5-
# 6| 3: [Class] A
3+
#-----| -1: (Imports)
4+
# 3| 1: [ImportType] import HashMap
5+
# 4| 2: [ImportType] import Map
6+
# 6| 1: [Class] A
67
#-----| -2: (Generic Parameters)
78
# 6| 0: [TypeVariable] T
89
# 7| 2: [Class] B
9-
# 10| 4: [Class] C
10+
# 10| 2: [Class] C
1011
# 11| 3: [FieldDeclaration] A<String> f, ...;
1112
# 11| -1: [TypeAccess] A<String>
1213
# 11| 0: [TypeAccess] String
@@ -22,7 +23,7 @@ generics/A.java:
2223
# 13| -3: [TypeAccess] HashMap<String,Object>
2324
# 13| 0: [TypeAccess] String
2425
# 13| 1: [TypeAccess] Object
25-
# 16| 5: [Class] D
26+
# 16| 3: [Class] D
2627
#-----| -2: (Generic Parameters)
2728
# 16| 0: [TypeVariable] V
2829
# 16| 0: [TypeAccess] Number

java/ql/test/library-tests/java7/Diamond/PrintAst.expected

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
Diamond.java:
22
# 0| [CompilationUnit] Diamond
3-
# 3| 1: [ImportType] import ArrayList
4-
# 4| 2: [ImportType] import HashMap
5-
# 5| 3: [ImportType] import List
6-
# 6| 4: [ImportType] import Map
7-
# 8| 5: [Class] Diamond
3+
#-----| -1: (Imports)
4+
# 3| 1: [ImportType] import ArrayList
5+
# 4| 2: [ImportType] import HashMap
6+
# 5| 3: [ImportType] import List
7+
# 6| 4: [ImportType] import Map
8+
# 8| 1: [Class] Diamond
89
# 10| 3: [FieldDeclaration] List<Integer> list, ...;
910
# 10| -1: [TypeAccess] List<Integer>
1011
# 10| 0: [TypeAccess] Integer

java/ql/test/library-tests/java7/MultiCatch/PrintAst.expected

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
MultiCatch.java:
22
# 0| [CompilationUnit] MultiCatch
3-
# 3| 1: [ImportType] import IOException
4-
# 4| 2: [ImportType] import SQLException
5-
# 6| 3: [Class] MultiCatch
3+
#-----| -1: (Imports)
4+
# 3| 1: [ImportType] import IOException
5+
# 4| 2: [ImportType] import SQLException
6+
# 6| 1: [Class] MultiCatch
67
# 7| 2: [Method] multiCatch
78
# 7| 3: [TypeAccess] void
89
#-----| 4: (Parameters)

java/ql/test/library-tests/reflection/PrintAst.expected

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
reflection/ReflectiveAccess.java:
22
# 0| [CompilationUnit] ReflectiveAccess
3-
# 3| 1: [ImportType] import Annotation
4-
# 5| 2: [Class] ReflectiveAccess
3+
#-----| -1: (Imports)
4+
# 3| 1: [ImportType] import Annotation
5+
# 5| 1: [Class] ReflectiveAccess
56
# 6| 2: [Interface] TestAnnotation
67
# 10| 3: [Class] TestClass
78
#-----| -3: (Annotations)

java/ql/test/library-tests/typeaccesses/PrintAst.expected

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ typeaccesses/Outer.java:
2626
# 7| 0: [IntegerLiteral] 1
2727
typeaccesses/TA.java:
2828
# 0| [CompilationUnit] TA
29-
# 3| 1: [ImportType] import ArrayList
30-
# 5| 2: [Class] TA
29+
#-----| -1: (Imports)
30+
# 3| 1: [ImportType] import ArrayList
31+
# 5| 1: [Class] TA
3132
#-----| -1: (Base Types)
3233
# 5| -1: [TypeAccess] ArrayList<TA>
3334
# 5| 0: [TypeAccess] TA

0 commit comments

Comments
 (0)