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

Merge branch 'general-improvements' into 'master'

Update comments on #770, #882, #841 and #905 and fix #841

Closes #905 and #841

See merge request !164
parents d909aaff eb8092a6
......@@ -13,4 +13,4 @@ target/
*.launch
!analysis.launch
*.orig
\ No newline at end of file
*.orig
This diff is collapsed.
......@@ -162,10 +162,8 @@ helper def: allMeasures(project : java!Model): Set(smm!Measure) =
--- Creates an instance of Measure for the switch statement missing one or more break statements
-- To test, use input model : missing-break-in-switch.xmi
-- #TODO: rename rule
rule createMeasureForMissingBreakInSwitch(ss: java!SwitchStatement) {
--- Creates an instance of Measure for the switch statement missing one or more break/return statements
rule MeasureMissingBreakInSwitch(ss: java!SwitchStatement) {
to
om: smm!ObservedMeasure (
measure <- missingBreakInSwitch,
......@@ -178,7 +176,7 @@ rule createMeasureForMissingBreakInSwitch(ss: java!SwitchStatement) {
measurement: smm!DirectMeasurement (
error <- 'Missing breaks in Switch in Class : ' + ss.originalCompilationUnit.name + '',
-- Indicates the number of breaks missing in the switch statement
value <- (thisModule.nbBranchesOfASwitchStatement(ss) - thisModule.nbIntentionalFallThroughOfASwitchStatement(ss)) - thisModule.nbBreakStatementOfASwitchStatement(ss)
value <- (thisModule.nbBranchesOfASwitchStatement(ss) - thisModule.nbEmptySwitchCasesOfASwitchStatement(ss)) - thisModule.nbBreakStatementOfASwitchStatement(ss)
)
do {
missingBreakInSwitch;
......@@ -547,7 +545,7 @@ rule MeasureCommentContent(element: Sequence(String)){
}
}
--- Creates a measure instance for each element that violates the rule : CommentRequired
--- Creates a new measure instance when comments are required but are not present on specific language elements
rule MeasureCommentRequired(element: java!BodyDeclaration, violatedProperties: Set(String)) {
to
om: smm!ObservedMeasure (
......@@ -1441,7 +1439,7 @@ rule MeasureTestClassWithoutTest(w: java!CompilationUnit) {
}
}
--- Rule that creates an instance of Measure for the switch statement with too few branches passed in parameter
--- Rule that creates an instance of Measure when a switch statement has too few branches
rule MeasureTooFewBranchesForASwitchStatement(switchStatement: java!SwitchStatement) {
to
om: smm!ObservedMeasure (
......@@ -1561,7 +1559,7 @@ rule MeasureUselessStringValueOf(method : java!MethodInvocation) {
}
}
--- Creates a measure instance when rule 'UseNotifyAllInsteadOfNotify' is violated
--- Creates a measure instance when 'notify()' is used instead of 'notifyAll()'
rule MeasureUseNotifyAllInsteadOfNotify(method: java!MethodInvocation) {
to
om: smm!ObservedMeasure (
......@@ -1895,25 +1893,6 @@ rule MesureShortVariableName(variable : java!VariableDeclaration) {
}
}
--- Creates a new Measure when the method toString() is unnecessarily invoked.
rule MeasureStringToString(method : java!MethodInvocation) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'StringToString',
shortDescription <- 'Avoid calling toString() on objects already known to be string instances; this is unnecessary.'
),
measurement: smm!DirectMeasurement (
error <- 'The Object ' + method.expression.variable.name + ' is already a String in ' + method.originalCompilationUnit.name
)
do {
noc;
}
}
--- A Measure instance if the class violates the rule 'TooManyFields'.
-- #FIXME: Typo
rule MesureTooManyFields(class : java!ClassDeclaration) {
......@@ -2076,19 +2055,37 @@ 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
--- Returns the number of break/return/continue/throw statements of the switch statement passed in parameter
helper def: nbBreakStatementOfASwitchStatement(ss:java!SwitchStatement) : Integer =
ss.statements->select(each | each.oclIsTypeOf(java!BreakStatement)).size()
ss.statements->select(each |
each.oclIsTypeOf(java!BreakStatement) or
each.oclIsTypeOf(java!ReturnStatement) or
each.oclIsTypeOf(java!ThrowStatement) or
each.oclIsTypeOf(java!ContinueStatement)
).size()
;
--- Returns the number of expressions of the switch statement passed in parameter
helper def: nbExpressionsStatementOfASwitchStatement(ss:java!SwitchStatement) : Integer =
ss.statements->select(each | each.oclIsTypeOf(java!ExpressionStatement)).size()
--- Returns the number of empty switch cases of a switch statement
helper def: nbEmptySwitchCasesOfASwitchStatement(ss:java!SwitchStatement) : Integer =
-- Get the number of SwitchCases whoses next sibling is also a SwitchCase
ss.statements
->excluding(ss.statements.last())
->select(statement |
statement.oclIsTypeOf(java!SwitchCase) and
thisModule.nextSiblingIsSwitchCase(ss.statements.indexOf(statement), ss.statements)
).size() +
-- Add the last one if it's a switch case
ss.statements->select(statement
| statement = ss.statements.last() and statement.oclIsTypeOf(java!SwitchCase)).size()
;
--- Returns the number of intentional fall-through (empty switch cases) of a switchStatement
helper def: nbIntentionalFallThroughOfASwitchStatement(ss:java!SwitchStatement) : Integer =
thisModule.nbBranchesOfASwitchStatement(ss) - thisModule.nbExpressionsStatementOfASwitchStatement(ss)
--- Returns true if the next sibling of current SwitchCase is also a SwitchCase
helper def : nextSiblingIsSwitchCase(
indexOfSwitchCase : Integer,
statements : Sequence(java!Statement)) : Boolean =
statements.at(indexOfSwitchCase+1).oclIsTypeOf(java!SwitchCase)
;
--- Returns the name of a BodyDeclaration
......
......@@ -2,7 +2,7 @@ library documentation;
--------------------------------------------- CommentRequired ---------------------------------------------
--- Returns a set of measures for each element that violates the rule : CommentRequired
--- Returns a set of measures for each element where comments are required but are missing
helper def: commentRequired() : Set(smm!Measure) =
java!BodyDeclaration.allInstances()
......@@ -15,7 +15,7 @@ helper def: commentRequired() : Set(smm!Measure) =
-> collect (elem | thisModule.MeasureCommentRequired(elem, thisModule.violatedCommentRequiredProperties(elem)))
;
--- Returns true if the given elem violates any of the CommentRequired rule properties
--- Returns true if there exists a CommentRequired property denoting that the given element requires a comment but does not have any
helper def: violatesCommentRequired(elem: java!BodyDeclaration): Boolean =
-- An element violates a property if there's at least one that's violated,
......@@ -29,7 +29,7 @@ helper def: violatesCommentRequired(elem: java!BodyDeclaration): Boolean =
)
;
--- Returns the list of violated CommentRequired properties as a string
--- Returns as a string, the list of CommentRequired properties (comments required but missing) that the given element violates
helper def: violatedCommentRequiredProperties(elem: java!BodyDeclaration): String =
-- Get the set of property tuples associated to elem by using elem data type as key to the Map
......@@ -40,7 +40,7 @@ helper def: violatedCommentRequiredProperties(elem: java!BodyDeclaration): Strin
->collect(tuple | propertyNames.concat(tuple.property))
;
--- Returns true if the given property tuple applies to the given elem, false otherwise
--- Returns true if the CommentRequired property tuple applies to the given element, false otherwise
helper def: matchesCommentRequiredProperty(
value:String, -- is either 'Required' or 'Ignore'
elem: java!BodyDeclaration,
......@@ -74,7 +74,7 @@ helper def: hasComments(elem: java!BodyDeclaration): Boolean =
not elem.comments.isEmpty()
;
--- Contains the default configuration for the CommentRequired rule
--- Defines properties denoting whether comments are required (or unwanted) for specific language elements
-- Can add, delete or modify properties here to customize them just like in PMD
helper def: commentRequiredPropertyDefinitions() :
Map(
......
......@@ -2,19 +2,20 @@ library errorProne;
---------------------------------- Missing break in switch---------------------------------------------
-- Returns a set of measures for each switch statement that violates the rule : missing break in switch
--- Returns a set of measures for every switch statement without a break statement for each case option
helper def: missingBreakInSwitch(): Set(smm!Measure) =
java!SwitchStatement.allInstances()
-> select (switchStatement | thisModule.switchStatementContainsMissingBreaks(switchStatement))
-> collect (switchStatement | thisModule.createMeasureForMissingBreakInSwitch(switchStatement))
-> collect (switchStatement | thisModule.MeasureMissingBreakInSwitch(switchStatement))
;
-- Returns true if the switch statement contains missing breaks, false otherwise
--- Returns true if the switch statement contains missing breaks, false otherwise
helper def: switchStatementContainsMissingBreaks(ss: java!SwitchStatement): Boolean =
-- If the number of switch cases (omitting those that empty => indicates intentional fallout) is bigger than
-- the number of break statements => we don't have a break statement for every switch case
-- in that case, return true, else false
(thisModule.nbBranchesOfASwitchStatement(ss) - thisModule.nbIntentionalFallThroughOfASwitchStatement(ss)) > thisModule.nbBreakStatementOfASwitchStatement(ss)
(thisModule.nbBranchesOfASwitchStatement(ss) - thisModule.nbEmptySwitchCasesOfASwitchStatement(ss)) > thisModule.nbBreakStatementOfASwitchStatement(ss)
;
--------------------------------------------- DoNotExtendJavaLangThrowable ---------------------------------------------
......@@ -441,4 +442,4 @@ helper def: suspiciousHashcodeMethodName(): Set(smm!Measure) =
java!MethodDeclaration.allInstances()
->select(md | md.name <> 'hashCode')
->select(md | md.name.toLower() = 'hashcode')
->collect(md | thisModule.MeasureSuspiciousHashcodeMethodName(md));
\ No newline at end of file
->collect(md | thisModule.MeasureSuspiciousHashcodeMethodName(md));
This diff is collapsed.
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