From e0349546ad655908abff173e5d17f3b24cb4cd62 Mon Sep 17 00:00:00 2001 From: Martin Date: Thu, 21 Nov 2019 20:17:27 +0100 Subject: [PATCH 1/2] AvoidFieldNameMatchingMethodName --- analysis.launch | 5 +- .../avoid-field-name-matching-method-name.xmi | 103 ++++++++++++++++++ src/main/atl/analysis.atl | 21 ++++ src/main/atl/bestPractices.atl | 27 ++++- src/main/atl/codestyle.atl | 4 + 5 files changed, 157 insertions(+), 3 deletions(-) create mode 100644 input/avoid-field-name-matching-method-name.xmi diff --git a/analysis.launch b/analysis.launch index acf2da3..62bb2a1 100644 --- a/analysis.launch +++ b/analysis.launch @@ -2,6 +2,7 @@ + @@ -42,8 +43,8 @@ - - + + diff --git a/input/avoid-field-name-matching-method-name.xmi b/input/avoid-field-name-matching-method-name.xmi new file mode 100644 index 0000000..de1f195 --- /dev/null +++ b/input/avoid-field-name-matching-method-name.xmi @@ -0,0 +1,103 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/atl/analysis.atl b/src/main/atl/analysis.atl index 0e8953f..ea5512b 100644 --- a/src/main/atl/analysis.atl +++ b/src/main/atl/analysis.atl @@ -41,6 +41,7 @@ helper def: allMeasures(project : java!Model): Set(smm!Measure) = thisModule.shortMethodName(), thisModule.tooManyStaticImports(), thisModule.AvoidDollarSigns() , + thisModule.avoidFieldNameMatchingMethodName(), -- Design rules -- @@ -363,3 +364,23 @@ rule measureAvoidThreadGroup(variable : java!VariableDeclarationFragment) { noc; } } + +-- A Measure instance if the class violates the rule 'AvoidFieldNameMatchingMethodName'. +rule MesureAvoidFieldNameMatchingMethodName(class : java!ClassDeclaration, method : java!MethodDeclaration) { + to + om: smm!ObservedMeasure ( + measure <- noc, + measurements <- measurement + ), + noc: smm!DimensionalMeasure ( + name <- 'AvoidFieldNameMatchingMethodName', + shortDescription <- 'It can be confusing to have a field name with the same name as a method. While this is permitted, having information (field) and actions (method) is not clear naming. Developers versed in Smalltalk often prefer this approach as the methods denote accessor methods.' + ), + measurement: smm!DirectMeasurement ( + error <- 'In the ' + class.name + ' class you have an field and an method with the same name : '+ method.name + ) + + do { + noc; + } +} diff --git a/src/main/atl/bestPractices.atl b/src/main/atl/bestPractices.atl index ee95d9c..91df302 100644 --- a/src/main/atl/bestPractices.atl +++ b/src/main/atl/bestPractices.atl @@ -21,4 +21,29 @@ helper def:isAvoidPrintStackTrace(s:java!MethodInvocation): Boolean = true else false - endif; \ No newline at end of file + endif; + +--------------------------------------------- AvoidFieldNameMatchingMethodName --------------------------------------------- + +-- Rule for metrics AvoidFieldNameMatchingMethodName : return the set of class Measures that violates the rule. +helper def: avoidFieldNameMatchingMethodName() : Set(smm!Measure) = + -- Browse through all class + java!ClassDeclaration.allInstances()->iterate(i; res : Set(smm!Measure) = Set{} | + -- Add a new measurement if there are more than 4 static imports in the class. + i.bodyDeclarations->iterate(field; res : Set(smm!Measure) = Set{} | + if field.oclIsTypeOf(java!FieldDeclaration) + then + i.bodyDeclarations->iterate(method; res : Set(smm!Measure) = Set{} | + if method.oclIsTypeOf(java!MethodDeclaration) + then + if method.name = field.fragments.first().name + then res->union(Set{thisModule.MesureAvoidFieldNameMatchingMethodName(i, method)}) + else res + endif + else res + endif + ) + else res + endif + ) + ); \ No newline at end of file diff --git a/src/main/atl/codestyle.atl b/src/main/atl/codestyle.atl index 9907269..0834128 100644 --- a/src/main/atl/codestyle.atl +++ b/src/main/atl/codestyle.atl @@ -25,3 +25,7 @@ helper def: tooManyStaticImports() : Set(smm!Measure) = else res endif ); + +-- Return the number of static imports in a specific ClassDeclaration. +helper def: numberOfStaticImportInClass(s:java!CompilationUnit) : Integer = + s.imports->select(i | i.static)->size(); \ No newline at end of file -- GitLab From f560f9cfadb22792c9d53ac08bb4716f9dd75e77 Mon Sep 17 00:00:00 2001 From: Martin Date: Fri, 22 Nov 2019 09:06:53 +0100 Subject: [PATCH 2/2] Applications de petites modifications dans mes trois issues, toomanymethod, toomanystaticimports ent avoidfieldnamematchingmethodname --- src/main/atl/analysis.atl | 25 +------------------------ src/main/atl/bestPractices.atl | 8 ++++---- src/main/atl/codestyle.atl | 8 ++------ src/main/atl/design.atl | 6 +++--- 4 files changed, 10 insertions(+), 37 deletions(-) diff --git a/src/main/atl/analysis.atl b/src/main/atl/analysis.atl index ea5512b..06e220a 100644 --- a/src/main/atl/analysis.atl +++ b/src/main/atl/analysis.atl @@ -34,7 +34,7 @@ helper def: allMeasures(project : java!Model): Set(smm!Measure) = -- Multithreading rules -- thisModule.dontCallThreadRun(), - thisModule.avoidThreadGroup(), + --thisModule.avoidThreadGroup(), -- Code Style rules -- @@ -246,12 +246,6 @@ helper def: nbBranchesOfASwitchStatement(switchStatement:java!SwitchStatement) : endif ); - - -helper def: numberMethodsInClasse(s:java!ClassDeclaration) : Integer = - -- Return the number of MethodsDeclaration in a class. - s.bodyDeclarations->select(r | r.oclIsTypeOf(java!MethodDeclaration))->reject(without | without.name.startsWith('get') or without.name.startsWith('set') or without.name.startsWith('is'))->size(); - -- A Measure instance if the class violates the rule 'TooManyMethods'. rule MesureTooManyMethods(class : java!ClassDeclaration) { to @@ -290,23 +284,6 @@ rule MesureReturnFromFinallyBlock(method : java!MethodDeclaration) { } } ---------------------------------------------- TooManyStaticImports --------------------------------------------- - --- Rule for metrics TooManyStaticImports : return the set of class Measures that violates the rule. -helper def: tooManyStaticImports() : Set(smm!Measure) = - -- Browse through all class(CompilationUnit) - java!CompilationUnit.allInstances()->iterate(i; res : Set(smm!Measure) = Set{} | - -- Add a new measurement if there are more than 4 static imports in the class. - if thisModule.numberOfStaticImportInClass(i) > 4 - then res->union(Set{thisModule.MesureTooManyStaticImports(i)}) - else res - endif - ); - --- Return the number of static imports in a specific ClassDeclaration. -helper def: numberOfStaticImportInClass(s:java!CompilationUnit) : Integer = - s.imports->select(i | i.static)->size(); - -- A Measure instance if the class violates the rule 'TooManyStaticImports'. rule MesureTooManyStaticImports(class : java!CompilationUnit) { to diff --git a/src/main/atl/bestPractices.atl b/src/main/atl/bestPractices.atl index 91df302..15d0333 100644 --- a/src/main/atl/bestPractices.atl +++ b/src/main/atl/bestPractices.atl @@ -30,10 +30,10 @@ helper def: avoidFieldNameMatchingMethodName() : Set(smm!Measure) = -- Browse through all class java!ClassDeclaration.allInstances()->iterate(i; res : Set(smm!Measure) = Set{} | -- Add a new measurement if there are more than 4 static imports in the class. - i.bodyDeclarations->iterate(field; res : Set(smm!Measure) = Set{} | + i.bodyDeclarations->iterate(field; resIter : Set(smm!Measure) = Set{} | if field.oclIsTypeOf(java!FieldDeclaration) then - i.bodyDeclarations->iterate(method; res : Set(smm!Measure) = Set{} | + i.bodyDeclarations->iterate(method; resFinal : Set(smm!Measure) = Set{} | if method.oclIsTypeOf(java!MethodDeclaration) then if method.name = field.fragments.first().name @@ -45,5 +45,5 @@ helper def: avoidFieldNameMatchingMethodName() : Set(smm!Measure) = ) else res endif - ) - ); \ No newline at end of file + ) +); \ No newline at end of file diff --git a/src/main/atl/codestyle.atl b/src/main/atl/codestyle.atl index 0834128..034700a 100644 --- a/src/main/atl/codestyle.atl +++ b/src/main/atl/codestyle.atl @@ -20,12 +20,8 @@ helper def: tooManyStaticImports() : Set(smm!Measure) = -- Browse through all class(CompilationUnit) java!CompilationUnit.allInstances()->iterate(i; res : Set(smm!Measure) = Set{} | -- Add a new measurement if there are more than 4 static imports in the class. - if thisModule.numberOfStaticImportInClass(i) > 4 + if i.types.first().oclIsTypeOf(java!ClassDeclaration) and i.imports->select(i | i.static)->size() > 4 then res->union(Set{thisModule.MesureTooManyStaticImports(i)}) else res endif - ); - --- Return the number of static imports in a specific ClassDeclaration. -helper def: numberOfStaticImportInClass(s:java!CompilationUnit) : Integer = - s.imports->select(i | i.static)->size(); \ No newline at end of file + ); \ No newline at end of file diff --git a/src/main/atl/design.atl b/src/main/atl/design.atl index a8b29c8..17fe050 100644 --- a/src/main/atl/design.atl +++ b/src/main/atl/design.atl @@ -15,12 +15,12 @@ helper def: tooManyFields() : Set(smm!Measure) = --------------------------------------------- TooManyMethods --------------------------------------------- -- Rule for metrics TooManyMethods : return the set of class Measures that violates the rule. - helper def: tooManyMethods() : Set(smm!Measure) = -- Browse through all class - java!ClassDeclaration.allInstances() -> reject(each | each.isProxy())->iterate(i; res : Set(smm!Measure) = Set{} | + java!ClassDeclaration.allInstances()->reject(each | each.isProxy())->iterate(i; res : Set(smm!Measure) = Set{} | -- Add a new measurement if there are more than 10 methods in the class. - if thisModule.numberMethodsInClasse(i) > 10 + if i.bodyDeclarations->select(r | r.oclIsTypeOf(java!MethodDeclaration))->reject(without | + without.name.startsWith('get') or without.name.startsWith('set') or without.name.startsWith('is'))->size() > 10 then res->union(Set{thisModule.MesureTooManyMethods(i)}) else res endif -- GitLab