Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Phodal
Coca
提交
bf9acea6
C
Coca
项目概览
Phodal
/
Coca
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
Coca
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
bf9acea6
编写于
10月 28, 2019
作者:
P
Phodal HUANG
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix: fix classes issue
上级
73cd4331
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
77 addition
and
10 deletion
+77
-10
adapter/JavaCallListener.go
adapter/JavaCallListener.go
+65
-9
adapter/models/JMethodCall.go
adapter/models/JMethodCall.go
+7
-0
helloworld.go
helloworld.go
+5
-1
未找到文件。
adapter/JavaCallListener.go
浏览文件 @
bf9acea6
...
...
@@ -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
""
}
adapter/models/JMethodCall.go
浏览文件 @
bf9acea6
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
)
}
helloworld.go
浏览文件 @
bf9acea6
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录