Skip to content

Commit 61e6fb3

Browse files
Avoid name clashes with types from java.lang #1716 (#1721)
1 parent b5578ce commit 61e6fb3

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/ConstructorUtils.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import org.utbot.framework.plugin.api.util.objectClassId
5555
import org.utbot.framework.plugin.api.util.shortClassId
5656
import org.utbot.framework.plugin.api.util.signature
5757
import org.utbot.framework.plugin.api.util.underlyingType
58+
import soot.Scene
5859
import java.lang.reflect.Method
5960
import java.lang.reflect.Modifier
6061

@@ -210,13 +211,24 @@ internal fun infiniteInts(): Sequence<Int> =
210211

211212
internal const val MAX_ARRAY_INITIALIZER_SIZE = 10
212213

214+
private val simpleNamesNotRequiringImports by lazy { findSimpleNamesNotRequiringImports() }
215+
216+
/**
217+
* Some class names do not require imports, but may lead to simple names clash.
218+
* For example, custom class [Compiler] may clash with [java.lang.Compiler].
219+
*/
220+
private fun findSimpleNamesNotRequiringImports(): Set<String> =
221+
Scene.v().classes
222+
.filter { it.packageName == "java.lang" }
223+
.mapNotNullTo(mutableSetOf()) { it.shortName }
224+
213225
/**
214226
* Checks if we have already imported a class with such simple name.
215227
* If so, we cannot import [type] (because it will be used with simple name and will be clashed with already imported)
216228
* and should use its fully qualified name instead.
217229
*/
218230
private fun CgContextOwner.doesNotHaveSimpleNameClash(type: ClassId): Boolean =
219-
importedClasses.none { it.simpleName == type.simpleName }
231+
importedClasses.none { it.simpleName == type.simpleName } && type.simpleName !in simpleNamesNotRequiringImports
220232

221233
fun CgContextOwner.importIfNeeded(type: ClassId) {
222234
// TODO: for now we consider that tests are generated in the same package as CUT, but this may change

0 commit comments

Comments
 (0)