Skip to content

Commit

Permalink
feat: <java> add package & import support
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Feb 3, 2020
1 parent 3b43527 commit c6c9961
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ open class JavaIdentApp {

open fun parse(str: String): JavaParser {
val fromString = CharStreams.fromString(str)
val lexer: JavaLexer = JavaLexer (fromString)
val lexer = JavaLexer(fromString)
val tokenStream = CommonTokenStream(lexer)
val javaParser = JavaParser(tokenStream)
return javaParser
val parser = JavaParser(tokenStream)
return parser
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,29 @@ package chapi.ast.javaast

import chapi.ast.antlr.JavaParser
import chapi.ast.antlr.JavaParserBaseListener
import domain.core.CodeDataStruct
import domain.core.CodeFile
import domain.core.CodeFunction
import domain.core.CodeImport

class JavaIdentListener(fileName: String) : JavaParserBaseListener() {
private var nodes : Array<CodeDataStruct> = arrayOf()
private var imports : Array<CodeImport> = arrayOf()
private var currentNode = CodeDataStruct()
private var currentFunction = CodeFunction()
private var codeFile: CodeFile = CodeFile(FullName = fileName)

override fun enterPackageDeclaration(ctx: JavaParser.PackageDeclarationContext?) {
super.enterPackageDeclaration(ctx)
codeFile.PackageName = ctx?.qualifiedName()!!.text
}

override fun enterImportDeclaration(ctx: JavaParser.ImportDeclarationContext?) {
super.enterImportDeclaration(ctx)
val codeImport = CodeImport(Source = ctx!!.qualifiedName()!!.text)
imports += codeImport

codeFile.Imports += codeImport
}

fun getNodeInfo(): CodeFile {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,26 @@ import org.junit.Test;
val codeFile = JavaIdentApp().analysis(code, fileName)
assertEquals(codeFile.FullName, fileName)
}

@Test
internal fun shouldGetFilePackageName() {
val code = """
package chapi.ast.javaast;
import org.junit.Test;
"""
val codeFile = JavaIdentApp().analysis(code, "")
assertEquals(codeFile.PackageName, "chapi.ast.javaast")
}

@Test
internal fun shouldGetFileImports() {
val code = """
package chapi.ast.javaast;
import org.junit.Test;
"""
val codeFile = JavaIdentApp().analysis(code, "")
assertEquals(codeFile.Imports[0].Source, "org.junit.Test")
}
}

0 comments on commit c6c9961

Please # to comment.