Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Phodal
Coca
提交
3e67cbf5
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 搜索 >>
未验证
提交
3e67cbf5
编写于
11月 05, 2019
作者:
P
Phodal Huang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: add some refactor code
上级
817d04d8
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
211 addition
and
12 deletion
+211
-12
bs/BadSmellApp.go
bs/BadSmellApp.go
+32
-1
bs/BadSmellListener.go
bs/BadSmellListener.go
+69
-8
bs/models/BsModel.go
bs/models/BsModel.go
+13
-3
bs/run/main.go
bs/run/main.go
+1
-0
examples/bs/DataClass.java
examples/bs/DataClass.java
+22
-0
examples/bs/MultipleIf.java
examples/bs/MultipleIf.java
+24
-0
refactor/unused_classes/UnusedClassesApp.go
refactor/unused_classes/UnusedClassesApp.go
+50
-0
未找到文件。
bs/BadSmellApp.go
浏览文件 @
3e67cbf5
package
main
package
bs
import
(
"encoding/json"
...
...
@@ -63,6 +63,7 @@ func analysisBadSmell(nodes []JFullClassNode) []BadSmellModel {
badSmellList
=
append
(
badSmellList
,
*&
BadSmellModel
{
node
.
Path
,
""
,
"lazyElement"
})
}
onlyHaveGetterAndSetter
:=
true
// Long Method
for
_
,
method
:=
range
node
.
Methods
{
if
method
.
StopLine
-
method
.
StartLine
>
50
{
...
...
@@ -70,11 +71,41 @@ func analysisBadSmell(nodes []JFullClassNode) []BadSmellModel {
badSmellList
=
append
(
badSmellList
,
*
longMethod
)
}
if
strings
.
Contains
(
method
.
Name
,
"get"
)
&&
strings
.
Contains
(
method
.
Name
,
"set"
)
{
onlyHaveGetterAndSetter
=
false
}
// longParameterList
if
len
(
method
.
Parameters
)
>
6
{
longParams
:=
&
BadSmellModel
{
node
.
Path
,
strconv
.
Itoa
(
method
.
StartLine
),
"longParameterList"
}
badSmellList
=
append
(
badSmellList
,
*
longParams
)
}
// longParameterList
if
method
.
MethodBs
.
IfSize
>
8
||
method
.
MethodBs
.
SwitchSize
>
8
{
longParams
:=
&
BadSmellModel
{
node
.
Path
,
strconv
.
Itoa
(
method
.
StartLine
),
"repeatedSwitches"
}
badSmellList
=
append
(
badSmellList
,
*
longParams
)
}
}
fmt
.
Println
(
onlyHaveGetterAndSetter
,
node
.
Type
,
len
(
node
.
Methods
))
if
onlyHaveGetterAndSetter
&&
node
.
Type
==
"Class"
&&
len
(
node
.
Methods
)
>
0
{
dataClass
:=
&
BadSmellModel
{
node
.
Path
,
""
,
"dataClass"
}
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
,
*&
BadSmellModel
{
node
.
Path
,
""
,
"refusedBequest"
})
}
}
// LargeClass
...
...
bs/BadSmellListener.go
浏览文件 @
3e67cbf5
package
main
package
bs
import
(
"github.com/antlr/antlr4/runtime/Go/antlr"
...
...
@@ -12,9 +12,13 @@ var imports []string
var
clzs
[]
string
var
currentPkg
string
var
currentClz
string
var
currentClzType
string
var
currentClzExtends
string
var
currentClzImplements
[]
string
var
methods
[]
JFullMethod
var
methodCalls
[]
JFullMethodCall
var
currentType
string
var
fields
=
make
(
map
[
string
]
string
)
var
localVars
=
make
(
map
[
string
]
string
)
...
...
@@ -26,6 +30,8 @@ func NewBadSmellListener() *BadSmellListener {
currentPkg
=
""
methods
=
nil
methodCalls
=
nil
currentClzImplements
=
nil
currentClzExtends
=
""
return
&
BadSmellListener
{}
}
...
...
@@ -37,8 +43,10 @@ func (s *BadSmellListener) getNodeInfo() *JFullClassNode {
return
&
JFullClassNode
{
currentPkg
,
currentClz
,
currentType
,
current
Clz
Type
,
""
,
currentClzExtends
,
currentClzImplements
,
methods
,
methodCalls
,
currentClassBs
,
...
...
@@ -55,12 +63,35 @@ func (s *BadSmellListener) EnterImportDeclaration(ctx *ImportDeclarationContext)
}
func
(
s
*
BadSmellListener
)
EnterClassDeclaration
(
ctx
*
ClassDeclarationContext
)
{
currentType
=
"Class"
current
Clz
Type
=
"Class"
currentClz
=
ctx
.
IDENTIFIER
()
.
GetText
()
if
ctx
.
EXTENDS
()
!=
nil
{
currentClzExtends
=
ctx
.
TypeType
()
.
GetText
()
}
if
ctx
.
IMPLEMENTS
()
!=
nil
{
typeList
:=
ctx
.
TypeList
()
.
(
*
TypeListContext
)
for
_
,
typ
:=
range
typeList
.
AllTypeType
()
{
typeData
:=
getTypeDATA
(
typ
.
(
*
TypeTypeContext
))
currentClzImplements
=
append
(
currentClzImplements
,
typeData
)
}
}
}
func
getTypeDATA
(
typ
*
TypeTypeContext
)
string
{
var
typeData
string
classOrInterface
:=
typ
.
ClassOrInterfaceType
()
.
(
*
ClassOrInterfaceTypeContext
)
if
classOrInterface
!=
nil
{
identifiers
:=
classOrInterface
.
AllIDENTIFIER
()
typeData
=
identifiers
[
len
(
identifiers
)
-
1
]
.
GetText
()
}
return
typeData
}
func
(
s
*
BadSmellListener
)
EnterInterfaceDeclaration
(
ctx
*
InterfaceDeclarationContext
)
{
currentType
=
"Interface"
current
Clz
Type
=
"Interface"
currentClz
=
ctx
.
IDENTIFIER
()
.
GetText
()
}
...
...
@@ -149,6 +180,7 @@ func (s *BadSmellListener) EnterMethodDeclaration(ctx *MethodDeclarationContext)
}
methodBSInfo
:=
*&
MethodBadSmellInfo
{
0
,
0
}
methodBadSmellInfo
:=
buildMethodBSInfo
(
ctx
,
methodBSInfo
)
method
:=
&
JFullMethod
{
name
,
...
...
@@ -159,19 +191,48 @@ func (s *BadSmellListener) EnterMethodDeclaration(ctx *MethodDeclarationContext)
stopLinePosition
,
methodBody
,
methodParams
,
methodB
S
Info
,
methodB
adSmell
Info
,
}
methods
=
append
(
methods
,
*
method
)
}
func
buildMethodBSInfo
(
context
*
MethodDeclarationContext
,
bsInfo
MethodBadSmellInfo
)
MethodBadSmellInfo
{
methodBody
:=
context
.
MethodBody
()
blockContext
:=
methodBody
.
GetChild
(
0
)
if
reflect
.
TypeOf
(
blockContext
)
.
String
()
==
"*parser.BlockContext"
{
blcStatement
:=
blockContext
.
(
*
BlockContext
)
.
AllBlockStatement
()
for
_
,
statement
:=
range
blcStatement
{
if
reflect
.
TypeOf
(
statement
.
GetChild
(
0
))
.
String
()
==
"*parser.StatementContext"
{
if
len
(
statement
.
GetChild
(
0
)
.
(
*
StatementContext
)
.
GetChildren
())
<
3
{
continue
}
statementCtx
:=
statement
.
GetChild
(
0
)
.
(
*
StatementContext
)
if
(
reflect
.
TypeOf
(
statementCtx
.
GetChild
(
1
))
.
String
())
==
"*parser.ParExpressionContext"
{
if
statementCtx
.
GetChild
(
0
)
.
(
antlr
.
ParseTree
)
.
GetText
()
==
"if"
{
bsInfo
.
IfSize
=
bsInfo
.
IfSize
+
1
}
if
statementCtx
.
GetChild
(
0
)
.
(
antlr
.
ParseTree
)
.
GetText
()
==
"switch"
{
bsInfo
.
SwitchSize
=
bsInfo
.
SwitchSize
+
1
}
}
}
}
}
return
bsInfo
}
func
(
s
*
BadSmellListener
)
EnterFormalParameterList
(
ctx
*
FormalParameterListContext
)
{
//fmt.Println(ctx.GetParent().GetParent().(antlr.RuleNode).get)
//fmt.Println(ctx.AllFormalParameter()
}
func
(
s
*
BadSmellListener
)
EnterAnnotation
(
ctx
*
AnnotationContext
)
{
if
currentType
==
"Class"
&&
ctx
.
QualifiedName
()
.
GetText
()
==
"Override"
{
if
current
Clz
Type
==
"Class"
&&
ctx
.
QualifiedName
()
.
GetText
()
==
"Override"
{
currentClassBs
.
OverrideSize
++
}
}
...
...
bs/models/BsModel.go
浏览文件 @
3e67cbf5
...
...
@@ -5,6 +5,8 @@ type JFullClassNode struct {
Class
string
Type
string
Path
string
Extends
string
Implements
[]
string
Methods
[]
JFullMethod
MethodCalls
[]
JFullMethodCall
ClassBS
ClassBadSmellInfo
...
...
@@ -34,8 +36,8 @@ type JFullMethod struct {
}
type
MethodBadSmellInfo
struct
{
SwitchSize
int
IfSize
int
SwitchSize
int
}
type
ClassBadSmellInfo
struct
{
...
...
@@ -48,8 +50,16 @@ type JFullParameter struct {
Type
string
}
func
NewJFullClassNode
()
*
JFullClassNode
{
info
:=
&
ClassBadSmellInfo
{
0
,
0
};
return
&
JFullClassNode
{
""
,
""
,
""
,
""
,
nil
,
nil
,
*
info
}
return
&
JFullClassNode
{
""
,
""
,
""
,
""
,
""
,
nil
,
nil
,
nil
,
*
info
}
}
bs/main.go
→
bs/
run/
main.go
浏览文件 @
3e67cbf5
...
...
@@ -2,6 +2,7 @@ package main
import
(
"encoding/json"
.
"github.com/phodal/coca/bs"
.
"github.com/phodal/coca/utils"
)
...
...
examples/bs/DataClass.java
0 → 100644
浏览文件 @
3e67cbf5
package
com.phodal.coca.analysis.identifier.model
;
public
class
DataClass
{
private
String
date
;
private
String
time
;
public
String
getDate
()
{
return
date
;
}
public
void
setDate
(
String
date
)
{
this
.
date
=
date
;
}
public
String
getTime
()
{
return
time
;
}
public
void
setTime
(
String
time
)
{
this
.
time
=
time
;
}
}
examples/bs/MultipleIf.java
0 → 100644
浏览文件 @
3e67cbf5
class
MultipleIf
{
public
static
void
main
(
String
args
[])
{
int
i
=
20
;
if
(
i
==
10
)
System
.
out
.
println
(
"i is 10"
);
else
if
(
i
==
15
)
System
.
out
.
println
(
"i is 15"
);
else
if
(
i
==
20
)
System
.
out
.
println
(
"i is 20"
);
if
(
i
==
20
)
System
.
out
.
println
(
"i is 20"
);
if
(
i
==
20
)
System
.
out
.
println
(
"i is 20"
);
if
(
i
==
20
)
System
.
out
.
println
(
"i is 20"
);
if
(
i
==
20
)
System
.
out
.
println
(
"i is 20"
);
if
(
i
==
20
)
System
.
out
.
println
(
"i is 20"
);
else
System
.
out
.
println
(
"i is not present"
);
}
}
refactor/unused_classes/UnusedClassesApp.go
0 → 100644
浏览文件 @
3e67cbf5
package
main
import
(
"encoding/json"
"fmt"
.
"github.com/phodal/coca/adapter/models"
.
"github.com/phodal/coca/utils"
"sort"
"strings"
)
var
parsedDeps
[]
JClassNode
func
main
()
{
var
analysisPackage
=
""
file
:=
ReadFile
(
"deps.json"
)
if
file
==
nil
{
return
}
_
=
json
.
Unmarshal
(
file
,
&
parsedDeps
)
sourceClasses
:=
make
(
map
[
string
]
string
)
targetlasses
:=
make
(
map
[
string
]
string
)
for
_
,
node
:=
range
parsedDeps
{
if
strings
.
Contains
(
node
.
Package
,
analysisPackage
)
{
className
:=
node
.
Package
+
"."
+
node
.
Class
sourceClasses
[
className
]
=
className
}
for
_
,
methodCall
:=
range
node
.
MethodCalls
{
if
strings
.
Contains
(
methodCall
.
Package
,
analysisPackage
)
{
className
:=
methodCall
.
Package
+
"."
+
methodCall
.
Class
targetlasses
[
className
]
=
className
}
}
}
var
excludePackage
[]
string
=
nil
for
_
,
clz
:=
range
sourceClasses
{
if
targetlasses
[
clz
]
!=
clz
{
excludePackage
=
append
(
excludePackage
,
clz
)
}
}
sort
.
Sort
(
sort
.
StringSlice
(
excludePackage
))
for
_
,
res
:=
range
excludePackage
{
fmt
.
Println
(
res
)
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录