@@ -36,7 +36,8 @@ open class JavaFullIdentListener(fileName: String, val classes: List<String>) :
3636
3737 private var imports: MutableList <CodeImport > = mutableListOf ()
3838 // Index for fast import lookup by class name (O(1) instead of O(n))
39- private var importsByClassName: MutableMap <String , CodeImport > = mutableMapOf ()
39+ // Using MutableList to handle import collisions (same class name from different packages)
40+ private var importsByClassName: MutableMap <String , MutableList <CodeImport >> = mutableMapOf ()
4041 private var importsByFullSource: MutableMap <String , CodeImport > = mutableMapOf ()
4142
4243 private var lastNode = CodeDataStruct ()
@@ -101,12 +102,20 @@ open class JavaFullIdentListener(fileName: String, val classes: List<String>) :
101102 codeContainer.Imports + = codeImport
102103
103104 // Build import indexes for fast lookup
104- val className = fullSource.substringAfterLast(' .' )
105- importsByClassName[className] = codeImport
106105 importsByFullSource[fullSource] = codeImport
107- // Also index by source for static imports
106+
108107 if (isStatic) {
108+ // For static imports, index by the declaring class name (from Source),
109+ // not by the static member name
110+ val sourceClassName = codeImport.Source .substringAfterLast(' .' )
111+ importsByClassName.getOrPut(sourceClassName) { mutableListOf () }.add(codeImport)
112+ // Also index by source for static imports (fully-qualified class name)
109113 importsByFullSource[codeImport.Source ] = codeImport
114+ } else if (! isWildcard) {
115+ // For non-static, non-wildcard imports, index by the imported class name
116+ // Skip wildcard imports as they cannot be efficiently indexed
117+ val className = fullSource.substringAfterLast(' .' )
118+ importsByClassName.getOrPut(className) { mutableListOf () }.add(codeImport)
110119 }
111120 }
112121
@@ -535,9 +544,11 @@ open class JavaFullIdentListener(fileName: String, val classes: List<String>) :
535544 // second, parse from import using index (O(1) lookup)
536545 val pureTargetType = buildPureTargetType(targetType)
537546 if (pureTargetType.isNotEmpty()) {
538- // Fast lookup using index
539- val importByClassName = importsByClassName[pureTargetType]
540- if (importByClassName != null ) {
547+ // Fast lookup using index - check if there are any imports for this class name
548+ val importsByName = importsByClassName[pureTargetType]
549+ if (importsByName != null && importsByName.isNotEmpty()) {
550+ // Use the first matching import (consistent with original behavior)
551+ val importByClassName = importsByName[0 ]
541552 val isStatic = importByClassName.UsageName .isNotEmpty() && importByClassName.UsageName .contains(pureTargetType)
542553 return JavaTargetType (
543554 targetType = importByClassName.Source ,
@@ -558,10 +569,10 @@ open class JavaFullIdentListener(fileName: String, val classes: List<String>) :
558569
559570 // others, may be from parent
560571 if (pureTargetType == " super" || pureTargetType == " this" ) {
561- val importByExtend = importsByClassName[currentClzExtend]
562- if (importByExtend != null ) {
572+ val importsByExtend = importsByClassName[currentClzExtend]
573+ if (importsByExtend != null && importsByExtend.isNotEmpty() ) {
563574 return JavaTargetType (
564- targetType = importByExtend .Source ,
575+ targetType = importsByExtend[ 0 ] .Source ,
565576 callType = CallType .SUPER
566577 )
567578 }
0 commit comments