Skip to content

Commit 357ed2f

Browse files
committed
Java: Add test for flexible constructor support
1 parent 9d72fab commit 357ed2f

File tree

6 files changed

+145
-0
lines changed

6 files changed

+145
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
unexpectedDiagnostic
2+
| <compilation flexible-constructors.testproj/trap/java/diagnostics/diagnostic.trap.gz> | -1 | 0 | file://:0:0:0:0 | Unknown location for jdk.internal.RequiresIdentity+Annotation | Unknown location for jdk.internal.RequiresIdentity+Annotation |
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class A {
2+
A(String msg) {
3+
System.out.println("A: " + msg);
4+
}
5+
}
6+
7+
class B extends A {
8+
B(String input) {
9+
var msg = input.trim().toUpperCase();
10+
super(msg);
11+
}
12+
}
13+
14+
class C {
15+
private final int x;
16+
17+
C(int x) {
18+
if (x < 0) throw new IllegalArgumentException();
19+
super();
20+
this.x = x;
21+
}
22+
}
23+
24+
record R(String name, int score) {
25+
public R(String name) {
26+
var score = name.length();
27+
this(name, score);
28+
}
29+
}
30+
31+
class Outer {
32+
private final String prefix = "outer";
33+
34+
class Inner {
35+
private final String full;
36+
37+
Inner(String suffix) {
38+
var combined = prefix + "_" + suffix;
39+
super();
40+
this.full = combined;
41+
}
42+
}
43+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
FlexibleConstructors.java:
2+
# 0| [CompilationUnit] FlexibleConstructors
3+
# 1| 1: [Class] A
4+
# 2| 1: [Constructor] A
5+
#-----| 4: (Parameters)
6+
# 2| 0: [Parameter] msg
7+
# 2| 0: [TypeAccess] String
8+
# 2| 5: [BlockStmt] { ... }
9+
# 3| 1: [ExprStmt] <Expr>;
10+
# 3| 0: [MethodCall] println(...)
11+
# 3| -1: [VarAccess] System.out
12+
# 3| -1: [TypeAccess] System
13+
# 3| 0: [AddExpr] ... + ...
14+
# 3| 0: [StringLiteral] "A: "
15+
# 3| 1: [VarAccess] msg
16+
# 7| 2: [Class] B
17+
#-----| -1: (Base Types)
18+
# 7| -1: [TypeAccess] A
19+
# 8| 1: [Constructor] B
20+
#-----| 4: (Parameters)
21+
# 8| 0: [Parameter] input
22+
# 8| 0: [TypeAccess] String
23+
# 8| 5: [BlockStmt] { ... }
24+
# 9| 0: [LocalVariableDeclStmt] var ...;
25+
# 9| 1: [LocalVariableDeclExpr] msg
26+
# 9| 0: [MethodCall] toUpperCase(...)
27+
# 9| -1: [MethodCall] trim(...)
28+
# 9| -1: [VarAccess] input
29+
# 10| 1: [SuperConstructorInvocationStmt] super(...)
30+
# 10| 0: [VarAccess] msg
31+
# 14| 3: [Class] C
32+
# 15| 1: [FieldDeclaration] int x;
33+
# 15| -1: [TypeAccess] int
34+
# 17| 2: [Constructor] C
35+
#-----| 4: (Parameters)
36+
# 17| 0: [Parameter] x
37+
# 17| 0: [TypeAccess] int
38+
# 17| 5: [BlockStmt] { ... }
39+
# 18| 0: [IfStmt] if (...)
40+
# 18| 0: [LTExpr] ... < ...
41+
# 18| 0: [VarAccess] x
42+
# 18| 1: [IntegerLiteral] 0
43+
# 18| 1: [ThrowStmt] throw ...
44+
# 18| 0: [ClassInstanceExpr] new IllegalArgumentException(...)
45+
# 18| -3: [TypeAccess] IllegalArgumentException
46+
# 19| 1: [SuperConstructorInvocationStmt] super(...)
47+
# 20| 2: [ExprStmt] <Expr>;
48+
# 20| 0: [AssignExpr] ...=...
49+
# 20| 0: [VarAccess] this.x
50+
# 20| -1: [ThisAccess] this
51+
# 20| 1: [VarAccess] x
52+
# 24| 4: [Class] R
53+
# 24| 2: [FieldDeclaration] String name;
54+
# 24| 3: [FieldDeclaration] int score;
55+
# 25| 4: [Constructor] R
56+
#-----| 4: (Parameters)
57+
# 25| 0: [Parameter] name
58+
# 25| 0: [TypeAccess] String
59+
# 25| 5: [BlockStmt] { ... }
60+
# 26| 0: [LocalVariableDeclStmt] var ...;
61+
# 26| 1: [LocalVariableDeclExpr] score
62+
# 26| 0: [MethodCall] length(...)
63+
# 26| -1: [VarAccess] name
64+
# 27| 1: [ThisConstructorInvocationStmt] this(...)
65+
# 27| 0: [VarAccess] name
66+
# 27| 1: [VarAccess] score
67+
# 31| 5: [Class] Outer
68+
# 32| 3: [FieldDeclaration] String prefix;
69+
# 32| -1: [TypeAccess] String
70+
# 32| 0: [StringLiteral] "outer"
71+
# 34| 4: [Class] Inner
72+
# 35| 1: [FieldDeclaration] String full;
73+
# 35| -1: [TypeAccess] String
74+
# 37| 2: [Constructor] Inner
75+
#-----| 4: (Parameters)
76+
# 37| 0: [Parameter] suffix
77+
# 37| 0: [TypeAccess] String
78+
# 37| 5: [BlockStmt] { ... }
79+
# 38| 0: [LocalVariableDeclStmt] var ...;
80+
# 38| 1: [LocalVariableDeclExpr] combined
81+
# 38| 0: [AddExpr] ... + ...
82+
# 38| 0: [AddExpr] ... + ...
83+
# 38| 0: [VarAccess] prefix
84+
# 38| 1: [StringLiteral] "_"
85+
# 38| 1: [VarAccess] suffix
86+
# 39| 1: [SuperConstructorInvocationStmt] super(...)
87+
# 40| 2: [ExprStmt] <Expr>;
88+
# 40| 0: [AssignExpr] ...=...
89+
# 40| 0: [VarAccess] this.full
90+
# 40| -1: [ThisAccess] this
91+
# 40| 1: [VarAccess] combined
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import default
2+
3+
from Constructor c, Stmt s, int index
4+
where
5+
c.getBody().getStmt(index) = s
6+
select c, s, index, s.toString()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
semmle/code/java/PrintAst.ql
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
//semmle-extractor-options: --javac-args --release 25 --enable-preview

0 commit comments

Comments
 (0)