未验证 提交 7304f3bf 编写于 作者: P Phodal Huang

refactor: move to domain

上级 2cf880a1
......@@ -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");
......
......@@ -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, ",")
......
......@@ -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)
}
......
......@@ -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)
......
......@@ -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)
}
}
......
......@@ -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)
}
}
}
......@@ -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,
......
......@@ -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: annotations,
Annotations: currentMethod.Annotations,
}
}
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: annotations,
Annotations: currentMethod.Annotations,
}
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)
}
......
......@@ -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
}
......@@ -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)] + "." + getMethodName(child)
if _, ok := diMap[jpackage.GetClassName(child)]; ok {
child = diMap[jpackage.GetClassName(child)] + "." + jpackage.GetMethodName(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
}
......@@ -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/") {
......
......@@ -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"))
}
......
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
}
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
})
}
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
}
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.
先完成此消息的编辑!
想要评论请 注册