multithreading.atl 2.27 KB
Newer Older
Gerson Sunyé's avatar
Gerson Sunyé committed
1
2
3
4
5
6
7
library multithreading;

--------------------------------------------- DontCallThreadRun ---------------------------------------------

--Goes through all the methods to check if the following rule is followed:
--Don't call Thread.run()
helper def: dontCallThreadRun() : Set(smm!Measure) =
8
9
10
11
12
13
14
15
16
17
18
19
		java!MethodInvocation.allInstances()
			 -> select (m | m.method.name='run' )
			 -> select (m | m.expression.isDontCallThreadRun() )
			 -> collect (m | thisModule.MeasureDontCallThreadRun(m));

--Detect a wrong usage of the method Thread.run()
--return true if run() is called on a Thread variable
helper context java!SingleVariableAccess def : isDontCallThreadRun(): Boolean =
	--check if the method is called like this: 
	--t.run() where t is of type Thread
	self.variable.variablesContainer.type.type.name.toString() = 'Thread';
			
Gerson Sunyé's avatar
Gerson Sunyé committed
20
21

--Detect a wrong usage of the method Thread.run()
22
23
24
25
26
27
28
--return true if run() is called on a Thread instance creation
helper context java!ClassInstanceCreation def : isDontCallThreadRun(): Boolean =
	--check if the method run is called like this:
	--new Thread().run();
	self.method.name.toString() = 'Thread';


Gerson Sunyé's avatar
Gerson Sunyé committed
29
30
31
32
33
34

--------------------------------------------- AvoidThreadGroup  ---------------------------------------------

--Goes through all the variables to check if the following rule is followed:
--Avoid using java.lang.ThreadGroup;
helper def: avoidThreadGroup() : Set(smm!Measure) =
35
36
37
38
    java!VariableDeclarationFragment.allInstances() 
		-> select(i | i.variablesContainer.type.toString() <> 'OclUndefined')
		-> select(s|s.variablesContainer.type.type.name.toString() = 'ThreadGroup')
		-> collect(i | thisModule.measureAvoidThreadGroup(i) );
39
40
41
42
43
44
45
46
47
48
49
50
	
--------------------------------------------- UseNotifyAllInsteadOfNotify ---------------------------------------------

-- Returns a set of measures each time the method 'notify()' is used instead of 'notifyAll()'
helper def: useNotifyAllInsteadOfNotify(): Set(smm!Measure) =
	
	-- Get the method declarations of notify() and for each of their MethodInvocation usages, create a measure
	java!MethodDeclaration.allInstances()
		->select(methodDeclaration | methodDeclaration.name = 'notify')
		->collect (methodDeclaration |methodDeclaration.usages
			->collect(methodInvocation | thisModule.MeasureUseNotifyAllInsteadOfNotify(methodInvocation)))
;