library performance; --------------------------------------------- UselessStringValueOf--------------------------------------------- --Goes through all the methods to check if the following rule is followed: --No need to call String.valueOf to append to a string; just use the valueOf() argument directly. helper def: uselessStringValueOf() : Set(smm!Measure) = java!MethodInvocation.allInstances() -> select (m | m.method.name='valueOf' ) -> select (m | m.refImmediateComposite().oclIsTypeOf(java!InfixExpression)) -> collect (m | thisModule.MeasureUselessStringValueOf(m)); ------------------------------------ TooFewBranchesForASwitchStatement-------------------------------------- -- Returns a set of measures for each switch statement that violates the rule : number of switch cases < 3 -- To test, use input model : tooFewBranchesForASwitchStatement.xmi helper def: tooFewBranchesForASwitchStatement(): Set(smm!Measure) = -- If the number of switch cases for the current switch statement is < 3 then add a new measure to the set java!SwitchStatement.allInstances() -> select (switchStatement | thisModule.nbBranchesOfASwitchStatement(switchStatement) < 3) -> collect (switchStatement | thisModule.MeasureTooFewBranchesForASwitchStatement(switchStatement)) ; --------------------------------------------- UseIndexOfChar--------------------------------------------- --Goes through all the MethodInvocation to check if the following rule is followed: --Use String.indexOf(char) when checking for the index of a single character; it executes faster. helper def: useIndexOfChar() : Set(smm!Measure) = java!MethodInvocation.allInstances() -> select (m | m.method.name='indexOf') -> select (m | m.arguments.size() = 1) -> select (m | not m.arguments.first().oclIsTypeOf(java!CharacterLiteral)) -> collect (m | thisModule.MeasureUseIndexOfChar(m)) ; --------------------------------------------- avoidThrowingNewInstanceOfSameException --------------------------------------------- -- Returns a set of Measures for each catch block throws a new instance of the caught exception helper def: avoidThrowingNewInstanceOfSameException() : Set(smm!Measure) = java!CatchClause.allInstances() ->iterate(catch; catchRes : Set(smm!Measure) = Set{} | if catch.body <> OclUndefined then catch.body.statements -> select(catchStatement | catchStatement.oclIsTypeOf(java!ThrowStatement) and catchStatement.expression.oclIsTypeOf(java!ClassInstanceCreation)) -> select(catchStatement | catchStatement.expression.type.type.name = catch.exception.type.type.name) -> collect(catchStatement | catchStatement.expression.arguments) -> iterate(arguments; argumentsRes : Set(smm!Measure) = Set{} | arguments -> select(argument | argument.oclIsTypeOf(java!SingleVariableAccess)) -> select(argument | argument.variable.name = catch.exception.name) -> collect(argument | thisModule.MeasureAvoidThrowingNewInstanceOfSameException(catch)) ) else catchRes endif ); --------------------------------------------- AvoidUsingShortType--------------------------------------------- --Detects the use of the primitive type 'short' for rule AvoidUsingShortType. Prefer the use of 'int' instead of 'short'. helper def: avoidUsingShortType() : Set(smm!Measure) = java!VariableDeclarationStatement.allInstances() ->select(variable | variable.type.type.name = 'short') ->collect(variable | thisModule.MeasureAvoidUsingShortType(variable)); --------------------------------------------- ShortInstantiation --------------------------------------------- -- Rule for metrics ShortInstantiation : return the set of class Measures that violates the rule. helper def: shortInstantiation() : Set(smm!Measure) = -- Take all ClassInstanceCreation with allInstances() java!ClassInstanceCreation.allInstances() -- Select all class who declare a Short type ->select(it| it.method.oclIsTypeOf(java!ConstructorDeclaration) and it.method.name = 'Short') -- collect every results and send an error message ->collect(it2|thisModule.MeasureShortInstantiation(it2.originalCompilationUnit)) ; --------------------------------------------- LongInstantiation --------------------------------------------- -- Rule for metricsLongInstantiation : return the set of class Measures that violates the rule. helper def: longInstantiation() : Set(smm!Measure) = -- Take all ClassInstanceCreation with allInstances() java!ClassInstanceCreation.allInstances() -- Select all class who declare a Long type ->select(it| it.method.oclIsTypeOf(java!ConstructorDeclaration) and it.method.name = 'Long') -- collect every results and send an error message ->collect(it2|thisModule.MeasureLongInstantiation(it2.originalCompilationUnit)) ; --------------------------------------------- AvoidRethrowingException --------------------------------------------- -- Rule for metrics AvoidRethrowingException : helper def: avoidRethrowingException() : Set (smm!Measure) = -- Browse through all try statements java!TryStatement.allInstances() ->iterate(tryStatement; res : Set(smm!Measure) = Set{} | tryStatement.catchClauses ->select(catchClause | catchClause.body <> OclUndefined and catchClause.body.statements.size() = 1) ->collect(catchClause | catchClause.body.statements) ->iterate(catchStatement; res1 : Set(smm!Measure) = Set{} | catchStatement ->select(catchState | catchState.oclIsTypeOf(java!ThrowStatement)) ->collect(catchState | thisModule.MeasureAvoidRethrowingException(catchState)) ) ); --------------------------------------------- IntegerInstantiation --------------------------------------------- -- Rule for metricsIntegerInstantiation : returns the set of class Measures that violates the rule. helper def: integerInstantiation() : Set(smm!Measure) = -- Take all ClassInstanceCreation with allInstances() java!ClassInstanceCreation.allInstances() -- Select all classes that declare an Integer type ->select(it|it.method.oclIsTypeOf(java!ConstructorDeclaration) and it.method.name = 'Integer') -- collect every result and sends an error message ->collect(it2|thisModule.MeasureIntegerInstantiation(it2.originalCompilationUnit)) ; --------------------------------------------- Use ArrayList Instead Of Vector --------------------------------------------- helper def: UseArrayListInsteadOfVector() : Set(smm!Measure) = -- Browse through all class(CompilationUnit) --On recupere tout les new Vector et on verifie que le Vector appelé appartient au package java.utile java!ConstructorDeclaration.allInstances()-> collect(i|i.abstractTypeDeclaration) ->select(p|p.name='Vector')->collect(i|i.package) ->select(p|p.name='util')->collect(i|i.package) ->select(p|p.name='java') ->collect(i |thisModule.RuleUseArrayListInsteadOfVector(i)); --------------------------------------------- StringToString --------------------------------------------- -- Rule for metrics stringToString : returns the set of class Measures that violates the rule. helper def: stringToString() : Set(smm!Measure) = java!MethodInvocation.allInstances() -> select( i | i.method.name = 'toString' and i.expression.variable.variablesContainer.type.type.name = 'String') -> collect ( i | thisModule.MeasureStringToString(i) ) ;