Commit 273fe4dc authored by Gerson Sunyé's avatar Gerson Sunyé
Browse files

More code reorganization: new library performance

parent 02102f6e
......@@ -7,6 +7,7 @@ create OUT: smm from IN: java;
uses design;
uses codestyle;
uses performance;
rule Java2SMM {
from
......@@ -20,7 +21,6 @@ rule Java2SMM {
name <- 'Java metrics',
measureElements <- thisModule.allMeasures(project)
)
}
helper def: allMeasures(project : java!Model): Set(smm!Measure) =
......@@ -53,33 +53,6 @@ helper def: allMeasures(project : java!Model): Set(smm!Measure) =
thisModule.useIndexOfChar()
};
--------------------------------------------- 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() -> 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
;
-- creates a new Measure when String.valueOf is called to append its argument to a string
rule MeasureUselessStringValueOf(method : java!MethodInvocation) {
......@@ -236,32 +209,6 @@ rule MesureTooManyFields(class : java!ClassDeclaration) {
}
}
--------------------------------------------- UseIndexOfChar---------------------------------------------
--Goes through all the methods 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;
-- creates a new Measure when the parameter of String.indexOf(char) is not of type char when checking for the index of a single character
rule MeasureUseIndexOfChar(method : java!MethodInvocation) {
......@@ -302,18 +249,6 @@ rule numberOfClasses() {
}
}
-- 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) =
-- Iterate through all SwitchStatement
java!SwitchStatement.allInstances()->iterate(current; measures: Set(smm!Measure) = Set{} |
-- If the number of switch cases for the current switch statement is < 3 then add a new measure to the set
if (thisModule.nbBranchesOfASwitchStatement(current) < 3)
then measures->union(Set{thisModule.createMeasureForTooFewBranchesForASwitchStatement(current)})
else measures
endif
);
-- Rule that creates an instance of Measure for the switch statement with too few branches passed in parameter
rule createMeasureForTooFewBranchesForASwitchStatement(switchStatement: java!SwitchStatement) {
......
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() -> 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
;
-- 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) =
-- Iterate through all SwitchStatement
java!SwitchStatement.allInstances()->iterate(current; measures: Set(smm!Measure) = Set{} |
-- If the number of switch cases for the current switch statement is < 3 then add a new measure to the set
if (thisModule.nbBranchesOfASwitchStatement(current) < 3)
then measures->union(Set{thisModule.createMeasureForTooFewBranchesForASwitchStatement(current)})
else measures
endif
);
--------------------------------------------- UseIndexOfChar---------------------------------------------
--Goes through all the methods 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;
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