From c6c99612a93b062e217ce5bc438b91eb56b3951f Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Mon, 3 Feb 2020 15:00:33 +0800 Subject: [PATCH] feat: add package & import support --- .../kotlin/chapi/ast/javaast/JavaIdentApp.kt | 6 ++--- .../chapi/ast/javaast/JavaIdentListener.kt | 16 ++++++++++++++ .../chapi/ast/javaast/JavaIdentAppTest.kt | 22 +++++++++++++++++++ 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/chapi-ast-java/src/main/kotlin/chapi/ast/javaast/JavaIdentApp.kt b/chapi-ast-java/src/main/kotlin/chapi/ast/javaast/JavaIdentApp.kt index 2d48c4ef..eb6a2193 100644 --- a/chapi-ast-java/src/main/kotlin/chapi/ast/javaast/JavaIdentApp.kt +++ b/chapi-ast-java/src/main/kotlin/chapi/ast/javaast/JavaIdentApp.kt @@ -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 } } diff --git a/chapi-ast-java/src/main/kotlin/chapi/ast/javaast/JavaIdentListener.kt b/chapi-ast-java/src/main/kotlin/chapi/ast/javaast/JavaIdentListener.kt index 6d84d593..f1044731 100644 --- a/chapi-ast-java/src/main/kotlin/chapi/ast/javaast/JavaIdentListener.kt +++ b/chapi-ast-java/src/main/kotlin/chapi/ast/javaast/JavaIdentListener.kt @@ -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 = arrayOf() + private var imports : Array = 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 { diff --git a/chapi-ast-java/src/test/kotlin/chapi/ast/javaast/JavaIdentAppTest.kt b/chapi-ast-java/src/test/kotlin/chapi/ast/javaast/JavaIdentAppTest.kt index 8e2908fa..2bc15fd0 100644 --- a/chapi-ast-java/src/test/kotlin/chapi/ast/javaast/JavaIdentAppTest.kt +++ b/chapi-ast-java/src/test/kotlin/chapi/ast/javaast/JavaIdentAppTest.kt @@ -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") + } }