Commit 3808da59 authored by Gerson SUNYE's avatar Gerson SUNYE
Browse files

Merge branch 'master' into 'master'

Fix#987 IntergerInstantiation

See merge request naomod/mde/projet-2019!55
parents a69cd852 8ab9017f
<?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="integer-instantiation">
<ownedElements name="(default package)">
<ownedElements xsi:type="java:ClassDeclaration" originalCompilationUnit="//@compilationUnits.0" name="IntegerInstantiationPass">
<modifier visibility="public"/>
<bodyDeclarations xsi:type="java:FieldDeclaration" originalCompilationUnit="//@compilationUnits.0">
<modifier visibility="private"/>
<type type="//@ownedElements.1/@ownedPackages.0/@ownedElements.0"/>
<fragments originalCompilationUnit="//@compilationUnits.0" name="i">
<initializer xsi:type="java:MethodInvocation" originalCompilationUnit="//@compilationUnits.0" method="//@ownedElements.1/@ownedPackages.0/@ownedElements.0/@bodyDeclarations.0">
<arguments xsi:type="java:NumberLiteral" originalCompilationUnit="//@compilationUnits.0" tokenValue="0"/>
<expression xsi:type="java:TypeAccess" type="//@ownedElements.1/@ownedPackages.0/@ownedElements.0"/>
</initializer>
</fragments>
</bodyDeclarations>
</ownedElements>
<ownedElements xsi:type="java:ClassDeclaration" originalCompilationUnit="//@compilationUnits.1" name="IntegerInstantiationNotPass">
<modifier visibility="public"/>
<bodyDeclarations xsi:type="java:FieldDeclaration" originalCompilationUnit="//@compilationUnits.1">
<modifier visibility="private"/>
<type type="//@ownedElements.1/@ownedPackages.0/@ownedElements.0"/>
<fragments originalCompilationUnit="//@compilationUnits.1" name="i">
<initializer xsi:type="java:ClassInstanceCreation" originalCompilationUnit="//@compilationUnits.1" method="//@ownedElements.1/@ownedPackages.0/@ownedElements.0/@bodyDeclarations.1">
<arguments xsi:type="java:NumberLiteral" originalCompilationUnit="//@compilationUnits.1" tokenValue="0"/>
<type type="//@ownedElements.1/@ownedPackages.0/@ownedElements.0"/>
</initializer>
</fragments>
</bodyDeclarations>
</ownedElements>
</ownedElements>
<ownedElements name="java" proxy="true">
<ownedPackages name="lang" proxy="true">
<ownedElements xsi:type="java:ClassDeclaration" name="Integer" proxy="true" usagesInTypeAccess="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@type //@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@fragments.0/@initializer/@expression //@ownedElements.0/@ownedElements.1/@bodyDeclarations.0/@type //@ownedElements.0/@ownedElements.1/@bodyDeclarations.0/@fragments.0/@initializer/@type">
<bodyDeclarations xsi:type="java:MethodDeclaration" name="valueOf" proxy="true" usages="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@fragments.0/@initializer">
<parameters name="arg0" proxy="true">
<type type="//@orphanTypes.0"/>
</parameters>
</bodyDeclarations>
<bodyDeclarations xsi:type="java:ConstructorDeclaration" name="Integer" proxy="true" usages="//@ownedElements.0/@ownedElements.1/@bodyDeclarations.0/@fragments.0/@initializer">
<parameters name="arg0" proxy="true">
<type type="//@orphanTypes.0"/>
</parameters>
</bodyDeclarations>
<superInterfaces type="//@ownedElements.1/@ownedPackages.0/@ownedElements.2"/>
<superClass type="//@ownedElements.1/@ownedPackages.0/@ownedElements.1"/>
</ownedElements>
<ownedElements xsi:type="java:ClassDeclaration" name="Number" proxy="true" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.0/@ownedElements.0/@superClass">
<superInterfaces type="//@ownedElements.1/@ownedPackages.1/@ownedElements.0"/>
</ownedElements>
<ownedElements xsi:type="java:InterfaceDeclaration" name="Comparable" proxy="true" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.0/@ownedElements.0/@superInterfaces.0">
<typeParameters name="T" proxy="true"/>
</ownedElements>
</ownedPackages>
<ownedPackages name="io" proxy="true">
<ownedElements xsi:type="java:InterfaceDeclaration" name="Serializable" proxy="true" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.0/@ownedElements.1/@superInterfaces.0"/>
</ownedPackages>
</ownedElements>
<orphanTypes xsi:type="java:PrimitiveTypeInt" name="int" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.0/@ownedElements.0/@bodyDeclarations.0/@parameters.0/@type //@ownedElements.1/@ownedPackages.0/@ownedElements.0/@bodyDeclarations.1/@parameters.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"/>
<orphanTypes xsi:type="java:PrimitiveTypeChar" name="char"/>
<orphanTypes xsi:type="java:PrimitiveTypeShort" name="short"/>
<orphanTypes xsi:type="java:PrimitiveTypeByte" name="byte"/>
<compilationUnits name="IntegerInstantiationPass.java" originalFilePath="/home/alexis/eclipse-workspace/integer-instantiation/src/IntegerInstantiationPass.java" types="//@ownedElements.0/@ownedElements.0"/>
<compilationUnits name="IntegerInstantiationNotPass.java" originalFilePath="/home/alexis/eclipse-workspace/integer-instantiation/src/IntegerInstantiationNotPass.java" types="//@ownedElements.0/@ownedElements.1"/>
</java:Model>
......@@ -45,7 +45,7 @@ helper def: allMeasures(project : java!Model): Set(smm!Measure) =
--
thisModule.shortMethodName(),
thisModule.tooManyStaticImports(),
thisModule.AvoidDollarSigns(),
thisModule.avoidDollarSigns(),
thisModule.shortClassName(),
thisModule.extendsObject(),
thisModule.unnecessaryReturn(),
......@@ -70,6 +70,7 @@ helper def: allMeasures(project : java!Model): Set(smm!Measure) =
thisModule.avoidPrintStackTrace(),
thisModule.avoidUsingShortType(),
thisModule.emptyStatementBlock(),
thisModule.integerInstantiation(),
-- Documentation rules
--
......@@ -812,3 +813,25 @@ rule MeasureUnnecessaryReturn(state : java!ReturnStatement) {
noc;
}
}
--------------------------------------------- IntegerInstantiation ---------------------------------------------
-- A Measure instance if the class violates the rule 'IntegerInstantiation'.
rule MeasureIntegerInstantiation(variable : java!CompilationUnit) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'IntegerInstantiation',
shortDescription <- 'Calling new Integer() causes memory allocation that can be avoided by the static Integer.valueOf(). It makes use of an internal cache that recycles earlier instances making it more memory efficient. Note that new Integer() is deprecated since JDK 9 for that reason.'
),
measurement: smm!DirectMeasurement (
error<-'In the Class '+ variable.name + ' an instantiation of Integer must be Integer.ValueOf().'
)
do {
noc;
}
}
......@@ -118,3 +118,17 @@ helper def: longInstantiation() : Set(smm!Measure) =
-- collect every results and send an error message
->collect(it2|thisModule.MeasureLongInstantiation(it2.originalCompilationUnit))
;
--------------------------------------------- IntegerInstantiation ---------------------------------------------
-- Rule for metricsIntegerInstantiation : returns the set of class Measures that violates the rule.
helper def: integerInstantiation() : Set(smm!Measure) =
-- Take all ClassInstanceCreation with allInstances()
java!ClassInstanceCreation.allInstances()
-- Select all classes that declare an Integer type
->select(it|it.method.oclIsTypeOf(java!ConstructorDeclaration) and it.method.name = 'Integer')
-- collect every result and sends an error message
->collect(it2|thisModule.MeasureIntegerInstantiation(it2.originalCompilationUnit))
;
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