Commit 1193718f authored by Roxane Kang Maréchal's avatar Roxane Kang Maréchal
Browse files

Added cases which were not taken into consideration and corrected intentional fallthrough detection

parent 75f0e0a6
This diff is collapsed.
......@@ -175,7 +175,8 @@ rule MeasureMissingBreakInSwitch(ss: java!SwitchStatement) {
measurement: smm!DirectMeasurement (
error <- 'Missing breaks in Switch in Class : ' + ss.originalCompilationUnit.name + '',
-- Indicates the number of breaks missing in the switch statement
value <- (thisModule.nbBranchesOfASwitchStatement(ss) - thisModule.nbIntentionalFallThroughOfASwitchStatement(ss)) - thisModule.nbBreakStatementOfASwitchStatement(ss)
-- value <- (thisModule.nbBranchesOfASwitchStatement(ss) - thisModule.nbIntentionalFallThroughOfASwitchStatement(ss)) - thisModule.nbBreakStatementOfASwitchStatement(ss)
value <- thisModule.nbEmptySwitchCasesOfASwitchStatement(ss)
)
do {
missingBreakInSwitch;
......@@ -2038,17 +2039,34 @@ helper def: numberFieldsInClasse(s:java!ClassDeclaration) : Integer =
--- Returns the number of breaks/return/continue/throw of the switch statement passed in parameter
helper def: nbBreakStatementOfASwitchStatement(ss:java!SwitchStatement) : Integer =
ss.statements->select(each | each.oclIsTypeOf(java!BreakStatement)).size()
ss.statements->select(each |
each.oclIsTypeOf(java!BreakStatement) or
each.oclIsTypeOf(java!ReturnStatement) or
each.oclIsTypeOf(java!ThrowStatement)
).size()
;
--- Returns the number of expressions of the switch statement passed in parameter
helper def: nbExpressionsStatementOfASwitchStatement(ss:java!SwitchStatement) : Integer =
ss.statements->select(each | each.oclIsTypeOf(java!ExpressionStatement)).size()
--- Returns the number of empty switch cases of a switch statement
helper def: nbEmptySwitchCasesOfASwitchStatement(ss:java!SwitchStatement) : Integer =
-- Get the number of SwitchCases whoses next sibling is also a SwitchCase
ss.statements
->excluding(ss.statements.last())
->select(statement |
statement.oclIsTypeOf(java!SwitchCase) and
thisModule.nextSiblingIsSwitchCase(ss.statements.indexOf(statement), ss.statements)
).size() +
-- Add the last one if it's a switch case
ss.statements->select(statement
| statement = ss.statements.last() and statement.oclIsTypeOf(java!SwitchCase)).size()
;
--- Returns the number of intentional fall-through (empty switch cases) of a switchStatement
helper def: nbIntentionalFallThroughOfASwitchStatement(ss:java!SwitchStatement) : Integer =
thisModule.nbBranchesOfASwitchStatement(ss) - thisModule.nbExpressionsStatementOfASwitchStatement(ss)
--- Returns true if the next sibling of current SwitchCase is also a SwitchCase
helper def : nextSiblingIsSwitchCase(
indexOfSwitchCase : Integer,
statements : Sequence(java!Statement)) : Boolean =
statements.at(indexOfSwitchCase+1).oclIsTypeOf(java!SwitchCase)
;
--- Returns the name of a BodyDeclaration
......
......@@ -14,7 +14,8 @@ helper def: switchStatementContainsMissingBreaks(ss: java!SwitchStatement): Bool
-- If the number of switch cases (omitting those that empty => indicates intentional fallout) is bigger than
-- the number of break statements => we don't have a break statement for every switch case
-- in that case, return true, else false
(thisModule.nbBranchesOfASwitchStatement(ss) - thisModule.nbIntentionalFallThroughOfASwitchStatement(ss)) > thisModule.nbBreakStatementOfASwitchStatement(ss)
(thisModule.nbBranchesOfASwitchStatement(ss) - thisModule.nbEmptySwitchCasesOfASwitchStatement(ss)) > thisModule.nbBreakStatementOfASwitchStatement(ss)
;
--------------------------------------------- DoNotExtendJavaLangThrowable ---------------------------------------------
......
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