Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Yannis LE BARS
Source Code Analyzer
Commits
7e87790d
Commit
7e87790d
authored
Dec 13, 2019
by
E19C506H
Browse files
AvoidCatchingNPE
parent
54d2ed2b
Changes
3
Hide whitespace changes
Inline
Side-by-side
input/avoid-catching-npe.xmi
0 → 100644
View file @
7e87790d
<?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=
"Test"
>
<ownedElements
name=
"(default package)"
>
<ownedElements
xsi:type=
"java:ClassDeclaration"
originalCompilationUnit=
"//@compilationUnits.0"
name=
"Foo"
>
<modifier
visibility=
"public"
/>
<bodyDeclarations
xsi:type=
"java:MethodDeclaration"
originalCompilationUnit=
"//@compilationUnits.0"
name=
"bar"
>
<modifier/>
<body
originalCompilationUnit=
"//@compilationUnits.0"
>
<statements
xsi:type=
"java:TryStatement"
originalCompilationUnit=
"//@compilationUnits.0"
>
<body
originalCompilationUnit=
"//@compilationUnits.0"
>
<comments
xsi:type=
"java:LineComment"
originalCompilationUnit=
"//@compilationUnits.0"
content=
"// do something"
enclosedByParent=
"true"
/>
</body>
<catchClauses
originalCompilationUnit=
"//@compilationUnits.0"
>
<exception
originalCompilationUnit=
"//@compilationUnits.0"
name=
"npe"
>
<modifier/>
<type
type=
"//@ownedElements.1/@ownedPackages.0/@ownedElements.0"
/>
</exception>
<body
originalCompilationUnit=
"//@compilationUnits.0"
/>
</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=
"NullPointerException"
proxy=
"true"
usagesInTypeAccess=
"//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.0/@catchClauses.0/@exception/@type"
>
<superClass
type=
"//@ownedElements.1/@ownedPackages.0/@ownedElements.1"
/>
</ownedElements>
<ownedElements
xsi:type=
"java:ClassDeclaration"
name=
"RuntimeException"
proxy=
"true"
usagesInTypeAccess=
"//@ownedElements.1/@ownedPackages.0/@ownedElements.0/@superClass"
>
<superClass
type=
"//@ownedElements.1/@ownedPackages.0/@ownedElements.2"
/>
</ownedElements>
<ownedElements
xsi:type=
"java:ClassDeclaration"
name=
"Exception"
proxy=
"true"
usagesInTypeAccess=
"//@ownedElements.1/@ownedPackages.0/@ownedElements.1/@superClass"
>
<superClass
type=
"//@ownedElements.1/@ownedPackages.0/@ownedElements.3"
/>
</ownedElements>
<ownedElements
xsi:type=
"java:ClassDeclaration"
name=
"Throwable"
proxy=
"true"
usagesInTypeAccess=
"//@ownedElements.1/@ownedPackages.0/@ownedElements.2/@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.3/@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"
usagesInTypeAccess=
"//@ownedElements.0/@ownedElements.0/@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=
"Foo.java"
originalFilePath=
"D:\workSpace\Test\src\Foo.java"
commentList=
"//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.0/@body/@comments.0"
types=
"//@ownedElements.0/@ownedElements.0"
/>
</java:Model>
src/main/atl/analysis.atl
View file @
7e87790d
...
...
@@ -53,6 +53,7 @@ helper def: allMeasures(project: java!Model): Set(smm!Measure) =
thisModule.ExcessiveClassLength(),
thisModule.CloneMethodMustBePublic(),
thisModule.AvoidEnumAsIdentifier(),
thisModule.AvoidCatchingNPE(),
-- Design rules
--
thisModule.tooManyFields(),
...
...
@@ -1288,4 +1289,25 @@ rule MeasureAvoidEnumAsIdentifier(w: java!VariableDeclaration) {
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 <- 'AvoidCatchingNPE'
),
measurement: smm!DirectMeasurement (
error <- w.originalCompilationUnit.name+ ' in this class there is a Code that Code should never
throw NullPointerExceptions under normal circumstances. A catch block may hide the
original error, causing other, more subtle problems later on.'
)
do {
noc;
}
}
\ No newline at end of file
src/main/atl/codestyle.atl
View file @
7e87790d
...
...
@@ -110,5 +110,11 @@ helper def: AvoidEnumAsIdentifier() : Set(smm!Measure) =
->collect(w | thisModule.MeasureCloneMethodMustBePublic(w));
-- Rule for metrics AvoidCatchingNPE : return the set of class Measures that violates the rule.
helper def: AvoidCatchingNPE() : Set(smm!Measure) =
java!CatchClause.allInstances()
->select (w | w.exception.type.type.name = 'NullPointerException')
->collect(w | thisModule.MeasureAvoidCatchingNPE(w));
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment