Commit 28e21e40 authored by Gerson SUNYE's avatar Gerson SUNYE
Browse files

Merge branch 'master' into 'master'

Issue 726 AvoidRethrowingException

See merge request naomod/mde/projet-2019!61
parents c65d1873 d4049080
<?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="Issue726">
<ownedElements name="(default package)">
<ownedElements xsi:type="java:ClassDeclaration" originalCompilationUnit="//@compilationUnits.0" name="RethrowingErrorOnly">
<modifier visibility="public"/>
<bodyDeclarations xsi:type="java:MethodDeclaration" originalCompilationUnit="//@compilationUnits.0" name="rethrowingOnly">
<modifier visibility="public"/>
<body originalCompilationUnit="//@compilationUnits.0">
<statements xsi:type="java:TryStatement" originalCompilationUnit="//@compilationUnits.0">
<body originalCompilationUnit="//@compilationUnits.0">
<statements xsi:type="java:ExpressionStatement" originalCompilationUnit="//@compilationUnits.0">
<expression xsi:type="java:MethodInvocation" originalCompilationUnit="//@compilationUnits.0" method="//@ownedElements.1/@ownedPackages.1/@ownedElements.1/@bodyDeclarations.0">
<arguments xsi:type="java:StringLiteral" originalCompilationUnit="//@compilationUnits.0" escapedValue="&quot;Rethrowing only&quot;"/>
<expression xsi:type="java:SingleVariableAccess" variable="//@ownedElements.1/@ownedPackages.0/@ownedElements.0/@bodyDeclarations.0/@fragments.0">
<qualifier xsi:type="java:TypeAccess" type="//@ownedElements.1/@ownedPackages.0/@ownedElements.0"/>
</expression>
</expression>
</statements>
</body>
<catchClauses originalCompilationUnit="//@compilationUnits.0">
<exception originalCompilationUnit="//@compilationUnits.0" name="e" usageInVariableAccess="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.0/@catchClauses.0/@body/@statements.0/@expression">
<modifier/>
<type type="//@ownedElements.1/@ownedPackages.0/@ownedElements.6"/>
</exception>
<body originalCompilationUnit="//@compilationUnits.0">
<statements xsi:type="java:ThrowStatement" originalCompilationUnit="//@compilationUnits.0">
<expression xsi:type="java:SingleVariableAccess" variable="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.0/@catchClauses.0/@exception"/>
</statements>
</body>
</catchClauses>
</statements>
</body>
<returnType type="//@orphanTypes.5"/>
</bodyDeclarations>
</ownedElements>
<ownedElements xsi:type="java:ClassDeclaration" originalCompilationUnit="//@compilationUnits.1" name="DoingStuffAndRethrowing">
<modifier visibility="public"/>
<bodyDeclarations xsi:type="java:MethodDeclaration" originalCompilationUnit="//@compilationUnits.1" name="doingStuff">
<modifier visibility="public"/>
<body originalCompilationUnit="//@compilationUnits.1">
<statements xsi:type="java:TryStatement" originalCompilationUnit="//@compilationUnits.1">
<body originalCompilationUnit="//@compilationUnits.1">
<statements xsi:type="java:ExpressionStatement" originalCompilationUnit="//@compilationUnits.1">
<expression xsi:type="java:MethodInvocation" originalCompilationUnit="//@compilationUnits.1" method="//@ownedElements.1/@ownedPackages.1/@ownedElements.1/@bodyDeclarations.0">
<arguments xsi:type="java:StringLiteral" originalCompilationUnit="//@compilationUnits.1" escapedValue="&quot;Doing stuff and rethrowing&quot;"/>
<expression xsi:type="java:SingleVariableAccess" variable="//@ownedElements.1/@ownedPackages.0/@ownedElements.0/@bodyDeclarations.0/@fragments.0">
<qualifier xsi:type="java:TypeAccess" type="//@ownedElements.1/@ownedPackages.0/@ownedElements.0"/>
</expression>
</expression>
</statements>
</body>
<catchClauses originalCompilationUnit="//@compilationUnits.1">
<exception originalCompilationUnit="//@compilationUnits.1" name="e" usageInVariableAccess="//@ownedElements.0/@ownedElements.1/@bodyDeclarations.0/@body/@statements.0/@catchClauses.0/@body/@statements.3/@expression">
<modifier/>
<type type="//@ownedElements.1/@ownedPackages.0/@ownedElements.6"/>
</exception>
<body originalCompilationUnit="//@compilationUnits.1">
<statements xsi:type="java:VariableDeclarationStatement" originalCompilationUnit="//@compilationUnits.1">
<type type="//@orphanTypes.0"/>
<fragments originalCompilationUnit="//@compilationUnits.1" name="x" usageInVariableAccess="//@ownedElements.0/@ownedElements.1/@bodyDeclarations.0/@body/@statements.0/@catchClauses.0/@body/@statements.1/@fragments.0/@initializer">
<initializer xsi:type="java:NumberLiteral" originalCompilationUnit="//@compilationUnits.1" tokenValue="42"/>
</fragments>
<modifier/>
</statements>
<statements xsi:type="java:VariableDeclarationStatement" originalCompilationUnit="//@compilationUnits.1">
<type type="//@orphanTypes.0"/>
<fragments originalCompilationUnit="//@compilationUnits.1" name="y" usageInVariableAccess="//@ownedElements.0/@ownedElements.1/@bodyDeclarations.0/@body/@statements.0/@catchClauses.0/@body/@statements.2/@expression/@arguments.0">
<initializer xsi:type="java:SingleVariableAccess" variable="//@ownedElements.0/@ownedElements.1/@bodyDeclarations.0/@body/@statements.0/@catchClauses.0/@body/@statements.0/@fragments.0"/>
</fragments>
<modifier/>
</statements>
<statements xsi:type="java:ExpressionStatement" originalCompilationUnit="//@compilationUnits.1">
<expression xsi:type="java:MethodInvocation" originalCompilationUnit="//@compilationUnits.1" method="//@ownedElements.1/@ownedPackages.1/@ownedElements.1/@bodyDeclarations.1">
<arguments xsi:type="java:SingleVariableAccess" variable="//@ownedElements.0/@ownedElements.1/@bodyDeclarations.0/@body/@statements.0/@catchClauses.0/@body/@statements.1/@fragments.0"/>
<expression xsi:type="java:SingleVariableAccess" variable="//@ownedElements.1/@ownedPackages.0/@ownedElements.0/@bodyDeclarations.0/@fragments.0">
<qualifier xsi:type="java:TypeAccess" type="//@ownedElements.1/@ownedPackages.0/@ownedElements.0"/>
</expression>
</expression>
</statements>
<statements xsi:type="java:ThrowStatement" originalCompilationUnit="//@compilationUnits.1">
<expression xsi:type="java:SingleVariableAccess" variable="//@ownedElements.0/@ownedElements.1/@bodyDeclarations.0/@body/@statements.0/@catchClauses.0/@exception"/>
</statements>
</body>
</catchClauses>
</statements>
</body>
<returnType type="//@orphanTypes.5"/>
</bodyDeclarations>
</ownedElements>
</ownedElements>
<ownedElements name="java" proxy="true">
<ownedPackages name="lang" proxy="true">
<ownedElements xsi:type="java:ClassDeclaration" name="System" proxy="true" usagesInTypeAccess="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.0/@body/@statements.0/@expression/@expression/@qualifier //@ownedElements.0/@ownedElements.1/@bodyDeclarations.0/@body/@statements.0/@body/@statements.0/@expression/@expression/@qualifier //@ownedElements.0/@ownedElements.1/@bodyDeclarations.0/@body/@statements.0/@catchClauses.0/@body/@statements.2/@expression/@expression/@qualifier">
<bodyDeclarations xsi:type="java:FieldDeclaration" proxy="true">
<fragments name="out" proxy="true" usageInVariableAccess="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.0/@body/@statements.0/@expression/@expression //@ownedElements.0/@ownedElements.1/@bodyDeclarations.0/@body/@statements.0/@body/@statements.0/@expression/@expression //@ownedElements.0/@ownedElements.1/@bodyDeclarations.0/@body/@statements.0/@catchClauses.0/@body/@statements.2/@expression/@expression"/>
</bodyDeclarations>
</ownedElements>
<ownedElements xsi:type="java:ClassDeclaration" name="String" proxy="true" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.1/@ownedElements.1/@bodyDeclarations.0/@parameters.0/@type">
<superInterfaces type="//@ownedElements.1/@ownedPackages.1/@ownedElements.0"/>
<superInterfaces type="//@ownedElements.1/@ownedPackages.0/@ownedElements.2"/>
<superInterfaces type="//@ownedElements.1/@ownedPackages.0/@ownedElements.3"/>
</ownedElements>
<ownedElements xsi:type="java:InterfaceDeclaration" name="Comparable" proxy="true" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.0/@ownedElements.1/@superInterfaces.1">
<typeParameters name="T" proxy="true"/>
</ownedElements>
<ownedElements xsi:type="java:InterfaceDeclaration" name="CharSequence" proxy="true" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.0/@ownedElements.1/@superInterfaces.2"/>
<ownedElements xsi:type="java:InterfaceDeclaration" name="AutoCloseable" proxy="true" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.1/@ownedElements.4/@superInterfaces.0"/>
<ownedElements xsi:type="java:InterfaceDeclaration" name="Appendable" proxy="true" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.1/@ownedElements.1/@superInterfaces.0"/>
<ownedElements xsi:type="java:ClassDeclaration" name="Exception" proxy="true" usagesInTypeAccess="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.0/@catchClauses.0/@exception/@type //@ownedElements.0/@ownedElements.1/@bodyDeclarations.0/@body/@statements.0/@catchClauses.0/@exception/@type">
<superClass type="//@ownedElements.1/@ownedPackages.0/@ownedElements.7"/>
</ownedElements>
<ownedElements xsi:type="java:ClassDeclaration" name="Throwable" proxy="true" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.0/@ownedElements.6/@superClass">
<superInterfaces type="//@ownedElements.1/@ownedPackages.1/@ownedElements.0"/>
</ownedElements>
</ownedPackages>
<ownedPackages name="io" proxy="true">
<ownedElements xsi:type="java:InterfaceDeclaration" name="Serializable" proxy="true" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.0/@ownedElements.1/@superInterfaces.0 //@ownedElements.1/@ownedPackages.0/@ownedElements.7/@superInterfaces.0"/>
<ownedElements xsi:type="java:ClassDeclaration" name="PrintStream" proxy="true">
<bodyDeclarations xsi:type="java:MethodDeclaration" name="println" proxy="true" usages="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.0/@body/@statements.0/@expression //@ownedElements.0/@ownedElements.1/@bodyDeclarations.0/@body/@statements.0/@body/@statements.0/@expression">
<parameters name="arg0" proxy="true">
<type type="//@ownedElements.1/@ownedPackages.0/@ownedElements.1"/>
</parameters>
</bodyDeclarations>
<bodyDeclarations xsi:type="java:MethodDeclaration" name="println" proxy="true" usages="//@ownedElements.0/@ownedElements.1/@bodyDeclarations.0/@body/@statements.0/@catchClauses.0/@body/@statements.2/@expression">
<parameters name="arg0" proxy="true">
<type type="//@orphanTypes.0"/>
</parameters>
</bodyDeclarations>
<superInterfaces type="//@ownedElements.1/@ownedPackages.0/@ownedElements.5"/>
<superInterfaces type="//@ownedElements.1/@ownedPackages.1/@ownedElements.4"/>
<superClass type="//@ownedElements.1/@ownedPackages.1/@ownedElements.2"/>
</ownedElements>
<ownedElements xsi:type="java:ClassDeclaration" name="FilterOutputStream" proxy="true" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.1/@ownedElements.1/@superClass">
<superClass type="//@ownedElements.1/@ownedPackages.1/@ownedElements.3"/>
</ownedElements>
<ownedElements xsi:type="java:ClassDeclaration" name="OutputStream" proxy="true" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.1/@ownedElements.2/@superClass">
<superInterfaces type="//@ownedElements.1/@ownedPackages.1/@ownedElements.4"/>
<superInterfaces type="//@ownedElements.1/@ownedPackages.1/@ownedElements.5"/>
</ownedElements>
<ownedElements xsi:type="java:InterfaceDeclaration" name="Closeable" proxy="true" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.1/@ownedElements.3/@superInterfaces.0 //@ownedElements.1/@ownedPackages.1/@ownedElements.1/@superInterfaces.1">
<superInterfaces type="//@ownedElements.1/@ownedPackages.0/@ownedElements.4"/>
</ownedElements>
<ownedElements xsi:type="java:InterfaceDeclaration" name="Flushable" proxy="true" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.1/@ownedElements.3/@superInterfaces.1"/>
</ownedPackages>
</ownedElements>
<orphanTypes xsi:type="java:PrimitiveTypeInt" name="int" usagesInTypeAccess="//@ownedElements.0/@ownedElements.1/@bodyDeclarations.0/@body/@statements.0/@catchClauses.0/@body/@statements.0/@type //@ownedElements.0/@ownedElements.1/@bodyDeclarations.0/@body/@statements.0/@catchClauses.0/@body/@statements.1/@type //@ownedElements.1/@ownedPackages.1/@ownedElements.1/@bodyDeclarations.1/@parameters.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" usagesInTypeAccess="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@returnType //@ownedElements.0/@ownedElements.1/@bodyDeclarations.0/@returnType"/>
<orphanTypes xsi:type="java:PrimitiveTypeChar" name="char"/>
<orphanTypes xsi:type="java:PrimitiveTypeShort" name="short"/>
<orphanTypes xsi:type="java:PrimitiveTypeByte" name="byte"/>
<compilationUnits name="RethrowingErrorOnly.java" originalFilePath="/Users/alexis/eclipse-workspace/Issue726/src/RethrowingErrorOnly.java" types="//@ownedElements.0/@ownedElements.0"/>
<compilationUnits name="DoingStuffAndRethrowing.java" originalFilePath="/Users/alexis/eclipse-workspace/Issue726/src/DoingStuffAndRethrowing.java" types="//@ownedElements.0/@ownedElements.1"/>
</java:Model>
......@@ -70,6 +70,7 @@ helper def: allMeasures(project : java!Model): Set(smm!Measure) =
thisModule.avoidPrintStackTrace(),
thisModule.avoidUsingShortType(),
thisModule.emptyStatementBlock(),
thisModule.avoidRethrowingException(),
thisModule.integerInstantiation(),
-- Documentation rules
......@@ -838,6 +839,29 @@ rule MeasureCompareObjectsWithEquals(expression : java!InfixExpression) {
}
}
--------------------------------------------- AvoidRethrowingException ---------------------------------------------
--A Measure instance if the class violates the rule AvoidRethrowingException.
rule MeasureAvoidRethrowingException(statement : java!TryStatement) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'AvoidRethrowingException ',
shortDescription <- 'Avoid merely rethrowing an exception.'
),
measurement: smm!DirectMeasurement (
error<-'In the Class '+ statement.originalCompilationUnit.name + ' an exception was caught and merely rethrown.'
)
do {
noc;
}
}
--------------------------------------------- IntegerInstantiation ---------------------------------------------
-- A Measure instance if the class violates the rule 'IntegerInstantiation'.
rule MeasureIntegerInstantiation(variable : java!CompilationUnit) {
......@@ -847,6 +871,7 @@ rule MeasureIntegerInstantiation(variable : java!CompilationUnit) {
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'IntegerInstantiation',
shortDescription <- 'Calling new Integer() causes memory allocation that can be avoided by the static Integer.valueOf(). It makes use of an internal cache that recycles earlier instances making it more memory efficient. Note that new Integer() is deprecated since JDK 9 for that reason.'
),
......
......@@ -119,6 +119,20 @@ helper def: longInstantiation() : Set(smm!Measure) =
->collect(it2|thisModule.MeasureLongInstantiation(it2.originalCompilationUnit))
;
--------------------------------------------- AvoidRethrowingException ---------------------------------------------
-- Rule for metrics AvoidRethrowingException :
helper def: avoidRethrowingException() : Set (smm!Measure) =
-- Browse through all try statements
java!TryStatement.allInstances() ->iterate(tryStatement; res : Set(smm!Measure) = Set{} |
tryStatement.catchClauses
->select(catchClause | catchClause.body <> OclUndefined and catchClause.body.statements.size() = 1)
->collect(catchClause | catchClause.body.statements)
->iterate(catchStatement; res1 : Set(smm!Measure) = Set{} |
catchStatement
->select(catchState | catchState.oclIsTypeOf(java!ThrowStatement))
->collect(catchState | thisModule.MeasureAvoidRethrowingException(catchState))
)
);
--------------------------------------------- IntegerInstantiation ---------------------------------------------
-- Rule for metricsIntegerInstantiation : returns the set of class Measures that violates the rule.
......@@ -130,5 +144,3 @@ helper def: integerInstantiation() : Set(smm!Measure) =
-- collect every result and sends an error message
->collect(it2|thisModule.MeasureIntegerInstantiation(it2.originalCompilationUnit))
;
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