diff --git a/chapi-ast-java/src/main/kotlin/chapi/ast/javaast/JavaFullIdentListener.kt b/chapi-ast-java/src/main/kotlin/chapi/ast/javaast/JavaFullIdentListener.kt index a5f9acee..617b3c8f 100644 --- a/chapi-ast-java/src/main/kotlin/chapi/ast/javaast/JavaFullIdentListener.kt +++ b/chapi-ast-java/src/main/kotlin/chapi/ast/javaast/JavaFullIdentListener.kt @@ -237,7 +237,23 @@ open class JavaFullIdentListener( } override fun exitMethodDeclaration(ctx: JavaParser.MethodDeclarationContext?) { -// currentFunction = CodeFunction(IsConstructor = false) + if (localVars.isNotEmpty()) { + addLocalVarsToFunction() + } + } + + private fun addLocalVarsToFunction() { + val currentMethodName = getMethodMapName(currentFunction) + val mapFunc = methodMap[currentMethodName] + if (mapFunc != null) { + var vars: Array = arrayOf() + for (entry in localVars) { + val param = CodeProperty(TypeValue = entry.key, TypeType = entry.value) + vars += param + } + mapFunc.LocalVariables = vars + methodMap[currentMethodName] = mapFunc + } } override fun enterMethodCall(ctx: JavaParser.MethodCallContext?) { diff --git a/chapi-ast-java/src/test/kotlin/chapi/ast/javaast/JavaFullIdentListenerTest.kt b/chapi-ast-java/src/test/kotlin/chapi/ast/javaast/JavaFullIdentListenerTest.kt index f061c102..67c48865 100644 --- a/chapi-ast-java/src/test/kotlin/chapi/ast/javaast/JavaFullIdentListenerTest.kt +++ b/chapi-ast-java/src/test/kotlin/chapi/ast/javaast/JavaFullIdentListenerTest.kt @@ -310,7 +310,6 @@ public class HostDependentDownloadableContribution { """ val codeFile = JavaAnalyser().identFullInfo(code, "") - println(codeFile.DataStructures[0].toString()) assertEquals(codeFile.DataStructures[0].Functions.size, 1) val dsFunction = codeFile.DataStructures[0].Functions[0] assertEquals(dsFunction.InnerStructures.size, 2) @@ -321,4 +320,25 @@ public class HostDependentDownloadableContribution { assertEquals(dsFunction.InnerStructures[1].Functions[0].Name, "getOsName") assertEquals(dsFunction.InnerStructures[1].Functions[1].Name, "getOsArch") } + + + @Test + fun shouldEnableGetLocalVarsForFunction() { + var code = """ +package adapters.outbound.persistence.blog; + +public class LexerTest { + static void main(String[] args) { + long creditCardNumber = 1234_5678_9012_3456L; + long socialSecurityNumber = 999_99_9999L; + } +} + """ + + val codeFile = JavaAnalyser().identFullInfo(code, "") + println(codeFile.DataStructures[0].Functions[0]) + assertEquals(codeFile.DataStructures[0].Functions[0].LocalVariables.size, 3) + assertEquals(codeFile.DataStructures[0].Functions[0].LocalVariables[0].TypeValue, "args") + assertEquals(codeFile.DataStructures[0].Functions[0].LocalVariables[0].TypeType, "String[]") + } } diff --git a/chapi-domain/src/main/kotlin/chapi/domain/core/CodeFunction.kt b/chapi-domain/src/main/kotlin/chapi/domain/core/CodeFunction.kt index 0d1b5c38..0ccfb297 100644 --- a/chapi-domain/src/main/kotlin/chapi/domain/core/CodeFunction.kt +++ b/chapi-domain/src/main/kotlin/chapi/domain/core/CodeFunction.kt @@ -18,7 +18,7 @@ open class CodeFunction( var InnerFunctions: Array = arrayOf(), var Position: CodePosition = CodePosition(), var Extension: JsonElement = JsonObject(HashMap()), - + var LocalVariables : Array = arrayOf(), var IsConstructor: Boolean = false // todo: move to extension ) { private var extensionMap = HashMap()