Skip to content

Commit ffdc752

Browse files
authored
Merge pull request github#6059 from smowton/smowton/fix/qualified-name-generic-types
Adapt to static methods and nested types returning unbound declaring types
2 parents 8518e7c + 9c91d1a commit ffdc752

File tree

10 files changed

+64
-24
lines changed

10 files changed

+64
-24
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lgtm,codescanning
2+
* Static inner classes and static methods' enclosing and declaring types are now unbound rather than raw types. This means that, for example, Map.Entry's name is now `Map$Entry` not `Map<>$Entry` as before. This may impact custom queries that explicitly named these types.

java/ql/src/semmle/code/java/Generics.qll

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,25 @@ class GenericType extends RefType {
7575
* Gets the number of type parameters of this generic type.
7676
*/
7777
int getNumberOfTypeParameters() { result = strictcount(getATypeParameter()) }
78+
79+
override string getAPrimaryQlClass() { result = "GenericType" }
7880
}
7981

8082
/** A generic type that is a class. */
81-
class GenericClass extends GenericType, Class { }
83+
class GenericClass extends GenericType, Class {
84+
override string getAPrimaryQlClass() {
85+
result = Class.super.getAPrimaryQlClass() or
86+
result = GenericType.super.getAPrimaryQlClass()
87+
}
88+
}
8289

8390
/** A generic type that is an interface. */
84-
class GenericInterface extends GenericType, Interface { }
91+
class GenericInterface extends GenericType, Interface {
92+
override string getAPrimaryQlClass() {
93+
result = Interface.super.getAPrimaryQlClass() or
94+
result = GenericType.super.getAPrimaryQlClass()
95+
}
96+
}
8597

8698
/**
8799
* A common super-class for Java types that may have a type bound.
@@ -115,6 +127,8 @@ abstract class BoundedType extends RefType, @boundedtype {
115127
or
116128
result = getUpperBoundType().(BoundedType).getAnUltimateUpperBoundType()
117129
}
130+
131+
override string getAPrimaryQlClass() { result = "BoundedType" }
118132
}
119133

120134
/**
@@ -354,13 +368,25 @@ class ParameterizedType extends RefType {
354368

355369
/** Holds if this type originates from source code. */
356370
override predicate fromSource() { typeVars(_, _, _, _, this) and RefType.super.fromSource() }
371+
372+
override string getAPrimaryQlClass() { result = "ParameterizedType" }
357373
}
358374

359375
/** A parameterized type that is a class. */
360-
class ParameterizedClass extends Class, ParameterizedType { }
376+
class ParameterizedClass extends Class, ParameterizedType {
377+
override string getAPrimaryQlClass() {
378+
result = Class.super.getAPrimaryQlClass() or
379+
result = ParameterizedType.super.getAPrimaryQlClass()
380+
}
381+
}
361382

362383
/** A parameterized type that is an interface. */
363-
class ParameterizedInterface extends Interface, ParameterizedType { }
384+
class ParameterizedInterface extends Interface, ParameterizedType {
385+
override string getAPrimaryQlClass() {
386+
result = Interface.super.getAPrimaryQlClass() or
387+
result = ParameterizedType.super.getAPrimaryQlClass()
388+
}
389+
}
364390

365391
/**
366392
* The raw version of a generic type is the type that is formed by
@@ -384,13 +410,25 @@ class RawType extends RefType {
384410

385411
/** Holds if this type originates from source code. */
386412
override predicate fromSource() { not any() }
413+
414+
override string getAPrimaryQlClass() { result = "RawType" }
387415
}
388416

389417
/** A raw type that is a class. */
390-
class RawClass extends Class, RawType { }
418+
class RawClass extends Class, RawType {
419+
override string getAPrimaryQlClass() {
420+
result = Class.super.getAPrimaryQlClass() or
421+
result = RawType.super.getAPrimaryQlClass()
422+
}
423+
}
391424

392425
/** A raw type that is an interface. */
393-
class RawInterface extends Interface, RawType { }
426+
class RawInterface extends Interface, RawType {
427+
override string getAPrimaryQlClass() {
428+
result = Interface.super.getAPrimaryQlClass() or
429+
result = RawType.super.getAPrimaryQlClass()
430+
}
431+
}
394432

395433
// -------- Generic callables --------
396434
/**

java/ql/src/semmle/code/java/dataflow/internal/ContainerFlow.qll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ private import semmle.code.java.dataflow.ExternalFlow
77

88
private class EntryType extends RefType {
99
EntryType() {
10-
this.getSourceDeclaration().getASourceSupertype*().hasQualifiedName("java.util", "Map<>$Entry")
10+
this.getSourceDeclaration().getASourceSupertype*().hasQualifiedName("java.util", "Map$Entry")
1111
}
1212

1313
RefType getValueType() {
14-
exists(GenericType t | t.hasQualifiedName("java.util", "Map<>$Entry") |
14+
exists(GenericType t | t.hasQualifiedName("java.util", "Map$Entry") |
1515
indirectlyInstantiates(this, t, 1, result)
1616
)
1717
}
@@ -95,10 +95,10 @@ private class ContainerFlowSummaries extends SummaryModelCsv {
9595
override predicate row(string row) {
9696
row =
9797
[
98-
"java.util;Map<>$Entry;true;getKey;;;MapKey of Argument[-1];ReturnValue;value",
99-
"java.util;Map<>$Entry;true;getValue;;;MapValue of Argument[-1];ReturnValue;value",
100-
"java.util;Map<>$Entry;true;setValue;;;MapValue of Argument[-1];ReturnValue;value",
101-
"java.util;Map<>$Entry;true;setValue;;;Argument[0];MapValue of Argument[-1];value",
98+
"java.util;Map$Entry;true;getKey;;;MapKey of Argument[-1];ReturnValue;value",
99+
"java.util;Map$Entry;true;getValue;;;MapValue of Argument[-1];ReturnValue;value",
100+
"java.util;Map$Entry;true;setValue;;;MapValue of Argument[-1];ReturnValue;value",
101+
"java.util;Map$Entry;true;setValue;;;Argument[0];MapValue of Argument[-1];value",
102102
"java.lang;Iterable;true;iterator;();;Element of Argument[-1];Element of ReturnValue;value",
103103
"java.lang;Iterable;true;spliterator;();;Element of Argument[-1];Element of ReturnValue;value",
104104
"java.util;Iterator;true;next;;;Element of Argument[-1];ReturnValue;value",

java/ql/src/semmle/code/java/frameworks/play/Play.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ class PlayMvcHttpRequestHeader extends RefType {
2626
}
2727

2828
/**
29-
* A `play.mvc.BodyParser<>$Of` annotation.
29+
* A `play.mvc.BodyParser$Of` annotation.
3030
*/
3131
class PlayBodyParserAnnotation extends Annotation {
32-
PlayBodyParserAnnotation() { this.getType().hasQualifiedName("play.mvc", "BodyParser<>$Of") }
32+
PlayBodyParserAnnotation() { this.getType().hasQualifiedName("play.mvc", "BodyParser$Of") }
3333
}
3434

3535
/**

java/ql/test/library-tests/dataflow/collections/B.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,22 @@ static void sink(Object obj) { }
4949

5050
void foo() throws InterruptedException {
5151
{
52-
// "java.util;Map<>$Entry;true;getKey;;;MapKey of Argument[-1];ReturnValue;value",
52+
// "java.util;Map$Entry;true;getKey;;;MapKey of Argument[-1];ReturnValue;value",
5353
Object out = null;
5454
Object in = storeMapKeyEntry(source()); out = ((Map.Entry)in).getKey(); sink(out); // $ hasValueFlow
5555
}
5656
{
57-
// "java.util;Map<>$Entry;true;getValue;;;MapValue of Argument[-1];ReturnValue;value",
57+
// "java.util;Map$Entry;true;getValue;;;MapValue of Argument[-1];ReturnValue;value",
5858
Object out = null;
5959
Object in = storeMapValueEntry(source()); out = ((Map.Entry)in).getValue(); sink(out); // $ hasValueFlow
6060
}
6161
{
62-
// "java.util;Map<>$Entry;true;setValue;;;MapValue of Argument[-1];ReturnValue;value",
62+
// "java.util;Map$Entry;true;setValue;;;MapValue of Argument[-1];ReturnValue;value",
6363
Object out = null;
6464
Object in = storeMapValueEntry(source()); out = ((Map.Entry)in).setValue(null); sink(out); // $ hasValueFlow
6565
}
6666
{
67-
// "java.util;Map<>$Entry;true;setValue;;;Argument[0];MapValue of Argument[-1];value",
67+
// "java.util;Map$Entry;true;setValue;;;Argument[0];MapValue of Argument[-1];value",
6868
Map.Entry out = null;
6969
Object in = source(); out.setValue(in); sink(readMapValue(out)); // $ hasValueFlow
7070
}

java/ql/test/library-tests/dependency-counts/PrintAst.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Example.java:
33
#-----| -1: (Imports)
44
# 1| 1: [ImportType] import Set
55
# 2| 2: [ImportType] import List
6-
# 4| 1: [Interface] Example
6+
# 4| 1: [GenericType,Interface,ParameterizedType] Example
77
#-----| -2: (Generic Parameters)
88
# 4| 0: [TypeVariable] A
99
#-----| -1: (Base Types)

java/ql/test/library-tests/dependency/PrintAst.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
dependency/A.java:
22
# 0| [CompilationUnit] A
3-
# 3| 1: [Class] A
3+
# 3| 1: [Class,GenericType,ParameterizedType] A
44
#-----| -2: (Generic Parameters)
55
# 3| 0: [TypeVariable] T
66
# 6| 2: [Class] B
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
| play.mvc.BodyParser<>$Of |
1+
| play.mvc.BodyParser$Of |

java/ql/test/library-tests/generics/PrintAst.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ generics/A.java:
33
#-----| -1: (Imports)
44
# 3| 1: [ImportType] import HashMap
55
# 4| 2: [ImportType] import Map
6-
# 6| 1: [Class] A
6+
# 6| 1: [Class,GenericType,ParameterizedType] A
77
#-----| -2: (Generic Parameters)
88
# 6| 0: [TypeVariable] T
99
# 7| 2: [Class] B
@@ -23,7 +23,7 @@ generics/A.java:
2323
# 13| -3: [TypeAccess] HashMap<String,Object>
2424
# 13| 0: [TypeAccess] String
2525
# 13| 1: [TypeAccess] Object
26-
# 16| 3: [Class] D
26+
# 16| 3: [Class,GenericType,ParameterizedType] D
2727
#-----| -2: (Generic Parameters)
2828
# 16| 0: [TypeVariable] V
2929
# 16| 0: [TypeAccess] Number

java/ql/test/library-tests/typeaccesses/PrintAst.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ typeaccesses/TA.java:
8484
# 13| 0: [ExprStmt] <Expr>;
8585
# 13| 0: [MethodAccess] method3(...)
8686
# 13| -1: [TypeAccess] TA
87-
# 14| 10: [Class] Inner
87+
# 14| 10: [Class,GenericType,ParameterizedType] Inner
8888
#-----| -2: (Generic Parameters)
8989
# 14| 0: [TypeVariable] T
9090
# 14| 0: [TypeAccess] ArrayList<TA>

0 commit comments

Comments
 (0)