Skip to content

Commit 42a046e

Browse files
authored
Merge pull request #7004 from Marcono1234/marcono1234/deprecate-StringLiteral-getRepresentedString
Java: Deprecate `StringLiteral.getRepresentedString()`
2 parents 18b0806 + 6689280 commit 42a046e

28 files changed

+109
-104
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lgtm,codescanning
2+
* The predicate `StringLiteral.getRepresentedString()` has been deprecated for removal in a future version because it is just an alias for `getValue()`. That predicate should be used instead.

java/ql/lib/semmle/code/java/Expr.qll

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class CompileTimeConstantExpr extends Expr {
166166
*/
167167
pragma[nomagic]
168168
string getStringValue() {
169-
result = this.(StringLiteral).getRepresentedString()
169+
result = this.(StringLiteral).getValue()
170170
or
171171
result =
172172
this.(AddExpr).getLeftOperand().(CompileTimeConstantExpr).getStringValue() +
@@ -745,9 +745,21 @@ class CharacterLiteral extends Literal, @characterliteral {
745745
*/
746746
class StringLiteral extends Literal, @stringliteral {
747747
/**
748+
* Gets the string represented by this string literal, that is, the content
749+
* of the literal without enclosing quotes and with escape sequences translated.
750+
*
751+
* Unpaired Unicode surrogate characters (U+D800 to U+DFFF) are replaced with the
752+
* replacement character U+FFFD.
753+
*/
754+
override string getValue() { result = super.getValue() }
755+
756+
/**
757+
* DEPRECATED: This predicate will be removed in a future version because
758+
* it is just an alias for `getValue()`; that predicate should be used instead.
759+
*
748760
* Gets the literal string without the quotes.
749761
*/
750-
string getRepresentedString() { result = this.getValue() }
762+
deprecated string getRepresentedString() { result = this.getValue() }
751763

752764
/** Holds if this string literal is a text block (`""" ... """`). */
753765
predicate isTextBlock() { this.getLiteral().matches("\"\"\"%") }

java/ql/lib/semmle/code/java/JDKAnnotations.qll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ class SuppressWarningsAnnotation extends Annotation {
2525
}
2626

2727
/** Gets the name of a warning suppressed by this annotation. */
28-
string getASuppressedWarning() {
29-
result = this.getASuppressedWarningLiteral().getRepresentedString()
30-
}
28+
string getASuppressedWarning() { result = this.getASuppressedWarningLiteral().getValue() }
3129
}
3230

3331
/** A `@Target` annotation. */

java/ql/lib/semmle/code/java/Reflection.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class ReflectiveClassIdentifierMethodAccess extends ReflectiveClassIdentifier, M
7575
/**
7676
* If the argument to this call is a `StringLiteral`, then return that string.
7777
*/
78-
string getTypeName() { result = this.getArgument(0).(StringLiteral).getRepresentedString() }
78+
string getTypeName() { result = this.getArgument(0).(StringLiteral).getValue() }
7979

8080
override RefType getReflectivelyIdentifiedClass() {
8181
// We only handle cases where the class is specified as a string literal to this call.
@@ -360,7 +360,7 @@ class ReflectiveMethodAccess extends ClassMethodAccess {
360360
this.getInferredClassType().inherits(result)
361361
) and
362362
// Only consider instances where the method name is provided as a `StringLiteral`.
363-
result.hasName(this.getArgument(0).(StringLiteral).getRepresentedString())
363+
result.hasName(this.getArgument(0).(StringLiteral).getValue())
364364
}
365365
}
366366

@@ -400,6 +400,6 @@ class ReflectiveFieldAccess extends ClassMethodAccess {
400400
this.getInferredClassType().inherits(result)
401401
)
402402
) and
403-
result.hasName(this.getArgument(0).(StringLiteral).getRepresentedString())
403+
result.hasName(this.getArgument(0).(StringLiteral).getValue())
404404
}
405405
}

java/ql/lib/semmle/code/java/StringFormat.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ private predicate formatStringFragment(Expr fmt) {
279279
private predicate formatStringValue(Expr e, string fmtvalue) {
280280
formatStringFragment(e) and
281281
(
282-
e.(StringLiteral).getRepresentedString() = fmtvalue
282+
e.(StringLiteral).getValue() = fmtvalue
283283
or
284284
e.getType() instanceof IntegralType and fmtvalue = "1" // dummy value
285285
or
@@ -318,7 +318,7 @@ private predicate formatStringValue(Expr e, string fmtvalue) {
318318
getprop.hasName("getProperty") and
319319
getprop.getDeclaringType().hasQualifiedName("java.lang", "System") and
320320
getprop.getNumberOfParameters() = 1 and
321-
ma.getAnArgument().(StringLiteral).getRepresentedString() = prop and
321+
ma.getAnArgument().(StringLiteral).getValue() = prop and
322322
(prop = "line.separator" or prop = "file.separator" or prop = "path.separator") and
323323
fmtvalue = "x" // dummy value
324324
)

java/ql/lib/semmle/code/java/UnitTests.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class TestNGTestMethod extends Method {
162162
testAnnotation = this.getAnAnnotation() and
163163
// The data provider must have the same name as the referenced data provider
164164
result.getDataProviderName() =
165-
testAnnotation.getValue("dataProvider").(StringLiteral).getRepresentedString()
165+
testAnnotation.getValue("dataProvider").(StringLiteral).getValue()
166166
|
167167
// Either the data provider should be on the current class, or a supertype
168168
this.getDeclaringType().getAnAncestor() = result.getDeclaringType()
@@ -258,7 +258,7 @@ class TestNGDataProviderMethod extends Method {
258258
.(TestNGDataProviderAnnotation)
259259
.getValue("name")
260260
.(StringLiteral)
261-
.getRepresentedString()
261+
.getValue()
262262
}
263263
}
264264

java/ql/lib/semmle/code/java/dataflow/internal/TaintTrackingUtil.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,8 @@ private predicate unsafeEscape(MethodAccess ma) {
300300
// Removing `<script>` tags using a string-replace method is
301301
// unsafe if such a tag is embedded inside another one (e.g. `<scr<script>ipt>`).
302302
exists(StringReplaceMethod m | ma.getMethod() = m |
303-
ma.getArgument(0).(StringLiteral).getRepresentedString() = "(<script>)" and
304-
ma.getArgument(1).(StringLiteral).getRepresentedString() = ""
303+
ma.getArgument(0).(StringLiteral).getValue() = "(<script>)" and
304+
ma.getArgument(1).(StringLiteral).getValue() = ""
305305
)
306306
}
307307

java/ql/lib/semmle/code/java/frameworks/spring/SpringComponentScan.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ class SpringComponentScan extends Annotation {
3737
*/
3838
string getBasePackages() {
3939
// "value" and "basePackages" are synonymous, and are simple strings
40-
result = this.getAValue("basePackages").(StringLiteral).getRepresentedString()
40+
result = this.getAValue("basePackages").(StringLiteral).getValue()
4141
or
42-
result = this.getAValue("value").(StringLiteral).getRepresentedString()
42+
result = this.getAValue("value").(StringLiteral).getValue()
4343
or
4444
exists(TypeLiteral typeLiteral |
4545
// Base package classes are type literals whose package should be considered a base package.
@@ -201,7 +201,7 @@ class SpringComponent extends RefType {
201201
.getType()
202202
.hasQualifiedName("org.springframework.context.annotation", "Profile")
203203
|
204-
result = profileAnnotation.getAValue("value").(StringLiteral).getRepresentedString()
204+
result = profileAnnotation.getAValue("value").(StringLiteral).getValue()
205205
)
206206
}
207207
}

java/ql/lib/semmle/code/java/security/ControlledString.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ private predicate boxedToString(Method method) {
2222
* it is better to use a prepared query than to just put single quotes around the string.
2323
*/
2424
predicate endsInQuote(Expr expr) {
25-
exists(string str | str = expr.(StringLiteral).getRepresentedString() | str.matches("%'"))
25+
exists(string str | str = expr.(StringLiteral).getValue() | str.matches("%'"))
2626
or
2727
exists(Variable var | expr = var.getAnAccess() | endsInQuote(var.getAnAssignedValue()))
2828
or

java/ql/lib/semmle/code/java/security/HttpsUrls.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ private import semmle.code.java.frameworks.Networking
1212
*/
1313
class HttpStringLiteral extends StringLiteral {
1414
HttpStringLiteral() {
15-
exists(string s | this.getRepresentedString() = s |
15+
exists(string s | this.getValue() = s |
1616
s = "http"
1717
or
1818
s.matches("http://%") and
1919
not s.substring(7, s.length()) instanceof PrivateHostName and
2020
not TaintTracking::localExprTaint(any(StringLiteral p |
21-
p.getRepresentedString() instanceof PrivateHostName
21+
p.getValue() instanceof PrivateHostName
2222
), this.getParent*())
2323
)
2424
}

0 commit comments

Comments
 (0)