Commit 4dd0eb53 authored by Gerson SUNYE's avatar Gerson SUNYE
Browse files

Merge branch 'master' into 'master'

Clean rules from issues 616, 873, 878, 909 & 910

See merge request naomod/mde/projet-2019!89
parents 56ff07a5 e13df261
......@@ -5,23 +5,10 @@ library bestPractices;
--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;
java!MethodInvocation.allInstances()
-> select (m | m.method.name = 'printStackTrace')
-> collect (m | thisModule.MeasureAvoidPrintStackTrace(m))
;
--------------------------------------------- AvoidFieldNameMatchingMethodName ---------------------------------------------
......
......@@ -5,47 +5,37 @@ library multithreading;
--Goes through all the methods to check if the following rule is followed:
--Don't call Thread.run()
helper def: dontCallThreadRun() : Set(smm!Measure) =
java!MethodInvocation.allInstances() -> iterate(i; res : Set(smm!Measure) = Set{} |
if (thisModule.isDontCallThreadRun(i))
then res->union(Set{thisModule.MeasureDontCallThreadRun(i)})
else res
endif
);
java!MethodInvocation.allInstances()
-> select (m | m.method.name='run' )
-> select (m | m.expression.isDontCallThreadRun() )
-> collect (m | thisModule.MeasureDontCallThreadRun(m));
--Detect a wrong usage of the method Thread.run()
--return true if run() is called on a Thread variable
helper context java!SingleVariableAccess def : isDontCallThreadRun(): Boolean =
--check if the method is called like this:
--t.run() where t is of type Thread
self.variable.variablesContainer.type.type.name.toString() = 'Thread';
--Detect a wrong usage of the method Thread.run()
--return true if run() is called on a Thread variable or on a Thread instance creation
helper def:isDontCallThreadRun(s:java!MethodInvocation): Boolean =
if s.method.name = 'run' then
--check if the method is called like this:
--t.run() where t is of type Thread
if s.expression.oclType().toString() = 'java!SingleVariableAccess' then
if s.expression.variable.variablesContainer.type.type.name.toString() = 'Thread' then
true
else false
endif
--check if the method run is called like this:
--new Thread().run();
else
if s.expression.oclType().toString() = 'java!ClassInstanceCreation' then
if s.expression.method.name.toString() = 'Thread' then
true
else false
endif
else false
endif
endif
else false
endif;
--return true if run() is called on a Thread instance creation
helper context java!ClassInstanceCreation def : isDontCallThreadRun(): Boolean =
--check if the method run is called like this:
--new Thread().run();
self.method.name.toString() = 'Thread';
--------------------------------------------- AvoidThreadGroup ---------------------------------------------
--Goes through all the variables to check if the following rule is followed:
--Avoid using java.lang.ThreadGroup;
helper def: avoidThreadGroup() : Set(smm!Measure) =
java!VariableDeclarationFragment.allInstances() ->
select(i | i.variablesContainer.type.toString() <> 'OclUndefined')->
select(s|s.variablesContainer.type.type.name.toString() = 'ThreadGroup')->collect(i | thisModule.measureAvoidThreadGroup(i) );
java!VariableDeclarationFragment.allInstances()
-> select(i | i.variablesContainer.type.toString() <> 'OclUndefined')
-> select(s|s.variablesContainer.type.type.name.toString() = 'ThreadGroup')
-> collect(i | thisModule.measureAvoidThreadGroup(i) );
--------------------------------------------- UseNotifyAllInsteadOfNotify ---------------------------------------------
......
......@@ -5,28 +5,10 @@ library performance;
--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() -> 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.isUselessStringValueOf(i) then
res->union(Set{thisModule.MeasureUselessStringValueOf(i)})
else
res
endif
);
--Detect a wrong usage of the method String.valueOf()
--return true if String.valueOf is used to append an argument to a String
helper def:isUselessStringValueOf(s:java!MethodInvocation): Boolean =
if s.method.name='valueOf' then
if s.refImmediateComposite().oclType().toString() = 'java!InfixExpression' then
true
else
false
endif
else
false
endif
;
java!MethodInvocation.allInstances()
-> select (m | m.method.name='valueOf' )
-> select (m | m.refImmediateComposite().oclIsTypeOf(java!InfixExpression))
-> collect (m | thisModule.MeasureUselessStringValueOf(m));
------------------------------------ TooFewBranchesForASwitchStatement--------------------------------------
......@@ -41,30 +23,15 @@ helper def: tooFewBranchesForASwitchStatement(): Set(smm!Measure) =
--------------------------------------------- UseIndexOfChar---------------------------------------------
--Goes through all the methods to check if the following rule is followed:
--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() -> 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.isWrongUsageIndexOfChar(i) then
res->union(Set{thisModule.MeasureUseIndexOfChar(i)})
else
res
endif
);
--Detect a wrong usage of the method String.indexOf(char)
--return true if the argument of String.indexOf(char) is not of type char when checking for the index of a single character
helper def:isWrongUsageIndexOfChar(s:java!MethodInvocation): Boolean =
if s.method.name = 'indexOf' then
if s.arguments.size() = 1 and s.arguments.first().oclType().toString() <> 'java!CharacterLiteral' then
true
else
false
endif
else
false
endif;
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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment