Commit 701957f7 authored by Gerson SUNYE's avatar Gerson SUNYE
Browse files

Merge branch 'master' into 'master'

fix issue #722 whileloopsmustusebraces

Closes #722

See merge request !182
parents ff4dd2b4 51f8fa3f
......@@ -45,7 +45,7 @@
<mapEntry key="OUT" value="smm"/>
</mapAttribute>
<mapAttribute key="Path">
<mapEntry key="IN" value="/projet-2019/input/example.xmi"/>
<mapEntry key="IN" value="/projet-2019/input/while-loops-must-use-braces.xmi"/>
<mapEntry key="OUT" value="/projet-2019/output/metrics.xmi"/>
<mapEntry key="java" value="uri:http://www.eclipse.org/MoDisco/Java/0.2.incubation/java"/>
<mapEntry key="smm" value="uri:http://www.eclipse.org/MoDisco/SMM/1.0.Beta2/smm"/>
......
<?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="whilebraces">
<ownedElements name="whilebraces">
<ownedElements xsi:type="java:ClassDeclaration" originalCompilationUnit="//@compilationUnits.1" name="Whilebraces">
<modifier visibility="public"/>
<bodyDeclarations xsi:type="java:MethodDeclaration" originalCompilationUnit="//@compilationUnits.1" name="whilewithbraces">
<modifier visibility="public"/>
<body originalCompilationUnit="//@compilationUnits.1">
<statements xsi:type="java:VariableDeclarationStatement" originalCompilationUnit="//@compilationUnits.1">
<type type="//@orphanTypes.0"/>
<fragments originalCompilationUnit="//@compilationUnits.1" name="i" usageInVariableAccess="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.1/@expression/@leftOperand //@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.1/@body/@statements.0/@expression/@operand">
<initializer xsi:type="java:PrefixExpression" originalCompilationUnit="//@compilationUnits.1" operator="-">
<operand xsi:type="java:NumberLiteral" originalCompilationUnit="//@compilationUnits.1" tokenValue="10"/>
</initializer>
</fragments>
<modifier/>
</statements>
<statements xsi:type="java:WhileStatement" originalCompilationUnit="//@compilationUnits.1">
<expression xsi:type="java:InfixExpression" originalCompilationUnit="//@compilationUnits.1" operator="&lt;">
<rightOperand xsi:type="java:NumberLiteral" originalCompilationUnit="//@compilationUnits.1" tokenValue="0"/>
<leftOperand xsi:type="java:SingleVariableAccess" variable="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.0/@fragments.0"/>
</expression>
<body xsi:type="java:Block" originalCompilationUnit="//@compilationUnits.1">
<statements xsi:type="java:ExpressionStatement" originalCompilationUnit="//@compilationUnits.1">
<expression xsi:type="java:PostfixExpression" originalCompilationUnit="//@compilationUnits.1">
<operand xsi:type="java:SingleVariableAccess" variable="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.0/@fragments.0"/>
</expression>
</statements>
</body>
</statements>
</body>
<returnType type="//@orphanTypes.5"/>
</bodyDeclarations>
<bodyDeclarations xsi:type="java:MethodDeclaration" originalCompilationUnit="//@compilationUnits.1" name="whilewoutbraces">
<modifier visibility="public"/>
<body originalCompilationUnit="//@compilationUnits.1">
<statements xsi:type="java:VariableDeclarationStatement" originalCompilationUnit="//@compilationUnits.1">
<type type="//@orphanTypes.0"/>
<fragments originalCompilationUnit="//@compilationUnits.1" name="i" usageInVariableAccess="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.1/@body/@statements.1/@expression/@leftOperand //@ownedElements.0/@ownedElements.0/@bodyDeclarations.1/@body/@statements.1/@body/@expression/@operand">
<initializer xsi:type="java:PrefixExpression" originalCompilationUnit="//@compilationUnits.1" operator="-">
<operand xsi:type="java:NumberLiteral" originalCompilationUnit="//@compilationUnits.1" tokenValue="10"/>
</initializer>
</fragments>
<modifier/>
</statements>
<statements xsi:type="java:WhileStatement" originalCompilationUnit="//@compilationUnits.1">
<expression xsi:type="java:InfixExpression" originalCompilationUnit="//@compilationUnits.1" operator="&lt;">
<rightOperand xsi:type="java:NumberLiteral" originalCompilationUnit="//@compilationUnits.1" tokenValue="0"/>
<leftOperand xsi:type="java:SingleVariableAccess" variable="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.1/@body/@statements.0/@fragments.0"/>
</expression>
<body xsi:type="java:ExpressionStatement" originalCompilationUnit="//@compilationUnits.1">
<expression xsi:type="java:PostfixExpression" originalCompilationUnit="//@compilationUnits.1">
<operand xsi:type="java:SingleVariableAccess" variable="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.1/@body/@statements.0/@fragments.0"/>
</expression>
</body>
</statements>
</body>
<returnType type="//@orphanTypes.5"/>
</bodyDeclarations>
</ownedElements>
</ownedElements>
<orphanTypes xsi:type="java:PrimitiveTypeInt" name="int" usagesInTypeAccess="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.0/@type //@ownedElements.0/@ownedElements.0/@bodyDeclarations.1/@body/@statements.0/@type"/>
<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 //@ownedElements.0/@ownedElements.0/@bodyDeclarations.1/@returnType"/>
<orphanTypes xsi:type="java:PrimitiveTypeChar" name="char"/>
<orphanTypes xsi:type="java:PrimitiveTypeShort" name="short"/>
<orphanTypes xsi:type="java:PrimitiveTypeByte" name="byte"/>
<compilationUnits name="module-info.java" originalFilePath="C:\Users\Thorrigan\Documents\Master\workspace\whilebraces\src\module-info.java"/>
<compilationUnits name="Whilebraces.java" originalFilePath="C:\Users\Thorrigan\Documents\Master\workspace\whilebraces\src\whilebraces\Whilebraces.java" package="//@ownedElements.0" types="//@ownedElements.0/@ownedElements.0"/>
</java:Model>
......@@ -63,6 +63,7 @@ helper def: allMeasures(project : java!Model): Set(smm!Measure) =
thisModule.extendsObject(),
thisModule.unnecessaryReturn(),
thisModule.useShortArrayInitializer(),
thisModule.whileBraces(),
-- Design rules
--
......@@ -145,7 +146,7 @@ helper def: allMeasures(project : java!Model): Set(smm!Measure) =
thisModule.nonCaseLabelInSwitchStatement(),
thisModule.nonStaticInitializer(),
thisModule.nullAssignment(),
thisModule.simpleDateFormatNeedsLocale(),
--thisModule.simpleDateFormatNeedsLocale(),
thisModule.suspiciousEqualsMethodName(),
thisModule.suspiciousHashcodeMethodName(),
thisModule.testClassWithoutTest(),
......@@ -162,7 +163,7 @@ helper def: allMeasures(project : java!Model): Set(smm!Measure) =
thisModule.switchDensity(),
-- #FIXME: thisModule.unusedLocalVariable(),
thisModule.useAssertSameInsteadOfAssertTrue(),
thisModule.useAssertTrueInsteadOfAssertEquals()
thisModule.useAssertTrueInsteadOfAssertEquals(),
-- Bugged rules:
--
......@@ -2421,7 +2422,25 @@ rule MesureUseAssertTrueInsteadOfAssertEquals(class : java!MethodInvocation) {
}
--------------------------------------------- WhileLoopsMustUseBraces ---------------------------------------------
--- creates a new Measure when a class that contains a while statement without braces..
rule MeasureWhileLoopsMustUseBraces(condition : java!WhileStatement) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'While Statement must use braces',
shortDescription <- 'Avoid using While statements without using surrounding braces.'
),
measurement: smm!DirectMeasurement (
error <- 'The class ' + condition.originalCompilationUnit.name + ' has a While statement without braces '
)
do {
noc;
}
}
------------------------------------------------------------------------------------------
......
......@@ -218,3 +218,12 @@ helper def: useShortArrayInitializer() : Set(smm!Measure) =
java!VariableDeclarationFragment.allInstances()
-> select(i | i.initializer.oclIsTypeOf(java!ArrayCreation) = true)
-> collect (i | thisModule.MeasureUseShortArrayInitializer(i));
-- ------------------------------------------- WhileLoopsMustUseBraces ---------------------------------------------
--- Helper for issue WhileLoopsMustUseBraces : return a Measure for each class that contains a while statement without braces.
helper def: whileBraces() : Set(smm!Measure) =
java!WhileStatement.allInstances()
-> select(i | i.body <> OclUndefined)
-> select(i | not i.body.oclIsTypeOf(java!Block))
-> collect(i | thisModule.MeasureWhileLoopsMustUseBraces(i));
......@@ -626,3 +626,4 @@ helper def: nonCaseLabelInSwitchStatement() : Set(smm!Measure) =
-> select(i | i.name <> OclUndefined)
-> select(i | i.name <> 'case :' and i.name <> 'default:')
-> collect (i | thisModule.MeasureNonCaseLabelInSwitchStatement(i));
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