design.atl 4.13 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
library design;

-- Rule for metrics TooManyFields : return the set of class Measures that violates the rule.
-- To test, example_toomanyfields.xmi, two classe must violates the rule.
helper def: tooManyFields() : Set(smm!Measure) =
	-- Browse through all class 
	java!ClassDeclaration.allInstances() -> reject(each | each.isProxy())->iterate(i; res : Set(smm!Measure) = Set{} |
		-- Add a new measurement if there are more than 15 fields in the class.
		if thisModule.numberFieldsInClasse(i) >15
		then res->union(Set{thisModule.MesureTooManyFields(i)})
		else res
		endif
	);

--------------------------------------------- TooManyMethods ---------------------------------------------

-- Rule for metrics TooManyMethods : return the set of class Measures that violates the rule.
helper def: tooManyMethods() : Set(smm!Measure) =
	-- Browse through all class 
20
	java!ClassDeclaration.allInstances()->reject(each | each.isProxy())->iterate(i; res : Set(smm!Measure) = Set{} |
21
		-- Add a new measurement if there are more than 10 methods in the class.
22
23
		if i.bodyDeclarations->select(r | r.oclIsTypeOf(java!MethodDeclaration))->reject(without |
			without.name.startsWith('get') or without.name.startsWith('set') or without.name.startsWith('is'))->size() > 10
24
25
26
		then res->union(Set{thisModule.MesureTooManyMethods(i)})
		else res
		endif
Gerson Sunyé's avatar
Gerson Sunyé committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
	);

--------------------------------------------- ReturnFromFinallyBlock ---------------------------------------------
helper def: returnFromFinallyBlock() : Set(smm!Measure) =
	java!MethodDeclaration.allInstances() ->iterate(method; methodRes : Set(smm!Measure) = Set{} |
		if method.body <> OclUndefined
		then method.body.statements -> iterate(methodStatement; methodStatementRes : Set(smm!Measure) = Set{} |
			if methodStatement.oclIsTypeOf(java!TryStatement)
			then if methodStatement.finally <> OclUndefined
				then methodStatement.finally.statements ->iterate(finallyStatement; finallyStatementRes : Set(smm!Measure) = Set{} |
					if finallyStatement.oclIsTypeOf(java!ReturnStatement)
					then methodRes->union(Set{thisModule.MesureReturnFromFinallyBlock(method)})
					else methodRes
					endif
				)
				else methodRes
				endif
			else methodRes
			endif
		)
		else methodRes
		endif
	);

51
52
53
54
55
56
57
58
59
60
61
62
--------------------------------------------- returnEmptyArrayRatherThanNull ---------------------------------------------
-- Helper for issue ReturnEmptyArrayRatherThanNull : returns a Measure for each method that returns an array rather than null
helper def: returnEmptyArrayRatherThanNull() : Set(smm!Measure) =
	java!MethodDeclaration.allInstances() 
	-> select (method | method.returnType <> OclUndefined and method.body <> OclUndefined)
	-> select (method | method.returnType.type.oclIsTypeOf(java!ArrayType))
	-> collect(method | method.body.statements)	
	-> iterate(statements; statementRes : Set(smm!Measure) = Set{} |
		statements 
		-> select (statement | statement.oclIsTypeOf(java!ReturnStatement) and statement.expression.oclIsTypeOf(java!NullLiteral))
		-> collect(statement | thisModule.MesureReturnEmptyArrayRatherThanNull(statement))
	);
63
64
65
66
67
68
69
70

--------------------------------------------- ExcessiveParameterList ---------------------------------------------
-- Helper for issue ExcessiveParameterList : return a Measure for each method that has too many parameters.
helper def: excessiveParameterList() : Set(smm!Measure) =
	java!MethodDeclaration.allInstances() 
	-> select (method | method.body <> OclUndefined and method.parameters.size() > 10)
	-> collect(method | thisModule.MesureExcessiveParameterList(method));

71
72
73
74
75
76
77
78
79
80
81
82
83
84

--------------------------------------------- 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))
	;