bestPractices.atl 1.76 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
		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{} |
32
		-- Foreach FieldDeclaration, check all methods
33
34
		i.bodyDeclarations->iterate(field; resIter : Set(smm!Measure) = Set{} | 
		if field.oclIsTypeOf(java!FieldDeclaration)
35
36
37
38
		then i.bodyDeclarations
			-- Check if the name method and the name field is the same
			->select(method | method.oclIsTypeOf(java!MethodDeclaration) and method.name = field.fragments.first().name)
			->collect(method | thisModule.MesureAvoidFieldNameMatchingMethodName(i, method))
39
40
41
42
		else res
		endif
	)
);