Commit a0134aec authored by Gerson SUNYE's avatar Gerson SUNYE
Browse files

Merge branch 'fix/FinalFieldCouldBeStatic' into 'master'

Fix #743  FinalFieldCouldBeStatic

Closes #743

See merge request naomod/mde/projet-2019!39
parents af81f962 d1faf61f
<?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="ExempleMDE">
<ownedElements name="fr">
<ownedPackages name="unantes">
<ownedPackages name="FinalFieldCouldBeStatic">
<ownedElements xsi:type="java:ClassDeclaration" originalCompilationUnit="//@compilationUnits.0" name="FinalField">
<modifier visibility="public"/>
<bodyDeclarations xsi:type="java:FieldDeclaration" originalCompilationUnit="//@compilationUnits.0">
<modifier visibility="public" inheritance="final"/>
<type type="//@orphanTypes.0"/>
<fragments originalCompilationUnit="//@compilationUnits.0" name="foo">
<initializer xsi:type="java:NumberLiteral" originalCompilationUnit="//@compilationUnits.0" tokenValue="42"/>
</fragments>
</bodyDeclarations>
</ownedElements>
<ownedElements xsi:type="java:ClassDeclaration" originalCompilationUnit="//@compilationUnits.1" name="NoFinalField">
<modifier visibility="public"/>
<bodyDeclarations xsi:type="java:FieldDeclaration" originalCompilationUnit="//@compilationUnits.1">
<modifier visibility="public"/>
<type type="//@orphanTypes.0"/>
<fragments originalCompilationUnit="//@compilationUnits.1" name="foo">
<initializer xsi:type="java:NumberLiteral" originalCompilationUnit="//@compilationUnits.1" tokenValue="42"/>
</fragments>
</bodyDeclarations>
</ownedElements>
<ownedElements xsi:type="java:ClassDeclaration" originalCompilationUnit="//@compilationUnits.2" name="NoField">
<modifier visibility="public"/>
</ownedElements>
<ownedElements xsi:type="java:ClassDeclaration" originalCompilationUnit="//@compilationUnits.3" name="FinalFieldAndStatic">
<modifier visibility="public"/>
<bodyDeclarations xsi:type="java:FieldDeclaration" originalCompilationUnit="//@compilationUnits.3">
<modifier visibility="public" inheritance="final" static="true"/>
<type type="//@orphanTypes.0"/>
<fragments originalCompilationUnit="//@compilationUnits.3" name="foo">
<initializer xsi:type="java:NumberLiteral" originalCompilationUnit="//@compilationUnits.3" tokenValue="42"/>
</fragments>
</bodyDeclarations>
</ownedElements>
</ownedPackages>
</ownedPackages>
</ownedElements>
<orphanTypes xsi:type="java:PrimitiveTypeInt" name="int" usagesInTypeAccess="//@ownedElements.0/@ownedPackages.0/@ownedPackages.0/@ownedElements.0/@bodyDeclarations.0/@type //@ownedElements.0/@ownedPackages.0/@ownedPackages.0/@ownedElements.1/@bodyDeclarations.0/@type //@ownedElements.0/@ownedPackages.0/@ownedPackages.0/@ownedElements.3/@bodyDeclarations.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="FinalField.java" originalFilePath="/comptes/E135794C/workspaces/Modeling-photon/ExempleMDE/src/fr/unantes/FinalFieldCouldBeStatic/FinalField.java" package="//@ownedElements.0/@ownedPackages.0/@ownedPackages.0" types="//@ownedElements.0/@ownedPackages.0/@ownedPackages.0/@ownedElements.0"/>
<compilationUnits name="NoFinalField.java" originalFilePath="/comptes/E135794C/workspaces/Modeling-photon/ExempleMDE/src/fr/unantes/FinalFieldCouldBeStatic/NoFinalField.java" package="//@ownedElements.0/@ownedPackages.0/@ownedPackages.0" types="//@ownedElements.0/@ownedPackages.0/@ownedPackages.0/@ownedElements.1"/>
<compilationUnits name="NoField.java" originalFilePath="/comptes/E135794C/workspaces/Modeling-photon/ExempleMDE/src/fr/unantes/FinalFieldCouldBeStatic/NoField.java" package="//@ownedElements.0/@ownedPackages.0/@ownedPackages.0" types="//@ownedElements.0/@ownedPackages.0/@ownedPackages.0/@ownedElements.2"/>
<compilationUnits name="FinalFieldAndStatic.java" originalFilePath="/comptes/E135794C/workspaces/Modeling-photon/ExempleMDE/src/fr/unantes/FinalFieldCouldBeStatic/FinalFieldAndStatic.java" package="//@ownedElements.0/@ownedPackages.0/@ownedPackages.0" types="//@ownedElements.0/@ownedPackages.0/@ownedPackages.0/@ownedElements.3"/>
</java:Model>
This diff is collapsed.
......@@ -57,6 +57,7 @@ helper def: allMeasures(project : java!Model): Set(smm!Measure) =
thisModule.shortInstantiation(),
thisModule.returnEmptyArrayRatherThanNull(),
thisModule.excessiveParameterList(),
thisModule.finalFieldCouldBeStatic(),
-- Performance rules
--
......@@ -103,18 +104,6 @@ rule MeasureUselessStringValueOf(method : java!MethodInvocation) {
------------------------------------------------------------------------------------------
-- Rule for metrics AvoidDollarSigns : return the set of class Measures that violates the rule.
helper def: AvoidDollarSigns() : Set(smm!Measure) =
-- Browse through all variable/method/class/interface.
java!TypeDeclaration.allInstances()->union(java!MethodDeclaration.allInstances())->union(java!VariableDeclaration.allInstances())->iterate(i; res : Set(smm!Measure) = Set{} |
-- Add a new measurement if the name contains a dollar.
if i.name.indexOf('$') <> -1
then res->union(Set{thisModule.MesureAvoidDollarSigns(i)})
else res
endif
);
-- A Measure instance if the class violates the rule 'AvoidDollarSigns'.
rule MesureAvoidDollarSigns(node : java!ASTNode) {
to
......@@ -196,11 +185,6 @@ rule MeasureDontCallThreadRun(method : java!MethodInvocation) {
}
helper def: numberFieldsInClasse(s:java!ClassDeclaration) : Integer =
-- Return the number of FieldDeclaration in a class.
s.bodyDeclarations-> select(r | r.oclIsTypeOf(java!FieldDeclaration))->size();
-- A Measure instance if the class violates the rule 'TooManyFields'.
rule MesureTooManyFields(class : java!ClassDeclaration) {
to
......@@ -351,6 +335,10 @@ helper def: nbBranchesOfASwitchStatement(switchStatement:java!SwitchStatement) :
switchStatement.statements->select(each | each.oclIsTypeOf(java!SwitchCase)).size()
;
-- Returns the number of Fields in Class
helper def: numberFieldsInClasse(s:java!ClassDeclaration) : Integer =
-- Return the number of FieldDeclaration in a class.
s.bodyDeclarations-> select(r | r.oclIsTypeOf(java!FieldDeclaration))->size();
-- Returns the number of breaks of the switch statement passed in parameter
helper def: nbBreakStatementOfASwitchStatement(ss:java!SwitchStatement) : Integer =
......@@ -687,3 +675,25 @@ rule MeasureDoNotExtendJavaLangError(variable : java!ClassDeclaration) {
noc;
}
}
--------------------------------------------- MeasureFinalFieldCouldBeStatic ---------------------------------------------
-- A Measure instance if the class violates the rule MeasureFinalFieldCouldBeStatic.
rule MeasureFinalFieldCouldBeStatic(field : java!FieldDeclaration) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'FinalFieldCouldBeStatic',
shortDescription <- 'If a final field is assigned to a compile-time constant, it could be made static, thus saving overhead in each object at runtime.'
),
measurement: smm!DirectMeasurement (
error<-'The field '+ field.fragments->collect(i | i.name)->first() + ' could be static in the class ' + field.originalCompilationUnit.name +'.'
)
do {
noc;
}
}
......@@ -2,17 +2,22 @@ library codestyle;
------------------------------------------------------------------------------------------
-- Rule for metrics shortMethodName : return the set of method Measures that violates the rule.
helper def: shortMethodName() : Set(smm!Measure) =
-- Browse through all method
java!MethodDeclaration.allInstances() -> reject(each | each.isProxy())->iterate(i; res : Set(smm!Measure) = Set{} |
-- Add a new measurement if the size of the method name is less than 3.
if i.name.size() < 3
then res->union(Set{thisModule.MesureShortMethodName(i)})
-- Rule for metrics AvoidDollarSigns : return the set of class Measures that violates the rule.
helper def: avoidDollarSigns() : Set(smm!Measure) =
-- Browse through all variable/method/class/interface.
java!TypeDeclaration.allInstances()->union(java!MethodDeclaration.allInstances())->union(java!VariableDeclaration.allInstances())->iterate(i; res : Set(smm!Measure) = Set{} |
-- Add a new measurement if the name contains a dollar.
if i.name.indexOf('$') <> -1
then res->union(Set{thisModule.MesureAvoidDollarSigns(i)})
else res
endif
);
-- Rule for metrics shortMethodName : return the set of method Measures that violates the rule.
helper def: shortMethodName() : Set(smm!Measure) =
-- Browse through all method, add a new measurement if the size of the method name is less than 3.
java!MethodDeclaration.allInstances() -> reject(each | each.isProxy())->select(i|i.name.size() < 3)->collect(j|thisModule.MesureShortMethodName(j));
--------------------------------------------- ShortClassName ---------------------------------------------
-- Rule for metrics shortClassName : return the set of class Measures that violates the rule.
helper def: shortClassName() : Set(smm!Measure) =
......
library design;
--------------------------------------------- TooManyFields ---------------------------------------------
-- Rule for metrics TooManyFields : return the set of class Measures that violates the rule.
-- To test, example_toomanyfields.xmi, two classe must violates the rule.
helper def: tooManyFields() : Set(smm!Measure) =
-- Browse through all class
java!ClassDeclaration.allInstances() -> reject(each | each.isProxy())->iterate(i; res : Set(smm!Measure) = Set{} |
-- Add a new measurement if there are more than 15 fields in the class.
if thisModule.numberFieldsInClasse(i) >15
then res->union(Set{thisModule.MesureTooManyFields(i)})
else res
endif
);
java!ClassDeclaration.allInstances() -> reject(each | each.isProxy())->select(i | thisModule.numberFieldsInClasse(i) >15)->collect(j | thisModule.MesureTooManyFields(j));
--------------------------------------------- TooManyMethods ---------------------------------------------
......@@ -82,3 +78,11 @@ helper def: doNotExtendJavaLangError() : Set(smm!Measure) =
-- collect all results and send an error message
->collect(it4|thisModule.MeasureDoNotExtendJavaLangError(it4))
;
--------------------------------------------- FinalFieldCouldBeStatic ---------------------------------------------
-- Rule for metrics FinalFieldCouldBeStatic : return the set of field Measures that violates the rule.
helper def: finalFieldCouldBeStatic() : Set(smm!Measure) =
-- Browse through all field that are final and not static
java!FieldDeclaration.allInstances()->select(i | i.modifier <> OclUndefined)->select(i | i.modifier.inheritance->toString() = 'final' and not i.modifier.static)->collect(j | thisModule.MeasureFinalFieldCouldBeStatic(j));
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