Commit 091aaddf authored by Marion HUNAULT's avatar Marion HUNAULT Committed by Gerson SUNYE
Browse files

Fix #677 duplicate import

parent 77876326
......@@ -46,6 +46,7 @@ helper def: allMeasures(project : java!Model): Set(smm!Measure) =
--
thisModule.avoidDollarSigns(),
thisModule.avoidPrefixingMethodParameters(),
thisModule.duplicateImport(),
thisModule.extendsObject(),
thisModule.fieldDeclarationsShouldBeAtStartOfClass(),
thisModule.genericsNaming(),
......@@ -733,6 +734,26 @@ rule MeasureDontUseFloatTypeForLoopIndices(){
}
-- ----------------------------------------- DuplicateImport ----------------------------------------------
-- Create a new Measure when an import is duplicated in a class.
rule MeasureDuplicateImport(import : java!ImportDeclaration) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'DuplicateImport',
shortDescription <- 'Duplicate or overlapping import statements should be avoided.'
),
measurement: smm!DirectMeasurement (
error <- 'In the Class ' + import.importedElement.name + ' there are duplicated imports.'
)
do {
noc;
}
}
-- -----------------------------------------EmptyCatchBlock----------------------------------------------
--- A Measure instance if the class violate the rule EmptyCatchBlock
rule MeasureEmptyCatchBlock(catch : java!CatchClause) {
......@@ -1300,6 +1321,26 @@ rule MeasureOneDeclarationPerLine(s: java!VariableDeclarationStatement){
}
}
-- ------------------------------------------- ShortClassName ---------------------------------------------
--- Create a new Measure when a class name is too short.
rule MesureShortClassName(class : java!ClassDeclaration) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'ShortClassName',
shortDescription <- 'Short Classnames with fewer than e.g. five characters are not recommended.'
),
measurement: smm!DirectMeasurement (
error <- 'The Class ' + class.name + ' is too short.'
)
do {
noc;
}
}
-- ------------------------------------------- ShortInstantiation ---------------------------------------------
--- A Measure instance if the class violates the rule 'ShortInstantiation'.
rule MeasureShortInstantiation(variable : java!CompilationUnit) {
......@@ -1321,6 +1362,26 @@ rule MeasureShortInstantiation(variable : java!CompilationUnit) {
}
}
-- ------------------------------------------- ShortVariableName ----------------------------------------------
--- Create a new Measure when variable name is too short.
rule MesureShortVariableName(variable : java!VariableDeclaration) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'ShortVariableName',
shortDescription <- 'Short Variable names with fewer than e.g. three characters are not recommended.'
),
measurement: smm!DirectMeasurement (
error <- 'The Variable ' + variable.name + ' is too short.'
)
do {
noc;
}
}
-- ------------------------------------------- SignatureDeclareThrowsException ---------------------------------------------
--- A Measure instance if the class violates the rule SignatureDeclareThrowsException.
rule MeasureSignatureDeclareThrowsException(class : java!ClassDeclaration) {
......@@ -1900,28 +1961,6 @@ rule MesureReturnFromFinallyBlock(method : java!MethodDeclaration) {
}
}
--- A Measure instance if the class violates the rule 'ShortClassName'.
-- #FIXME: Typo
rule MesureShortClassName(class : java!ClassDeclaration) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'ShortClassName',
shortDescription <- 'Short Classnames with fewer than e.g. five characters are not recommended.'
),
measurement: smm!DirectMeasurement (
error <- 'The Class ' + class.name + ' is too short.'
)
do {
noc;
}
}
--- A Measure instance if the class violates the rule 'ShortMethodName'.
-- #FIXME: Typo
rule MesureShortMethodName(method : java!MethodDeclaration) {
......@@ -1942,26 +1981,6 @@ rule MesureShortMethodName(method : java!MethodDeclaration) {
}
}
--- A Measure instance if the variable violates the rule 'ShortVariableName'.
-- #FIXME: Typo
rule MesureShortVariableName(variable : java!VariableDeclaration) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'ShortVariableName',
shortDescription <- 'Short Variable names with fewer than e.g. three characters are not recommended.'
),
measurement: smm!DirectMeasurement (
error <- 'The Variable ' + variable.name + ' is too short.'
)
do {
noc;
}
}
--- A Measure instance if the class violates the rule 'TooManyFields'.
-- #FIXME: Typo
rule MesureTooManyFields(class : java!ClassDeclaration) {
......@@ -2168,4 +2187,4 @@ helper context java!FieldDeclaration def: getBodyDeclarationName() : String =
-- ATTENTION !!!
-- Before adding a new rule in the end of the file, remember that rules respect an alphabetical order.
\ No newline at end of file
-- Before adding a new rule in the end of the file, remember that rules respect an alphabetical order.
library codestyle;
------------------------------------------------------------------------------------------
--- AvoidDollarSigns
-- 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())-> reject(each | each.isProxy())->select(i | i.name.indexOf('$') <> -1)
->collect(i | thisModule.MesureAvoidDollarSigns(i));
-- 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) =
java!ClassDeclaration.allInstances() -> reject(each | each.isProxy())
-> select( i | i.name.size() < 4)
-> collect (i | thisModule.MesureShortClassName(i));
--------------------------------------------- TooManyStaticImports ---------------------------------------------
-- Rule for metrics TooManyStaticImports : return the set of class Measures that violates the rule.
helper def: tooManyStaticImports() : Set(smm!Measure) =
java!CompilationUnit.allInstances()
-- Add a new measurement if there are more than 4 static imports in the file.
-> select (c | c.types.first().oclIsTypeOf(java!ClassDeclaration) and c.imports->select(i | i.static)->size() > 4)
-> collect(c | thisModule.MesureTooManyStaticImports(c));
--------------------------------------------- ExtendsObject ---------------------------------------------
-- Rule for metrics ExtendsObject
helper def: extendsObject() : Set(smm!Measure) =
-- select all class with a superTyper
java!ClassDeclaration.allInstances()->select(it | (it.superClass <> OclUndefined))
-- select all class create by the user
->select(it2| it2.proxy = false)
-- select all class who extend Object
->select(it3| it3.superClass.type.name = 'java.lang.Object' or it3.superClass.type.name = 'Object')
-- collect all results and send an error message
->collect(it4|thisModule.MeasureExtendsObject(it4))
;
--------------------------------------------- UnnecessaryReturn ---------------------------------------------
-- Rule for metrics UnnecessaryReturn :
helper def: unnecessaryReturn() : Set (smm!Measure) =
-- Browse through all method declarations
java!ReturnStatement.allInstances()
->select(state | not state.expression.oclIsTypeOf(java!SingleVariableAccess))
->collect(state | thisModule.MeasureUnnecessaryReturn(state));
--------------------------------------------- LongVariable ---------------------------------------------
-- Rule for metrics LongVariable
helper def: longVariable() : Set(smm!Measure) =
java!VariableDeclarationFragment.allInstances()
-> select( variable | variable.name.size() > 17)
-> collect (variable | thisModule.MeasureLongVariable(variable));
--------------------------------------------- ShortVariableName ---------------------------------------------
-- Rule for metrics shortVariableName : return the set of variables measures that violates the rule.
helper def: shortVariableName() : Set(smm!Measure) =
java!SingleVariableDeclaration.allInstances()
-> reject(each | each.isProxy() or not each.catchClause->oclIsUndefined() or not each.enhancedForStatement->oclIsUndefined())
-> select( i | i.name.size() < 3)
-> union( java!VariableDeclarationFragment.allInstances()
-> reject(each | each.isProxy() or each.variablesContainer.oclIsTypeOf(java!VariableDeclarationExpression))
-> select( i | i.name.size() < 3)
)
-> collect (i | thisModule.MesureShortVariableName(i));
----- Fix Issues #699
--- helper noPackageDeclaration
--- get all the compilation units
--- check if this units contains a defined package or not
--- if there is make a call for MesureAvoidNoPackageDeclaration rule
--- with the detected units
helper def: noPackageDeclaration() : Set(smm!Measure) =
java!CompilationUnit.allInstances()
->select(compUnit | compUnit.package.oclIsUndefined())
->collect(err | thisModule.MesureAvoidNoPackageDeclaration(err));
--------------------------------------------- GenericsNaming ---------------------------------------------
-- Rule for metrics GenericsNaming
helper def: genericsNaming() : Set(smm!Measure) =
java!TypeParameter.allInstances()
-> reject(type | type.name.size() = 1 and type.name.substring(1,1) = type.name.substring(1,1).toUpper())
-> collect(type | thisModule.MeasureGenericsNaming(type));
--------------------------------------------- MisLeadingVariableName ---------------------------------------------
-- Rule for metrics MisLeadingVariableName
helper def: misLeadingVariableName() : Set(smm!Measure) =
java!VariableDeclarationFragment.allInstances()
-> reject(variable | variable.variablesContainer.oclIsTypeOf(java!FieldDeclaration))
-> union(java!SingleVariableDeclaration.allInstances())
-> select(variable | variable.name.size() >= 2)
-> select(variable | variable.name.substring(1,2) = 'm_')
-> collect(variable | thisModule.MeasureMisLeadingVariableName(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));
--------------------------------------------- AvoidPrefixingMethodParameters ---------------------------------------------
->collect(i | thisModule.MesureAvoidDollarSigns(i))
;
--- AvoidPrefixingMethodParameters
-- Rule for metrics AvoidPrefixingMethodParameters
helper def: avoidPrefixingMethodParameters(): Set(smm!Measure) =
java!MethodDeclaration.allInstances()->select(method |
......@@ -136,10 +33,48 @@ helper context java!SingleVariableDeclaration def: isPrefixedParameter(): Boolea
thisModule.match('^in[A-Z].*$', self.name) or
thisModule.match('^out[A-Z].*$', self.name) or
thisModule.match('^in$', self.name) or
thisModule.match('^out$', self.name);
thisModule.match('^out$', self.name)
;
--- DuplicateImport
-- Return the set of imports measures that violates the rule duplicateImport().
helper def: duplicateImport() : Set(smm!Measure) =
java!ImportDeclaration.allInstances()
->iterate( i; res : Set(smm!Measure) = Set{} |
java!ImportDeclaration.allInstances()
->select( each | each.originalCompilationUnit = i.originalCompilationUnit )
->reject( each | each.importedElement.oclIsTypeOf(java!UnresolvedItem)
or each.importedElement.oclIsTypeOf(java!EnumConstantDeclaration) )
->reject( each | each.importedElement.package->oclIsUndefined() )
->select( each | each.importedElement.package = i.importedElement )
->collect( j | thisModule.MeasureDuplicateImport(j) )
->union(
java!ImportDeclaration.allInstances()
->select( each | each.originalCompilationUnit = i.originalCompilationUnit )
->reject( each | each.importedElement.oclIsTypeOf(java!UnresolvedItem)
or each.importedElement.oclIsTypeOf(java!EnumConstantDeclaration) )
->reject( each | each.importedElement.package->oclIsUndefined() )
->reject( each | each.importedElement.package.package->oclIsUndefined() )
->select( each | each.importedElement.package.package = i.importedElement )
->collect( j | thisModule.MeasureDuplicateImport(j) )
)
)
;
--------------------------------------------- FieldDeclarationsShouldBeAtStartOfClass ---------------------------------------------
--- ExtendsObject
-- Rule for metrics ExtendsObject
helper def: extendsObject() : Set(smm!Measure) =
-- select all class with a superTyper
java!ClassDeclaration.allInstances()->select(it | (it.superClass <> OclUndefined))
-- select all class create by the user
->select(it2| it2.proxy = false)
-- select all class who extend Object
->select(it3| it3.superClass.type.name = 'java.lang.Object' or it3.superClass.type.name = 'Object')
-- collect all results and send an error message
->collect(it4|thisModule.MeasureExtendsObject(it4))
;
--- FieldDeclarationsShouldBeAtStartOfClass
-- Rule for metrics FieldDeclarationsShouldBeAtStartOfClass
helper def: fieldDeclarationsShouldBeAtStartOfClass(): Set(smm!Measure) =
java!ClassDeclaration.allInstances()->select(class |
......@@ -166,4 +101,103 @@ helper context java!ClassDeclaration def: isNotAllFieldsDeclaredAtStartOfClass()
endif
endif
)
endif;
endif
;
--- GenericsNaming
-- Rule for metrics GenericsNaming
helper def: genericsNaming() : Set(smm!Measure) =
java!TypeParameter.allInstances()
-> reject(type | type.name.size() = 1 and type.name.substring(1,1) = type.name.substring(1,1).toUpper())
-> collect(type | thisModule.MeasureGenericsNaming(type))
;
--- 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))
;
--- LongVariable
-- Rule for metrics LongVariable
helper def: longVariable() : Set(smm!Measure) =
java!VariableDeclarationFragment.allInstances()
-> select( variable | variable.name.size() > 17)
-> collect (variable | thisModule.MeasureLongVariable(variable))
;
--- MisLeadingVariableName
-- Rule for metrics MisLeadingVariableName
helper def: misLeadingVariableName() : Set(smm!Measure) =
java!VariableDeclarationFragment.allInstances()
-> reject(variable | variable.variablesContainer.oclIsTypeOf(java!FieldDeclaration))
-> union(java!SingleVariableDeclaration.allInstances())
-> select(variable | variable.name.size() >= 2)
-> select(variable | variable.name.substring(1,2) = 'm_')
-> collect(variable | thisModule.MeasureMisLeadingVariableName(variable))
;
--- ShortClassName
-- Return the set of class Measures that violates the rule.
helper def: shortClassName() : Set(smm!Measure) =
java!ClassDeclaration.allInstances()
-> reject( i | i.isProxy() )
-> select( i | i.name.size() < 4 )
-> collect( i | thisModule.MesureShortClassName(i) )
;
--- ShortMethodName
-- 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))
;
--- ShortVariableName
-- Return the set of variables measures that violates the rule.
helper def: shortVariableName() : Set(smm!Measure) =
java!SingleVariableDeclaration.allInstances()
-> reject( each | each.isProxy() or not each.catchClause->oclIsUndefined() or not each.enhancedForStatement->oclIsUndefined())
-> select( i | i.name.size() < 3)
-> union(
java!VariableDeclarationFragment.allInstances()
-> reject( each | each.isProxy() or each.variablesContainer.oclIsTypeOf(java!VariableDeclarationExpression) )
-> select( i | i.name.size() < 3 )
)
-> collect(i | thisModule.MesureShortVariableName(i) )
;
--- TooManyStaticImports
-- Rule for metrics TooManyStaticImports : return the set of class Measures that violates the rule.
helper def: tooManyStaticImports() : Set(smm!Measure) =
java!CompilationUnit.allInstances()
-- Add a new measurement if there are more than 4 static imports in the file.
-> select (c | c.types.first().oclIsTypeOf(java!ClassDeclaration) and c.imports->select(i | i.static)->size() > 4)
-> collect(c | thisModule.MesureTooManyStaticImports(c))
;
--- UnnecessaryReturn
-- Rule for metrics UnnecessaryReturn :
helper def: unnecessaryReturn() : Set (smm!Measure) =
-- Browse through all method declarations
java!ReturnStatement.allInstances()
->select(state | not state.expression.oclIsTypeOf(java!SingleVariableAccess))
->collect(state | thisModule.MeasureUnnecessaryReturn(state))
;
----- Fix Issues #699
--- helper noPackageDeclaration
--- get all the compilation units
--- check if this units contains a defined package or not
--- if there is make a call for MesureAvoidNoPackageDeclaration rule
--- with the detected units
helper def: noPackageDeclaration() : Set(smm!Measure) =
java!CompilationUnit.allInstances()
->select(compUnit | compUnit.package.oclIsUndefined())
->collect(err | thisModule.MesureAvoidNoPackageDeclaration(err))
;
\ No newline at end of file
......@@ -249,4 +249,4 @@ helper def: uselessStringValueOf() : Set(smm!Measure) =
-> select (m | m.method.name='valueOf' )
-> select (m | m.refImmediateComposite().oclIsTypeOf(java!InfixExpression))
-> collect (m | thisModule.MeasureUselessStringValueOf(m))
;
;
\ No newline at end of file
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