-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[Java] Add CompileTimeConstantExpr.getStringified method #8360
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
smowton
merged 5 commits into
github:main
from
JLLeitschuh:feat/JLL/compile_time_constant_getStringified
Mar 11, 2022
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2e8b5f7
[Java] Add CompileTimeConstantExpr.getStringified method
JLLeitschuh 65457cc
Apply suggestions from code review
JLLeitschuh 363fff2
Cleanup from code review feedback
JLLeitschuh 3113b27
Fix style
smowton c99bad4
Recover old change note
atorralba 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
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,6 @@ | ||
--- | ||
category: minorAnalysis | ||
--- | ||
* Add new predicate `CompileTimeConstantExpr.getStringifiedValue` which attempts to compute the | ||
`String.valueOf` string rendering of a constant expression. This predicate is now used to | ||
compute the string value of an `AddExpr` that has the type `String`. |
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
2 changes: 1 addition & 1 deletion
2
java/ql/test/library-tests/constants/CompileTimeConstantExpr.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import semmle.code.java.Expr | ||
import java | ||
|
||
from CompileTimeConstantExpr constant, RefType tpe | ||
where | ||
|
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
28 changes: 28 additions & 0 deletions
28
java/ql/test/library-tests/constants/constants/Stringified.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,28 @@ | ||
package constants; | ||
|
||
public class Stringified { | ||
void stringified(final String notConstant) { | ||
String withNotConstant = "a" + notConstant; | ||
String string = "a" + "b"; //ab | ||
String stringWithChar = "ab" + 'c'; //abc | ||
String stringWithBool = "ab" + true; //abtrue | ||
String stringWithInt = "ab" + 42; //ab42 | ||
String stringWithDouble = "ab" + 43.0; //ab43.0 | ||
String stringWithFloat = "ab" + 44.0f; //ab44.0 | ||
String stringWithLong = "ab" + 45L; //ab45 | ||
String stringWithShort = "ab" + (short) 46; //ab46 | ||
String stringWithByte = "ab" + (byte) 47; //ab47 | ||
String charWithString = 'a' + "bc"; //abc | ||
String boolWithString = true + "bc"; //truebc | ||
String intWithString = 42 + "bc"; //42bc | ||
String doubleWithString = 43.0 + "bc"; //43.0bc | ||
String floatWithString = 44.0f + "bc"; //44.0bc | ||
String longWithString = 45L + "bc"; //45bc | ||
String shortWithString = (short) 46 + "bc"; //46bc | ||
String byteWithString = (byte) 47 + "bc"; //47bc | ||
|
||
String stringWithExponent = "a" + 10e1; //a100 | ||
String stringWithBooleanOr = "a" + (true || false); //atrue | ||
String stringWithIntDivide = "a" + (168 / 4); //a42 | ||
} | ||
} |
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
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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import semmle.code.java.Variable | ||
import java | ||
|
||
from Variable v, Expr init, RefType enclosing | ||
where | ||
|
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
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import semmle.code.java.Variable | ||
import java | ||
|
||
from Variable v, CompileTimeConstantExpr init, RefType enclosing, int constant | ||
where | ||
v.getInitializer() = init and | ||
init.getEnclosingCallable().getDeclaringType() = enclosing and | ||
enclosing.hasQualifiedName("constants", "Values") and | ||
enclosing.hasQualifiedName("constants", ["Values", "Stringified"]) and | ||
constant = init.getIntValue() | ||
select init, constant |
24 changes: 23 additions & 1 deletion
24
java/ql/test/library-tests/constants/getStringValue.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 |
---|---|---|
@@ -1,3 +1,25 @@ | ||
| constants/Values.java:19:29:19:31 | '*' | * | | ||
| constants/Stringified.java:6:25:6:33 | "a" + "b" | ab | | ||
| constants/Stringified.java:7:33:7:42 | ... + ... | ab99 | | ||
| constants/Stringified.java:7:33:7:42 | ... + ... | abc | | ||
| constants/Stringified.java:8:33:8:43 | ... + ... | abtrue | | ||
| constants/Stringified.java:9:32:9:40 | ... + ... | ab42 | | ||
| constants/Stringified.java:10:35:10:45 | ... + ... | ab43.0 | | ||
| constants/Stringified.java:11:34:11:45 | ... + ... | ab44.0 | | ||
| constants/Stringified.java:12:33:12:42 | ... + ... | ab45 | | ||
| constants/Stringified.java:13:34:13:50 | ... + ... | ab46 | | ||
| constants/Stringified.java:14:33:14:48 | ... + ... | ab47 | | ||
| constants/Stringified.java:15:33:15:42 | ... + ... | 97bc | | ||
| constants/Stringified.java:15:33:15:42 | ... + ... | abc | | ||
| constants/Stringified.java:16:33:16:43 | ... + ... | truebc | | ||
| constants/Stringified.java:17:32:17:40 | ... + ... | 42bc | | ||
| constants/Stringified.java:18:35:18:45 | ... + ... | 43.0bc | | ||
| constants/Stringified.java:19:34:19:45 | ... + ... | 44.0bc | | ||
| constants/Stringified.java:20:33:20:42 | ... + ... | 45bc | | ||
| constants/Stringified.java:21:34:21:50 | ... + ... | 46bc | | ||
| constants/Stringified.java:22:33:22:48 | ... + ... | 47bc | | ||
| constants/Stringified.java:24:37:24:46 | ... + ... | a100.0 | | ||
| constants/Stringified.java:25:38:25:58 | ... + ... | atrue | | ||
| constants/Stringified.java:26:38:26:52 | ... + ... | a42 | | ||
| constants/Values.java:91:37:91:45 | "a" + "b" | ab | | ||
| constants/Values.java:92:35:92:44 | ... + ... | ab99 | | ||
| constants/Values.java:92:35:92:44 | ... + ... | abc | |
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import semmle.code.java.Variable | ||
import java | ||
|
||
from Variable v, CompileTimeConstantExpr init, RefType enclosing, string constant | ||
where | ||
v.getInitializer() = init and | ||
init.getEnclosingCallable().getDeclaringType() = enclosing and | ||
enclosing.hasQualifiedName("constants", "Values") and | ||
enclosing.hasQualifiedName("constants", ["Values", "Stringified"]) and | ||
constant = init.getStringValue() | ||
select init, constant |
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.
Uh oh!
There was an error while loading. Please reload this page.