Commit 7942400d authored by BRAULT Benjamin's avatar BRAULT Benjamin
Browse files

Merge branch 'master' into 'SimplifyBooleanAssertion'

# Conflicts:
#   src/main/atl/analysis.atl
parents 0e7e3e4c d90cd559
......@@ -13,7 +13,7 @@ uses bestPractices;
uses documentation;
uses errorProne;
rule Java2SMM {
from
project: java!Model
......@@ -30,89 +30,112 @@ rule Java2SMM {
helper def: allMeasures(project : java!Model): Set(smm!Measure) =
Set{
Set{
-- Example rule
--
thisModule.numberOfClasses(),
-- Multithreading rules
--
thisModule.dontCallThreadRun(),
thisModule.avoidThreadGroup(),
thisModule.useNotifyAllInsteadOfNotify(),
-- Code Style rules
--
thisModule.shortMethodName(),
thisModule.tooManyStaticImports(),
thisModule.avoidDollarSigns(),
thisModule.extendsObject(),
thisModule.genericsNaming(),
thisModule.longVariable(),
thisModule.misLeadingVariableName(),
thisModule.noPackageDeclaration(),
thisModule.shortClassName(),
thisModule.shortMethodName(),
thisModule.shortVariableName(),
thisModule.extendsObject(),
thisModule.tooManyStaticImports(),
thisModule.unnecessaryReturn(),
thisModule.longVariable(),
-- Design rules
--
thisModule.tooManyFields(),
thisModule.tooManyMethods(),
thisModule.returnFromFinallyBlock(),
thisModule.longInstantiation(),
thisModule.shortInstantiation(),
--thisModule.returnEmptyArrayRatherThanNull(),
thisModule.ExcessiveClassLength(),
thisModule.IfBraces(),
thisModule.excessiveParameterList(),
thisModule.finalFieldCouldBeStatic(),
thisModule.longInstantiation(),
thisModule.returnFromFinallyBlock(),
thisModule.shortInstantiation(),
thisModule.signatureDeclareThrowsException(),
thisModule.avoidThrowingNullPointerException(),
thisModule.tooManyFields(),
thisModule.tooManyMethods(),
thisModule.SimplifyBooleanAssertion(),
thisModule.abstractClassWithoutAnyMethod(),
-- Performance rules
--
thisModule.uselessStringValueOf(),
thisModule.tooFewBranchesForASwitchStatement(),
thisModule.useIndexOfChar(),
thisModule.avoidPrintStackTrace(),
thisModule.avoidRethrowingException(),
thisModule.avoidUsingShortType(),
thisModule.emptyStatementBlock(),
thisModule.avoidRethrowingException(),
thisModule.integerInstantiation(),
thisModule.UseArrayListInsteadOfVector(),
thisModule.useIndexOfChar(),
thisModule.uselessStringValueOf(),
thisModule.tooFewBranchesForASwitchStatement(),
-- Documentation rules
--
--thisModule.commentContent(),
--thisModule.commentRequired(),
thisModule.commentContent(),
-- Error prone rules
--
thisModule.missingBreakInSwitch(),
thisModule.suspiciousEqualsMethodName(),
thisModule.doNotExtendJavaLangThrowable(),
thisModule.doNotExtendJavaLangError(),
thisModule.compareObjectsWithEquals(),
thisModule.avoidCallingFinalize(),
thisModule.emptyTryBlock(),
thisModule.emptyWhileStmt(),
thisModule.AvoidCatchingNPE(),
thisModule.avoidCatchingThrowable(),
thisModule.AvoidEnumAsIdentifier(),
thisModule.CloneMethodMustBePublic(),
thisModule.doNotCallGarbageCollectionExplicitly(),
thisModule.doNotExtendJavaLangError(),
thisModule.doNotExtendJavaLangThrowable(),
thisModule.dontImportSun(),
thisModule.emptyCatchBlock(),
thisModule.EmptyInitializer(),
thisModule.emptySwitchStatement(),
thisModule.emptySynchronizedBlock(),
thisModule.emptyFinallyBlock(),
thisModule.emptyfinalizeMethod(),
--thisModule.doNotThrowExceptionInFinally(),
--thisModule.finalizeShouldBeProtected(),
thisModule.emptyTryBlock(),
thisModule.importFromSamePackage(),
thisModule.missingBreakInSwitch(),
thisModule.suspiciousEqualsMethodName(),
thisModule.UnconditionalIfStatement(),
thisModule.UseProperClassLoader(),
-- Best practices rules
thisModule.avoidThrowingNewInstanceOfSameException(),
thisModule.useAssertTrueInsteadOfAssertEquals(),
thisModule.switchDensity(),
thisModule.useAssertSameInsteadOfAssertTrue(),
thisModule.switchDensity()
--thisModule.replaceVectorToList()
--thisModule.unusedPrivateMethod ()
--thisModule.avoidStringBufferField()
thisModule.useAssertTrueInsteadOfAssertEquals()
-- Bugged rules:
--
-- thisModule.avoidThrowingNullPointerException(),
-- thisModule.UseArrayListInsteadOfVector(),
-- thisModule.commentRequired(),
-- thisModule.compareObjectsWithEquals(),
-- thisModule.emptyWhileStmt(),
-- thisModule.emptyFinallyBlock(),
-- thisModule.emptyfinalizeMethod(),
-- thisModule.doNotThrowExceptionInFinally(),
-- thisModule.finalizeShouldBeProtected(),
-- thisModule.avoidDollarSigns(),
-- thisModule.returnEmptyArrayRatherThanNull(),
-- thisModule.replaceVectorToList()
-- thisModule.unusedPrivateMethod ()
-- thisModule.avoidStringBufferField()
-- thisModule.avoidThrowingNewInstanceOfSameException(),
-- thisModule.stringToString(),
-- thisModule.dontCallThreadRun(),
-- thisModule.methodWithSameNameAsEnclosingClass(),
-- thisModule.dontUseFloatTypeForLoopIndices(),
-- thisModule.methodWithSameNameAsEnclosingClass(),
};
......@@ -216,7 +239,24 @@ rule MesureShortVariableName(variable : java!VariableDeclaration) {
}
}
-- A Measure instance if the method violates the rule 'StringToString'.
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;
}
}
-- creates a new Measure when Thread.run() is used instead of Thread.start()
rule MeasureDontCallThreadRun(method : java!MethodInvocation) {
......@@ -643,7 +683,7 @@ rule MeasureCommentRequired(element: java!BodyDeclaration, violatedProperties: S
commentRequired;
}
}
-- Creates a measure instance when a bad word is found in a comment
rule MeasureCommentContent(element: Sequence(String)){
to
om: smm!ObservedMeasure (
......@@ -663,6 +703,26 @@ rule MeasureCommentContent(element: Sequence(String)){
}
-- Creates a measure instance when a for loop uses a float as loop indice
rule MeasureDontUseFloatTypeForLoopIndices(){
to
om: smm!ObservedMeasure (
measure <- dontUseFloatTypeForLoopIndices,
measurements <- measurement
),
dontUseFloatTypeForLoopIndices: smm!DimensionalMeasure (
name <- 'DontUseFloatTypeForLoopIndices()',
shortDescription <- 'Don’t use floating point for loop indices.'
),
measurement: smm!DirectMeasurement (
error <- 'Use integer instead of floats as loop indice'
)
do {
dontUseFloatTypeForLoopIndices;
}
}
--- Returns the name of a BodyDeclaration
helper context java!BodyDeclaration def: getBodyDeclarationName() : String =
self.name;
......@@ -1226,6 +1286,294 @@ rule MesureUseAssertSameInsteadOfAssertTrue(tab :java!Package) {
}
}
-- A Measure instance if the class violates the rule AvoidNoPackageDeclaration.
rule MesureAvoidNoPackageDeclaration(class : java!CompilationUnit) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'AvoidNoPackageDeclaration',
shortDescription <- 'you need to declare some package.'
),
measurement: smm!DirectMeasurement (
error <- class.name + ' class have no package declaration'
)
do {
noc;
}
}
-- A Measure instance if the class violates the rule UnconditionalIfStatement.
rule MeasureUnconditionalIfStatement(w: java!IfStatement) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'UnconditionalIfStatement',
shortDescription <- ': UnconditionalIfStatement'
),
measurement: smm!DirectMeasurement (
error <- 'UnconditionalIfStatement is detected in ' + w.originalCompilationUnit.name
)
do {
noc;
}
}
-- A Measure instance if the class violates the rule EmptyInitializer.
rule MeasureEmptyInitializer(w: java!Initializer) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'EmptyInitializer',
shortDescription <- ': EmptyInitializer'
),
measurement: smm!DirectMeasurement (
error <- ' class '+w.originalCompilationUnit.name +' hase an empty Initializer! do not declare an EmptyInitializer, EmptyInitializer! this block need to be deleted'
)
do {
noc;
}
}
-- A Measure instance if the class violates the rule ExcessiveClassLength.
rule MeasureExcessiveClassLength(w: java!MethodDeclaration) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'ExcessiveClassLength',
shortDescription <- ': class file lengths are usually indications that the class may be burdened with excessive responsibilities that could be provided by external classes or functions'
),
measurement: smm!DirectMeasurement (
error <- ' class '+w.originalCompilationUnit.name+ ' has a block with more than 1000 line of code per a method it may need a separation in the code'
)
do {
noc;
}
}
-- A Measure instance if the class violates the rule CloneMethodMustBePublic.
rule MeasureCloneMethodMustBePublic(w: java!MethodDeclaration) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'CloneMethodMustBePublic',
shortDescription <- ': Clone Method Must Be Public'
),
measurement: smm!DirectMeasurement (
error <- ' class '+w.originalCompilationUnit.name+ ' has a clone methode witch need to be public '
)
do {
noc;
}
}
-- A Measure instance if the class violates the rule AvoidEnumAsIdentifier.
rule MeasureAvoidEnumAsIdentifier(w: java!VariableDeclaration) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'AvoidEnumAsIdentifier',
shortDescription <- ': Avoid Enum As Identifier'
),
measurement: smm!DirectMeasurement (
error <- ' class '+w.originalCompilationUnit.name+ ' has a variable named to enum witch is a reserved name'
)
do {
noc;
}
}
-- A Measure instance if the class violates the rule AvoidCatchingNPE.
rule MeasureAvoidCatchingNPE(w: java!CatchClause) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'AvoidCatchingNPE',
shortDescription <- ': Avoid Catching NullPointerExceptions'
),
measurement: smm!DirectMeasurement (
error <- ' class '+w.originalCompilationUnit.name+ ' has a Code that should never
throw NullPointerExceptions under normal circumstances. A catch block may hide the
original error, causing other, more subtle problems later on.'
)
do {
noc;
}
}
-- A Measure instance if the class violates the rule UseProperClassLoader.
rule MeasureUseProperClassLoader(w: java!MethodInvocation) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'UseProperClassLoader',
shortDescription <- ': Use ProperClassLoader'
),
measurement: smm!DirectMeasurement (
error <- ' class'+ w.originalCompilationUnit.name+ ' Use a statement with getClassLoader(),
use Thread.currentThread().getContextClassLoader() instead '
)
do {
noc;
}
}
-------------------------------------------GenericsNaming----------------------------------------------
-- A Measure instance if the class violate the rule GenericsNaming
rule MeasureGenericsNaming(type :java!TypeParameter) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'GenericsNaming',
shortDescription <- 'Names for references to generic values should be limited to a single uppercase letter.'
),
measurement: smm!DirectMeasurement (
error<- 'The generic type ' + type.name + ' should be limited to a single uppercase letter.'
)
do {
noc;
}
}
-------------------------------------------MisLeadingVariableName----------------------------------------------
-- A Measure instance if the class violate the rule MisLeadingVariableName
rule MeasureMisLeadingVariableName(variable :java!VariableDeclarationFragment) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'MisLeadingVariableName',
shortDescription <- 'Detects when a non-field has a name starting with �m_�. This usually denotes a field and could be confusing.'
),
measurement: smm!DirectMeasurement (
error<- 'Variable ' + variable.name + ' should not start with �m_� as it is not a field.'
)
do {
noc;
}
}
-------------------------------------------AvoidCatchingThrowable----------------------------------------------
-- A Measure instance if the class violate the rule MisLeadingVariableName
rule MeasureAvoidCatchingThrowable(catch :java!CatchClause) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'AvoidCatchingThrowable',
shortDescription <- 'Catching Throwable errors is not recommended since its scope is very broad. It includes runtime issues such as OutOfMemoryError that should be exposed and managed separately.'
),
measurement: smm!DirectMeasurement (
error<- 'Throwable ' + catch.exception.name + ' should not be catched.'
)
do {
noc;
}
}
-------------------------------------------DoNotCallGarbageCollectionExplicitly----------------------------------------------
-- A Measure instance if the class violate the rule DoNotCallGarbageCollectionExplicitly
rule MeasureDoNotCallGarbageCollectionExplicitly(method : java!MethodInvocation) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'DoNotCallGarbageCollectionExplicitly',
shortDescription <- 'Calls to System.gc(), Runtime.getRuntime().gc(), and System.runFinalization() are not advised.'
),
measurement: smm!DirectMeasurement (
error<- 'Garbage collector is called explicitly in ' + method.originalCompilationUnit.name
)
do {
noc;
}
}
-------------------------------------------EmptyCatchBlock----------------------------------------------
-- A Measure instance if the class violate the rule EmptyCatchBlock
rule MeasureEmptyCatchBlock(catch : java!CatchClause) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'EmptyCatchBlock',
shortDescription <- 'Empty Catch Block finds instances where an exception is caught, but nothing is done. In most circumstances, this swallows an exception which should either be acted on or reported.'
),
measurement: smm!DirectMeasurement (
error<- 'Empty catch block in ' + catch.originalCompilationUnit.name
)
do {
noc;
}
}
-------------------------------------------ImportFromSamePackage----------------------------------------------
-- A Measure instance if the class violate the rule ImportFromSamePackage
rule MeasureImportFromSamePackage(import : java!ImportDeclaration) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'ImportFromSamePackage',
shortDescription <- 'There is no need to import a type that lives in the same package.'
),
measurement: smm!DirectMeasurement (
error<- 'No need to import ' + import.importedElement.name + ' in ' + import.originalCompilationUnit.name
)
do {
noc;
}
}
-------------------------------------------MethodWithSameNameAsEnclosingClass----------------------------------------------
-- A Measure instance if the class violate the rule MethodWithSameNameAsEnclosingClass
rule MeasureMethodWithSameNameAsEnclosingClass(method : java!MethodDeclaration) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'MethodWithSameNameAsEnclosingClass',
shortDescription <- 'Non-constructor methods should not have the same name as the enclosing class.'
),
measurement: smm!DirectMeasurement (
error<- 'Method ' + method.name + ' has the same name as ' + method.originalCompilationUnit.name + ' and is not a contructor.'
)
do {
noc;
}
}
--------------------------------------------- ExcessiveImports ---------------------------------------------
-- creates a new Measure when a class has more than 4 imports
rule MeasureExcessiveImports(i : java!CompilationUnit) {
......@@ -1247,8 +1595,28 @@ rule MeasureExcessiveImports(i : java!CompilationUnit) {
}
}
--------------------------------------------- IfStmtsMustUseBraces ---------------------------------------------
-- creates a new Measure when a class that contains an if statement without braces.
rule MeasureIfStmtsMustUseBraces(i : java!IfStatement) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'If then Statement must use braces',
shortDescription <- 'Deprecated Avoid using if statements without using braces to surround the code block. If the code formatting...'
),
measurement: smm!DirectMeasurement (
error <- 'The class ' + condition.originalCompilationUnit.name + ' has an if statement without braces. '
)
do {
noc;
}
}
--------------------------------------------- SimplifyBooleanAssertion ---------------------------------------------
-- creates a new Measure for each method that contains an assertion of type : !(expression).
-- creates a new Measure when a class contains a negation in an assertTrue or assertFalse.
rule MeasureSimplifyBooleanAssertion(i : java!PrefixExpression) {
to
om: smm!ObservedMeasure (
......@@ -1267,3 +1635,24 @@ rule MeasureSimplifyBooleanAssertion(i : java!PrefixExpression) {
}
}
--------------------------------------------- AbstractClassWithoutAnyMethod ---------------------------------------------
-- creates a new Measure when an abstract class does not provides any methods.
rule MeasureAbstractClassWithoutAnyMethod(i : java!ClassDeclaration) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'AbstractClassWithoutAnyMethod',
shortDescription <- 'If an abstract class does not provides any methods, it may be acting as a simple data containerth...'
),
measurement: smm!DirectMeasurement (
error<-'The class anstract' + i.name + ' has not method.'
)
do {
noc;
}
}
......@@ -146,4 +146,4 @@ helper def: abstractClassWithoutAnyMethod() : Set(smm!Measure) =
-> select(i | i.bodyDeclarations -> select(r | r.oclIsTypeOf(java!MethodDeclaration)) -> size() = 0)
-> select(i | i.modifier <> OclUndefined)
-> select(i | i.modifier.inheritance -> toString() = 'abstract')
-> collect(i | thisModule.MeasureAbstractClassWithoutAnyMethod(i));
\ No newline at end of file
-> collect(i | thisModule.MeasureAbstractClassWithoutAnyMethod(i));
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