Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Phodal
Coca
提交
f6258432
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,发现更多精彩内容 >>
未验证
提交
f6258432
编写于
12月 19, 2019
作者:
P
Phodal Huang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: add annotation support for interface api call
上级
f9feda8c
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
134 addition
and
36 deletion
+134
-36
cmd/analysis.go
cmd/analysis.go
+1
-1
cmd/api.go
cmd/api.go
+4
-4
core/adapter/api/JavaApiApp.go
core/adapter/api/JavaApiApp.go
+27
-1
core/adapter/api/JavaApiListener.go
core/adapter/api/JavaApiListener.go
+65
-4
core/adapter/call/JavaCallApp.go
core/adapter/call/JavaCallApp.go
+1
-2
core/adapter/call/JavaCallListener.go
core/adapter/call/JavaCallListener.go
+2
-3
core/adapter/identifier/JavaIdentifierApp.go
core/adapter/identifier/JavaIdentifierApp.go
+5
-4
core/adapter/identifier/JavaIdentifierListener.go
core/adapter/identifier/JavaIdentifierListener.go
+21
-9
core/models/JIdentifier.go
core/models/JIdentifier.go
+6
-8
core/models/JMethod.go
core/models/JMethod.go
+2
-0
未找到文件。
cmd/analysis.go
浏览文件 @
f6258432
...
...
@@ -34,7 +34,7 @@ var analysisCmd = &cobra.Command{
var
classes
[]
string
=
nil
for
_
,
node
:=
range
iNodes
{
classes
=
append
(
classes
,
node
.
Package
+
"."
+
node
.
Name
)
classes
=
append
(
classes
,
node
.
Package
+
"."
+
node
.
Class
Name
)
}
callApp
:=
new
(
JavaCallApp
)
...
...
cmd/api.go
浏览文件 @
f6258432
...
...
@@ -36,14 +36,14 @@ var apiCmd = &cobra.Command{
Long
:
``
,
Run
:
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
path
:=
*&
apiCmdConfig
.
Path
dep
endence
:=
*&
apiCmdConfig
.
DependencePath
dep
Path
:=
*&
apiCmdConfig
.
DependencePath
apiPrefix
:=
*&
apiCmdConfig
.
AggregateApi
var
restApis
[]
RestApi
if
path
!=
""
{
if
*&
apiCmdConfig
.
ForceUpdate
{
app
:=
new
(
JavaApiApp
)
restApis
=
app
.
AnalysisPath
(
path
,
dep
endence
)
restApis
=
app
.
AnalysisPath
(
path
,
dep
Path
)
cModel
,
_
:=
json
.
MarshalIndent
(
restApis
,
""
,
"
\t
"
)
WriteToCocaFile
(
"apis.json"
,
string
(
cModel
))
}
else
{
...
...
@@ -55,9 +55,9 @@ var apiCmd = &cobra.Command{
}
var
parsedDeps
[]
models
.
JClassNode
file
:=
ReadFile
(
dep
endence
)
file
:=
ReadFile
(
dep
Path
)
if
file
==
nil
{
log
.
Fatal
(
"lost file:"
+
dep
endence
)
log
.
Fatal
(
"lost file:"
+
dep
Path
)
}
_
=
json
.
Unmarshal
(
file
,
&
parsedDeps
)
...
...
core/adapter/api/JavaApiApp.go
浏览文件 @
f6258432
package
api
import
(
"coca/core/adapter/identifier"
"coca/core/models"
"coca/core/support"
"encoding/json"
...
...
@@ -25,6 +26,13 @@ func (j *JavaApiApp) AnalysisPath(codeDir string, depPath string) []RestApi {
_
=
json
.
Unmarshal
(
file
,
&
parsedDeps
)
identifiers
:=
LoadIdentify
(
depPath
)
var
identifiersMap
=
make
(
map
[
string
]
models
.
JIdentifier
)
for
_
,
ident
:=
range
identifiers
{
identifiersMap
[
ident
.
Package
+
"."
+
ident
.
ClassName
]
=
ident
}
files
:=
support
.
GetJavaFiles
(
codeDir
)
for
index
:=
range
files
{
file
:=
files
[
index
]
...
...
@@ -35,7 +43,7 @@ func (j *JavaApiApp) AnalysisPath(codeDir string, depPath string) []RestApi {
parser
:=
support
.
ProcessFile
(
file
)
context
:=
parser
.
CompilationUnit
()
listener
:=
NewJavaApiListener
()
listener
:=
NewJavaApiListener
(
identifiersMap
)
listener
.
appendClasses
(
parsedDeps
)
antlr
.
NewParseTreeWalker
()
.
Walk
(
listener
,
context
)
...
...
@@ -46,3 +54,21 @@ func (j *JavaApiApp) AnalysisPath(codeDir string, depPath string) []RestApi {
return
*&
allApis
}
func
LoadIdentify
(
importPath
string
)
[]
models
.
JIdentifier
{
var
identifiers
[]
models
.
JIdentifier
apiContent
:=
support
.
ReadCocaFile
(
"identify.json"
)
if
apiContent
==
nil
{
identifierApp
:=
new
(
identifier
.
JavaIdentifierApp
)
ident
:=
identifierApp
.
AnalysisPath
(
importPath
)
identModel
,
_
:=
json
.
MarshalIndent
(
ident
,
""
,
"
\t
"
)
support
.
WriteToCocaFile
(
"identify.json"
,
string
(
identModel
))
return
*&
ident
}
_
=
json
.
Unmarshal
(
apiContent
,
&
identifiers
)
return
*&
identifiers
}
\ No newline at end of file
core/adapter/api/JavaApiListener.go
浏览文件 @
f6258432
...
...
@@ -32,10 +32,21 @@ var RestApis []RestApi
var
currentClz
string
var
currentPkg
string
func
NewJavaApiListener
()
*
JavaApiListener
{
var
identMap
map
[
string
]
models2
.
JIdentifier
var
imports
[]
string
var
currentExtends
=
""
var
currentImplements
=
""
func
NewJavaApiListener
(
ident
map
[
string
]
models2
.
JIdentifier
)
*
JavaApiListener
{
isSpringRestController
=
false
currentClz
=
""
currentPkg
=
""
currentExtends
=
""
currentImplements
=
""
imports
=
nil
identMap
=
ident
params
:=
make
(
map
[
string
]
string
)
currentRestApi
=
*&
RestApi
{
""
,
""
,
""
,
""
,
""
,
params
,
""
,
""
}
...
...
@@ -46,6 +57,11 @@ type JavaApiListener struct {
parser
.
BaseJavaParserListener
}
func
(
s
*
JavaApiListener
)
EnterImportDeclaration
(
ctx
*
parser
.
ImportDeclarationContext
)
{
importText
:=
ctx
.
QualifiedName
()
.
GetText
()
imports
=
append
(
imports
,
importText
)
}
func
(
s
*
JavaApiListener
)
EnterPackageDeclaration
(
ctx
*
parser
.
PackageDeclarationContext
)
{
currentPkg
=
ctx
.
QualifiedName
()
.
GetText
()
}
...
...
@@ -55,6 +71,14 @@ func (s *JavaApiListener) EnterClassDeclaration(ctx *parser.ClassDeclarationCont
if
ctx
.
IDENTIFIER
()
!=
nil
{
currentClz
=
ctx
.
IDENTIFIER
()
.
GetText
()
}
if
ctx
.
EXTENDS
()
!=
nil
{
currentExtends
=
ctx
.
TypeType
()
.
GetText
()
}
if
ctx
.
IMPLEMENTS
()
!=
nil
{
currentImplements
=
ctx
.
TypeList
()
.
GetText
()
}
}
func
(
s
*
JavaApiListener
)
ExitClassDeclaration
(
ctx
*
parser
.
ClassDeclarationContext
)
{
...
...
@@ -83,7 +107,7 @@ func (s *JavaApiListener) EnterAnnotation(ctx *parser.AnnotationContext) {
baseApiUrlName
=
text
[
1
:
len
(
text
)
-
1
]
}
}
}
else
if
ctx
.
ElementValue
()
!=
nil
{
}
else
if
ctx
.
ElementValue
()
!=
nil
{
text
:=
ctx
.
ElementValue
()
.
GetText
()
baseApiUrlName
=
text
[
1
:
len
(
text
)
-
1
]
}
else
{
...
...
@@ -124,7 +148,7 @@ func (s *JavaApiListener) EnterAnnotation(ctx *parser.AnnotationContext) {
}
if
pair
.
IDENTIFIER
()
.
GetText
()
==
"value"
{
text
:=
pair
.
ElementValue
()
.
GetText
()
currentRestApi
.
Uri
=
baseApiUrlName
+
text
[
1
:
len
(
text
)
-
1
]
currentRestApi
.
Uri
=
baseApiUrlName
+
text
[
1
:
len
(
text
)
-
1
]
}
}
}
...
...
@@ -161,7 +185,44 @@ func addApiMethod(annotationName string) {
var
requestBodyClass
string
func
contains
(
slice
[]
string
,
item
string
)
bool
{
set
:=
make
(
map
[
string
]
struct
{},
len
(
slice
))
for
_
,
s
:=
range
slice
{
set
[
s
]
=
struct
{}{}
}
_
,
ok
:=
set
[
item
]
return
ok
}
func
(
s
*
JavaApiListener
)
EnterMethodDeclaration
(
ctx
*
parser
.
MethodDeclarationContext
)
{
methodName
:=
ctx
.
IDENTIFIER
()
.
GetText
()
if
currentImplements
!=
""
{
var
superClz
=
""
for
index
:=
range
imports
{
imp
:=
imports
[
index
]
if
strings
.
HasSuffix
(
imp
,
"."
+
currentImplements
)
{
superClz
=
imp
}
}
if
_
,
ok
:=
identMap
[
superClz
];
ok
{
for
_
,
method
:=
range
identMap
[
superClz
]
.
Methods
{
if
method
.
Name
==
methodName
{
if
contains
(
method
.
Annotations
,
"ServiceMethod"
)
{
currentRestApi
.
PackageName
=
currentPkg
currentRestApi
.
ClassName
=
currentClz
currentRestApi
.
MethodName
=
methodName
RestApis
=
append
(
RestApis
,
currentRestApi
)
return
}
}
}
}
}
if
hasEnterRestController
&&
ctx
.
FormalParameters
()
!=
nil
{
if
ctx
.
FormalParameters
()
.
GetChild
(
0
)
==
nil
||
ctx
.
FormalParameters
()
.
GetChild
(
1
)
==
nil
{
return
...
...
@@ -169,7 +230,7 @@ func (s *JavaApiListener) EnterMethodDeclaration(ctx *parser.MethodDeclarationCo
currentRestApi
.
PackageName
=
currentPkg
currentRestApi
.
ClassName
=
currentClz
currentRestApi
.
MethodName
=
ctx
.
IDENTIFIER
()
.
GetText
()
currentRestApi
.
MethodName
=
methodName
if
ctx
.
FormalParameters
()
.
GetText
()
==
"()"
{
currentRestApi
.
RequestBodyClass
=
requestBodyClass
hasEnterRestController
=
false
...
...
core/adapter/call/JavaCallApp.go
浏览文件 @
f6258432
package
call
import
(
"coca/core/adapter/identifier"
"coca/core/models"
"coca/core/support"
"fmt"
...
...
@@ -14,7 +13,7 @@ var nodeInfos []models.JClassNode
type
JavaCallApp
struct
{
}
func
(
j
*
JavaCallApp
)
AnalysisPath
(
codeDir
string
,
classes
[]
string
,
identNodes
[]
identifier
.
JIdentifier
)
[]
models
.
JClassNode
{
func
(
j
*
JavaCallApp
)
AnalysisPath
(
codeDir
string
,
classes
[]
string
,
identNodes
[]
models
.
JIdentifier
)
[]
models
.
JClassNode
{
nodeInfos
=
nil
files
:=
support
.
GetJavaFiles
(
codeDir
)
for
index
:=
range
files
{
...
...
core/adapter/call/JavaCallListener.go
浏览文件 @
f6258432
package
call
import
(
"coca/core/adapter/identifier"
"coca/core/languages/java"
"coca/core/models"
"github.com/antlr/antlr4/runtime/Go/antlr"
...
...
@@ -28,9 +27,9 @@ var methodMap = make(map[string]models.JMethod)
var
methodQueue
[]
models
.
JMethod
var
classQueue
[]
string
var
identNodes
[]
identifier
.
JIdentifier
var
identNodes
[]
models
.
JIdentifier
func
NewJavaCallListener
(
nodes
[]
identifier
.
JIdentifier
)
*
JavaCallListener
{
func
NewJavaCallListener
(
nodes
[]
models
.
JIdentifier
)
*
JavaCallListener
{
currentClz
=
""
currentPkg
=
""
currentMethod
=
models
.
NewJMethod
()
...
...
core/adapter/identifier/JavaIdentifierApp.go
浏览文件 @
f6258432
package
identifier
import
(
"coca/core/models"
"coca/core/support"
"github.com/antlr/antlr4/runtime/Go/antlr"
)
var
nodeInfos
[]
JIdentifier
=
nil
var
nodeInfos
[]
models
.
JIdentifier
=
nil
type
JavaIdentifierApp
struct
{
}
func
(
j
*
JavaIdentifierApp
)
AnalysisPath
(
codeDir
string
)
[]
JIdentifier
{
func
(
j
*
JavaIdentifierApp
)
AnalysisPath
(
codeDir
string
)
[]
models
.
JIdentifier
{
nodeInfos
=
nil
files
:=
support
.
GetJavaFiles
(
codeDir
)
for
index
:=
range
files
{
...
...
@@ -19,13 +20,13 @@ func (j *JavaIdentifierApp) AnalysisPath(codeDir string) []JIdentifier {
parser
:=
support
.
ProcessFile
(
file
)
context
:=
parser
.
CompilationUnit
()
clzInfo
:=
NewJIdentifier
()
clzInfo
:=
models
.
NewJIdentifier
()
listener
:=
new
(
JavaIdentifierListener
)
listener
.
InitNode
(
clzInfo
)
antlr
.
NewParseTreeWalker
()
.
Walk
(
listener
,
context
)
if
clzInfo
.
Name
!=
""
{
if
clzInfo
.
Class
Name
!=
""
{
clzInfo
.
Methods
=
clzInfo
.
GetMethods
()
nodeInfos
=
append
(
nodeInfos
,
*
clzInfo
)
}
...
...
core/adapter/identifier/JavaIdentifierListener.go
浏览文件 @
f6258432
...
...
@@ -3,9 +3,11 @@ package identifier
import
(
"coca/core/languages/java"
"coca/core/models"
"fmt"
"reflect"
)
var
node
*
JIdentifier
var
node
*
models
.
JIdentifier
var
currentMethod
models
.
JMethod
var
hasEnterClass
=
false
...
...
@@ -24,7 +26,7 @@ func (s *JavaIdentifierListener) EnterClassDeclaration(ctx *parser.ClassDeclarat
node
.
Type
=
"Class"
if
ctx
.
IDENTIFIER
()
!=
nil
{
node
.
Name
=
ctx
.
IDENTIFIER
()
.
GetText
()
node
.
Class
Name
=
ctx
.
IDENTIFIER
()
.
GetText
()
}
if
ctx
.
EXTENDS
()
!=
nil
{
...
...
@@ -32,11 +34,22 @@ func (s *JavaIdentifierListener) EnterClassDeclaration(ctx *parser.ClassDeclarat
}
}
func
(
s
*
JavaIdentifierListener
)
ExitClassDeclaration
(
ctx
*
parser
.
ClassDeclarationContext
)
{
hasEnterClass
=
false
}
func
(
s
*
JavaIdentifierListener
)
EnterInterfaceBodyDeclaration
(
ctx
*
parser
.
InterfaceBodyDeclarationContext
)
{
fmt
.
Println
(
ctx
.
GetText
())
for
_
,
modifier
:=
range
ctx
.
AllModifier
()
{
modifier
:=
modifier
.
(
*
parser
.
ModifierContext
)
.
GetChild
(
0
)
if
reflect
.
TypeOf
(
modifier
.
GetChild
(
0
))
.
String
()
==
"*parser.AnnotationContext"
{
annotationContext
:=
modifier
.
GetChild
(
0
)
.
(
*
parser
.
AnnotationContext
)
currentMethod
.
Annotations
=
append
(
currentMethod
.
Annotations
,
annotationContext
.
QualifiedName
()
.
GetText
())
}
}
}
func
(
s
*
JavaIdentifierListener
)
EnterInterfaceMethodDeclaration
(
ctx
*
parser
.
InterfaceMethodDeclarationContext
)
{
hasEnterMethod
=
true
...
...
@@ -61,7 +74,7 @@ func (s *JavaIdentifierListener) EnterInterfaceMethodDeclaration(ctx *parser.Int
}
}
func
(
s
*
JavaIdentifierListener
)
ExitInterface
Declaration
(
ctx
*
parser
.
Interface
DeclarationContext
)
{
func
(
s
*
JavaIdentifierListener
)
ExitInterface
MethodDeclaration
(
ctx
*
parser
.
InterfaceMethod
DeclarationContext
)
{
hasEnterMethod
=
false
node
.
AddMethod
(
currentMethod
)
...
...
@@ -110,18 +123,17 @@ func (s *JavaIdentifierListener) EnterAnnotation(ctx *parser.AnnotationContext)
if
annotationName
==
"Override"
{
isOverrideMethod
=
true
}
if
hasEnterClass
{
if
!
hasEnterMethod
{
currentMethod
.
Annotations
=
append
(
currentMethod
.
Annotations
,
annotationName
)
}
currentMethod
.
Annotations
=
append
(
currentMethod
.
Annotations
,
annotationName
)
}
}
func
(
s
*
JavaIdentifierListener
)
EnterInterfaceDeclaration
(
ctx
*
parser
.
InterfaceDeclarationContext
)
{
node
.
Type
=
"Interface"
node
.
Name
=
ctx
.
IDENTIFIER
()
.
GetText
()
node
.
Class
Name
=
ctx
.
IDENTIFIER
()
.
GetText
()
}
func
(
s
*
JavaIdentifierListener
)
InitNode
(
identifier
*
JIdentifier
)
{
func
(
s
*
JavaIdentifierListener
)
InitNode
(
identifier
*
models
.
JIdentifier
)
{
node
=
identifier
}
core/
adapter/identifier
/JIdentifier.go
→
core/
models
/JIdentifier.go
浏览文件 @
f6258432
package
identifier
package
models
import
"coca/core/models"
var
methods
[]
models
.
JMethod
var
methods
[]
JMethod
type
JIdentifier
struct
{
Package
string
Name
string
ClassName
string
Type
string
ExtendsName
string
Extends
[]
JIdentifier
Methods
[]
models
.
JMethod
Methods
[]
JMethod
}
func
NewJIdentifier
()
*
JIdentifier
{
...
...
@@ -19,10 +17,10 @@ func NewJIdentifier() *JIdentifier {
return
identifier
}
func
(
identifier
*
JIdentifier
)
AddMethod
(
method
models
.
JMethod
)
{
func
(
identifier
*
JIdentifier
)
AddMethod
(
method
JMethod
)
{
methods
=
append
(
methods
,
method
)
}
func
(
identifier
*
JIdentifier
)
GetMethods
()
[]
models
.
JMethod
{
func
(
identifier
*
JIdentifier
)
GetMethods
()
[]
JMethod
{
return
methods
}
core/models/JMethod.go
浏览文件 @
f6258432
...
...
@@ -13,6 +13,8 @@ type JMethod struct {
Annotations
[]
string
}
// TODO support annnotation
func
NewJMethod
()
JMethod
{
return
*&
JMethod
{
Name
:
""
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录