Skip to content

Commit 908f025

Browse files
Java: PrintAst: Fix a couple of issues related to Annotations
1 parent c20f802 commit 908f025

File tree

1 file changed

+45
-7
lines changed

1 file changed

+45
-7
lines changed

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

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ private predicate isNotNeeded(Element el) {
5757
not isInitBlock(im.getDeclaringType(), e.getParent*())
5858
)
5959
)
60+
or
61+
exists(Constructor c | c.isDefaultConstructor() |
62+
el = c
63+
or
64+
el.(ExprOrStmt).getEnclosingCallable() = c
65+
)
66+
or
67+
isNotNeeded(el.(Expr).getParent*().(Annotation).getAnnotatedElement())
68+
or
69+
isNotNeeded(el.(Parameter).getCallable())
6070
}
6171

6272
/**
@@ -87,7 +97,7 @@ private predicate locationSortKeys(Element ast, string file, int line, int colum
8797
*/
8898
private newtype TPrintAstNode =
8999
TElementNode(Element el) { shouldPrint(el, _) } or
90-
TAnnotationsNode(Annotatable ann) { shouldPrint(ann, _) and ann.hasAnnotation() } or
100+
TAnnotationsNode(Annotatable ann) { shouldPrint(ann, _) and ann.hasAnnotation() and not partOfAnnotation(ann)} or
91101
TParametersNode(Callable c) { shouldPrint(c, _) and not c.hasNoParameters() } or
92102
TBaseTypesNode(ClassOrInterface ty) { shouldPrint(ty, _) } or
93103
TGenericTypeNode(GenericType ty) { shouldPrint(ty, _) } or
@@ -185,6 +195,24 @@ abstract class ElementNode extends PrintAstNode, TElementNode {
185195
final Element getElement() { result = element }
186196
}
187197

198+
private predicate partOfAnnotation(Expr e) {
199+
e instanceof Annotation
200+
or
201+
e instanceof ArrayInit and
202+
partOfAnnotation(e.getParent())
203+
}
204+
205+
private Expr getAnAnnotationChild(Expr e) {
206+
partOfAnnotation(e) and
207+
(
208+
result = e.(Annotation).getValue(_)
209+
or
210+
result = e.(ArrayInit).getAnInit()
211+
or
212+
result = e.(ArrayInit).(Annotatable).getAnAnnotation()
213+
)
214+
}
215+
188216
/**
189217
* An node representing an `Expr` or a `Stmt`.
190218
*/
@@ -193,7 +221,8 @@ final class ExprStmtNode extends ElementNode {
193221

194222
override PrintAstNode getChild(int childIndex) {
195223
exists(Element el | result.(ElementNode).getElement() = el |
196-
el.(Expr).isNthChildOf(element, childIndex)
224+
el.(Expr).isNthChildOf(element, childIndex) and
225+
not partOfAnnotation(element)
197226
or
198227
el.(Stmt).isNthChildOf(element, childIndex)
199228
or
@@ -202,10 +231,19 @@ final class ExprStmtNode extends ElementNode {
202231
or
203232
childIndex = 0 and
204233
el = element.(LocalClassDeclStmt).getLocalClass()
234+
or
235+
partOfAnnotation(element) and
236+
el = rank[childIndex](Element ch, string file, int line, int column |
237+
ch = getAnAnnotationChild(element) and locationSortKeys(ch, file, line, column)
238+
|
239+
ch order by file, line, column
240+
)
205241
)
206242
or
207-
childIndex = -2 and
208-
result.(AnnotationsNode).getAnnotated() = element.(LocalVariableDeclExpr).getVariable()
243+
exists(Element el | result.(AnnotationsNode).getAnnotated() = el |
244+
childIndex = -2 and
245+
el = element.(LocalVariableDeclExpr).getVariable()
246+
)
209247
}
210248
}
211249

@@ -253,7 +291,7 @@ final class ParameterNode extends ElementNode {
253291
}
254292
}
255293

256-
private predicate isInitBlock(Class c, Block b) {
294+
private predicate isInitBlock(Class c, BlockStmt b) {
257295
exists(InitializerMethod m | b.getParent() = m.getBody() and m.getDeclaringType() = c)
258296
}
259297

@@ -367,7 +405,7 @@ final class TypeVariableNode extends ElementNode {
367405
final class AnnotationsNode extends PrintAstNode, TAnnotationsNode {
368406
Annotatable ann;
369407

370-
AnnotationsNode() { this = TAnnotationsNode(ann) }
408+
AnnotationsNode() { this = TAnnotationsNode(ann) and not isNotNeeded(ann) }
371409

372410
override string toString() { result = "(Annotations)" }
373411

@@ -395,7 +433,7 @@ final class AnnotationsNode extends PrintAstNode, TAnnotationsNode {
395433
final class ParametersNode extends PrintAstNode, TParametersNode {
396434
Callable c;
397435

398-
ParametersNode() { this = TParametersNode(c) }
436+
ParametersNode() { this = TParametersNode(c) and not isNotNeeded(c) }
399437

400438
override string toString() { result = "(Parameters)" }
401439

0 commit comments

Comments
 (0)