performance.atl 11.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
library performance;

--- AddEmptyString
helper def: addEmptyString() : Set(smm!Measure) =
	java!InfixExpression.allInstances()
	->select(expression | expression.operator.name = '+')
	->select(expression | (expression.leftOperand.oclIsTypeOf(java!StringLiteral) and expression.rightOperand.oclIsTypeOf(java!NumberLiteral)) or (expression.leftOperand.oclIsTypeOf(java!NumberLiteral) and expression.rightOperand.oclIsTypeOf(java!StringLiteral)))
	->collect(expression | thisModule.MesureAddEmptyString(expression.originalCompilationUnit))
;
	
--- AvoidInstantiatingObjectInLoops
helper def: avoidInstantiatingObjectsInLoops(): Set(smm!Measure) =
	java!Statement.allInstances()
		->select(s | s.oclIsTypeOf(java!ForStatement) or s.oclIsTypeOf(java!DoStatement) or s.oclIsTypeOf(java!WhileStatement))
		->select(s | s.body.statements
			->select(s2 | s2.oclIsTypeOf(java!VariableDeclarationStatement))
			->select(s2 | s2.fragments->exists(f | f.oclIsTypeOf(java!VariableDeclarationFragment)))
			->exists(s2 | s2.fragments->exists(f | f.initializer.oclIsTypeOf(java!ClassInstanceCreation)))
		)
		->collect(s | thisModule.MeasureAvoidInstantiatingObjectInLoops(s))
;

--- AvoidRethrowingException
-- Rule for metrics AvoidRethrowingException :
helper def: avoidRethrowingException() : Set (smm!Measure) = 
	-- Browse through all try statements
	java!TryStatement.allInstances() ->iterate(tryStatement; res : Set(smm!Measure) = Set{} |
		tryStatement.catchClauses
		->select(catchClause | catchClause.body <> OclUndefined and catchClause.body.statements.size() = 1)
		->collect(catchClause | catchClause.body.statements)
		->iterate(catchStatement; res1 : Set(smm!Measure) = Set{} |
		catchStatement
		->select(catchState | catchState.oclIsTypeOf(java!ThrowStatement))
		->collect(catchState | thisModule.MeasureAvoidRethrowingException(catchState))
		)
	)
;

--- AvoidThrowingNewInstanceOfSameException
-- Returns a set of Measures for each catch block throws a new instance of the caught exception
helper def: avoidThrowingNewInstanceOfSameException() : Set(smm!Measure) =
	java!CatchClause.allInstances() ->iterate(catch; catchRes : Set(smm!Measure) = Set{} |
		if catch.body <> OclUndefined
		then catch.body.statements 
			-> select(catchStatement | catchStatement.oclIsTypeOf(java!ThrowStatement) 
					and catchStatement.expression.oclIsTypeOf(java!ClassInstanceCreation))
			-> select(catchStatement | catchStatement.expression.type.type.name = catch.exception.type.type.name)
			-> collect(catchStatement | catchStatement.expression.arguments)
			-> iterate(arguments; argumentsRes : Set(smm!Measure) = Set{} |
				arguments
				-> select(argument | argument.oclIsTypeOf(java!SingleVariableAccess))
				-> select(argument | argument.variable.name = catch.exception.name)
				-> collect(argument | thisModule.MeasureAvoidThrowingNewInstanceOfSameException(catch))
			)
		else catchRes
		endif
	)
;

--- AvoidUsingShortType
-- Detects the use of the primitive type 'short' for rule AvoidUsingShortType. Prefer the use of 'int' instead of 'short'.
helper def: avoidUsingShortType() : Set(smm!Measure) =
	java!VariableDeclarationStatement.allInstances()
		->select(variable | variable.type.type.name = 'short')
		->collect(variable | thisModule.MeasureAvoidUsingShortType(variable))
;

--- InefficientEmptyStringCheck
helper def: inefficientEmptyStringCheck() : Set(smm!Measure) =
	java!MethodInvocation.allInstances()
		->select(i | i.method.name = 'isEmpty')
		->select(i | i.expression.oclIsTypeOf(java!MethodInvocation))
		->select(i | thisModule.expressionMethodIs(i.expression,'trim'))
		->collect(i | thisModule.MeasureInefficientEmptyStringCheck(i));

helper def: inefficientEmptyStringCheck2() : Set(smm!Measure) =
	java!InfixExpression.allInstances()
		->select(i | i.rightOperand.oclIsTypeOf(java!NumberLiteral))
		->select(i | i.rightOperand.tokenValue = '0')
		->select(i | i.leftOperand.oclIsTypeOf(java!MethodInvocation))
		->select(i | i.leftOperand.method.name = 'length')
		->select(i | i.leftOperand.expression.oclIsTypeOf(java!MethodInvocation))
		->select(i | i.leftOperand.expression.method.name = 'trim')
		->collect(i | thisModule.MeasureInefficientEmptyStringCheck(i));

helper def: expressionMethodIs(expr: java!Expression,str: String) : Boolean =
	expr.method.name = str;
	
--- InsufficientStringBufferDeclaration
helper def: insufficientStringBufferDeclaration() : Set(smm!Measure) =
	java!MethodInvocation.allInstances()
		->select(it| it.method.name = 'append')
		->select(it1| it1.method.abstractTypeDeclaration.name = 'StringBuffer' or it1.method.abstractTypeDeclaration.name = 'StringBuilder')
		->select(it2|thisModule.unsuffisentInisialization(it2.expression, it2.arguments.first()))
		->collect(it2|thisModule.MeasureInsufficientStringBufferDeclaration(it2.originalCompilationUnit))
	;

-- -2 to ignone " at the begin and at end
helper def:unsuffisentInisialization(variable:java!SingleVariableAccess, str:java!StringLiteral): Boolean =
	str.escapedValue.size()-2 > thisModule.FixedSize(variable.variable.initializer);

helper def:FixedSize(variable:java!ClassInstanceCreation): Integer =
	if variable.arguments.size() = 0 then
		16
	else
		if variable.arguments.first().oclIsTypeOf(java!NumberLiteral) then
			thisModule.sizeOf(variable.arguments.first())
		else
			thisModule.sizeOfLitteral(variable.arguments.first())
		endif
	endif;

helper def:sizeOfLitteral(variable:java!StringLiteral): Integer =
	variable.escapedValue.size();

helper def:sizeOf(variable:java!NumberLiteral): Integer =
	variable.tokenValue.toInteger();

--- IntegerInstantiation
-- Rule for metricsIntegerInstantiation : returns the set of class Measures that violates the rule.
helper def: integerInstantiation() : Set(smm!Measure) =
	-- Take all ClassInstanceCreation with allInstances()
	java!ClassInstanceCreation.allInstances()
		-- Select all classes that declare an Integer type
		->select(it|it.method.oclIsTypeOf(java!ConstructorDeclaration) and it.method.name = 'Integer')
		-- collect every result and sends an error message
		->collect(it2|thisModule.MeasureIntegerInstantiation(it2.originalCompilationUnit))
;

--- LongInstantiation
-- Rule for metricsLongInstantiation : return the set of class Measures that violates the rule.
helper def: longInstantiation() : Set(smm!Measure) =
	-- Take all ClassInstanceCreation with allInstances()
	java!ClassInstanceCreation.allInstances()
		-- Select all class who declare a Long type
		->select(it| it.method.oclIsTypeOf(java!ConstructorDeclaration) and it.method.name = 'Long')
		-- collect every results and send an error message
		->collect(it2|thisModule.MeasureLongInstantiation(it2.originalCompilationUnit))
;

--- ShortInstantiation
-- Rule for metrics ShortInstantiation : return the set of class Measures that violates the rule.
helper def: shortInstantiation() : Set(smm!Measure) =
	-- Take all ClassInstanceCreation with allInstances()
	java!ClassInstanceCreation.allInstances()
		-- Select all class who declare a Short type
		->select(it| it.method.oclIsTypeOf(java!ConstructorDeclaration) and it.method.name = 'Short')
		-- collect every results and send an error message
		->collect(it2|thisModule.MeasureShortInstantiation(it2.originalCompilationUnit))
;

--- Start with
helper def: startsWith() : Set(smm!Measure) =
-- Take all MethodInvocation with allInstances()
	java!MethodInvocation.allInstances()
		->select(it| it.method.name= 'startsWith')
		->select(it2| thisModule.isLengthUnder1(it2.arguments.first()))
		->collect(err|thisModule.MeasureStartWith(err))
;

-- <=3 because we have to ignore '"' at begin and at end
helper def:isLengthUnder1(s:java!StringLiteral): Boolean =
	s.escapedValue.size() <= 3
;

--- StringToString
-- Rule for metrics stringToString : returns the set of class Measures that violates the rule.
helper def: stringToString() : Set(smm!Measure) =
    java!MethodInvocation.allInstances()
        -> select( i | i.method.name = 'toString')
		-> select( i | i.expression.oclIsTypeOf(java!SingleVariableAccess))
        -> select( i | i.expression.variable.oclIsTypeOf(java!VariableDeclarationFragment))
        -> select( i | not  i.expression.variable.variablesContainer.type.oclIsUndefined())
        -> select( i | not  i.expression.variable.variablesContainer.type.type.oclIsUndefined())
        -> select( i | i.expression.variable.variablesContainer.type.type.name = 'String')
        -> collect ( i | thisModule.MeasureStringToString(i) )
;

--- TooFewBranchesForASwitchStatement
-- Returns a set of measures for each switch statement that violates the rule : number of switch cases < 3
-- To test, use input model : tooFewBranchesForASwitchStatement.xmi
helper def: tooFewBranchesForASwitchStatement(): Set(smm!Measure) =
	-- If the number of switch cases for the current switch statement is < 3 then add a new measure to the set
	java!SwitchStatement.allInstances()
		-> select (switchStatement | thisModule.nbBranchesOfASwitchStatement(switchStatement) < 3)
		-> collect (switchStatement | thisModule.MeasureTooFewBranchesForASwitchStatement(switchStatement))
;

--- UseArraysAsList
helper def: useArraysAsList() : Set(smm!Measure) =
	java!ForStatement.allInstances()
	->select(st | st.body.statements.size() = 1)
	->select(st | st.body.statements.get(0).expression.method.name = 'add')
	->select(st | st.body.statements.get(0).expression.method.abstractTypeDeclaration.name = 'Collection' 
		or st.body.statements.get(0).expression.method.abstractTypeDeclaration.name = 'List' 
		or st.body.statements.get(0).expression.method.abstractTypeDeclaration.name = 'ArrayList')
	->select(st | st.body.statements.get(0).expression.expression.variable.initializer.type.type.type.type.name = 'ArrayList')
	->select(st | st.body.statements.get(0).expression.arguments.size() = 1
		and st.body.statements.get(0).expression.arguments.get(0).oclIsTypeOf(java!ArrayAccess))
	->select(st | st.body.statements.get(0).expression.arguments.get(0).array.variable.type.type.oclIsTypeOf(java!ArrayType))
	->collect(st | thisModule.MeasureUseArraysAsList(st.originalCompilationUnit))
;
	
--- Use ArrayList Instead Of Vector
helper  def: UseArrayListInsteadOfVector() : Set(smm!Measure) =
	-- Browse through all class(CompilationUnit)
	-- On recupere tout les new Vector et on verifie que le Vector appelé appartient au package java.utile
		java!ConstructorDeclaration.allInstances()->
		collect(i|i.abstractTypeDeclaration)
		->select(p|p.name='Vector')->collect(i|i.package)
		->select(p|p.name='util')->collect(i|i.package)
		->select(p|p.name='java')
		->collect(i |thisModule.RuleUseArrayListInsteadOfVector(i))
;

--- UseIndexOfChar
--Goes through all the MethodInvocation to check if the following rule is followed:
--Use String.indexOf(char) when checking for the index of a single character; it executes faster.
helper def: useIndexOfChar() : Set(smm!Measure) =
		java!MethodInvocation.allInstances()
			 -> select (m | m.method.name='indexOf') 
			 -> select (m | m.arguments.size() = 1)
			 -> select (m | not m.arguments.first().oclIsTypeOf(java!CharacterLiteral))
			 -> collect (m | thisModule.MeasureUseIndexOfChar(m))
;

--- UselessStringValueOf
--Goes through all the methods to check if the following rule is followed:
--No need to call String.valueOf to append to a string; just use the valueOf() argument directly.
helper def: uselessStringValueOf() : Set(smm!Measure) =
		java!MethodInvocation.allInstances()
			 -> select (m | m.method.name='valueOf' )
			 -> select (m | m.refImmediateComposite().oclIsTypeOf(java!InfixExpression))
			 -> collect (m | thisModule.MeasureUselessStringValueOf(m))
;