Commit e5057704 authored by LePandaRoox's avatar LePandaRoox
Browse files

Fixed issue #688 IfStmtsMustUseBraces

parent 6815a21f
<?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="IfStmtsMustUseBraces">
<ownedElements name="(default package)">
<ownedElements xsi:type="java:ClassDeclaration" originalCompilationUnit="//@compilationUnits.0" name="ClassWithIfStmtsMustUseBraces">
<modifier visibility="public"/>
<bodyDeclarations xsi:type="java:ConstructorDeclaration" originalCompilationUnit="//@compilationUnits.0" name="ClassWithIfStmtsMustUseBraces">
<modifier visibility="public"/>
<body originalCompilationUnit="//@compilationUnits.0"/>
</bodyDeclarations>
<bodyDeclarations xsi:type="java:MethodDeclaration" originalCompilationUnit="//@compilationUnits.0" name="nobraces">
<modifier visibility="public"/>
<body originalCompilationUnit="//@compilationUnits.0">
<statements xsi:type="java:VariableDeclarationStatement" originalCompilationUnit="//@compilationUnits.0">
<type type="//@orphanTypes.0"/>
<fragments originalCompilationUnit="//@compilationUnits.0" name="x" usageInVariableAccess="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.1/@body/@statements.1/@expression/@leftHandSide //@ownedElements.0/@ownedElements.0/@bodyDeclarations.1/@body/@statements.2/@expression/@leftOperand //@ownedElements.0/@ownedElements.0/@bodyDeclarations.1/@body/@statements.2/@thenStatement/@expression/@operand //@ownedElements.0/@ownedElements.0/@bodyDeclarations.1/@body/@statements.3/@expression/@leftOperand //@ownedElements.0/@ownedElements.0/@bodyDeclarations.1/@body/@statements.3/@thenStatement/@expression/@operand"/>
<modifier/>
</statements>
<statements xsi:type="java:ExpressionStatement" originalCompilationUnit="//@compilationUnits.0">
<expression xsi:type="java:Assignment" originalCompilationUnit="//@compilationUnits.0">
<leftHandSide xsi:type="java:SingleVariableAccess" variable="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.1/@body/@statements.0/@fragments.0"/>
<rightHandSide xsi:type="java:NumberLiteral" originalCompilationUnit="//@compilationUnits.0" tokenValue="2"/>
</expression>
</statements>
<statements xsi:type="java:IfStatement" originalCompilationUnit="//@compilationUnits.0">
<expression xsi:type="java:InfixExpression" originalCompilationUnit="//@compilationUnits.0" operator="==">
<rightOperand xsi:type="java:NumberLiteral" originalCompilationUnit="//@compilationUnits.0" tokenValue="2"/>
<leftOperand xsi:type="java:SingleVariableAccess" variable="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.1/@body/@statements.0/@fragments.0"/>
</expression>
<thenStatement xsi:type="java:ExpressionStatement" originalCompilationUnit="//@compilationUnits.0">
<expression xsi:type="java:PostfixExpression" originalCompilationUnit="//@compilationUnits.0">
<operand xsi:type="java:SingleVariableAccess" variable="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.1/@body/@statements.0/@fragments.0"/>
</expression>
</thenStatement>
</statements>
<statements xsi:type="java:IfStatement" originalCompilationUnit="//@compilationUnits.0">
<expression xsi:type="java:InfixExpression" originalCompilationUnit="//@compilationUnits.0" operator="==">
<rightOperand xsi:type="java:NumberLiteral" originalCompilationUnit="//@compilationUnits.0" tokenValue="3"/>
<leftOperand xsi:type="java:SingleVariableAccess" variable="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.1/@body/@statements.0/@fragments.0"/>
</expression>
<thenStatement xsi:type="java:ExpressionStatement" originalCompilationUnit="//@compilationUnits.0">
<expression xsi:type="java:PostfixExpression" originalCompilationUnit="//@compilationUnits.0">
<operand xsi:type="java:SingleVariableAccess" variable="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.1/@body/@statements.0/@fragments.0"/>
</expression>
</thenStatement>
</statements>
</body>
<returnType type="//@orphanTypes.5"/>
</bodyDeclarations>
<bodyDeclarations xsi:type="java:MethodDeclaration" originalCompilationUnit="//@compilationUnits.0" name="braces">
<modifier visibility="public"/>
<body originalCompilationUnit="//@compilationUnits.0">
<statements xsi:type="java:VariableDeclarationStatement" originalCompilationUnit="//@compilationUnits.0">
<type type="//@orphanTypes.0"/>
<fragments originalCompilationUnit="//@compilationUnits.0" name="z" usageInVariableAccess="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.2/@body/@statements.1/@expression/@leftHandSide //@ownedElements.0/@ownedElements.0/@bodyDeclarations.2/@body/@statements.2/@expression/@leftOperand //@ownedElements.0/@ownedElements.0/@bodyDeclarations.2/@body/@statements.2/@thenStatement/@statements.0/@expression/@operand //@ownedElements.0/@ownedElements.0/@bodyDeclarations.2/@body/@statements.3/@expression/@leftOperand //@ownedElements.0/@ownedElements.0/@bodyDeclarations.2/@body/@statements.3/@thenStatement/@statements.0/@expression/@operand"/>
<modifier/>
</statements>
<statements xsi:type="java:ExpressionStatement" originalCompilationUnit="//@compilationUnits.0">
<expression xsi:type="java:Assignment" originalCompilationUnit="//@compilationUnits.0">
<leftHandSide xsi:type="java:SingleVariableAccess" variable="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.2/@body/@statements.0/@fragments.0"/>
<rightHandSide xsi:type="java:NumberLiteral" originalCompilationUnit="//@compilationUnits.0" tokenValue="2"/>
</expression>
</statements>
<statements xsi:type="java:IfStatement" originalCompilationUnit="//@compilationUnits.0">
<expression xsi:type="java:InfixExpression" originalCompilationUnit="//@compilationUnits.0" operator="==">
<rightOperand xsi:type="java:NumberLiteral" originalCompilationUnit="//@compilationUnits.0" tokenValue="2"/>
<leftOperand xsi:type="java:SingleVariableAccess" variable="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.2/@body/@statements.0/@fragments.0"/>
</expression>
<thenStatement xsi:type="java:Block" originalCompilationUnit="//@compilationUnits.0">
<statements xsi:type="java:ExpressionStatement" originalCompilationUnit="//@compilationUnits.0">
<expression xsi:type="java:PostfixExpression" originalCompilationUnit="//@compilationUnits.0">
<operand xsi:type="java:SingleVariableAccess" variable="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.2/@body/@statements.0/@fragments.0"/>
</expression>
</statements>
</thenStatement>
</statements>
<statements xsi:type="java:IfStatement" originalCompilationUnit="//@compilationUnits.0">
<expression xsi:type="java:InfixExpression" originalCompilationUnit="//@compilationUnits.0" operator="==">
<rightOperand xsi:type="java:NumberLiteral" originalCompilationUnit="//@compilationUnits.0" tokenValue="3"/>
<leftOperand xsi:type="java:SingleVariableAccess" variable="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.2/@body/@statements.0/@fragments.0"/>
</expression>
<thenStatement xsi:type="java:Block" originalCompilationUnit="//@compilationUnits.0">
<statements xsi:type="java:ExpressionStatement" originalCompilationUnit="//@compilationUnits.0">
<expression xsi:type="java:PostfixExpression" originalCompilationUnit="//@compilationUnits.0">
<operand xsi:type="java:SingleVariableAccess" variable="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.2/@body/@statements.0/@fragments.0"/>
</expression>
</statements>
</thenStatement>
</statements>
</body>
<returnType type="//@orphanTypes.5"/>
</bodyDeclarations>
</ownedElements>
</ownedElements>
<orphanTypes xsi:type="java:PrimitiveTypeInt" name="int" usagesInTypeAccess="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.1/@body/@statements.0/@type //@ownedElements.0/@ownedElements.0/@bodyDeclarations.2/@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.1/@returnType //@ownedElements.0/@ownedElements.0/@bodyDeclarations.2/@returnType"/>
<orphanTypes xsi:type="java:PrimitiveTypeChar" name="char"/>
<orphanTypes xsi:type="java:PrimitiveTypeShort" name="short"/>
<orphanTypes xsi:type="java:PrimitiveTypeByte" name="byte"/>
<compilationUnits name="ClassWithIfStmtsMustUseBraces.java" originalFilePath="C:\Users\forma\eclipse-workspace_2019\IfStmtsMustUseBraces\src\ClassWithIfStmtsMustUseBraces.java" types="//@ownedElements.0/@ownedElements.0"/>
</java:Model>
......@@ -50,6 +50,7 @@ helper def: allMeasures(project : java!Model): Set(smm!Measure) =
thisModule.extendsObject(),
thisModule.unnecessaryReturn(),
thisModule.longVariable(),
thisModule.IfBraces(),
-- Design rules
--
......@@ -1178,3 +1179,24 @@ rule MeasureSimplifyBooleanAssertion(i : java!PrefixExpression) {
}
}
--------------------------------------------- IfStmtsMustUseBraces ---------------------------------------------
-- creates a new Measure when a class that contains an if statement without braces.
rule MeasureIfStmtsMustUseBraces(i : java!IfStatement) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'If then Statement must use braces',
shortDescription <- 'Deprecated Avoid using if statements without using braces to surround the code block. If the code formatting...'
),
measurement: smm!DirectMeasurement (
error<-'The class '+ i.originalCompilationUnit.name + 'has an if statement without braces.'
)
do {
noc;
}
}
......@@ -59,3 +59,10 @@ helper def: longVariable() : Set(smm!Measure) =
-> select( variable | variable.name.size() > 17)
-> collect (variable | thisModule.MeasureLongVariable(variable));
--------------------------------------------- IfStmtsMustUseBraces ---------------------------------------------
-- Helper for issue IfStmtsMustUseBraces : return a Measure for each class that contains an if statement without braces.
helper def: IfBraces() : Set(smm!Measure) =
java!IfStatement.allInstances()
-> select(i | i.thenStatement <> OclUndefined)
-> select(i | not i.thenStatement.oclIsTypeOf(java!Block))
-> collect(i | thisModule.MeasureIfStmtsMustUseBraces(i));
\ No newline at end of file
Supports Markdown
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