Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Yannis LE BARS
Source Code Analyzer
Commits
12da6687
Commit
12da6687
authored
Dec 05, 2019
by
Faenya
Browse files
Fix Issue 715 UnnecessaryReturn, Issue 755 SignatureDeclareThrowsException, Issue 763 SwitchDensity
parent
347e5c01
Changes
4
Hide whitespace changes
Inline
Side-by-side
src/main/atl/analysis.atl
View file @
12da6687
...
...
@@ -48,6 +48,7 @@ helper def: allMeasures(project : java!Model): Set(smm!Measure) =
thisModule.AvoidDollarSigns(),
thisModule.shortClassName(),
thisModule.extendsObject(),
thisModule.unnecessaryReturn(),
-- Design rules
--
...
...
@@ -59,6 +60,7 @@ helper def: allMeasures(project : java!Model): Set(smm!Measure) =
thisModule.returnEmptyArrayRatherThanNull(),
thisModule.excessiveParameterList(),
thisModule.finalFieldCouldBeStatic(),
thisModule.signatureDeclareThrowsException(),
-- Performance rules
--
...
...
@@ -82,6 +84,7 @@ helper def: allMeasures(project : java!Model): Set(smm!Measure) =
-- Best practicices rules
thisModule.avoidThrowingNewInstanceOfSameException()
thisModule.switchDensity()
};
...
...
@@ -741,3 +744,71 @@ rule MeasureExtendsObject(variable : java!ClassDeclaration) {
noc;
}
}
--------------------------------------------- SwitchDensity ---------------------------------------------
-- A Measure instance if the class violates the rule SwitchDensity.
rule MeasureSwitchDensity(switch : java!SwitchStatement) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'SwitchDensity ',
shortDescription <- 'Statement to label ratio is too high (> 10)'
),
measurement: smm!DirectMeasurement (
error<-'In the Class '+ switch.originalCompilationUnit.name + ' a switch case contains too many statements.'
)
do {
noc;
}
}
--------------------------------------------- SignatureDeclareThrowsException ---------------------------------------------
-- A Measure instance if the class violates the rule SignatureDeclareThrowsException.
rule MeasureSignatureDeclareThrowsException(class : java!ClassDeclaration) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'SignatureDeclareThrowsException ',
shortDescription <- 'A method/constructor should not explicitly throw the generic java.lang.Exception'
),
measurement: smm!DirectMeasurement (
error<-'In the Class '+ class.name + ' a method/constructor explicitly throws the generic java.lang.Exception.'
)
do {
noc;
}
}
--------------------------------------------- UnnecessaryReturn ---------------------------------------------
-- A Measure instance if the class violates the rule UnnecessaryReturn.
rule MeasureUnnecessaryReturn(state : java!ReturnStatement) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'UnnecessaryReturn ',
shortDescription <- 'Avoid the use of unnecessary return statements.'
),
measurement: smm!DirectMeasurement (
error<-'In the Class '+ state.originalCompilationUnit.name + ' an unnecessary return was used.'
)
do {
noc;
}
}
src/main/atl/bestPractices.atl
View file @
12da6687
...
...
@@ -49,4 +49,16 @@ helper def: emptyStatementBlock() : Set(smm!Measure) =
java!Block.allInstances()
->select(block | block.statements.isEmpty())
->collect(block | thisModule.MeasureEmptyStatementBlock(block));
--------------------------------------------- SwitchDensity ---------------------------------------------
-- Rule for metrics SwitchDensity :
helper def: switchDensity() : Set (smm!Measure) =
-- Browse through all Switch Statements
java!SwitchStatement.allInstances()->iterate(switchCase; res : Set(smm!Measure) = Set{} |
switchCase.statements
->select(switchStatement | switchStatement.oclIsTypeOf(java!Block))
->select(switchStatement | switchStatement.statements <> OclUndefined)
->select(switchStatement | switchStatement.statements.size() -1 >= 10)
->collect(switchStatement | thisModule.MeasureSwitchDensity(switchCase))
);
src/main/atl/codestyle.atl
View file @
12da6687
...
...
@@ -48,3 +48,11 @@ helper def: extendsObject() : Set(smm!Measure) =
-- collect all results and send an error message
->collect(it4|thisModule.MeasureDoNotExtendJavaLangError(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));
src/main/atl/design.atl
View file @
12da6687
...
...
@@ -86,3 +86,23 @@ helper def: doNotExtendJavaLangError() : Set(smm!Measure) =
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));
--------------------------------------------- SignatureDeclareThrowsException ---------------------------------------------
-- Rule for metrics SignatureDeclareThrowsException :
helper def: signatureDeclareThrowsException() : Set (smm!Measure) =
-- Browse through all Switch Statements
java!ClassDeclaration.allInstances()->iterate(c; res : Set(smm!Measure) = Set{} |
c.bodyDeclarations
->select(bodyDeclaration | bodyDeclaration.oclIsTypeOf(java!ConstructorDeclaration)
or bodyDeclaration.oclIsTypeOf(java!MethodDeclaration))
->select(bodyDeclaration | bodyDeclaration.thrownExceptions <> OclUndefined)
->collect(bodyDeclaration | bodyDeclaration.thrownExceptions)->iterate(thrownException; res1 : Set(smm!Measure) = Set{} |
thrownException
->select(exception | exception.oclIsTypeOf(java!TypeAccess))
->select(exception | exception.type <> OclUndefined)
->select(exception | exception.type.oclIsTypeOf(java!ClassDeclaration))
->select(exception | exception.type.name = 'Exception')
->collect(exception | thisModule.MeasureSignatureDeclareThrowsException(c))
)
);
\ No newline at end of file
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment