Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Phodal
Coca
提交
7304f3bf
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 搜索 >>
未验证
提交
7304f3bf
编写于
12月 31, 2019
作者:
P
Phodal Huang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor: move to domain
上级
2cf880a1
变更
17
隐藏空白更改
内联
并排
Showing
17 changed file
with
164 addition
and
116 deletion
+164
-116
_fixtures/tbs/regression/CallAssertInClassTests.java
_fixtures/tbs/regression/CallAssertInClassTests.java
+5
-0
cmd/api.go
cmd/api.go
+2
-20
cmd/bs.go
cmd/bs.go
+3
-3
core/ast/bs/bad_smell_app.go
core/ast/bs/bad_smell_app.go
+6
-6
core/ast/bs/bad_smell_listener.go
core/ast/bs/bad_smell_listener.go
+20
-20
core/ast/common_listener/common_listener.go
core/ast/common_listener/common_listener.go
+12
-0
core/ast/full/java_full_listener.go
core/ast/full/java_full_listener.go
+13
-5
core/ast/identifier/java_identifier_listener.go
core/ast/identifier/java_identifier_listener.go
+13
-20
core/context/bs/bs_app.go
core/context/bs/bs_app.go
+16
-25
core/context/call/call_graph.go
core/context/call/call_graph.go
+6
-16
core/context/git/git.go
core/context/git/git.go
+4
-0
core/context/tbs/tbs_app_test.go
core/context/tbs/tbs_app_test.go
+1
-0
core/domain/bs_domain/bad_smell.go
core/domain/bs_domain/bad_smell.go
+17
-1
core/domain/call_api.go
core/domain/call_api.go
+16
-0
core/domain/rest_api.go
core/domain/rest_api.go
+16
-0
core/infrastructure/constants/java_target_config.go
core/infrastructure/constants/java_target_config.go
+0
-0
core/infrastructure/jpackage/jpackage.go
core/infrastructure/jpackage/jpackage.go
+14
-0
未找到文件。
_fixtures/tbs/regression/CallAssertInClassTests.java
浏览文件 @
7304f3bf
...
...
@@ -30,7 +30,12 @@ import static org.mockito.Mockito.verify;
/**
* @author Stephane Nicoll
*/
@RunWith
(
PowerMockRunner
.
class
)
public
class
CallAssertInClassTests
extends
AbstractApplicationEventListenerTests
{
@Mock
Connection
connection
=
PowerMockito
.
mock
(
Connection
.
class
);
@Test
// Demonstrates we cant inject that event because the listener has a raw type
public
void
genericListenerRawTypeTypeErasure
()
{
GenericTestEvent
<
String
>
stringEvent
=
createGenericTestEvent
(
"test"
);
...
...
cmd/api.go
浏览文件 @
7304f3bf
...
...
@@ -14,7 +14,6 @@ import (
"log"
"os"
"regexp"
"sort"
"strconv"
"strings"
)
...
...
@@ -66,15 +65,13 @@ var apiCmd = &cobra.Command{
parsedDeps
:=
cmd_util
.
GetDepsFromJson
(
depPath
)
restFieldsApi
:=
filterApi
(
apiPrefix
,
restApis
)
restFieldsApi
:=
domain
.
FilterApiByPrefix
(
apiPrefix
,
restApis
)
analyser
:=
call
.
NewCallGraph
()
dotContent
,
counts
:=
analyser
.
AnalysisByFiles
(
restFieldsApi
,
parsedDeps
,
diMap
)
if
*&
apiCmdConfig
.
Sort
{
sort
.
Slice
(
counts
,
func
(
i
,
j
int
)
bool
{
return
counts
[
i
]
.
Size
<
counts
[
j
]
.
Size
})
domain
.
SortApi
(
counts
)
}
if
apiCmdConfig
.
ShowCount
{
...
...
@@ -104,21 +101,6 @@ func forceUpdateApi() {
coca_file
.
WriteToCocaFile
(
"apis.json"
,
string
(
cModel
))
}
func
filterApi
(
apiPrefix
string
,
apis
[]
domain
.
RestApi
,
)
[]
domain
.
RestApi
{
var
restFieldsApi
[]
domain
.
RestApi
if
apiPrefix
!=
""
{
for
_
,
api
:=
range
apis
{
if
strings
.
HasPrefix
(
api
.
Uri
,
apiPrefix
)
{
restFieldsApi
=
append
(
restFieldsApi
,
api
)
}
}
}
else
{
restFieldsApi
=
apis
}
return
restFieldsApi
}
func
replacePackage
(
content
string
)
string
{
var
packageRegex
string
packageNameArray
:=
strings
.
Split
(
apiCmdConfig
.
RemovePackageNames
,
","
)
...
...
cmd/bs.go
浏览文件 @
7304f3bf
...
...
@@ -3,7 +3,7 @@ package cmd
import
(
"encoding/json"
"github.com/phodal/coca/core/ast/bs"
"github.com/phodal/coca/core/domain"
"github.com/phodal/coca/core/domain
/bs_domain
"
"github.com/phodal/coca/core/infrastructure/coca_file"
"github.com/phodal/coca/core/infrastructure/str_helper"
"github.com/spf13/cobra"
...
...
@@ -44,8 +44,8 @@ var badsmellCmd = &cobra.Command{
},
}
func
sortSmellByType
(
models
[]
domain
.
BadSmellModel
)
map
[
string
][]
domain
.
BadSmellModel
{
sortSmells
:=
make
(
map
[
string
][]
domain
.
BadSmellModel
)
func
sortSmellByType
(
models
[]
bs_domain
.
BadSmellModel
)
map
[
string
][]
bs_
domain
.
BadSmellModel
{
sortSmells
:=
make
(
map
[
string
][]
bs_
domain
.
BadSmellModel
)
for
_
,
model
:=
range
models
{
sortSmells
[
model
.
Bs
]
=
append
(
sortSmells
[
model
.
Bs
],
model
)
}
...
...
core/ast/bs/bad_smell_app.go
浏览文件 @
7304f3bf
...
...
@@ -5,13 +5,13 @@ import (
"fmt"
"github.com/antlr/antlr4/runtime/Go/antlr"
"github.com/phodal/coca/core/context/bs"
"github.com/phodal/coca/core/domain"
"github.com/phodal/coca/core/domain
/bs_domain
"
"github.com/phodal/coca/core/infrastructure/coca_file"
.
"github.com/phodal/coca/languages/java"
"path/filepath"
)
var
nodeInfos
[]
domain
.
BsJClass
var
nodeInfos
[]
bs_
domain
.
BsJClass
type
BadSmellApp
struct
{
}
...
...
@@ -20,11 +20,11 @@ func NewBadSmellApp() *BadSmellApp {
return
&
BadSmellApp
{}
}
func
(
j
*
BadSmellApp
)
AnalysisPath
(
codeDir
string
,
ignoreRules
[]
string
)
[]
domain
.
BadSmellModel
{
func
(
j
*
BadSmellApp
)
AnalysisPath
(
codeDir
string
,
ignoreRules
[]
string
)
[]
bs_
domain
.
BadSmellModel
{
nodeInfos
=
nil
files
:=
coca_file
.
GetJavaFiles
(
codeDir
)
for
index
:=
range
files
{
nodeInfo
:=
domain
.
NewJFullClassNode
()
nodeInfo
:=
bs_
domain
.
NewJFullClassNode
()
file
:=
files
[
index
]
displayName
:=
filepath
.
Base
(
file
)
...
...
@@ -56,8 +56,8 @@ func (j *BadSmellApp) AnalysisPath(codeDir string, ignoreRules []string) []domai
return
filteredBsList
}
func
FilterBadSmellList
(
models
[]
domain
.
BadSmellModel
,
ignoreRules
map
[
string
]
bool
)
[]
domain
.
BadSmellModel
{
var
results
[]
domain
.
BadSmellModel
func
FilterBadSmellList
(
models
[]
bs_domain
.
BadSmellModel
,
ignoreRules
map
[
string
]
bool
)
[]
bs_
domain
.
BadSmellModel
{
var
results
[]
bs_
domain
.
BadSmellModel
for
_
,
model
:=
range
models
{
if
!
ignoreRules
[
model
.
Bs
]
{
results
=
append
(
results
,
model
)
...
...
core/ast/bs/bad_smell_listener.go
浏览文件 @
7304f3bf
...
...
@@ -2,7 +2,7 @@ package bs
import
(
"github.com/antlr/antlr4/runtime/Go/antlr"
"github.com/phodal/coca/core/domain"
"github.com/phodal/coca/core/domain
/bs_domain
"
.
"github.com/phodal/coca/languages/java"
"reflect"
"strings"
...
...
@@ -17,13 +17,13 @@ var currentClzType string
var
currentClzExtends
string
var
currentClzImplements
[]
string
var
methods
[]
domain
.
BsJMethod
var
methodCalls
[]
domain
.
BsJMethodCall
var
methods
[]
bs_
domain
.
BsJMethod
var
methodCalls
[]
bs_
domain
.
BsJMethodCall
var
fields
=
make
(
map
[
string
]
string
)
var
localVars
=
make
(
map
[
string
]
string
)
var
formalParameters
=
make
(
map
[
string
]
string
)
var
currentClassBs
domain
.
ClassBadSmellInfo
var
currentClassBs
bs_
domain
.
ClassBadSmellInfo
func
NewBadSmellListener
()
*
BadSmellListener
{
currentClz
=
""
...
...
@@ -39,8 +39,8 @@ type BadSmellListener struct {
BaseJavaParserListener
}
func
(
s
*
BadSmellListener
)
getNodeInfo
()
domain
.
BsJClass
{
return
*&
domain
.
BsJClass
{
func
(
s
*
BadSmellListener
)
getNodeInfo
()
bs_
domain
.
BsJClass
{
return
*&
bs_
domain
.
BsJClass
{
currentPkg
,
currentClz
,
currentClzType
,
...
...
@@ -115,7 +115,7 @@ func (s *BadSmellListener) EnterInterfaceMethodDeclaration(ctx *InterfaceMethodD
typeType
:=
ctx
.
TypeTypeOrVoid
()
.
GetText
()
var
methodParams
[]
domain
.
JFullParameter
=
nil
var
methodParams
[]
bs_
domain
.
JFullParameter
=
nil
parameters
:=
ctx
.
FormalParameters
()
if
parameters
!=
nil
{
if
reflect
.
TypeOf
(
parameters
.
GetChild
(
1
))
.
String
()
==
"*parser.FormalParameterListContext"
{
...
...
@@ -125,14 +125,14 @@ func (s *BadSmellListener) EnterInterfaceMethodDeclaration(ctx *InterfaceMethodD
paramContext
:=
param
.
(
*
FormalParameterContext
)
paramType
:=
paramContext
.
TypeType
()
.
GetText
()
paramValue
:=
paramContext
.
VariableDeclaratorId
()
.
(
*
VariableDeclaratorIdContext
)
.
IDENTIFIER
()
.
GetText
()
methodParams
=
append
(
methodParams
,
*&
domain
.
JFullParameter
{
paramType
,
paramValue
})
methodParams
=
append
(
methodParams
,
*&
bs_
domain
.
JFullParameter
{
paramType
,
paramValue
})
}
}
}
methodBSInfo
:=
domain
.
NewMethodBadSmellInfo
()
methodBSInfo
:=
bs_
domain
.
NewMethodBadSmellInfo
()
method
:=
&
domain
.
BsJMethod
{
method
:=
&
bs_
domain
.
BsJMethod
{
Name
:
name
,
Type
:
typeType
,
StartLine
:
startLine
,
...
...
@@ -180,7 +180,7 @@ func (s *BadSmellListener) EnterMethodDeclaration(ctx *MethodDeclarationContext)
typeType
:=
ctx
.
TypeTypeOrVoid
()
.
GetText
()
methodBody
:=
ctx
.
MethodBody
()
.
GetText
()
var
methodParams
[]
domain
.
JFullParameter
=
nil
var
methodParams
[]
bs_
domain
.
JFullParameter
=
nil
parameters
:=
ctx
.
FormalParameters
()
if
parameters
!=
nil
{
if
reflect
.
TypeOf
(
parameters
.
GetChild
(
1
))
.
String
()
==
"*parser.FormalParameterListContext"
{
...
...
@@ -190,17 +190,17 @@ func (s *BadSmellListener) EnterMethodDeclaration(ctx *MethodDeclarationContext)
paramContext
:=
param
.
(
*
FormalParameterContext
)
paramType
:=
paramContext
.
TypeType
()
.
GetText
()
paramValue
:=
paramContext
.
VariableDeclaratorId
()
.
(
*
VariableDeclaratorIdContext
)
.
IDENTIFIER
()
.
GetText
()
methodParams
=
append
(
methodParams
,
domain
.
JFullParameter
{
paramType
,
paramValue
})
methodParams
=
append
(
methodParams
,
bs_
domain
.
JFullParameter
{
paramType
,
paramValue
})
localVars
[
paramValue
]
=
paramType
}
}
}
methodBSInfo
:=
domain
.
NewMethodBadSmellInfo
()
methodBSInfo
:=
bs_
domain
.
NewMethodBadSmellInfo
()
methodBadSmellInfo
:=
buildMethodBSInfo
(
ctx
,
methodBSInfo
)
method
:=
&
domain
.
BsJMethod
{
method
:=
&
bs_
domain
.
BsJMethod
{
Name
:
name
,
Type
:
typeType
,
StartLine
:
startLine
,
...
...
@@ -230,7 +230,7 @@ func getModifier(ctx *MethodDeclarationContext) string {
return
modifier
}
func
buildMethodBSInfo
(
context
*
MethodDeclarationContext
,
bsInfo
domain
.
MethodBadSmellInfo
)
domain
.
MethodBadSmellInfo
{
func
buildMethodBSInfo
(
context
*
MethodDeclarationContext
,
bsInfo
bs_domain
.
MethodBadSmellInfo
)
bs_
domain
.
MethodBadSmellInfo
{
methodBody
:=
context
.
MethodBody
()
blockContext
:=
methodBody
.
GetChild
(
0
)
if
reflect
.
TypeOf
(
blockContext
)
.
String
()
==
"*parser.BlockContext"
{
...
...
@@ -249,7 +249,7 @@ func buildMethodBSInfo(context *MethodDeclarationContext, bsInfo domain.MethodBa
startLine
:=
parCtx
.
GetStart
()
.
GetLine
()
endLine
:=
parCtx
.
GetStop
()
.
GetLine
()
info
:=
domain
.
NewIfPairInfo
()
info
:=
bs_
domain
.
NewIfPairInfo
()
info
.
StartLine
=
startLine
info
.
EndLine
=
endLine
bsInfo
.
IfInfo
=
append
(
bsInfo
.
IfInfo
,
info
)
...
...
@@ -314,14 +314,14 @@ func (s *BadSmellListener) EnterMethodCall(ctx *MethodCallContext) {
targetType
=
currentClzExtends
}
if
fullType
!=
""
{
jMethodCall
:=
*&
domain
.
BsJMethodCall
{
removeTarget
(
fullType
),
""
,
targetType
,
callee
,
startLine
,
startLinePosition
,
stopLine
,
stopLinePosition
}
jMethodCall
:=
*&
bs_
domain
.
BsJMethodCall
{
removeTarget
(
fullType
),
""
,
targetType
,
callee
,
startLine
,
startLinePosition
,
stopLine
,
stopLinePosition
}
methodCalls
=
append
(
methodCalls
,
jMethodCall
)
}
else
{
if
ctx
.
GetText
()
==
targetType
{
jMethodCall
:=
*&
domain
.
BsJMethodCall
{
currentPkg
,
""
,
currentClz
,
callee
,
startLine
,
startLinePosition
,
stopLine
,
stopLinePosition
}
jMethodCall
:=
*&
bs_
domain
.
BsJMethodCall
{
currentPkg
,
""
,
currentClz
,
callee
,
startLine
,
startLinePosition
,
stopLine
,
stopLinePosition
}
methodCalls
=
append
(
methodCalls
,
jMethodCall
)
}
else
{
jMethodCall
:=
*&
domain
.
BsJMethodCall
{
currentPkg
,
"NEEDFIX"
,
targetType
,
callee
,
startLine
,
startLinePosition
,
stopLine
,
stopLinePosition
}
jMethodCall
:=
*&
bs_
domain
.
BsJMethodCall
{
currentPkg
,
"NEEDFIX"
,
targetType
,
callee
,
startLine
,
startLinePosition
,
stopLine
,
stopLinePosition
}
methodCalls
=
append
(
methodCalls
,
jMethodCall
)
}
}
...
...
@@ -340,7 +340,7 @@ func (s *BadSmellListener) EnterExpression(ctx *ExpressionContext) {
stopLine
:=
ctx
.
GetStop
()
.
GetLine
()
stopLinePosition
:=
startLinePosition
+
len
(
text
)
jMethodCall
:=
&
domain
.
BsJMethodCall
{
removeTarget
(
fullType
),
""
,
targetType
,
methodName
,
startLine
,
startLinePosition
,
stopLine
,
stopLinePosition
}
jMethodCall
:=
&
bs_
domain
.
BsJMethodCall
{
removeTarget
(
fullType
),
""
,
targetType
,
methodName
,
startLine
,
startLinePosition
,
stopLine
,
stopLinePosition
}
methodCalls
=
append
(
methodCalls
,
*
jMethodCall
)
}
}
...
...
core/ast/common_listener/common_listener.go
浏览文件 @
7304f3bf
...
...
@@ -3,6 +3,7 @@ package common_listener
import
(
"github.com/phodal/coca/core/domain"
"github.com/phodal/coca/languages/java"
"reflect"
)
func
BuildAnnotation
(
ctx
*
parser
.
AnnotationContext
)
domain
.
Annotation
{
...
...
@@ -30,3 +31,14 @@ func BuildAnnotation(ctx *parser.AnnotationContext) domain.Annotation {
return
annotation
}
func
BuildAnnotationForMethod
(
context
*
parser
.
ModifierContext
,
method
*
domain
.
JMethod
)
{
if
context
.
ClassOrInterfaceModifier
()
!=
nil
{
if
reflect
.
TypeOf
(
context
.
ClassOrInterfaceModifier
()
.
GetChild
(
0
))
.
String
()
==
"*parser.AnnotationContext"
{
annotationCtx
:=
context
.
ClassOrInterfaceModifier
()
.
GetChild
(
0
)
.
(
*
parser
.
AnnotationContext
)
annotation
:=
BuildAnnotation
(
annotationCtx
)
method
.
Annotations
=
append
(
method
.
Annotations
,
annotation
)
}
}
}
core/ast/full/java_full_listener.go
浏览文件 @
7304f3bf
...
...
@@ -98,7 +98,10 @@ func (s *JavaFullListener) exitBody() {
return
}
classNodes
=
append
(
classNodes
,
*
currentNode
)
if
currentNode
.
Class
!=
""
{
classNodes
=
append
(
classNodes
,
*
currentNode
)
}
currentNode
=
domain
.
NewClassNode
()
initClass
()
}
...
...
@@ -171,6 +174,10 @@ func (s *JavaFullListener) EnterInterfaceMethodDeclaration(ctx *parser.Interface
typeType
:=
ctx
.
TypeTypeOrVoid
()
.
GetText
()
if
reflect
.
TypeOf
(
ctx
.
GetParent
()
.
GetParent
()
.
GetChild
(
0
))
.
String
()
==
"*parser.ModifierContext"
{
common_listener
.
BuildAnnotationForMethod
(
ctx
.
GetParent
()
.
GetParent
()
.
GetChild
(
0
)
.
(
*
parser
.
ModifierContext
),
&
currentMethod
)
}
method
:=
&
domain
.
JMethod
{
Name
:
name
,
Type
:
typeType
,
StartLine
:
startLine
,
StartLinePosition
:
startLinePosition
,
StopLine
:
stopLine
,
StopLinePosition
:
stopLinePosition
}
updateMethod
(
method
)
}
...
...
@@ -230,10 +237,7 @@ func (s *JavaFullListener) EnterAnnotation(ctx *parser.AnnotationContext) {
isOverrideMethod
=
false
}
if
hasEnterClass
{
annotation
:=
common_listener
.
BuildAnnotation
(
ctx
)
currentMethod
.
Annotations
=
append
(
currentMethod
.
Annotations
,
annotation
)
}
else
{
if
!
hasEnterClass
{
annotation
:=
common_listener
.
BuildAnnotation
(
ctx
)
if
currentType
==
"CreatorClass"
{
currentCreatorNode
.
Annotations
=
append
(
currentCreatorNode
.
Annotations
,
annotation
)
...
...
@@ -279,6 +283,10 @@ func (s *JavaFullListener) EnterMethodDeclaration(ctx *parser.MethodDeclarationC
typeType
:=
ctx
.
TypeTypeOrVoid
()
.
GetText
()
if
reflect
.
TypeOf
(
ctx
.
GetParent
()
.
GetParent
()
.
GetChild
(
0
))
.
String
()
==
"*parser.ModifierContext"
{
common_listener
.
BuildAnnotationForMethod
(
ctx
.
GetParent
()
.
GetParent
()
.
GetChild
(
0
)
.
(
*
parser
.
ModifierContext
),
&
currentMethod
)
}
method
:=
&
domain
.
JMethod
{
Name
:
name
,
Type
:
typeType
,
...
...
core/ast/identifier/java_identifier_listener.go
浏览文件 @
7304f3bf
...
...
@@ -104,13 +104,6 @@ func (s *JavaIdentifierListener) ExitConstructorDeclaration(ctx *parser.Construc
func
(
s
*
JavaIdentifierListener
)
EnterInterfaceBodyDeclaration
(
ctx
*
parser
.
InterfaceBodyDeclarationContext
)
{
hasEnterClass
=
true
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
)
common_listener
.
BuildAnnotation
(
annotationContext
)
}
}
}
func
(
s
*
JavaIdentifierListener
)
EnterInterfaceMethodDeclaration
(
ctx
*
parser
.
InterfaceMethodDeclarationContext
)
{
...
...
@@ -122,7 +115,10 @@ func (s *JavaIdentifierListener) EnterInterfaceMethodDeclaration(ctx *parser.Int
//XXX: find the start position of {, not public
typeType
:=
ctx
.
TypeTypeOrVoid
()
.
GetText
()
annotations
:=
currentMethod
.
Annotations
if
reflect
.
TypeOf
(
ctx
.
GetParent
()
.
GetParent
()
.
GetChild
(
0
))
.
String
()
==
"*parser.ModifierContext"
{
common_listener
.
BuildAnnotationForMethod
(
ctx
.
GetParent
()
.
GetParent
()
.
GetChild
(
0
)
.
(
*
parser
.
ModifierContext
),
&
currentMethod
)
}
currentMethod
=
*&
domain
.
JMethod
{
Name
:
name
,
Type
:
typeType
,
...
...
@@ -131,14 +127,13 @@ func (s *JavaIdentifierListener) EnterInterfaceMethodDeclaration(ctx *parser.Int
StopLine
:
stopLine
,
StopLinePosition
:
stopLinePosition
,
Override
:
isOverrideMethod
,
Annotations
:
a
nnotations
,
Annotations
:
currentMethod
.
A
nnotations
,
}
}
func
(
s
*
JavaIdentifierListener
)
ExitInterfaceMethodDeclaration
(
ctx
*
parser
.
InterfaceMethodDeclarationContext
)
{
currentNode
.
AddMethod
(
currentMethod
)
_
=
domain
.
NewJMethod
()
currentMethod
=
domain
.
NewJMethod
()
}
var
isOverrideMethod
=
false
...
...
@@ -154,7 +149,10 @@ func (s *JavaIdentifierListener) EnterMethodDeclaration(ctx *parser.MethodDeclar
typeType
:=
ctx
.
TypeTypeOrVoid
()
.
GetText
()
annotations
:=
currentMethod
.
Annotations
if
reflect
.
TypeOf
(
ctx
.
GetParent
()
.
GetParent
()
.
GetChild
(
0
))
.
String
()
==
"*parser.ModifierContext"
{
common_listener
.
BuildAnnotationForMethod
(
ctx
.
GetParent
()
.
GetParent
()
.
GetChild
(
0
)
.
(
*
parser
.
ModifierContext
),
&
currentMethod
)
}
currentMethod
=
*&
domain
.
JMethod
{
Name
:
name
,
Type
:
typeType
,
...
...
@@ -163,7 +161,7 @@ func (s *JavaIdentifierListener) EnterMethodDeclaration(ctx *parser.MethodDeclar
StopLine
:
stopLine
,
StopLinePosition
:
stopLinePosition
,
Override
:
isOverrideMethod
,
Annotations
:
a
nnotations
,
Annotations
:
currentMethod
.
A
nnotations
,
}
if
reflect
.
TypeOf
(
ctx
.
GetParent
()
.
GetParent
())
.
String
()
==
"*parser.ClassBodyDeclarationContext"
{
...
...
@@ -179,22 +177,17 @@ func (s *JavaIdentifierListener) EnterMethodDeclaration(ctx *parser.MethodDeclar
}
func
(
s
*
JavaIdentifierListener
)
ExitMethodDeclaration
(
ctx
*
parser
.
MethodDeclarationContext
)
{
currentNode
.
AddMethod
(
currentMethod
)
_
=
domain
.
NewJMethod
()
currentMethod
=
domain
.
NewJMethod
()
}
func
(
s
*
JavaIdentifierListener
)
EnterAnnotation
(
ctx
*
parser
.
AnnotationContext
)
{
// Todo: support override method
annotationName
:=
ctx
.
QualifiedName
()
.
GetText
()
if
annotationName
==
"Override"
{
isOverrideMethod
=
true
}
if
hasEnterClass
{
annotation
:=
common_listener
.
BuildAnnotation
(
ctx
)
currentMethod
.
Annotations
=
append
(
currentMethod
.
Annotations
,
annotation
)
}
else
{
if
!
hasEnterClass
{
annotation
:=
common_listener
.
BuildAnnotation
(
ctx
)
currentNode
.
Annotations
=
append
(
currentNode
.
Annotations
,
annotation
)
}
...
...
core/context/bs/bs_app.go
浏览文件 @
7304f3bf
...
...
@@ -2,17 +2,16 @@ package bs
import
(
"encoding/json"
"github.com/phodal/coca/core/domain"
"github.com/phodal/coca/core/domain
/bs_domain
"
"strconv"
"strings"
)
func
AnalysisBadSmell
(
nodes
[]
domain
.
BsJClass
)
[]
domain
.
BadSmellModel
{
var
badSmellList
[]
domain
.
BadSmellModel
func
AnalysisBadSmell
(
nodes
[]
bs_domain
.
BsJClass
)
[]
bs_
domain
.
BadSmellModel
{
var
badSmellList
[]
bs_
domain
.
BadSmellModel
for
_
,
node
:=
range
nodes
{
// To be Defined number
if
node
.
Type
==
"Class"
&&
len
(
node
.
Methods
)
<
1
{
badSmellList
=
append
(
badSmellList
,
*&
domain
.
BadSmellModel
{
node
.
Path
,
""
,
"lazyElement"
,
""
,
0
})
badSmellList
=
append
(
badSmellList
,
*&
bs_
domain
.
BadSmellModel
{
node
.
Path
,
""
,
"lazyElement"
,
""
,
0
})
}
onlyHaveGetterAndSetter
:=
true
...
...
@@ -21,11 +20,11 @@ func AnalysisBadSmell(nodes []domain.BsJClass) []domain.BadSmellModel {
methodLength
:=
method
.
StopLine
-
method
.
StartLine
if
methodLength
>
30
{
description
:=
"method length: "
+
strconv
.
Itoa
(
methodLength
)
longMethod
:=
&
domain
.
BadSmellModel
{
node
.
Path
,
strconv
.
Itoa
(
method
.
StartLine
),
"longMethod"
,
description
,
methodLength
}
longMethod
:=
&
bs_
domain
.
BadSmellModel
{
node
.
Path
,
strconv
.
Itoa
(
method
.
StartLine
),
"longMethod"
,
description
,
methodLength
}
badSmellList
=
append
(
badSmellList
,
*
longMethod
)
}
if
!
(
strings
.
Contains
(
method
.
Name
,
"get"
)
||
strings
.
Contains
(
method
.
Name
,
"set"
))
{
if
!
(
method
.
IsGetterSetter
(
))
{
onlyHaveGetterAndSetter
=
false
}
...
...
@@ -33,26 +32,26 @@ func AnalysisBadSmell(nodes []domain.BsJClass) []domain.BadSmellModel {
if
len
(
method
.
Parameters
)
>
5
{
paramsJson
,
_
:=
json
.
Marshal
(
method
.
Parameters
)
str
:=
string
(
paramsJson
[
:
])
longParams
:=
&
domain
.
BadSmellModel
{
node
.
Path
,
strconv
.
Itoa
(
method
.
StartLine
),
"longParameterList"
,
str
,
len
(
method
.
Parameters
)}
longParams
:=
&
bs_
domain
.
BadSmellModel
{
node
.
Path
,
strconv
.
Itoa
(
method
.
StartLine
),
"longParameterList"
,
str
,
len
(
method
.
Parameters
)}
badSmellList
=
append
(
badSmellList
,
*
longParams
)
}
// repeatedSwitches
if
method
.
MethodBs
.
IfSize
>
8
{
longParams
:=
&
domain
.
BadSmellModel
{
node
.
Path
,
strconv
.
Itoa
(
method
.
StartLine
),
"repeatedSwitches"
,
"ifSize"
,
method
.
MethodBs
.
IfSize
}
longParams
:=
&
bs_
domain
.
BadSmellModel
{
node
.
Path
,
strconv
.
Itoa
(
method
.
StartLine
),
"repeatedSwitches"
,
"ifSize"
,
method
.
MethodBs
.
IfSize
}
badSmellList
=
append
(
badSmellList
,
*
longParams
)
}
// repeatedSwitches
if
method
.
MethodBs
.
SwitchSize
>
8
{
longParams
:=
&
domain
.
BadSmellModel
{
node
.
Path
,
strconv
.
Itoa
(
method
.
StartLine
),
"repeatedSwitches"
,
"switchSize"
,
method
.
MethodBs
.
SwitchSize
}
longParams
:=
&
bs_
domain
.
BadSmellModel
{
node
.
Path
,
strconv
.
Itoa
(
method
.
StartLine
),
"repeatedSwitches"
,
"switchSize"
,
method
.
MethodBs
.
SwitchSize
}
badSmellList
=
append
(
badSmellList
,
*
longParams
)
}
// complex if
for
_
,
info
:=
range
method
.
MethodBs
.
IfInfo
{
if
info
.
EndLine
-
info
.
StartLine
>=
2
{
longParams
:=
&
domain
.
BadSmellModel
{
node
.
Path
,
strconv
.
Itoa
(
info
.
StartLine
),
"complexCondition"
,
"complexCondition"
,
0
}
longParams
:=
&
bs_
domain
.
BadSmellModel
{
node
.
Path
,
strconv
.
Itoa
(
info
.
StartLine
),
"complexCondition"
,
"complexCondition"
,
0
}
badSmellList
=
append
(
badSmellList
,
*
longParams
)
}
}
...
...
@@ -60,21 +59,14 @@ func AnalysisBadSmell(nodes []domain.BsJClass) []domain.BadSmellModel {
// dataClass
if
onlyHaveGetterAndSetter
&&
node
.
Type
==
"Class"
&&
len
(
node
.
Methods
)
>
0
{
dataClass
:=
&
domain
.
BadSmellModel
{
node
.
Path
,
""
,
"dataClass"
,
""
,
len
(
node
.
Methods
)}
dataClass
:=
&
bs_
domain
.
BadSmellModel
{
node
.
Path
,
""
,
"dataClass"
,
""
,
len
(
node
.
Methods
)}
badSmellList
=
append
(
badSmellList
,
*
dataClass
)
}
//Refused Bequest
if
node
.
Extends
!=
""
{
hasCallParentMethod
:=
false
for
_
,
methodCall
:=
range
node
.
MethodCalls
{
if
methodCall
.
Class
==
node
.
Extends
{
hasCallParentMethod
=
true
}
}
if
!
hasCallParentMethod
{
badSmellList
=
append
(
badSmellList
,
*&
domain
.
BadSmellModel
{
node
.
Path
,
""
,
"refusedBequest"
,
""
,
0
})
if
node
.
HaveCallParent
()
{
badSmellList
=
append
(
badSmellList
,
*&
bs_domain
.
BadSmellModel
{
node
.
Path
,
""
,
"refusedBequest"
,
""
,
0
})
}
}
...
...
@@ -82,21 +74,20 @@ func AnalysisBadSmell(nodes []domain.BsJClass) []domain.BadSmellModel {
normalClassLength
:=
withOutGetterSetterClass
(
node
.
Methods
)
if
node
.
Type
==
"Class"
&&
normalClassLength
>
20
{
description
:=
"methods number (without getter/setter): "
+
strconv
.
Itoa
(
normalClassLength
)
badSmellList
=
append
(
badSmellList
,
*&
domain
.
BadSmellModel
{
node
.
Path
,
""
,
"largeClass"
,
description
,
normalClassLength
})
badSmellList
=
append
(
badSmellList
,
*&
bs_
domain
.
BadSmellModel
{
node
.
Path
,
""
,
"largeClass"
,
description
,
normalClassLength
})
}
}
return
badSmellList
}
func
withOutGetterSetterClass
(
fullMethods
[]
domain
.
BsJMethod
)
int
{
func
withOutGetterSetterClass
(
fullMethods
[]
bs_
domain
.
BsJMethod
)
int
{
var
normalMethodSize
=
0
for
_
,
method
:=
range
fullMethods
{
if
!
strings
.
Contains
(
method
.
Name
,
"get"
)
&&
!
strings
.
Contains
(
method
.
Name
,
"set"
)
{
if
!
(
method
.
IsGetterSetter
()
)
{
normalMethodSize
++
}
}
return
normalMethodSize
}
core/context/call/call_graph.go
浏览文件 @
7304f3bf
...
...
@@ -2,6 +2,7 @@ package call
import
(
"github.com/phodal/coca/core/domain"
"github.com/phodal/coca/core/infrastructure/jpackage"
"strings"
)
...
...
@@ -39,8 +40,8 @@ func BuildCallChain(funcName string, methodMap map[string][]string, diMap map[st
if
len
(
methodMap
[
funcName
])
>
0
{
var
arrayResult
=
""
for
_
,
child
:=
range
methodMap
[
funcName
]
{
if
_
,
ok
:=
diMap
[
getClz
(
child
)];
ok
{
child
=
diMap
[
getClz
(
child
)]
+
"."
+
g
etMethodName
(
child
)
if
_
,
ok
:=
diMap
[
jpackage
.
GetClassName
(
child
)];
ok
{
child
=
diMap
[
jpackage
.
GetClassName
(
child
)]
+
"."
+
jpackage
.
G
etMethodName
(
child
)
}
if
len
(
methodMap
[
child
])
>
0
{
arrayResult
=
arrayResult
+
BuildCallChain
(
child
,
methodMap
,
diMap
)
...
...
@@ -54,19 +55,9 @@ func BuildCallChain(funcName string, methodMap map[string][]string, diMap map[st
return
"
\n
"
}
func
getClz
(
child
string
)
string
{
split
:=
strings
.
Split
(
child
,
"."
)
return
strings
.
Join
(
split
[
:
len
(
split
)
-
1
],
"."
)
}
func
getMethodName
(
child
string
)
string
{
split
:=
strings
.
Split
(
child
,
"."
)
return
strings
.
Join
(
split
[
len
(
split
)
-
1
:
],
"."
)
}
func
(
c
CallGraph
)
AnalysisByFiles
(
restApis
[]
domain
.
RestApi
,
deps
[]
domain
.
JClassNode
,
diMap
map
[
string
]
string
)
(
string
,
[]
CallApiCount
)
{
func
(
c
CallGraph
)
AnalysisByFiles
(
restApis
[]
domain
.
RestApi
,
deps
[]
domain
.
JClassNode
,
diMap
map
[
string
]
string
)
(
string
,
[]
domain
.
CallApi
)
{
methodMap
:=
BuildMethodMap
(
deps
)
var
apiCallSCounts
[]
CallApiCount
var
apiCallSCounts
[]
domain
.
CallApi
results
:=
"digraph G {
\n
"
...
...
@@ -78,7 +69,7 @@ func (c CallGraph) AnalysisByFiles(restApis []domain.RestApi, deps []domain.JCla
apiCallChain
:=
BuildCallChain
(
caller
,
methodMap
,
diMap
)
chain
=
chain
+
apiCallChain
count
:=
&
CallApiCount
{
count
:=
&
domain
.
CallApi
{
HttpMethod
:
restApi
.
HttpMethod
,
Caller
:
caller
,
Uri
:
restApi
.
Uri
,
...
...
@@ -107,4 +98,3 @@ func BuildMethodMap(clzs []domain.JClassNode) map[string][]string {
return
methodMap
}
core/context/git/git.go
浏览文件 @
7304f3bf
...
...
@@ -137,6 +137,10 @@ func GetRelatedFiles(commitMessages []CommitMessage, relatedConfig []byte) []apr
var
dataset
[][]
string
for
_
,
commitMessage
:=
range
commitMessages
{
var
set
[]
string
GIT_RELATED_MAX_SIZE
:=
10
if
len
(
commitMessage
.
Changes
)
>
GIT_RELATED_MAX_SIZE
{
continue
}
for
_
,
change
:=
range
commitMessage
.
Changes
{
if
strings
.
HasSuffix
(
change
.
File
,
".java"
)
&&
!
strings
.
HasSuffix
(
change
.
File
,
"Test.java"
)
{
if
strings
.
Contains
(
change
.
File
,
"core/main/java/"
)
{
...
...
core/context/tbs/tbs_app_test.go
浏览文件 @
7304f3bf
...
...
@@ -17,6 +17,7 @@ func TestTbsApp_EmptyTest(t *testing.T) {
result
:=
buildTbsResult
(
codePath
)
g
.
Expect
(
result
[
0
]
.
FileName
)
.
To
(
Equal
(
"../../../_fixtures/tbs/code/EmptyTest.java"
))
g
.
Expect
(
result
[
0
]
.
Line
)
.
To
(
Equal
(
8
))
g
.
Expect
(
result
[
0
]
.
Type
)
.
To
(
Equal
(
"EmptyTest"
))
}
...
...
core/domain/bad_smell.go
→
core/domain/b
s_domain/b
ad_smell.go
浏览文件 @
7304f3bf
package
domain
package
bs_domain
import
"strings"
type
BsJClass
struct
{
Package
string
...
...
@@ -93,3 +95,17 @@ type BadSmellModel struct {
Description
string
`json:"Description,omitempty"`
Size
int
`size:"Description,omitempty"`
}
func
(
b
*
BsJMethod
)
IsGetterSetter
()
bool
{
return
strings
.
HasPrefix
(
b
.
Name
,
"set"
)
||
strings
.
HasPrefix
(
b
.
Name
,
"get"
)
}
func
(
b
*
BsJClass
)
HaveCallParent
()
bool
{
hasCallParentMethod
:=
false
for
_
,
methodCall
:=
range
b
.
MethodCalls
{
if
methodCall
.
Class
==
b
.
Extends
{
hasCallParentMethod
=
true
}
}
return
hasCallParentMethod
}
core/
context/call/call_model
.go
→
core/
domain/call_api
.go
浏览文件 @
7304f3bf
package
call
package
domain
type
CallApiCount
struct
{
import
"sort"
type
CallApi
struct
{
HttpMethod
string
Uri
string
Caller
string
Size
int
}
func
SortApi
(
apis
[]
CallApi
)
{
sort
.
Slice
(
apis
,
func
(
i
,
j
int
)
bool
{
return
apis
[
i
]
.
Size
<
apis
[
j
]
.
Size
})
}
core/domain/rest_api.go
浏览文件 @
7304f3bf
package
domain
import
"strings"
type
RestApi
struct
{
Uri
string
HttpMethod
string
...
...
@@ -15,3 +17,17 @@ func (r *RestApi) BuildFullMethodPath() string {
return
r
.
PackageName
+
"."
+
r
.
ClassName
+
"."
+
r
.
MethodName
}
func
FilterApiByPrefix
(
apiPrefix
string
,
apis
[]
RestApi
,
)
[]
RestApi
{
var
restFieldsApi
[]
RestApi
if
apiPrefix
!=
""
{
for
_
,
api
:=
range
apis
{
if
strings
.
HasPrefix
(
api
.
Uri
,
apiPrefix
)
{
restFieldsApi
=
append
(
restFieldsApi
,
api
)
}
}
}
else
{
restFieldsApi
=
apis
}
return
restFieldsApi
}
core/infrastructure/constants/java_target_con
stants
.go
→
core/infrastructure/constants/java_target_con
fig
.go
浏览文件 @
7304f3bf
文件已移动
core/infrastructure/jpackage/jpackage.go
0 → 100644
浏览文件 @
7304f3bf
package
jpackage
import
"strings"
func
GetClassName
(
child
string
)
string
{
split
:=
strings
.
Split
(
child
,
"."
)
return
strings
.
Join
(
split
[
:
len
(
split
)
-
1
],
"."
)
}
func
GetMethodName
(
child
string
)
string
{
split
:=
strings
.
Split
(
child
,
"."
)
return
strings
.
Join
(
split
[
len
(
split
)
-
1
:
],
"."
)
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录