Skip to content
Snippets Groups Projects
Commit 8af1054a authored by Simon Barner's avatar Simon Barner
Browse files

Add <T extends IAnnotatedSpecification> T getAnnotation(IModelElement...

Add <T extends IAnnotatedSpecification> T getAnnotation(IModelElement modelElement, Class<T> clazz),

a method to safely obtain an annotation for a given modelElement (and to create it, if it does not exist yet).
refs 1841
parent a8036a6d
No related branches found
No related tags found
No related merge requests found
......@@ -17,7 +17,14 @@ $Id$
+--------------------------------------------------------------------------*/
package org.fortiss.tooling.base.utils;
import static org.fortiss.tooling.kernel.utils.EcoreUtils.getEClassForClass;
import static org.fortiss.tooling.kernel.utils.EcoreUtils.pickFirstInstanceOf;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.fortiss.tooling.base.model.element.IAnnotatedSpecification;
import org.fortiss.tooling.base.model.element.IDerivedAnnotation;
import org.fortiss.tooling.base.model.element.IModelElement;
/**
*
......@@ -49,4 +56,42 @@ public class AnnotationUtils {
return annotation.getDerivedValue();
}
/**
* Returns the annotation of a given {@link EClass} from a given {@code modelElement}. If the
* annotation does not exist yet, it is created and added to the specification list of the
* {@code modelElement}.
*/
@SuppressWarnings("unchecked")
private static <T extends IAnnotatedSpecification> T getAnnotation(IModelElement modelElement,
EClass eClass) {
T annotation =
(T)pickFirstInstanceOf((Class<IAnnotatedSpecification>)eClass.getInstanceClass(),
modelElement.getSpecifications());
if(annotation == null) {
annotation = (T)EcoreUtil.create(eClass);
modelElement.getSpecifications().add(annotation);
}
return annotation;
}
/**
* Returns the annotation of a given {@code Class<T>} from a given {@code modelElement}. If the
* annotation does not exist yet, it is created and added to the specification list of the
* {@code modelElement}.
*/
public static <T extends IAnnotatedSpecification> T getAnnotation(IModelElement modelElement,
Class<T> clazz) {
EClass eClass = getEClassForClass(clazz);
if(eClass != null) {
// Retrieve (potentially create) annotation
return getAnnotation(modelElement, eClass);
}
return null;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment