-
Notifications
You must be signed in to change notification settings - Fork 2k
Java: Add test for flexible constructor support #20136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
de6d9f4
Java: Add test for flexible constructor support
IdrissRio 6b022ed
Java: Address review comment. Add prologue field initialization tests
IdrissRio fffb4c0
Java: add flexible constructor test including CFG predecessor query
IdrissRio 6c773a7
Java: Add test to verify that the AST does not capture instance initi…
IdrissRio 1605438
Java: Accept new test result after extractor changes
IdrissRio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
java/ql/test/library-tests/flexible-constructors/CONSISTENCY/diags.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| unexpectedDiagnostic | ||
| | <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 | | ||
95 changes: 95 additions & 0 deletions
95
java/ql/test/library-tests/flexible-constructors/FlexibleConstructors.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| class A { | ||
| A(String msg) { | ||
| System.out.println("A: " + msg); | ||
| } | ||
| } | ||
|
|
||
| class B extends A { | ||
| B(String input) { | ||
| var msg = input.trim().toUpperCase(); | ||
| super(msg); | ||
| } | ||
| } | ||
|
|
||
| class C { | ||
| private final int x; | ||
|
|
||
| C(int x) { | ||
| if (x < 0) throw new IllegalArgumentException(); | ||
| super(); | ||
| this.x = x; | ||
| } | ||
| } | ||
|
|
||
| record R(String name, int score) { | ||
| public R(String name) { | ||
| var score = name.length(); | ||
| this(name, score); | ||
| } | ||
| } | ||
|
|
||
| class Outer { | ||
| private final String prefix = "outer"; | ||
|
|
||
| class Inner { | ||
| private String full; | ||
|
|
||
| Inner(String suffix) { | ||
| var combined = prefix + "_" + suffix; | ||
| super(); | ||
| this.full = combined; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class D { | ||
| private final String value; | ||
| private final int length; | ||
|
|
||
| D(String input) { | ||
| var processed = input.toLowerCase(); | ||
| value = processed; | ||
| this.length = processed.length(); | ||
| super(); | ||
| } | ||
| } | ||
|
|
||
| class E extends A { | ||
| private boolean isValid; | ||
| private String processed; | ||
|
|
||
| E(String data) { | ||
| var temp = data != null ? data.trim() : ""; | ||
| this.processed = temp; | ||
| isValid = !temp.isEmpty(); | ||
| super(temp); | ||
| } | ||
| } | ||
|
|
||
| class F { | ||
| private int x; | ||
| private final int y; | ||
| private int sum; | ||
|
|
||
| F(int a, int b) { | ||
| x = a; | ||
| this.y = b; | ||
| this.sum = a + b; | ||
| super(); | ||
| } | ||
| } | ||
|
|
||
| class G { | ||
| private String instance_val; | ||
|
|
||
| { | ||
| instance_val = "instance"; | ||
| } | ||
|
|
||
| G(String input) { | ||
| var tmp = input != null ? input : "default"; | ||
| var string = tmp + "_initialized"; | ||
| super(); | ||
| this.instance_val = string; | ||
| } | ||
| } |
2 changes: 2 additions & 0 deletions
2
java/ql/test/library-tests/flexible-constructors/InstanceInitializerCalls.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| | FlexibleConstructors.java:31:7:31:11 | <Expr>; | 1 | <obinit> | Instance initializer call at index 1 | | ||
| | FlexibleConstructors.java:89:5:89:5 | <Expr>; | 3 | <obinit> | Instance initializer call at index 3 | |
8 changes: 8 additions & 0 deletions
8
java/ql/test/library-tests/flexible-constructors/InstanceInitializerCalls.ql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import java | ||
|
|
||
| from MethodCall call, Method m | ||
| where | ||
| call.getMethod() = m and | ||
| m.getName() = "<obinit>" | ||
| select call.getEnclosingStmt(), call.getEnclosingStmt().getIndex(), call.getMethod().getName(), | ||
| "Instance initializer call at index " + call.getEnclosingStmt().getIndex() |
125 changes: 125 additions & 0 deletions
125
java/ql/test/library-tests/flexible-constructors/PrettyPrint.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| | FlexibleConstructors.java:1:7:1:7 | A | 0 | class A { | | ||
| | FlexibleConstructors.java:1:7:1:7 | A | 1 | A(String msg) { | | ||
| | FlexibleConstructors.java:1:7:1:7 | A | 2 | super(); | | ||
| | FlexibleConstructors.java:1:7:1:7 | A | 3 | System.out.println("A: " + msg); | | ||
| | FlexibleConstructors.java:1:7:1:7 | A | 4 | } | | ||
| | FlexibleConstructors.java:1:7:1:7 | A | 5 | } | | ||
| | FlexibleConstructors.java:7:7:7:7 | B | 0 | class B { | | ||
| | FlexibleConstructors.java:7:7:7:7 | B | 1 | B(String input) { | | ||
| | FlexibleConstructors.java:7:7:7:7 | B | 2 | var msg = input.trim().toUpperCase(); | | ||
| | FlexibleConstructors.java:7:7:7:7 | B | 3 | super(msg); | | ||
| | FlexibleConstructors.java:7:7:7:7 | B | 4 | } | | ||
| | FlexibleConstructors.java:7:7:7:7 | B | 5 | } | | ||
| | FlexibleConstructors.java:14:7:14:7 | C | 0 | class C { | | ||
| | FlexibleConstructors.java:14:7:14:7 | C | 1 | private final int x; | | ||
| | FlexibleConstructors.java:14:7:14:7 | C | 2 | | | ||
| | FlexibleConstructors.java:14:7:14:7 | C | 3 | C(int x) { | | ||
| | FlexibleConstructors.java:14:7:14:7 | C | 4 | if (x < 0) | | ||
| | FlexibleConstructors.java:14:7:14:7 | C | 5 | throw new IllegalArgumentException(); | | ||
| | FlexibleConstructors.java:14:7:14:7 | C | 6 | super(); | | ||
| | FlexibleConstructors.java:14:7:14:7 | C | 7 | this.x = x; | | ||
| | FlexibleConstructors.java:14:7:14:7 | C | 8 | } | | ||
| | FlexibleConstructors.java:14:7:14:7 | C | 9 | } | | ||
| | FlexibleConstructors.java:24:8:24:8 | R | 0 | final class R { | | ||
| | FlexibleConstructors.java:24:8:24:8 | R | 1 | public final boolean equals(Object p0) { <missing body> } | | ||
| | FlexibleConstructors.java:24:8:24:8 | R | 2 | | | ||
| | FlexibleConstructors.java:24:8:24:8 | R | 3 | public final int hashCode() { <missing body> } | | ||
| | FlexibleConstructors.java:24:8:24:8 | R | 4 | | | ||
| | FlexibleConstructors.java:24:8:24:8 | R | 5 | public String name() { <missing body> } | | ||
| | FlexibleConstructors.java:24:8:24:8 | R | 6 | | | ||
| | FlexibleConstructors.java:24:8:24:8 | R | 7 | public int score() { <missing body> } | | ||
| | FlexibleConstructors.java:24:8:24:8 | R | 8 | | | ||
| | FlexibleConstructors.java:24:8:24:8 | R | 9 | public final String toString() { <missing body> } | | ||
| | FlexibleConstructors.java:24:8:24:8 | R | 10 | | | ||
| | FlexibleConstructors.java:24:8:24:8 | R | 11 | R(String name, int score) { | | ||
| | FlexibleConstructors.java:24:8:24:8 | R | 12 | super(); | | ||
| | FlexibleConstructors.java:24:8:24:8 | R | 13 | this.name = name; | | ||
| | FlexibleConstructors.java:24:8:24:8 | R | 14 | this.score = score; | | ||
| | FlexibleConstructors.java:24:8:24:8 | R | 15 | } | | ||
| | FlexibleConstructors.java:24:8:24:8 | R | 16 | | | ||
| | FlexibleConstructors.java:24:8:24:8 | R | 17 | private final String name; | | ||
| | FlexibleConstructors.java:24:8:24:8 | R | 18 | | | ||
| | FlexibleConstructors.java:24:8:24:8 | R | 19 | private final int score; | | ||
| | FlexibleConstructors.java:24:8:24:8 | R | 20 | | | ||
| | FlexibleConstructors.java:24:8:24:8 | R | 21 | public R(String name) { | | ||
| | FlexibleConstructors.java:24:8:24:8 | R | 22 | var score = name.length(); | | ||
| | FlexibleConstructors.java:24:8:24:8 | R | 23 | this(name, score); | | ||
| | FlexibleConstructors.java:24:8:24:8 | R | 24 | } | | ||
| | FlexibleConstructors.java:24:8:24:8 | R | 25 | } | | ||
| | FlexibleConstructors.java:31:7:31:11 | Outer | 0 | class Outer { | | ||
| | FlexibleConstructors.java:31:7:31:11 | Outer | 1 | private void <obinit>() { | | ||
| | FlexibleConstructors.java:31:7:31:11 | Outer | 2 | prefix = "outer"; | | ||
| | FlexibleConstructors.java:31:7:31:11 | Outer | 3 | } | | ||
| | FlexibleConstructors.java:31:7:31:11 | Outer | 4 | | | ||
| | FlexibleConstructors.java:31:7:31:11 | Outer | 5 | Outer() { | | ||
| | FlexibleConstructors.java:31:7:31:11 | Outer | 6 | super(); | | ||
| | FlexibleConstructors.java:31:7:31:11 | Outer | 7 | <obinit>(); | | ||
| | FlexibleConstructors.java:31:7:31:11 | Outer | 8 | } | | ||
| | FlexibleConstructors.java:31:7:31:11 | Outer | 9 | | | ||
| | FlexibleConstructors.java:31:7:31:11 | Outer | 10 | private final String prefix; | | ||
| | FlexibleConstructors.java:31:7:31:11 | Outer | 11 | | | ||
| | FlexibleConstructors.java:31:7:31:11 | Outer | 12 | class Inner { | | ||
| | FlexibleConstructors.java:31:7:31:11 | Outer | 13 | private String full; | | ||
| | FlexibleConstructors.java:31:7:31:11 | Outer | 14 | | | ||
| | FlexibleConstructors.java:31:7:31:11 | Outer | 15 | Inner(String suffix) { | | ||
| | FlexibleConstructors.java:31:7:31:11 | Outer | 16 | var combined = prefix + "_" + suffix; | | ||
| | FlexibleConstructors.java:31:7:31:11 | Outer | 17 | super(); | | ||
| | FlexibleConstructors.java:31:7:31:11 | Outer | 18 | this.full = combined; | | ||
| | FlexibleConstructors.java:31:7:31:11 | Outer | 19 | } | | ||
| | FlexibleConstructors.java:31:7:31:11 | Outer | 20 | } | | ||
| | FlexibleConstructors.java:31:7:31:11 | Outer | 21 | } | | ||
| | FlexibleConstructors.java:45:7:45:7 | D | 0 | class D { | | ||
| | FlexibleConstructors.java:45:7:45:7 | D | 1 | private final String value; | | ||
| | FlexibleConstructors.java:45:7:45:7 | D | 2 | | | ||
| | FlexibleConstructors.java:45:7:45:7 | D | 3 | private final int length; | | ||
| | FlexibleConstructors.java:45:7:45:7 | D | 4 | | | ||
| | FlexibleConstructors.java:45:7:45:7 | D | 5 | D(String input) { | | ||
| | FlexibleConstructors.java:45:7:45:7 | D | 6 | var processed = input.toLowerCase(); | | ||
| | FlexibleConstructors.java:45:7:45:7 | D | 7 | value = processed; | | ||
| | FlexibleConstructors.java:45:7:45:7 | D | 8 | this.length = processed.length(); | | ||
| | FlexibleConstructors.java:45:7:45:7 | D | 9 | super(); | | ||
| | FlexibleConstructors.java:45:7:45:7 | D | 10 | } | | ||
| | FlexibleConstructors.java:45:7:45:7 | D | 11 | } | | ||
| | FlexibleConstructors.java:57:7:57:7 | E | 0 | class E { | | ||
| | FlexibleConstructors.java:57:7:57:7 | E | 1 | private boolean isValid; | | ||
| | FlexibleConstructors.java:57:7:57:7 | E | 2 | | | ||
| | FlexibleConstructors.java:57:7:57:7 | E | 3 | private String processed; | | ||
| | FlexibleConstructors.java:57:7:57:7 | E | 4 | | | ||
| | FlexibleConstructors.java:57:7:57:7 | E | 5 | E(String data) { | | ||
| | FlexibleConstructors.java:57:7:57:7 | E | 6 | var temp = data != null ? data.trim() : ""; | | ||
| | FlexibleConstructors.java:57:7:57:7 | E | 7 | this.processed = temp; | | ||
| | FlexibleConstructors.java:57:7:57:7 | E | 8 | isValid = !temp.isEmpty(); | | ||
| | FlexibleConstructors.java:57:7:57:7 | E | 9 | super(temp); | | ||
| | FlexibleConstructors.java:57:7:57:7 | E | 10 | } | | ||
| | FlexibleConstructors.java:57:7:57:7 | E | 11 | } | | ||
| | FlexibleConstructors.java:69:7:69:7 | F | 0 | class F { | | ||
| | FlexibleConstructors.java:69:7:69:7 | F | 1 | private int x; | | ||
| | FlexibleConstructors.java:69:7:69:7 | F | 2 | | | ||
| | FlexibleConstructors.java:69:7:69:7 | F | 3 | private final int y; | | ||
| | FlexibleConstructors.java:69:7:69:7 | F | 4 | | | ||
| | FlexibleConstructors.java:69:7:69:7 | F | 5 | private int sum; | | ||
| | FlexibleConstructors.java:69:7:69:7 | F | 6 | | | ||
| | FlexibleConstructors.java:69:7:69:7 | F | 7 | F(int a, int b) { | | ||
| | FlexibleConstructors.java:69:7:69:7 | F | 8 | x = a; | | ||
| | FlexibleConstructors.java:69:7:69:7 | F | 9 | this.y = b; | | ||
| | FlexibleConstructors.java:69:7:69:7 | F | 10 | this.sum = a + b; | | ||
| | FlexibleConstructors.java:69:7:69:7 | F | 11 | super(); | | ||
| | FlexibleConstructors.java:69:7:69:7 | F | 12 | } | | ||
| | FlexibleConstructors.java:69:7:69:7 | F | 13 | } | | ||
| | FlexibleConstructors.java:82:7:82:7 | G | 0 | class G { | | ||
| | FlexibleConstructors.java:82:7:82:7 | G | 1 | private void <obinit>() { | | ||
| | FlexibleConstructors.java:82:7:82:7 | G | 2 | { | | ||
| | FlexibleConstructors.java:82:7:82:7 | G | 3 | instance_val = "instance"; | | ||
| | FlexibleConstructors.java:82:7:82:7 | G | 4 | } | | ||
| | FlexibleConstructors.java:82:7:82:7 | G | 5 | } | | ||
| | FlexibleConstructors.java:82:7:82:7 | G | 6 | | | ||
| | FlexibleConstructors.java:82:7:82:7 | G | 7 | private String instance_val; | | ||
| | FlexibleConstructors.java:82:7:82:7 | G | 8 | | | ||
| | FlexibleConstructors.java:82:7:82:7 | G | 9 | G(String input) { | | ||
| | FlexibleConstructors.java:82:7:82:7 | G | 10 | var tmp = input != null ? input : "default"; | | ||
| | FlexibleConstructors.java:82:7:82:7 | G | 11 | var string = tmp + "_initialized"; | | ||
| | FlexibleConstructors.java:82:7:82:7 | G | 12 | super(); | | ||
| | FlexibleConstructors.java:82:7:82:7 | G | 13 | <obinit>(); | | ||
| | FlexibleConstructors.java:82:7:82:7 | G | 14 | this.instance_val = string; | | ||
| | FlexibleConstructors.java:82:7:82:7 | G | 15 | } | | ||
| | FlexibleConstructors.java:82:7:82:7 | G | 16 | } | |
5 changes: 5 additions & 0 deletions
5
java/ql/test/library-tests/flexible-constructors/PrettyPrint.ql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import semmle.code.java.PrettyPrintAst | ||
|
|
||
| from ClassOrInterface cori, string s, int line | ||
| where pp(cori, s, line) and cori.fromSource() | ||
| select cori, line, s |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's this about?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This annotation is internally generated and has no specific source location. The issue is fixed in another PR (https://github.com/github/semmle-code/pull/53614/commits/ec9c0e744141ac02fbb1b62f3f5a4af2593168f1). The
diags.expectedcan be removed once this PR is rebased on top of it.