library bestPractices; --------------------------------------------- AvoidPrintStackTrace --------------------------------------------- --Goes through all the methods to check if the following rule is followed: --Avoid printStackTrace() helper def: avoidPrintStackTrace() : Set(smm!Measure) = java!MethodInvocation.allInstances() -> iterate(i; res : Set(smm!Measure) = Set{} | -- Add a new measurement if the argument of String.indexOf() is not of type char when checking for the index of a single character if thisModule.isAvoidPrintStackTrace(i) then res->union(Set{thisModule.MeasureAvoidPrintStackTrace(i)}) else res endif ); --Detect a wrong usage of the method printStackTrace() --return true if the method is called printStackTrace helper def:isAvoidPrintStackTrace(s:java!MethodInvocation): Boolean = if s.method.name = 'printStackTrace' then true else false 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{} | -- Foreach FieldDeclaration, check all methods i.bodyDeclarations->iterate(field; resIter : Set(smm!Measure) = Set{} | if field.oclIsTypeOf(java!FieldDeclaration) then i.bodyDeclarations -- Check if the name method and the name field is the same ->select(method | method.oclIsTypeOf(java!MethodDeclaration) and method.name = field.fragments.first().name) ->collect(method | thisModule.MesureAvoidFieldNameMatchingMethodName(i, method)) else res endif ) );