From a15410f91080e0f4e0fdf9fb6eb0ac7cbea98c53 Mon Sep 17 00:00:00 2001 From: Martin Date: Wed, 20 Nov 2019 15:32:25 +0100 Subject: [PATCH] TooManyMethods --- input/tooManyMethods.xmi | 265 ++++++++++++++++++++++++++++++++++++++ src/main/atl/analysis.atl | 45 ++++++- 2 files changed, 309 insertions(+), 1 deletion(-) create mode 100644 input/tooManyMethods.xmi diff --git a/input/tooManyMethods.xmi b/input/tooManyMethods.xmi new file mode 100644 index 0000000..ebb0693 --- /dev/null +++ b/input/tooManyMethods.xmi @@ -0,0 +1,265 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/atl/analysis.atl b/src/main/atl/analysis.atl index 11ce0c1..17a30db 100644 --- a/src/main/atl/analysis.atl +++ b/src/main/atl/analysis.atl @@ -24,7 +24,8 @@ helper def: allMeasures(project : java!Model): Set(smm!Measure) = Set{ thisModule.numberOfClasses(), thisModule.tooFewBranchesForASwitchStatement(), - thisModule.useIndexOfChar() --to test, use input/useIndexOfChar_java.xmi + thisModule.useIndexOfChar(), --to test, use input/useIndexOfChar_java.xmi + thisModule.tooManyMethods() --to test, use input/tooManyMethods.xmi -- -- Placeholder for other Measures -- @@ -133,3 +134,45 @@ helper def: nbBranchesOfASwitchStatement(switchStatement:java!SwitchStatement) : else nbSwitchCases endif ); + +--------------------------------------------- TooManyMethods --------------------------------------------- + +-- Rule for metrics TooManyMethods : return the set of class Measures that violates the rule. +-- To test, tooManyMethods.xmi, 1 class has enough methods, 2 classes have too many methods and 1 class has too many methods with its getters and setters but enough without them. + +helper def: tooManyMethods() : Set(smm!Measure) = + -- Browse through all class + 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 + then res->union(Set{thisModule.MesureTooManyMethods(i)}) + else res + 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 + om: smm!ObservedMeasure ( + measure <- noc, + measurements <- measurement + ), + noc: smm!DimensionalMeasure ( + name <- 'TooManyMethods', + shortDescription <- 'A class with too many methods is probably a good suspect for refactoring, in order to reduce its complexity and find a way to have more fine grained objects.' + ), + measurement: smm!DirectMeasurement ( + error <- class.originalCompilationUnit.name + ' have too many methods' + ) + + do { + noc; + } +} + +--------------------------------------------------------------------------------------------------------------------- \ No newline at end of file -- GitLab