Skip to content

Commit e2cfc17

Browse files
committed
Fix output of nested and generic type names, and disambiguate overloads where necessary
1 parent 6d9661f commit e2cfc17

File tree

1 file changed

+43
-8
lines changed

1 file changed

+43
-8
lines changed

java/ql/src/utils/GenerateFlowTestCase.qll

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ string getZero(PrimitiveType t) {
2929
t.hasName("long") and result = "0L"
3030
}
3131

32-
string getFiller(Type t) {
33-
t instanceof RefType and result = "null"
34-
or
35-
result = getZero(t)
32+
predicate mayBeAmbiguous(Callable c) {
33+
exists(Callable other, string package, string type, string name |
34+
c.hasQualifiedName(package, type, name) and
35+
other.hasQualifiedName(package, type, name) and
36+
other.getNumberOfParameters() = c.getNumberOfParameters() and
37+
other != c
38+
)
3639
}
3740

3841
Content getContent(SummaryComponent component) { component = SummaryComponent::content(result) }
@@ -61,8 +64,16 @@ RefType getRootType(RefType t) {
6164
else result = t
6265
}
6366

67+
RefType replaceTypeVariable(RefType t) {
68+
if t instanceof TypeVariable
69+
then result = t.(TypeVariable).getFirstUpperBoundType()
70+
else result = t
71+
}
72+
6473
Type getRootSourceDeclaration(Type t) {
65-
if t instanceof RefType then result = getRootType(t).getSourceDeclaration() else result = t
74+
if t instanceof RefType
75+
then result = getRootType(replaceTypeVariable(t)).getSourceDeclaration()
76+
else result = t
6677
}
6778

6879
newtype TRowTestSnippet =
@@ -94,6 +105,19 @@ class RowTestSnippet extends TRowTestSnippet {
94105
baseOutput + " / " + preservesValue
95106
}
96107

108+
string getFiller(int argIdx) {
109+
exists(Type t | t = callable.getParameterType(argIdx) |
110+
t instanceof RefType and
111+
(
112+
if mayBeAmbiguous(callable)
113+
then result = "(" + getShortNameIfPossible(t.(RefType).getSourceDeclaration()) + ")null"
114+
else result = "null"
115+
)
116+
or
117+
result = getZero(t)
118+
)
119+
}
120+
97121
string getArgument(int i) {
98122
(i = -1 or exists(callable.getParameter(i))) and
99123
if baseInput = SummaryComponentStack::argument(i)
@@ -102,7 +126,7 @@ class RowTestSnippet extends TRowTestSnippet {
102126
if baseOutput = SummaryComponentStack::argument(i)
103127
then result = "out"
104128
else (
105-
if i = -1 then result = "instance" else result = getFiller(getParameterType(callable, i))
129+
if i = -1 then result = "instance" else result = this.getFiller(i)
106130
)
107131
)
108132
}
@@ -238,6 +262,9 @@ class RowTestSnippet extends TRowTestSnippet {
238262
getRootSourceDeclaration([
239263
this.getOutputType(), this.getInputType(), callable.getDeclaringType()
240264
])
265+
or
266+
// Will refer to parameter types in disambiguating casts, like `(String)null`
267+
mayBeAmbiguous(callable) and result = getRootSourceDeclaration(callable.getAParamType())
241268
}
242269

243270
string getATestSnippetForRow(string row_) {
@@ -263,8 +290,16 @@ predicate isImportable(Type t) {
263290

264291
string getShortNameIfPossible(Type t) {
265292
getRootSourceDeclaration(t) = any(RowTestSnippet r).getADesiredImport() and
266-
if t instanceof RefType and not isImportable(getRootSourceDeclaration(t))
267-
then result = t.(RefType).getPackage().getName() + "." + t.getName()
293+
if t instanceof RefType
294+
then
295+
exists(RefType replaced, string nestedName |
296+
replaced = replaceTypeVariable(t) and
297+
nestedName = replaced.nestedName().replaceAll("$", ".")
298+
|
299+
if isImportable(getRootSourceDeclaration(t))
300+
then result = nestedName
301+
else result = replaced.getPackage().getName() + "." + nestedName
302+
)
268303
else result = t.getName()
269304
}
270305

0 commit comments

Comments
 (0)