Skip to content

Commit

Permalink
feat: <java> add local vars to function for #1
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Feb 17, 2020
1 parent a5b4099 commit f35b143
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<CodeProperty> = 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?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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[]")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ open class CodeFunction(
var InnerFunctions: Array<CodeFunction> = arrayOf(),
var Position: CodePosition = CodePosition(),
var Extension: JsonElement = JsonObject(HashMap()),

var LocalVariables : Array<CodeProperty> = arrayOf(),
var IsConstructor: Boolean = false // todo: move to extension
) {
private var extensionMap = HashMap<String, JsonElement>()
Expand Down

0 comments on commit f35b143

Please # to comment.