From f01d6473ec566229f4fa972032c09b693f134b1f Mon Sep 17 00:00:00 2001 From: aliegeois Date: Sat, 21 Dec 2019 18:50:38 +0100 Subject: [PATCH 1/2] fix #618 "AvoidReassigningParameters" --- input/avoid-reassigning-parameters.xmi | 64 ++++++++++++++++++++++++++ src/main/atl/analysis.atl | 22 +++++++++ src/main/atl/bestPractices.atl | 24 ++++++++++ 3 files changed, 110 insertions(+) create mode 100644 input/avoid-reassigning-parameters.xmi diff --git a/input/avoid-reassigning-parameters.xmi b/input/avoid-reassigning-parameters.xmi new file mode 100644 index 0000000..c07d10d --- /dev/null +++ b/input/avoid-reassigning-parameters.xmi @@ -0,0 +1,64 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/atl/analysis.atl b/src/main/atl/analysis.atl index 22b8897..a8c2b46 100644 --- a/src/main/atl/analysis.atl +++ b/src/main/atl/analysis.atl @@ -121,6 +121,7 @@ helper def: allMeasures(project : java!Model): Set(smm!Measure) = thisModule.UseProperClassLoader(), -- Best practices rules + thisModule.avoidReassigningParameters(), thisModule.forLoopVariableCount(), thisModule.switchDensity(), thisModule.useAssertSameInsteadOfAssertTrue(), @@ -1836,4 +1837,25 @@ rule MeasureFieldDeclarationsShouldBeAtStartOfClass(class: java!ClassDeclaration do { noc; } +} + +------------------------------------------- AvoidReassigningParameters ---------------------------------------------- + +-- A Measure instance if the class violates the rule AvoidReassigningParameters. +rule MeasureAvoidReassigningParameters(assignment: java!SingleVariableAccess) { + to + om: smm!ObservedMeasure ( + measure <- noc, + measurements <- measurement + ), + noc: smm!DimensionalMeasure ( + name <- 'AvoidReassigningParameters', + shortDescription <- 'Reassigning values to incoming parameters is not recommended. Use temporary local variables instead.' + ), + measurement: smm!DirectMeasurement ( + error<- assignment.leftHandSide.variable.name + ' should not be reassigned' + ) + do { + noc; + } } \ No newline at end of file diff --git a/src/main/atl/bestPractices.atl b/src/main/atl/bestPractices.atl index 2cc4449..f677beb 100644 --- a/src/main/atl/bestPractices.atl +++ b/src/main/atl/bestPractices.atl @@ -117,3 +117,27 @@ helper context java!Expression def: nbLocalVariableDeclarations(): Integer = helper context java!VariableDeclarationExpression def: nbLocalVariableDeclarations(): Integer = self.fragments.size(); -- The fragments collection contains all the variable declarations of an initializer* -- Initializer* : for(int x = 0; ...), "int x = 0" is the initializer part of the for loop + +--------------------------------------------- AvoidReassigningParameters --------------------------------------------- + +helper def: avoidReassigningParameters(): Set(smm!Measure) = + java!AbstractMethodDeclaration.allInstances()->select(method | + method.parameters.size() > 0 -- need at least 1 parameter + )->collect(method | + method.body.statements->select(statement | + statement.oclIsTypeOf(java!ExpressionStatement) -- statement must be an expression + )->collect(expressionStatement | + expressionStatement.expression + )->select(expression | + expression.oclIsTypeOf(java!Assignment) -- the expression must be an assignment + )->select(assignment | + assignment.leftHandSide.oclIsTypeOf(java!SingleVariableAccess) -- the receiving end of the assignment must be a variable access + )->select(assignment | + method.parameters->collect(singleVariableDeclaration | + singleVariableDeclaration.name -- for each parameter of the method we retrieve its name + )->includes(assignment.leftHandSide.variable.name) -- one of the parameter is on the receiving of an assignment + )->collect(assignment | + thisModule.MeasureAvoidReassigningParameters(assignment) + ) + + )->flatten(); \ No newline at end of file -- GitLab From 3cc4ee09f6401d84657d1002b84a9c3fb811904b Mon Sep 17 00:00:00 2001 From: aliegeois Date: Sat, 21 Dec 2019 21:06:11 +0100 Subject: [PATCH 2/2] correction of #618 --- src/main/atl/bestPractices.atl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/atl/bestPractices.atl b/src/main/atl/bestPractices.atl index 168babb..dd6844d 100644 --- a/src/main/atl/bestPractices.atl +++ b/src/main/atl/bestPractices.atl @@ -122,7 +122,7 @@ helper context java!VariableDeclarationExpression def: nbLocalVariableDeclaratio helper def: avoidReassigningParameters(): Set(smm!Measure) = java!AbstractMethodDeclaration.allInstances()->select(method | - method.parameters.size() > 0 -- need at least 1 parameter + method.parameters.size() > 0 and not method.body.oclIsUndefined() -- need at least one parameter and a body )->collect(method | method.body.statements->select(statement | statement.oclIsTypeOf(java!ExpressionStatement) -- statement must be an expression -- GitLab