Commit 837aeb13 authored by Romain F. T's avatar Romain F. T
Browse files

fix #646

parent 1d912ac7
<?xml version="1.0" encoding="ASCII"?>
<java:Model xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:java="http://www.eclipse.org/MoDisco/Java/0.2.incubation/java" name="switch-stmts-should-have-default">
<ownedElements name="(default package)">
<ownedElements xsi:type="java:ClassDeclaration" originalCompilationUnit="//@compilationUnits.0" name="FooTest646">
<modifier visibility="public"/>
<bodyDeclarations xsi:type="java:MethodDeclaration" originalCompilationUnit="//@compilationUnits.0" name="methodWithSwitchKo">
<modifier visibility="public"/>
<body originalCompilationUnit="//@compilationUnits.0">
<statements xsi:type="java:SwitchStatement" originalCompilationUnit="//@compilationUnits.0">
<statements xsi:type="java:SwitchCase" originalCompilationUnit="//@compilationUnits.0">
<expression xsi:type="java:Integer" originalCompilationUnit="//@compilationUnits.0" value="1"/>
</statements>
<statements xsi:type="java:SwitchCase" originalCompilationUnit="//@compilationUnits.0">
<expression xsi:type="java:Integer" originalCompilationUnit="//@compilationUnits.0" value="2"/>
</statements>
<statements xsi:type="java:SwitchCase" originalCompilationUnit="//@compilationUnits.0">
<expression xsi:type="java:Integer" originalCompilationUnit="//@compilationUnits.0" value="3"/>
</statements>
</statements>
</body>
<returnType type="//@orphanTypes.5"/>
</bodyDeclarations>
</ownedElements>
<ownedElements xsi:type="java:ClassDeclaration" originalCompilationUnit="//@compilationUnits.1" name="FooTest646_2">
<modifier visibility="public"/>
<bodyDeclarations xsi:type="java:MethodDeclaration" originalCompilationUnit="//@compilationUnits.1" name="methodWithSwitchOk">
<modifier visibility="public"/>
<body originalCompilationUnit="//@compilationUnits.1">
<statements xsi:type="java:SwitchStatement" originalCompilationUnit="//@compilationUnits.1">
<statements xsi:type="java:SwitchCase" originalCompilationUnit="//@compilationUnits.1">
<expression xsi:type="java:Integer" originalCompilationUnit="//@compilationUnits.1" value="1"/>
</statements>
<statements xsi:type="java:SwitchCase" originalCompilationUnit="//@compilationUnits.1">
<expression xsi:type="java:Integer" originalCompilationUnit="//@compilationUnits.1" value="2"/>
</statements>
<statements xsi:type="java:SwitchCase" originalCompilationUnit="//@compilationUnits.1" default="true"/>
</statements>
</body>
<returnType type="//@orphanTypes.5"/>
</bodyDeclarations>
</ownedElements>
</ownedElements>
<orphanTypes xsi:type="java:PrimitiveTypeInt" name="int"/>
<orphanTypes xsi:type="java:PrimitiveTypeLong" name="long"/>
<orphanTypes xsi:type="java:PrimitiveTypeFloat" name="float"/>
<orphanTypes xsi:type="java:PrimitiveTypeDouble" name="double"/>
<orphanTypes xsi:type="java:PrimitiveTypeBoolean" name="boolean"/>
<orphanTypes xsi:type="java:PrimitiveTypeVoid" name="void" usagesInTypeAccess="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@returnType"/>
<orphanTypes xsi:type="java:PrimitiveTypeChar" name="char"/>
<orphanTypes xsi:type="java:PrimitiveTypeShort" name="short"/>
<orphanTypes xsi:type="java:PrimitiveTypeByte" name="byte"/>
<compilationUnits name="FooTest646.java" originalFilePath="/home/roschan/Projets/Eclipse/eclipse-workspace3/ATL/switch-stmts-should-have-default/src/FooTest646.java" types="//@ownedElements.0/@ownedElements.0"/>
<compilationUnits name="FooTest646_2.java" originalFilePath="/home/roschan/Projets/Eclipse/eclipse-workspace3/ATL/switch-stmts-should-have-default/src/FooTest646_2.java" types="//@ownedElements.0/@ownedElements.1"/>
</java:Model>
......@@ -160,6 +160,7 @@ helper def: allMeasures(project : java!Model): Set(smm!Measure) =
-- sa préférence donc en attendant je mets 1 (valeur par défaut).
thisModule.oneDeclarationPerLine(),
thisModule.switchDensity(),
thisModule.switchStmtsShouldHaveDefault(),
-- #FIXME: thisModule.unusedLocalVariable(),
thisModule.useAssertSameInsteadOfAssertTrue(),
thisModule.useAssertTrueInsteadOfAssertEquals()
......@@ -202,6 +203,26 @@ rule MeasureMissingBreakInSwitch(ss: java!SwitchStatement) {
}
}
--- Creates an instance of Measure for the switch statement missing a default case
rule MeasureSwitchStmtsShouldHaveDefault(ss: java!SwitchStatement) {
to
om: smm!ObservedMeasure (
measure <- switchStmtsShouldHaveDefault,
measurements <- measurement
),
switchStmtsShouldHaveDefault: smm!DimensionalMeasure (
name <- 'Switch statements should have default case',
shortDescription <- 'All switch statements should include a default option to catch any unspecified values.'
),
measurement: smm!DirectMeasurement (
error <- 'Missing default case in switch in class ' + ss.originalCompilationUnit.name + ''
--TODO indiquer le numéro de ligne ? jsp comment
)
do {
switchStmtsShouldHaveDefault;
}
}
-- ------------------ AbstractClassWithoutAnyMethod ----------------------------
--- creates a new Measure when an abstract class does not provides any methods.
rule MeasureAbstractClassWithoutAnyMethod(i : java!ClassDeclaration) {
......
......@@ -30,7 +30,6 @@ helper def: avoidFieldNameMatchingMethodName() : Set(smm!Measure) =
)
);
--- Goes through all the methods to check if the following rule is followed:
--- Avoid printStackTrace()
helper def: avoidPrintStackTrace() : Set(smm!Measure) =
......@@ -190,13 +189,25 @@ helper def: switchDensity() : Set (smm!Measure) =
->collect(switchStatement | thisModule.MeasureSwitchDensity(switchCase))
);
--- helper for the rule for metrics SwitchStmtsShouldHaveDefault
helper def: switchStmtsShouldHaveDefault() : Set(ssm!Measures) =
java!SwitchStatement.allInstances()
-> select(switchSt | not switchSt.hasDefaultCase())
-> collect(switchSt | thisModule.MeasureSwitchStmtsShouldHaveDefault(switchSt));
-- TODO improve comment
helper context java!SwitchStatement def: hasDefaultCase() : Boolean =
self.statements
-> select(case | case.oclIsTypeOf(java!SwitchCase))
-> select(case | case.isDefault())
-> size() > 0;
--- Rule for metrics UnusedLocalVariable :
helper def: unusedLocalVariable(): Set(ssm!Measures) =
java!VariableDeclarationFragment.allInstances()
-> select(variable | variable.originalCompilationUnit.name = 'LocalVariable.java')
-> select(variable | variable.usageInVariableAccess.isEmpty())
-> collect(variable | thisModule.MeasureUnusedLocalVariable(variable))
;
java!VariableDeclarationFragment.allInstances()
-> select(variable | variable.originalCompilationUnit.name = 'LocalVariable.java')
-> select(variable | variable.usageInVariableAccess.isEmpty())
-> collect(variable | thisModule.MeasureUnusedLocalVariable(variable));
-- #TODO: Add comment
helper def: useAssertSameInsteadOfAssertTrue() : Set(smm!Measure) =
......
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