bestPractices.atl 1.84 KB
Newer Older
Raphael PAGE's avatar
Raphael PAGE committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
library bestPractices;

--------------------------------------------- AvoidPrintStackTrace ---------------------------------------------

--Goes through all the methods to check if the following rule is followed:
--Avoid printStackTrace()
helper def: avoidPrintStackTrace() : Set(smm!Measure) =
	java!MethodInvocation.allInstances() -> iterate(i; res : Set(smm!Measure) = Set{} |
		-- Add a new measurement if the argument of String.indexOf() is not of type char when checking for the index of a single character
		if thisModule.isAvoidPrintStackTrace(i) then
			res->union(Set{thisModule.MeasureAvoidPrintStackTrace(i)})
		else 
			res
		endif
	);

--Detect a wrong usage of the method printStackTrace()
--return true if the method is called printStackTrace
helper def:isAvoidPrintStackTrace(s:java!MethodInvocation): Boolean =
		if s.method.name = 'printStackTrace' then
			true
		else
			false
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
		endif;

--------------------------------------------- AvoidFieldNameMatchingMethodName ---------------------------------------------

-- Rule for metrics AvoidFieldNameMatchingMethodName : return the set of class Measures that violates the rule.
helper def: avoidFieldNameMatchingMethodName() : Set(smm!Measure) =
	-- Browse through all class
	java!ClassDeclaration.allInstances()->iterate(i; res : Set(smm!Measure) = Set{} |
		-- Add a new measurement if there are more than 4 static imports in the class.
		i.bodyDeclarations->iterate(field; resIter : Set(smm!Measure) = Set{} | 
		if field.oclIsTypeOf(java!FieldDeclaration)
		then
			i.bodyDeclarations->iterate(method; resFinal : Set(smm!Measure) = Set{} |
				if method.oclIsTypeOf(java!MethodDeclaration)
				then
					if method.name = field.fragments.first().name
					then res->union(Set{thisModule.MesureAvoidFieldNameMatchingMethodName(i, method)})
					else res
					endif
				else res
				endif
			)
		else res
		endif
	)
);