Skip to content

Commit eda676d

Browse files
committed
Add support for Annotation types stub generation
1 parent 3d00a61 commit eda676d

File tree

3 files changed

+52
-19
lines changed

3 files changed

+52
-19
lines changed

java/ql/src/utils/stub-generator/MinimalStubsFromSource.ql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class UsedInSource extends GeneratedDeclaration {
2020
this = any(RefType t | t.fromSource())
2121
or
2222
this = any(TypeAccess ta | ta.fromSource())
23+
or
24+
this = any(Annotation a | a.getAnnotatedElement().fromSource()).getType()
2325
)
2426
}
2527
}

java/ql/src/utils/stub-generator/Stubs.qll

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ abstract private class GeneratedType extends ClassOrInterface {
1616
}
1717

1818
private string stubKeyword() {
19-
this instanceof Interface and result = "interface"
19+
this instanceof Interface and
20+
(
21+
this instanceof AnnotationType and result = "@interface"
22+
or
23+
result = "interface"
24+
)
2025
or
2126
this instanceof Class and
2227
(if this instanceof EnumType then result = "enum" else result = "class")
@@ -27,19 +32,24 @@ abstract private class GeneratedType extends ClassOrInterface {
2732
}
2833

2934
private string stubStaticModifier() {
30-
if this.isStatic() then result = "static " else result = ""
35+
if this.(NestedType).isStatic() then result = "static " else result = ""
3136
}
3237

3338
private string stubAccessibilityModifier() {
3439
if this.isPublic() then result = "public " else result = ""
3540
}
3641

42+
private string stubAnnotations() {
43+
result = concat("@" + stubAnnotation(this.(AnnotationType).getAnAnnotation()) + "\n")
44+
}
45+
3746
/** Gets the entire Java stub code for this type. */
3847
final string getStub() {
3948
result =
40-
this.stubAbstractModifier() + this.stubStaticModifier() + this.stubAccessibilityModifier() +
41-
this.stubKeyword() + " " + this.getName() + stubGenericArguments(this, true) +
42-
this.stubBaseTypesString() + "\n{\n" + this.stubMembers() + "}"
49+
this.stubAnnotations() + this.stubAbstractModifier() + this.stubStaticModifier() +
50+
this.stubAccessibilityModifier() + this.stubKeyword() + " " + this.getName() +
51+
stubGenericArguments(this, true) + this.stubBaseTypesString() + "\n{\n" + this.stubMembers()
52+
+ "}"
4353
}
4454

4555
private RefType getAnInterestingBaseType() {
@@ -60,7 +70,9 @@ abstract private class GeneratedType extends ClassOrInterface {
6070
else cls = ""
6171
) and
6272
(
63-
if exists(this.getAnInterestingBaseType().(Interface))
73+
if
74+
exists(this.getAnInterestingBaseType().(Interface)) and
75+
not this instanceof AnnotationType
6476
then (
6577
(if this instanceof Class then int_kw = " implements " else int_kw = " extends ") and
6678
interface = concat(stubTypeName(this.getAnInterestingBaseType().(Interface)), ", ")
@@ -96,15 +108,14 @@ abstract private class GeneratedType extends ClassOrInterface {
96108
}
97109

98110
final Type getAGeneratedType() {
99-
result = this.getAnInterestingBaseType()
100-
or
101-
result = this.getAGeneratedMember().(Callable).getReturnType()
102-
or
103-
result = this.getAGeneratedMember().(Callable).getAParameter().getType()
104-
or
105-
result = this.getAGeneratedMember().(Field).getType()
106-
or
107-
result = this.getAGeneratedMember().(NestedType)
111+
result = this.getAnInterestingBaseType() or
112+
result = this.getAGeneratedMember().(Callable).getReturnType() or
113+
result = this.getAGeneratedMember().(Callable).getAParameter().getType() or
114+
result = this.getAGeneratedMember().(Field).getType() or
115+
result = this.getAGeneratedMember().(NestedType) or
116+
result = this.(AnnotationType).getAnAnnotation().getType() or
117+
result = this.(AnnotationType).getAnAnnotation().getAValue().getType() or
118+
result = this.(AnnotationType).getAnAnnotation().getAValue().(ArrayInit).getAnInit().getType()
108119
}
109120
}
110121

@@ -391,6 +402,24 @@ private string stubMember(Member m) {
391402
)
392403
}
393404

405+
private string stubAnnotation(Annotation a) {
406+
if exists(a.getAValue())
407+
then result = a.toString() + "(" + concat(stubAnnotationValue(a.getAValue()), ",") + ")"
408+
else result = a.toString()
409+
}
410+
411+
private string stubAnnotiationSimpleValue(Expr value) {
412+
result = value.(FieldAccess).getField().getQualifiedName() or
413+
result = value.(Literal).toString()
414+
}
415+
416+
private string stubAnnotationValue(Expr value) {
417+
result = stubAnnotiationSimpleValue(value)
418+
or
419+
value instanceof ArrayInit and
420+
result = "{" + concat(stubAnnotiationSimpleValue(value.(ArrayInit).getAnInit()), ",") + "}"
421+
}
422+
394423
bindingset[s]
395424
private string indent(string s) { result = " " + s.replaceAll("\n", "\n ") + "\n" }
396425

java/ql/src/utils/stub-generator/makeStubs.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ def check_file_exists(path):
9999
dbDir = os.path.join(workDir, "db")
100100

101101

102-
def print_javac_output():
103-
logFiles = glob.glob(os.path.join(dbDir, "log", "javac-output*"))
102+
def print_javac_output(db_dir):
103+
logFiles = glob.glob(os.path.join(db_dir, "log", "javac-output*"))
104104

105105
if not(logFiles):
106106
print("\nNo javac output found.")
@@ -124,7 +124,7 @@ def run(cmd):
124124
print("Stubbing qltest in", testDir)
125125

126126
if run(['codeql', 'database', 'create', '--language=java', '--source-root='+projectDir, dbDir]):
127-
print_javac_output()
127+
print_javac_output(dbDir)
128128
print("codeql database create failed. Please fix up the test before proceeding.")
129129
exit(1)
130130

@@ -167,7 +167,9 @@ def run(cmd):
167167
print("Verifying stub correctness")
168168

169169
if run(['codeql', 'test', 'run', testDir]):
170-
print_javac_output()
170+
test_db_dir = glob.glob(os.path.join(testDir, "*.testproj"))
171+
if test_db_dir:
172+
print_javac_output(test_db_dir[0])
171173
print('\nTest failed. You may need to fix up the generated stubs.')
172174
exit(1)
173175

0 commit comments

Comments
 (0)