diff --git a/input/avoid-rethrowing-exception.xmi b/input/avoid-rethrowing-exception.xmi new file mode 100644 index 0000000000000000000000000000000000000000..e1ab94d0cf5f94a0bad85131c730dd99f7ff7f6e --- /dev/null +++ b/input/avoid-rethrowing-exception.xmi @@ -0,0 +1,157 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/atl/analysis.atl b/src/main/atl/analysis.atl index aa51725c01ceb8b9a1df1aae3d3e89c728a98ed0..31bef9efee2dd3da053a01f36b19f27983b6ca98 100644 --- a/src/main/atl/analysis.atl +++ b/src/main/atl/analysis.atl @@ -70,6 +70,7 @@ helper def: allMeasures(project : java!Model): Set(smm!Measure) = thisModule.avoidPrintStackTrace(), thisModule.avoidUsingShortType(), thisModule.emptyStatementBlock(), + thisModule.avoidRethrowingException(), thisModule.integerInstantiation(), -- Documentation rules @@ -838,6 +839,29 @@ rule MeasureCompareObjectsWithEquals(expression : java!InfixExpression) { } } + +--------------------------------------------- AvoidRethrowingException --------------------------------------------- + --A Measure instance if the class violates the rule AvoidRethrowingException. +rule MeasureAvoidRethrowingException(statement : java!TryStatement) { + to + om: smm!ObservedMeasure ( + measure <- noc, + measurements <- measurement + ), + noc: smm!DimensionalMeasure ( + + name <- 'AvoidRethrowingException ', + shortDescription <- 'Avoid merely rethrowing an exception.' + + ), + measurement: smm!DirectMeasurement ( + error<-'In the Class '+ statement.originalCompilationUnit.name + ' an exception was caught and merely rethrown.' + ) + do { + noc; + } +} + --------------------------------------------- IntegerInstantiation --------------------------------------------- -- A Measure instance if the class violates the rule 'IntegerInstantiation'. rule MeasureIntegerInstantiation(variable : java!CompilationUnit) { @@ -847,6 +871,7 @@ rule MeasureIntegerInstantiation(variable : java!CompilationUnit) { measurements <- measurement ), noc: smm!DimensionalMeasure ( + name <- 'IntegerInstantiation', shortDescription <- 'Calling new Integer() causes memory allocation that can be avoided by the static Integer.valueOf(). It makes use of an internal cache that recycles earlier instances making it more memory efficient. Note that new Integer() is deprecated since JDK 9 for that reason.' ), diff --git a/src/main/atl/performance.atl b/src/main/atl/performance.atl index 467ab6744e3ec8dbae54e82f96c59c116c308024..d6faad364131522cb3639adb05e70a666223bb27 100644 --- a/src/main/atl/performance.atl +++ b/src/main/atl/performance.atl @@ -119,6 +119,20 @@ helper def: longInstantiation() : Set(smm!Measure) = ->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. @@ -130,5 +144,3 @@ helper def: integerInstantiation() : Set(smm!Measure) = -- collect every result and sends an error message ->collect(it2|thisModule.MeasureIntegerInstantiation(it2.originalCompilationUnit)) ; - -