SDA SE WikiSoftware Engineering for Smart Data Analytics & Smart Data Analytics for Software Engineering
A strength of JTransformer is that it capable of doing analysis on the source code and transformations. In this section, we are writing a transformation that could be applied to code found by the System.out.println detector from the "Write your own analysis"-Example.
If you want to learn what Conditional Transformations are and how you can use them (run a ct, run a sequence of cts, examine the results of a ct …) please have a look at this page: Conditional Tranformations. If you need more examples for transformations you can have a look at the Tutorial Project 1).
The transformation for our System.out.println detector has to replace the call to System.out.println by a call to a logging method. How the logging method can be detected has to be defined in the condition part of the ct.
:- multifile(user:ct/3). % Dont forget the declaration!
user:ct( replaceSysoutWithLogging(CallId), % HEAD
( % CONDITION
sysout_analysis:sysout_call(CallId,CallParent, Enclosing, FieldAccess, StaticTypeRef, Argument),
% Call the analysis predicate and bind all IDs
get_term(FieldAccess, FieldAccessTerm), % get the terms which should be deleted
get_term(StaticTypeRef, StaticTypeRefTerm),
classT(MyLoggerClass, _, 'MyLogger', _, _), % Get the ID belonging to class 'MyLogger'
methodT(LoggingMethod, MyLoggerClass, println, _, MethodType, _, _, _),
% ... and the ID for the logging method
new_id(NewTypeRef) % NewTypeRef is a yet unused ID
),
( % TRANSFORMATION:
delete(FieldAccessTerm), % delete the field access
delete(StaticTypeRefTerm), % delete the static type reference
replace(callT(CallId, CallParent, Enclosing, NewTypeRef, [Argument], LoggingMethod, [], MethodType))
% replace the existing callT fact with the new one
% (call the logging method instead of System.out.println)
add(staticTypeRefT(NewTypeRef, CallId, Enclosing, MyLoggerClass)),
% add the static type reference to the class "MyLogger"
)
).