diff --git a/analysis.launch b/analysis.launch index acf2da3313b87555e832d4a36b55f8cca38cb7f5..62bb2a14f35bae2e3cdb213122c694df08ab50ea 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 0000000000000000000000000000000000000000..de1f195306cfa65d8a72fe6d4049418952f371c7 --- /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 0e8953f65f449a7b26446470925860effb0ff156..06e220ac127cd7e32afdf5b0516999f151f87c54 100644 --- a/src/main/atl/analysis.atl +++ b/src/main/atl/analysis.atl @@ -34,13 +34,14 @@ helper def: allMeasures(project : java!Model): Set(smm!Measure) = -- Multithreading rules -- thisModule.dontCallThreadRun(), - thisModule.avoidThreadGroup(), + --thisModule.avoidThreadGroup(), -- Code Style rules -- thisModule.shortMethodName(), thisModule.tooManyStaticImports(), thisModule.AvoidDollarSigns() , + thisModule.avoidFieldNameMatchingMethodName(), -- Design rules -- @@ -245,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 @@ -289,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 @@ -363,3 +341,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 ee95d9c16855573e58ac10d4ca414c93d540a87a..15d0333c304f2079dbe69c0c22065847b22a75a9 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; resIter : Set(smm!Measure) = Set{} | + if field.oclIsTypeOf(java!FieldDeclaration) + then + i.bodyDeclarations->iterate(method; resFinal : 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 99072692616cfa2f32010d69e220f7e459d6be64..034700a55b8a3405ea0bdee9b86085fb3fec0907 100644 --- a/src/main/atl/codestyle.atl +++ b/src/main/atl/codestyle.atl @@ -20,8 +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 - ); + ); \ No newline at end of file diff --git a/src/main/atl/design.atl b/src/main/atl/design.atl index a8b29c8a58cae3f290bd18a5d6ce1e349929e0d7..17fe05095662f5c28aa57704d26e5b992edb20bb 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