From 450e613acc1451d5135fa84ec731c39522df50a0 Mon Sep 17 00:00:00 2001 From: Martin Date: Thu, 21 Nov 2019 13:43:31 +0100 Subject: [PATCH] TooManyStaticMethods --- input/tooManyStaticImports.xmi | 99 ++++++++++++++++++++++++++++++++++ src/main/atl/analysis.atl | 47 ++++++++++++++-- 2 files changed, 143 insertions(+), 3 deletions(-) create mode 100644 input/tooManyStaticImports.xmi diff --git a/input/tooManyStaticImports.xmi b/input/tooManyStaticImports.xmi new file mode 100644 index 0000000..d7ba583 --- /dev/null +++ b/input/tooManyStaticImports.xmi @@ -0,0 +1,99 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/atl/analysis.atl b/src/main/atl/analysis.atl index 4b56eef..f5a817b 100644 --- a/src/main/atl/analysis.atl +++ b/src/main/atl/analysis.atl @@ -23,10 +23,11 @@ rule Java2SMM { helper def: allMeasures(project : java!Model): Set(smm!Measure) = Set{ thisModule.numberOfClasses(), - thisModule.tooManyFields() -- to test, use input/example_toomanyfields.xmi + thisModule.tooManyFields(), -- to test, use input/example_toomanyfields.xmi thisModule.tooFewBranchesForASwitchStatement(), thisModule.useIndexOfChar(), --to test, use input/useIndexOfChar_java.xmi - thisModule.tooManyMethods() --to test, use input/tooManyMethods.xmi + thisModule.tooManyMethods(), --to test, use input/tooManyMethods.xmi + thisModule.tooManyStaticImports() --to test, use input/tooManyStaticImports.xmi -- -- Placeholder for other Measures -- @@ -205,10 +206,50 @@ rule MesureTooManyMethods(class : java!ClassDeclaration) { 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' + error <- class.originalCompilationUnit.name + ' has too many methods' ) do { noc; } } + +--------------------------------------------- TooManyStaticImports --------------------------------------------- + +-- Rule for metrics TooManyStaticImports : return the set of class Measures that violates the rule. +-- To test, tooManyStaticImports.xmi, 1 class has enough static imports, 2 classes have too many static imports. + +helper def: tooManyStaticImports() : 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 4 static imports in the class. + if thisModule.numberStaticImportInClasse(i) > 4 + then res->union(Set{thisModule.MesureTooManyStaticImports(i)}) + else res + endif + ); + + +helper def: numberStaticImportInClasse(s:java!ClassDeclaration) : Integer = + -- Return the number of static imports in a specific ClassDeclaration. + java!ImportDeclaration.allInstances()->select(i | i.static and i.originalCompilationUnit.types.first() = s)->size(); + +-- A Measure instance if the class violates the rule 'TooManyStaticImports'. +rule MesureTooManyStaticImports(class : java!ClassDeclaration) { + to + om: smm!ObservedMeasure ( + measure <- noc, + measurements <- measurement + ), + noc: smm!DimensionalMeasure ( + name <- 'TooManyStaticImports', + shortDescription <- 'If you overuse the static import feature, it can make your program unreadable and unmaintainable, polluting its namespace with all the static members you import. Readers of your code (including you, a few months after you wrote it) will not know which class a static member comes from (Sun 1.5 Language Guide).' + ), + measurement: smm!DirectMeasurement ( + error <- class.originalCompilationUnit.name + ' has too many static imports' + ) + + do { + noc; + } +} \ No newline at end of file -- GitLab