@@ -11,12 +11,12 @@ data class JavaTargetType(var targetType: String = "", var callType: CallType =
1111
1212open class JavaFullIdentListener (fileName : String , val classes : List <String >) : JavaAstListener() {
1313 private var isEnterFunction: Boolean = false
14- private var currentAnnotations: List <CodeAnnotation > = listOf ()
14+ private var currentAnnotations: MutableList <CodeAnnotation > = mutableListOf ()
1515
1616 private var currentCreatorNode: CodeDataStruct = CodeDataStruct ()
1717 private var isOverrideMethod: Boolean = false
18- private var fields = listOf <CodeField >()
19- private var methodCalls = listOf <CodeCall >()
18+ private var fields: MutableList <CodeField > = mutableListOf ()
19+ private var methodCalls: MutableList <CodeCall > = mutableListOf ()
2020 private var methodMap = mutableMapOf<String , CodeFunction >()
2121 private var creatorMethodMap = mutableMapOf<String , CodeFunction >()
2222 private var localVars = mutableMapOf<String , String >()
@@ -27,14 +27,17 @@ open class JavaFullIdentListener(fileName: String, val classes: List<String>) :
2727
2828 private var currentClzExtend: String = " "
2929 private var hasEnterClass: Boolean = false
30- private var classNodes: List <CodeDataStruct > = listOf ()
30+ private var classNodes: MutableList <CodeDataStruct > = mutableListOf ()
3131
3232 private var innerNode: CodeDataStruct = CodeDataStruct ()
3333 private var classNodeStack = Stack <CodeDataStruct >()
3434
35- private var methodQueue: List <CodeFunction > = listOf ()
35+ private var methodQueue: MutableList <CodeFunction > = mutableListOf ()
3636
37- private var imports: List <CodeImport > = listOf ()
37+ private var imports: MutableList <CodeImport > = mutableListOf ()
38+ // Index for fast import lookup by class name (O(1) instead of O(n))
39+ private var importsByClassName: MutableMap <String , CodeImport > = mutableMapOf ()
40+ private var importsByFullSource: MutableMap <String , CodeImport > = mutableMapOf ()
3841
3942 private var lastNode = CodeDataStruct ()
4043 private var currentNode = CodeDataStruct ()
@@ -94,8 +97,17 @@ open class JavaFullIdentListener(fileName: String, val classes: List<String>) :
9497
9598 codeImport.PathSegments = fullSource.split(" ." )
9699
97- imports + = codeImport
100+ imports.add( codeImport)
98101 codeContainer.Imports + = codeImport
102+
103+ // Build import indexes for fast lookup
104+ val className = fullSource.substringAfterLast(' .' )
105+ importsByClassName[className] = codeImport
106+ importsByFullSource[fullSource] = codeImport
107+ // Also index by source for static imports
108+ if (isStatic) {
109+ importsByFullSource[codeImport.Source ] = codeImport
110+ }
99111 }
100112
101113 override fun enterClassDeclaration (ctx : JavaParser .ClassDeclarationContext ? ) {
@@ -173,10 +185,10 @@ open class JavaFullIdentListener(fileName: String, val classes: List<String>) :
173185 }
174186
175187 private fun exitBody () {
176- currentNode.Fields = fields
188+ currentNode.Fields = fields.toList()
177189 currentNode.setMethodsFromMap(methodMap)
178190
179- classNodes + = currentNode
191+ classNodes.add( currentNode)
180192 initClass()
181193 }
182194
@@ -186,8 +198,8 @@ open class JavaFullIdentListener(fileName: String, val classes: List<String>) :
186198 currentFunction = CodeFunction (IsConstructor = false )
187199
188200 methodMap = mutableMapOf ()
189- methodCalls = listOf ()
190- fields = listOf ()
201+ methodCalls = mutableListOf ()
202+ fields = mutableListOf ()
191203 isOverrideMethod = false
192204 }
193205
@@ -248,8 +260,9 @@ open class JavaFullIdentListener(fileName: String, val classes: List<String>) :
248260 }
249261
250262 private fun buildMethodParameters (params : JavaParser .FormalParametersContext ? ): List <CodeProperty > {
251- var methodParams = listOf<CodeProperty >()
252- if (params == null ) return methodParams
263+ if (params == null ) return emptyList()
264+
265+ val methodParams = mutableListOf<CodeProperty >()
253266
254267 // New grammar structure: formalParameters can contain formalParameter directly
255268 // and/or formalParameterList
@@ -269,7 +282,7 @@ open class JavaFullIdentListener(fileName: String, val classes: List<String>) :
269282 TypeType = paramType,
270283 TypeRef = JavaTypeRefBuilder .build(formalParam.typeType())
271284 )
272- methodParams + = parameter
285+ methodParams.add( parameter)
273286 }
274287 }
275288
@@ -287,7 +300,7 @@ open class JavaFullIdentListener(fileName: String, val classes: List<String>) :
287300 TypeType = paramType,
288301 TypeRef = JavaTypeRefBuilder .build(param.typeType())
289302 )
290- methodParams + = parameter
303+ methodParams.add( parameter)
291304 }
292305 }
293306 }
@@ -297,7 +310,7 @@ open class JavaFullIdentListener(fileName: String, val classes: List<String>) :
297310
298311 override fun exitMethodDeclaration (ctx : JavaParser .MethodDeclarationContext ? ) {
299312 this .isEnterFunction = false
300- this .currentAnnotations = listOf ()
313+ this .currentAnnotations = mutableListOf ()
301314 if (localVars.isNotEmpty()) {
302315 addLocalVarsToFunction()
303316 }
@@ -352,7 +365,7 @@ open class JavaFullIdentListener(fileName: String, val classes: List<String>) :
352365 }
353366
354367 private fun sendResultToMethodCallMap (codeCall : CodeCall ) {
355- methodCalls + = codeCall
368+ methodCalls.add( codeCall)
356369
357370 val currentMethodName = getMethodMapName(currentFunction)
358371 val method = methodMap[currentMethodName]
@@ -486,7 +499,7 @@ open class JavaFullIdentListener(fileName: String, val classes: List<String>) :
486499 creatorMethodMap[getMethodMapName(codeFunction)] = codeFunction
487500 } else {
488501 currentFunction = codeFunction
489- methodQueue + = currentFunction
502+ methodQueue.add( currentFunction)
490503 methodMap[getMethodMapName(codeFunction)] = codeFunction
491504 }
492505 }
@@ -497,7 +510,7 @@ open class JavaFullIdentListener(fileName: String, val classes: List<String>) :
497510 name = methodQueue[methodQueue.size - 1 ].Name
498511 }
499512
500- return codeContainer.PackageName + " . " + currentClz + " . " + name + " : " + method.Position .StartLine .toString()
513+ return " ${ codeContainer.PackageName } . ${ currentClz} . ${ name} : ${ method.Position .StartLine } "
501514 }
502515
503516 private fun buildExtend (extendName : String ): String {
@@ -514,26 +527,22 @@ open class JavaFullIdentListener(fileName: String, val classes: List<String>) :
514527 val callType: CallType = CallType .FUNCTION
515528 if (currentClz == targetType) {
516529 return JavaTargetType (
517- targetType = codeContainer.PackageName + " . " + targetType,
530+ targetType = " ${ codeContainer.PackageName } . ${ targetType} " ,
518531 callType = CallType .SELF
519532 )
520533 }
521534
522- // second, parse from import
535+ // second, parse from import using index (O(1) lookup)
523536 val pureTargetType = buildPureTargetType(targetType)
524- if (pureTargetType != " " ) {
525- for (imp in imports) {
526- if (imp.Source .endsWith(pureTargetType)) {
527- return JavaTargetType (
528- targetType = imp.Source ,
529- callType = CallType .CHAIN
530- )
531- } else if (imp.UsageName .isNotEmpty() && imp.UsageName .contains(pureTargetType)) {
532- return JavaTargetType (
533- targetType = imp.Source ,
534- callType = CallType .STATIC
535- )
536- }
537+ if (pureTargetType.isNotEmpty()) {
538+ // Fast lookup using index
539+ val importByClassName = importsByClassName[pureTargetType]
540+ if (importByClassName != null ) {
541+ val isStatic = importByClassName.UsageName .isNotEmpty() && importByClassName.UsageName .contains(pureTargetType)
542+ return JavaTargetType (
543+ targetType = importByClassName.Source ,
544+ callType = if (isStatic) CallType .STATIC else CallType .CHAIN
545+ )
537546 }
538547 }
539548
@@ -549,13 +558,12 @@ open class JavaFullIdentListener(fileName: String, val classes: List<String>) :
549558
550559 // others, may be from parent
551560 if (pureTargetType == " super" || pureTargetType == " this" ) {
552- for (imp in imports) {
553- if (imp.Source .endsWith(currentClzExtend)) {
554- return JavaTargetType (
555- targetType = imp.Source ,
556- callType = CallType .SUPER
557- )
558- }
561+ val importByExtend = importsByClassName[currentClzExtend]
562+ if (importByExtend != null ) {
563+ return JavaTargetType (
564+ targetType = importByExtend.Source ,
565+ callType = CallType .SUPER
566+ )
559567 }
560568 }
561569
@@ -619,15 +627,15 @@ open class JavaFullIdentListener(fileName: String, val classes: List<String>) :
619627 typeValue,
620628 typeKey,
621629 Modifiers = listOf (),
622- Annotations = this .currentAnnotations,
630+ Annotations = this .currentAnnotations.toList() ,
623631 TypeRef = typeRef
624632 )
625- fields + = field
633+ fields.add( field)
626634
627635 buildFieldCall(typeTypeText, ctx)
628636 }
629637
630- this .currentAnnotations = listOf ()
638+ this .currentAnnotations = mutableListOf ()
631639 }
632640
633641 private fun buildFieldCall (typeType : String , ctx : JavaParser .FieldDeclarationContext ) {
@@ -699,7 +707,7 @@ open class JavaFullIdentListener(fileName: String, val classes: List<String>) :
699707 currentNode.Annotations + = annotation
700708 }
701709 } else {
702- currentAnnotations + = this .buildAnnotation(ctx)
710+ currentAnnotations.add( this .buildAnnotation(ctx) )
703711 }
704712 }
705713
@@ -888,13 +896,11 @@ open class JavaFullIdentListener(fileName: String, val classes: List<String>) :
888896 }
889897
890898 open fun buildEnumImplements (ctx : JavaParser .EnumDeclarationContext ): List <String > {
891- var implements = listOf<String >()
892899 val type = ctx.typeList()
893900 var target = this .warpTargetFullType(type.text).targetType
894901 if (target == " " ) {
895902 target = type.text
896903 }
897- implements + = target
898- return implements
904+ return listOf (target)
899905 }
900906}
0 commit comments