Commit 4083074c authored by Louis QUESTEL's avatar Louis QUESTEL Committed by Gerson SUNYE
Browse files

fix #736 : DoNotExtendJavaLangError 2pts & optimisation on DoNotExtendJavaLangThrowable

parent b93f735f
<?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="DoNotExtendJavaLangError">
<ownedElements name="(default package)">
<ownedElements xsi:type="java:ClassDeclaration" originalCompilationUnit="//@compilationUnits.0" name="DoNotExtendJavaLangErrorPass1">
<modifier visibility="public"/>
<superClass type="//@ownedElements.1/@ownedPackages.0/@ownedElements.0"/>
</ownedElements>
<ownedElements xsi:type="java:ClassDeclaration" originalCompilationUnit="//@compilationUnits.1" name="DoNotExtendJavaLangErrorNotPass2">
<modifier visibility="public"/>
<superClass type="//@ownedElements.1/@ownedPackages.0/@ownedElements.1"/>
</ownedElements>
<ownedElements xsi:type="java:ClassDeclaration" originalCompilationUnit="//@compilationUnits.2" name="DoNotExtendJavaLangErrorNotPass1">
<modifier visibility="public"/>
<superClass type="//@ownedElements.1/@ownedPackages.0/@ownedElements.1"/>
</ownedElements>
<ownedElements xsi:type="java:ClassDeclaration" originalCompilationUnit="//@compilationUnits.3" name="DoNotExtendJavaLangErrorPass2">
<modifier visibility="public"/>
</ownedElements>
</ownedElements>
<ownedElements name="java" proxy="true">
<ownedPackages name="lang" proxy="true">
<ownedElements xsi:type="java:ClassDeclaration" name="Object" proxy="true" usagesInTypeAccess="//@ownedElements.0/@ownedElements.0/@superClass"/>
<ownedElements xsi:type="java:ClassDeclaration" name="Error" proxy="true" usagesInTypeAccess="//@ownedElements.0/@ownedElements.1/@superClass //@ownedElements.0/@ownedElements.2/@superClass">
<superClass type="//@ownedElements.1/@ownedPackages.0/@ownedElements.2"/>
</ownedElements>
<ownedElements xsi:type="java:ClassDeclaration" name="Throwable" proxy="true" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.0/@ownedElements.1/@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.2/@superInterfaces.0"/>
</ownedPackages>
</ownedElements>
<orphanTypes xsi:type="java:PrimitiveTypeInt" name="int"/>
<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"/>
<orphanTypes xsi:type="java:PrimitiveTypeChar" name="char"/>
<orphanTypes xsi:type="java:PrimitiveTypeShort" name="short"/>
<orphanTypes xsi:type="java:PrimitiveTypeByte" name="byte"/>
<compilationUnits name="DoNotExtendJavaLangErrorPass1.java" originalFilePath="C:\Users\33659\Gerson-workspace\DoNotExtendJavaLangError\src\DoNotExtendJavaLangErrorPass1.java" types="//@ownedElements.0/@ownedElements.0"/>
<compilationUnits name="DoNotExtendJavaLangErrorNotPass2.java" originalFilePath="C:\Users\33659\Gerson-workspace\DoNotExtendJavaLangError\src\DoNotExtendJavaLangErrorNotPass2.java" types="//@ownedElements.0/@ownedElements.1"/>
<compilationUnits name="DoNotExtendJavaLangErrorNotPass1.java" originalFilePath="C:\Users\33659\Gerson-workspace\DoNotExtendJavaLangError\src\DoNotExtendJavaLangErrorNotPass1.java" types="//@ownedElements.0/@ownedElements.2"/>
<compilationUnits name="DoNotExtendJavaLangErrorPass2.java" originalFilePath="C:\Users\33659\Gerson-workspace\DoNotExtendJavaLangError\src\DoNotExtendJavaLangErrorPass2.java" types="//@ownedElements.0/@ownedElements.3"/>
</java:Model>
......@@ -3,10 +3,11 @@
<ownedElements name="throwable">
<ownedElements xsi:type="java:ClassDeclaration" originalCompilationUnit="//@compilationUnits.0" name="ThrowablePass3">
<modifier visibility="public"/>
<superClass type="//@ownedElements.1/@ownedPackages.0/@ownedElements.0"/>
</ownedElements>
<ownedElements xsi:type="java:ClassDeclaration" originalCompilationUnit="//@compilationUnits.1" name="ThrowablePass1">
<modifier visibility="public"/>
<superClass type="//@ownedElements.1/@ownedPackages.0/@ownedElements.0"/>
<superClass type="//@ownedElements.1/@ownedPackages.0/@ownedElements.2"/>
</ownedElements>
<ownedElements xsi:type="java:ClassDeclaration" originalCompilationUnit="//@compilationUnits.2" name="ThrowableNotPass4">
<modifier visibility="public"/>
......@@ -14,14 +15,21 @@
</ownedElements>
<ownedElements xsi:type="java:ClassDeclaration" originalCompilationUnit="//@compilationUnits.3" name="ThrowablePass2">
<modifier visibility="public"/>
<superClass type="//@ownedElements.1/@ownedPackages.0/@ownedElements.3"/>
</ownedElements>
</ownedElements>
<ownedElements name="java" proxy="true">
<ownedPackages name="lang" proxy="true">
<ownedElements xsi:type="java:ClassDeclaration" name="Object" proxy="true" usagesInTypeAccess="//@ownedElements.0/@ownedElements.1/@superClass"/>
<ownedElements xsi:type="java:ClassDeclaration" name="Throwable" proxy="true" usagesInTypeAccess="//@ownedElements.0/@ownedElements.2/@superClass">
<ownedElements xsi:type="java:ClassDeclaration" name="Exception" proxy="true" usagesInTypeAccess="//@ownedElements.0/@ownedElements.0/@superClass //@ownedElements.1/@ownedPackages.0/@ownedElements.3/@superClass">
<superClass type="//@ownedElements.1/@ownedPackages.0/@ownedElements.1"/>
</ownedElements>
<ownedElements xsi:type="java:ClassDeclaration" name="Throwable" proxy="true" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.0/@ownedElements.0/@superClass //@ownedElements.0/@ownedElements.2/@superClass">
<superInterfaces type="//@ownedElements.1/@ownedPackages.1/@ownedElements.0"/>
</ownedElements>
<ownedElements xsi:type="java:ClassDeclaration" name="Object" proxy="true" usagesInTypeAccess="//@ownedElements.0/@ownedElements.1/@superClass"/>
<ownedElements xsi:type="java:ClassDeclaration" name="RuntimeException" proxy="true" usagesInTypeAccess="//@ownedElements.0/@ownedElements.3/@superClass">
<superClass type="//@ownedElements.1/@ownedPackages.0/@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"/>
......
......@@ -73,9 +73,10 @@ helper def: allMeasures(project : java!Model): Set(smm!Measure) =
-- Error prone rules
--
thisModule.missingBreakInSwitch(),
thisModule.doNotExtendJavaLangThrowable(),
thisModule.doNotExtendJavaLangError(),
-- Best practicices rules
thisModule.doNotExtendJavaLangThrowable(),
thisModule.avoidThrowingNewInstanceOfSameException()
};
......@@ -643,3 +644,25 @@ rule MeasureEmptyStatementBlock(block : java!Block) {
noc;
}
}
--------------------------------------------- DoNotExtendJavaLangError ---------------------------------------------
-- A Measure instance if the class violates the rule DoNotExtendJavaLangError.
rule MeasureDoNotExtendJavaLangError(variable : java!ClassDeclaration) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'DoNotExtendJavaLangError ',
shortDescription <- 'Errors are system exceptions. Do not extend them.'
),
measurement: smm!DirectMeasurement (
error<-'In the Class '+ variable.name + ' Do not extend Error, Errors are system exceptions.'
)
do {
noc;
}
}
......@@ -68,3 +68,17 @@ helper def: excessiveParameterList() : Set(smm!Measure) =
-> select (method | method.body <> OclUndefined and method.parameters.size() > 10)
-> collect(method | thisModule.MesureExcessiveParameterList(method));
--------------------------------------------- DoNotExtendJavaLangError ---------------------------------------------
-- Rule for metrics DoNotExtendJavaLangDoNotExtendJavaLangError
helper def: doNotExtendJavaLangError() : 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 Error
->select(it3| it3.superClass.type.name = 'Error')
-- collect all results and send an error message
->collect(it4|thisModule.MeasureDoNotExtendJavaLangError(it4))
;
......@@ -23,8 +23,10 @@ helper def: switchStatementContainsMissingBreaks(ss: java!SwitchStatement): Bool
helper def: doNotExtendJavaLangThrowable() : 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 Throwable
->select(it2| it2.superClass.type.name = 'Throwable')
->select(it3| it3.superClass.type.name = 'Throwable')
-- collect all results and send an error message
->collect(it3|thisModule.MeasureDoNotExtendJavaLangThrowable(it3))
;
\ No newline at end of file
->collect(it4|thisModule.MeasureDoNotExtendJavaLangThrowable(it4))
;
Supports Markdown
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