Skip to content

Commit f680838

Browse files
Merge pull request github#12174 from joefarebrother/stubgen-name-conflicts
Java: Stub generator: Use fully qualified names to avoid conflicts
2 parents 0600a2b + 0b722bf commit f680838

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

java/ql/src/utils/stub-generator/Stubs.qll

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,22 @@ private string stubQualifier(RefType t) {
279279
exists(RefType et | et = t.(NestedType).getEnclosingType().getSourceDeclaration() |
280280
result = stubQualifier(et) + et.getName() + "."
281281
)
282-
else result = ""
282+
else
283+
if needsPackageName(t)
284+
then result = t.getPackage().getName() + "."
285+
else result = ""
286+
}
287+
288+
/**
289+
* Holds if `t` may clash with another type of the same name, so should be referred to using the fully qualified name
290+
*/
291+
private predicate needsPackageName(RefType t) {
292+
exists(GeneratedTopLevel top, RefType other |
293+
t.getSourceDeclaration() = [getAReferencedType(top), top].getSourceDeclaration() and
294+
other.getSourceDeclaration() = [getAReferencedType(top), top].getSourceDeclaration() and
295+
t.getName() = other.getName() and
296+
t != other
297+
)
283298
}
284299

285300
language[monotonicAggregates]
@@ -503,7 +518,8 @@ class GeneratedTopLevel extends TopLevelType instanceof GeneratedType {
503518
GeneratedTopLevel() { this = this.getSourceDeclaration() }
504519

505520
private TopLevelType getAnImportedType() {
506-
result = getAReferencedType(this).getSourceDeclaration()
521+
result = getAReferencedType(this).getSourceDeclaration() and
522+
not needsPackageName(result) // use the fully qualified name rather than importing it if it may cause name clashes
507523
}
508524

509525
private string stubAnImport() {

0 commit comments

Comments
 (0)