library codestyle; ------------------------------------------------------------------------------------------ -- Rule for metrics AvoidDollarSigns : return the set of class Measures that violates the rule. helper def: avoidDollarSigns() : Set(smm!Measure) = -- Browse through all variable/method/class/interface. java!TypeDeclaration.allInstances()->union(java!MethodDeclaration.allInstances())->union(java!VariableDeclaration.allInstances())->iterate(i; res : Set(smm!Measure) = Set{} | -- Add a new measurement if the name contains a dollar. if i.name.indexOf('$') <> -1 then res->union(Set{thisModule.MesureAvoidDollarSigns(i)}) else res endif ); -- Rule for metrics shortMethodName : return the set of method Measures that violates the rule. helper def: shortMethodName() : Set(smm!Measure) = -- Browse through all method, add a new measurement if the size of the method name is less than 3. java!MethodDeclaration.allInstances() -> reject(each | each.isProxy())->select(i|i.name.size() < 3)->collect(j|thisModule.MesureShortMethodName(j)); --------------------------------------------- ShortClassName --------------------------------------------- -- Rule for metrics shortClassName : return the set of class Measures that violates the rule. helper def: shortClassName() : Set(smm!Measure) = java!ClassDeclaration.allInstances() -> reject(each | each.isProxy()) -> select( i | i.name.size() < 4) -> collect (i | thisModule.MesureShortClassName(i)); --------------------------------------------- TooManyStaticImports --------------------------------------------- -- Rule for metrics TooManyStaticImports : return the set of class Measures that violates the rule. helper def: tooManyStaticImports() : Set(smm!Measure) = java!CompilationUnit.allInstances() -- Add a new measurement if there are more than 4 static imports in the file. -> select (c | c.types.first().oclIsTypeOf(java!ClassDeclaration) and c.imports->select(i | i.static)->size() > 4) -> collect(c | thisModule.MesureTooManyStaticImports(c)); --------------------------------------------- ExtendsObject --------------------------------------------- -- Rule for metrics ExtendsObject helper def: extendsObject() : Set(smm!Measure) = -- select all class with a superTyper java!ClassDeclaration.allInstances()->select(it | (it.superClass <> OclUndefined)) -- select all class create by the user ->select(it2| it2.proxy = false) -- select all class who extend Object ->select(it3| it3.superClass.type.name = 'java.lang.Object' or it3.superClass.type.name = 'Object') -- collect all results and send an error message ->collect(it4|thisModule.MeasureExtendsObject(it4)) ; --------------------------------------------- UnnecessaryReturn --------------------------------------------- -- Rule for metrics UnnecessaryReturn : helper def: unnecessaryReturn() : Set (smm!Measure) = -- Browse through all method declarations java!ReturnStatement.allInstances() ->select(state | not state.expression.oclIsTypeOf(java!SingleVariableAccess)) ->collect(state | thisModule.MeasureUnnecessaryReturn(state));