Commit 9884cadf authored by Gerson SUNYE's avatar Gerson SUNYE
Browse files

Merge branch 'AvoidUsingShortType' into 'master'

Avoid using short type

Closes #888

See merge request naomod/mde/projet-2019!29
parents 1a604225 67ad3f74
......@@ -45,7 +45,7 @@
<mapEntry key="OUT" value="smm"/>
</mapAttribute>
<mapAttribute key="Path">
<mapEntry key="IN" value="/projet-2019/input/avoid-field-name-matching-method-name.xmi"/>
<mapEntry key="IN" value="/projet-2019/input/avoid-using-short-type.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="AvoidUsingShortType">
<ownedElements name="avoidUsingShortType">
<ownedElements xsi:type="java:ClassDeclaration" originalCompilationUnit="//@compilationUnits.0" name="AvoidUsingShortType">
<modifier visibility="public"/>
<bodyDeclarations xsi:type="java:MethodDeclaration" originalCompilationUnit="//@compilationUnits.0" name="main">
<modifier visibility="public" static="true"/>
<body originalCompilationUnit="//@compilationUnits.0">
<statements xsi:type="java:VariableDeclarationStatement" originalCompilationUnit="//@compilationUnits.0">
<type type="//@orphanTypes.7"/>
<fragments originalCompilationUnit="//@compilationUnits.0" name="doNotUseShort" usageInVariableAccess="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.2/@expression/@leftHandSide">
<initializer xsi:type="java:NumberLiteral" originalCompilationUnit="//@compilationUnits.0" tokenValue="0"/>
</fragments>
<modifier/>
</statements>
<statements xsi:type="java:VariableDeclarationStatement" originalCompilationUnit="//@compilationUnits.0">
<type type="//@orphanTypes.7"/>
<fragments originalCompilationUnit="//@compilationUnits.0" name="shouldNotBeUsed" usageInVariableAccess="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.2/@expression/@rightHandSide">
<initializer xsi:type="java:NumberLiteral" originalCompilationUnit="//@compilationUnits.0" tokenValue="1"/>
</fragments>
<modifier/>
</statements>
<statements xsi:type="java:ExpressionStatement" originalCompilationUnit="//@compilationUnits.0">
<expression xsi:type="java:Assignment" originalCompilationUnit="//@compilationUnits.0" operator="+=">
<leftHandSide xsi:type="java:SingleVariableAccess" variable="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.0/@fragments.0"/>
<rightHandSide xsi:type="java:SingleVariableAccess" variable="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.1/@fragments.0"/>
</expression>
</statements>
</body>
<parameters originalCompilationUnit="//@compilationUnits.0" name="args">
<modifier/>
<type type="//@orphanTypes.9"/>
</parameters>
<returnType type="//@orphanTypes.5"/>
</bodyDeclarations>
</ownedElements>
</ownedElements>
<ownedElements name="java" proxy="true">
<ownedPackages name="lang" proxy="true">
<ownedElements xsi:type="java:ClassDeclaration" name="String" proxy="true" usagesInTypeAccess="//@orphanTypes.9/@elementType">
<superInterfaces type="//@ownedElements.1/@ownedPackages.1/@ownedElements.0"/>
<superInterfaces type="//@ownedElements.1/@ownedPackages.0/@ownedElements.1"/>
<superInterfaces type="//@ownedElements.1/@ownedPackages.0/@ownedElements.2"/>
</ownedElements>
<ownedElements xsi:type="java:InterfaceDeclaration" name="Comparable" proxy="true" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.0/@ownedElements.0/@superInterfaces.1">
<typeParameters name="T" proxy="true"/>
</ownedElements>
<ownedElements xsi:type="java:InterfaceDeclaration" name="CharSequence" proxy="true" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.0/@ownedElements.0/@superInterfaces.2"/>
</ownedPackages>
<ownedPackages name="io" proxy="true">
<ownedElements xsi:type="java:InterfaceDeclaration" name="Serializable" proxy="true" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.0/@ownedElements.0/@superInterfaces.0"/>
</ownedPackages>
</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" usagesInTypeAccess="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.0/@type //@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.1/@type"/>
<orphanTypes xsi:type="java:PrimitiveTypeByte" name="byte"/>
<orphanTypes xsi:type="java:ArrayType" name="java.lang.String[]" usagesInTypeAccess="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@parameters.0/@type" dimensions="1">
<elementType type="//@ownedElements.1/@ownedPackages.0/@ownedElements.0"/>
</orphanTypes>
<compilationUnits name="AvoidUsingShortType.java" originalFilePath="/home/glenn/photon-workspace/projetMDE/AvoidUsingShortType/src/avoidUsingShortType/AvoidUsingShortType.java" package="//@ownedElements.0" types="//@ownedElements.0/@ownedElements.0"/>
</java:Model>
......@@ -61,6 +61,7 @@ helper def: allMeasures(project : java!Model): Set(smm!Measure) =
thisModule.tooFewBranchesForASwitchStatement(),
thisModule.useIndexOfChar(),
thisModule.avoidPrintStackTrace(),
thisModule.avoidUsingShortType(),
-- Documentation rules
--
......@@ -231,6 +232,7 @@ rule numberOfClasses() {
}
}
-- Rule that creates an instance of Measure for the switch statement with too few branches passed in parameter
rule MeasureTooFewBranchesForASwitchStatement(switchStatement: java!SwitchStatement) {
to
......@@ -528,3 +530,26 @@ helper def: getAnnotationTypeDeclarationsFromBodyDeclaration(elem: java!BodyDecl
.and(usage.eContainer().eContainer().originalCompilationUnit.name = elem.originalCompilationUnit.name))
)
;
-- A Measure instance if the class violates the rule 'AvoidUsingShortType'
rule MeasureAvoidUsingShortType(typeShort : java!VariableDeclarationStatement) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'Avoid using short type',
shortDescription <- 'Java uses the \'short\' type to reduce memory usage, not to optimize calculation.'
+ ' In fact, the JVM does not have any arithmetic capabilities for the short type:'
+ ' the JVM must convert the short into an int, do the proper calculation and convert'
+ ' the int back to a short. Thus any storage gains found through use of the \'short\' type'
+ ' may be offset by adverse impacts on performance.'
),
measurement: smm!DirectMeasurement (
error <- 'The class ' + typeShort.originalCompilationUnit.name + ' has \'short\' type variable.'
)
do {
noc;
}
}
......@@ -84,4 +84,12 @@ helper def: avoidThrowingNewInstanceOfSameException() : Set(smm!Measure) =
)
else catchRes
endif
);
\ No newline at end of file
);
--------------------------------------------- AvoidUsingShortType---------------------------------------------
--Detects the use of the primitive type 'short' for rule AvoidUsingShortType. Prefer the use of 'int' instead of 'short'.
helper def: avoidUsingShortType() : Set(smm!Measure) =
java!VariableDeclarationStatement.allInstances()
->select(variable | variable.type.type.name = 'short')
->collect(variable | thisModule.MeasureAvoidUsingShortType(variable));
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