Commit a3d6b815 authored by LePandaRoox's avatar LePandaRoox
Browse files

Fixed issue #739

parent 60bac72f
<?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="ExcessiveImports">
<ownedElements name="(default package)">
<ownedElements xsi:type="java:ClassDeclaration" originalCompilationUnit="//@compilationUnits.0" name="ClassWithExcessiveImports">
<modifier visibility="public"/>
<bodyDeclarations xsi:type="java:FieldDeclaration" originalCompilationUnit="//@compilationUnits.0">
<modifier/>
<type type="//@orphanTypes.9"/>
<fragments originalCompilationUnit="//@compilationUnits.0" name="toto">
<initializer xsi:type="java:ClassInstanceCreation" originalCompilationUnit="//@compilationUnits.0" method="//@ownedElements.1/@ownedPackages.0/@ownedElements.0/@bodyDeclarations.0">
<type type="//@orphanTypes.9"/>
</initializer>
</fragments>
</bodyDeclarations>
</ownedElements>
<ownedElements xsi:type="java:ClassDeclaration" originalCompilationUnit="//@compilationUnits.1" name="ClassWithoutExcessiveImport">
<modifier visibility="public"/>
<bodyDeclarations xsi:type="java:FieldDeclaration" originalCompilationUnit="//@compilationUnits.1">
<modifier/>
<type type="//@orphanTypes.9"/>
<fragments originalCompilationUnit="//@compilationUnits.1" name="toto">
<initializer xsi:type="java:ClassInstanceCreation" originalCompilationUnit="//@compilationUnits.1" method="//@ownedElements.1/@ownedPackages.0/@ownedElements.0/@bodyDeclarations.0">
<type type="//@orphanTypes.9"/>
</initializer>
</fragments>
</bodyDeclarations>
</ownedElements>
</ownedElements>
<ownedElements name="java" proxy="true">
<ownedPackages name="util" proxy="true">
<ownedElements xsi:type="java:ClassDeclaration" name="ArrayList" proxy="true" usagesInImports="//@compilationUnits.0/@imports.0 //@compilationUnits.1/@imports.0" usagesInTypeAccess="//@orphanTypes.9/@type">
<bodyDeclarations xsi:type="java:ConstructorDeclaration" name="ArrayList" proxy="true" usages="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@fragments.0/@initializer //@ownedElements.0/@ownedElements.1/@bodyDeclarations.0/@fragments.0/@initializer"/>
<superInterfaces type="//@ownedElements.1/@ownedPackages.0/@ownedElements.4"/>
<superInterfaces type="//@ownedElements.1/@ownedPackages.0/@ownedElements.5"/>
<superInterfaces type="//@ownedElements.1/@ownedPackages.1/@ownedElements.1"/>
<superInterfaces type="//@ownedElements.1/@ownedPackages.2/@ownedElements.0"/>
<typeParameters name="E" proxy="true"/>
<superClass type="//@ownedElements.1/@ownedPackages.0/@ownedElements.1"/>
</ownedElements>
<ownedElements xsi:type="java:ClassDeclaration" name="AbstractList" proxy="true" usagesInImports="//@compilationUnits.0/@imports.1" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.0/@ownedElements.0/@superClass">
<superInterfaces type="//@ownedElements.1/@ownedPackages.0/@ownedElements.4"/>
<typeParameters name="E" proxy="true"/>
<superClass type="//@ownedElements.1/@ownedPackages.0/@ownedElements.2"/>
</ownedElements>
<ownedElements xsi:type="java:ClassDeclaration" name="AbstractCollection" proxy="true" usagesInImports="//@compilationUnits.0/@imports.2" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.0/@ownedElements.1/@superClass">
<superInterfaces type="//@ownedElements.1/@ownedPackages.0/@ownedElements.3"/>
<typeParameters name="E" proxy="true"/>
</ownedElements>
<ownedElements xsi:type="java:InterfaceDeclaration" name="Collection" proxy="true" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.0/@ownedElements.2/@superInterfaces.0 //@ownedElements.1/@ownedPackages.0/@ownedElements.4/@superInterfaces.0">
<superInterfaces type="//@ownedElements.1/@ownedPackages.1/@ownedElements.0"/>
<typeParameters name="E" proxy="true"/>
</ownedElements>
<ownedElements xsi:type="java:InterfaceDeclaration" name="List" proxy="true" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.0/@ownedElements.1/@superInterfaces.0 //@ownedElements.1/@ownedPackages.0/@ownedElements.0/@superInterfaces.0">
<superInterfaces type="//@ownedElements.1/@ownedPackages.0/@ownedElements.3"/>
<typeParameters name="E" proxy="true"/>
</ownedElements>
<ownedElements xsi:type="java:InterfaceDeclaration" name="RandomAccess" proxy="true" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.0/@ownedElements.0/@superInterfaces.1"/>
<ownedElements xsi:type="java:ClassDeclaration" name="Arrays" proxy="true" usagesInImports="//@compilationUnits.0/@imports.3"/>
<ownedElements xsi:type="java:ClassDeclaration" name="Calendar" proxy="true" usagesInImports="//@compilationUnits.0/@imports.4">
<superInterfaces type="//@ownedElements.1/@ownedPackages.2/@ownedElements.0"/>
<superInterfaces type="//@ownedElements.1/@ownedPackages.1/@ownedElements.1"/>
<superInterfaces type="//@ownedElements.1/@ownedPackages.1/@ownedElements.2"/>
</ownedElements>
</ownedPackages>
<ownedPackages name="lang" proxy="true">
<ownedElements xsi:type="java:InterfaceDeclaration" name="Iterable" proxy="true" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.0/@ownedElements.3/@superInterfaces.0">
<typeParameters name="T" proxy="true"/>
</ownedElements>
<ownedElements xsi:type="java:InterfaceDeclaration" name="Cloneable" proxy="true" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.0/@ownedElements.0/@superInterfaces.2 //@ownedElements.1/@ownedPackages.0/@ownedElements.7/@superInterfaces.1"/>
<ownedElements xsi:type="java:InterfaceDeclaration" name="Comparable" proxy="true" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.0/@ownedElements.7/@superInterfaces.2 //@ownedElements.1/@ownedPackages.1/@ownedElements.3/@superInterfaces.0">
<typeParameters name="T" proxy="true"/>
</ownedElements>
<ownedElements xsi:type="java:ClassDeclaration" name="Integer" proxy="true" usagesInTypeAccess="//@orphanTypes.9/@typeArguments.0">
<superInterfaces type="//@ownedElements.1/@ownedPackages.1/@ownedElements.2"/>
<superClass type="//@ownedElements.1/@ownedPackages.1/@ownedElements.4"/>
</ownedElements>
<ownedElements xsi:type="java:ClassDeclaration" name="Number" proxy="true" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.1/@ownedElements.3/@superClass">
<superInterfaces type="//@ownedElements.1/@ownedPackages.2/@ownedElements.0"/>
</ownedElements>
</ownedPackages>
<ownedPackages name="io" proxy="true">
<ownedElements xsi:type="java:InterfaceDeclaration" name="Serializable" proxy="true" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.0/@ownedElements.0/@superInterfaces.3 //@ownedElements.1/@ownedPackages.0/@ownedElements.7/@superInterfaces.0 //@ownedElements.1/@ownedPackages.1/@ownedElements.4/@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"/>
<orphanTypes xsi:type="java:ParameterizedType" name="java.util.ArrayList&lt;java.lang.Integer>" usagesInTypeAccess="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@type //@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@fragments.0/@initializer/@type //@ownedElements.0/@ownedElements.1/@bodyDeclarations.0/@type //@ownedElements.0/@ownedElements.1/@bodyDeclarations.0/@fragments.0/@initializer/@type">
<type type="//@ownedElements.1/@ownedPackages.0/@ownedElements.0"/>
<typeArguments type="//@ownedElements.1/@ownedPackages.1/@ownedElements.3"/>
</orphanTypes>
<compilationUnits name="ClassWithExcessiveImports.java" originalFilePath="C:\Users\forma\eclipse-workspace_2019\ExcessiveImports\src\ClassWithExcessiveImports.java" types="//@ownedElements.0/@ownedElements.0">
<imports originalCompilationUnit="//@compilationUnits.0" importedElement="//@ownedElements.1/@ownedPackages.0/@ownedElements.0"/>
<imports originalCompilationUnit="//@compilationUnits.0" importedElement="//@ownedElements.1/@ownedPackages.0/@ownedElements.1"/>
<imports originalCompilationUnit="//@compilationUnits.0" importedElement="//@ownedElements.1/@ownedPackages.0/@ownedElements.2"/>
<imports originalCompilationUnit="//@compilationUnits.0" importedElement="//@ownedElements.1/@ownedPackages.0/@ownedElements.6"/>
<imports originalCompilationUnit="//@compilationUnits.0" importedElement="//@ownedElements.1/@ownedPackages.0/@ownedElements.7"/>
</compilationUnits>
<compilationUnits name="ClassWithoutExcessiveImport.java" originalFilePath="C:\Users\forma\eclipse-workspace_2019\ExcessiveImports\src\ClassWithoutExcessiveImport.java" types="//@ownedElements.0/@ownedElements.1">
<imports originalCompilationUnit="//@compilationUnits.1" importedElement="//@ownedElements.1/@ownedPackages.0/@ownedElements.0"/>
</compilationUnits>
</java:Model>
......@@ -58,11 +58,12 @@ helper def: allMeasures(project : java!Model): Set(smm!Measure) =
thisModule.returnFromFinallyBlock(),
thisModule.longInstantiation(),
thisModule.shortInstantiation(),
thisModule.returnEmptyArrayRatherThanNull(),
--thisModule.returnEmptyArrayRatherThanNull(),
thisModule.excessiveParameterList(),
thisModule.finalFieldCouldBeStatic(),
thisModule.signatureDeclareThrowsException(),
thisModule.avoidThrowingNullPointerException(),
thisModule.excessiveImports(),
-- Performance rules
--
......@@ -78,8 +79,8 @@ helper def: allMeasures(project : java!Model): Set(smm!Measure) =
-- Documentation rules
--
thisModule.commentContent(),
thisModule.commentRequired(),
--thisModule.commentContent(),
--thisModule.commentRequired(),
-- Error prone rules
......@@ -1136,4 +1137,25 @@ rule MesureUseAssertSameInsteadOfAssertTrue(tab :java!Package) {
}
}
--------------------------------------------- ExcessiveImports ---------------------------------------------
-- creates a new Measure when a class has more than 4 imports
rule MeasureExcessiveImports(i : java!CompilationUnit) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'ExcessiveImports',
shortDescription <- 'A high number of imports can indicate a high degree of coupling within an object. This rule counts the number of unique imports and reports a violation if the count is above the user-specified threshold.'
),
measurement: smm!DirectMeasurement (
error<-'The class '+ i.name + ' has an excessive imports.',
value<- i.imports.size()
)
do {
noc;
}
}
......@@ -112,4 +112,12 @@ helper def: signatureDeclareThrowsException() : Set (smm!Measure) =
helper def: avoidThrowingNullPointerException() : Set(smm!Measure) =
java!ClassInstanceCreation.allInstances()
->select(c | c.method.name = 'NullPointerException')
->collect(c | thisModule.MeasureAvoidThrowingNullPointerException(c));
\ No newline at end of file
->collect(c | thisModule.MeasureAvoidThrowingNullPointerException(c));
--------------------------------------------- ExcessiveImports ---------------------------------------------
-- Helper for issue ExcessiveImports : return a Measure for each class that has too many imports.
helper def: excessiveImports() : Set(smm!Measure) =
java!CompilationUnit.allInstances()
-> select (i | i.imports <> OclUndefined)
-> select (i | i.imports.size() > 4)
-> collect(i | thisModule.MeasureExcessiveImports(i));
\ No newline at end of file
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