How to add new model elements in refining mode [message #985534] |
Thu, 15 November 2012 13:28 |
Andreas Brieg Messages: 48 Registered: November 2012 |
Member |
|
|
Since called rules are not yet supported in refining mode, I'm struggling a bit on how to add new elements to the model I'm refining. Specifically I'm talking about the MoDisco Java model http://www.eclipse.org/MoDisco/Java/0.2.incubation/java and I want to add Annotations.
I currently know of two such possibilities. The first one is creating a rule whose input element is of a different type than the output element. For example, if I want to create a Annotation for a method, I could write:
rule addAnnotationToMethod {
from
method : Java!MethodDeclaration
to
methodAnnotation : Java!Annotation (
-- fill in the attributes and references
)
}
However I'm unable to use this, because the methodAnnotation that would be created by this rule is a dangling object. The problem is that the model is missing the inverse relationship of the association from the method declaration to its declared annotations. So even if the annotation would be created by this rule, the method wouldn't be annotated.
The second possibility, which I'm currently using, makes use of newInstance:
rule addNotNullAnnotation {
from
parameterDeclaration: Java!SingleVariableDeclaration
to
modifiedDeclaration: Java!SingleVariableDeclaration (
annotations <- parameterDeclaration.annotations.
including(parameterDeclaration.notNullAnnotation())
)
}
helper context Java!ASTNode def: notNullAnnotation(): Java!Annotation =
Java!Annotation.newInstance().refSetValue('type', self.notNullAnnotationAccess()).
refSetValue('originalCompilationUnit', self.originalCompilationUnit).refSetValue('values', Sequence{});
helper context Java!ASTNode def: notNullAnnotationAccess(): Java!TypeAccess =
Java!TypeAccess.newInstance().refSetValue('type', thisModule.notNullAnnotationType).
refSetValue('originalCompilationUnit', self.originalCompilationUnit);
helper def: notNullAnnotationType: Java!Type =
Java!AnnotationTypeDeclaration.allInstances() -> select(e | e.qualifiedName() =
'javax.validation.constraints.NotNull').first();
However with this method I'm not sure if this will produce valid results in every case. For example what happens, if I will call Java!Annotation.allInstances() . Will the returned sequence also contain the elements that have been created by applying rule addNotNullAnnotation so far? If yes, than results would depend on the execution order of rules, which is undefined behaviour.
Another drawback of this method is that, I'm unable to profile, because the ATL profiler uses toString of model elements. But after calling Java!Annotation.newInstance() the instance isn't fully initialized and the toString method invocation fails with a NPE.
So how should I add new elements in refining mode?
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03411 seconds