未验证 提交 bf9acea6 编写于 作者: P Phodal HUANG

fix: fix classes issue

上级 73cd4331
......@@ -3,16 +3,20 @@ package adapter
import (
. "../language/java"
. "./models"
"fmt"
"github.com/antlr/antlr4/runtime/Go/antlr"
"reflect"
"strings"
)
var imports []string
var currentPkg = ""
var currentClz = ""
var currentPkg string
var currentClz string
var methodCalls []JMethodCall
var currentMethodCall *JMethodCall;
var currentMethodCall *JMethodCall
var fields = make(map[string]string)
var localVars = make(map[string]string)
var formalParameters = make(map[string]string)
type JavaCallListener struct {
BaseJavaParserListener
......@@ -27,7 +31,6 @@ func (s *JavaCallListener) EnterImportDeclaration(ctx *ImportDeclarationContext)
imports = append(imports, importText)
}
func (s *JavaCallListener) EnterClassDeclaration(ctx *ClassDeclarationContext) {
currentClz = ctx.IDENTIFIER().GetText()
}
......@@ -42,6 +45,22 @@ func (s *JavaCallListener) EnterInterfaceMethodDeclaration(ctx *InterfaceMethodD
methodCalls = append(methodCalls, *currentMethodCall)
}
func (s *JavaCallListener) EnterFormalParameter(ctx *FormalParameterContext) {
formalParameters[ctx.VariableDeclaratorId().GetText()] = ctx.TypeType().GetText()
}
func (s *JavaCallListener) EnterFieldDeclaration(ctx *FieldDeclarationContext) {
declarators := ctx.VariableDeclarators()
variableName := declarators.GetParent().GetChild(0).(antlr.ParseTree).GetText()
fields[variableName] = ctx.TypeType().GetText()
}
func (s *JavaCallListener) EnterLocalVariableDeclaration(ctx *LocalVariableDeclarationContext) {
typ := ctx.GetChild(0).(antlr.ParseTree).GetText()
variableName := ctx.GetChild(1).GetChild(0).GetChild(0).(antlr.ParseTree).GetText()
localVars[variableName] = typ
}
func (s *JavaCallListener) EnterMethodDeclaration(ctx *MethodDeclarationContext) {
methodName := ctx.IDENTIFIER().GetText()
currentMethodCall = &JMethodCall{currentPkg, currentClz, methodName}
......@@ -53,15 +72,52 @@ func (s *JavaCallListener) EnterMethodCall(ctx *MethodCallContext) {
var targetType = parseTargetType(ctx);
callee := ctx.GetChild(0).(antlr.ParseTree).GetText()
fmt.Println(targetType, callee)
fullType := warpTargetFullType(targetType);
if fullType != "" {
currentMethodCall.AddMethodCall(fullType, callee);
} else {
}
}
}
func parseTargetType(ctx *MethodCallContext) string {
var targetCtx antlr.ParseTree = ctx.GetParent().GetChild(0).(antlr.ParseTree)
var targetCtx = ctx.GetParent().GetChild(0).(antlr.ParseTree)
targetVar := targetCtx.GetText();
targetType := targetVar;
//TODO: update this reflect
typeOf := reflect.TypeOf(targetCtx).String()
if strings.HasSuffix(typeOf, "MethodCallContext") {
targetType = currentClz;
} else {
fieldType := fields[targetVar]
formalType := formalParameters[targetVar]
localVarType := localVars[targetVar]
if fieldType != "" {
targetType = fieldType
} else if formalType != "" {
targetType = formalType;
} else if localVarType != "" {
targetType = localVarType;
}
}
return targetType
}
func warpTargetFullType(targetType string) string {
if strings.EqualFold(currentClz, targetType) {
return currentPkg + "." + targetType;
}
for index := range imports {
imp := imports[index]
if strings.HasSuffix(imp, targetType) {
return imp
}
}
return ""
}
package models
import "fmt"
type JMethodCall struct {
Pkg string
Dlz string
......@@ -7,3 +9,8 @@ type JMethodCall struct {
//methodCalls map[string]string
//tableOps map[string]string
}
func (call *JMethodCall) AddMethodCall (targetType string, method string) {
fmt.Println(targetType, "->", method)
}
......@@ -3,6 +3,7 @@ package main
import (
//"./cmd"
"fmt"
"github.com/antlr/antlr4/runtime/Go/antlr"
"os"
"path/filepath"
......@@ -14,7 +15,7 @@ import (
func main() {
//cmd.Execute()
path := "."
path := "/Users/fdhuang/learn/coca/poc/src/main/"
if len(os.Args) > 1 {
path = os.Args[1:][0]
......@@ -26,6 +27,9 @@ func analysisPath(codeDir string) {
files := javaFiles(codeDir)
for index := range files {
file := files[index]
fmt.Println("Start parse java call: " + file)
parser := processFile(file)
context := parser.CompilationUnit()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册