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

SafetyUtils.getParentsWithType() --> EcoreUtils.getParentsWithType()

SafetyUtils.getParentSpecificationsWithType() --> AnnotationUtils.getParentAnnotationsWithType()

AnnotationUtils: Add TODO to remove workaround that should be no longer required after IDE dev tools update.
refs 2171
parent 8af1054a
No related branches found
No related tags found
No related merge requests found
......@@ -18,15 +18,23 @@ $Id$
package org.fortiss.tooling.base.utils;
import static org.fortiss.tooling.kernel.utils.EcoreUtils.getEClassForClass;
import static org.fortiss.tooling.kernel.utils.EcoreUtils.getParentsWithType;
import static org.fortiss.tooling.kernel.utils.EcoreUtils.pickFirstInstanceOf;
import java.util.List;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
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;
// TODO: Some of the methods in this class also apply to {@link IModelElementSpecification}s, not only
// {@link IAnnotatedSpecification}s.
/**
* Utility methods for dealing with {@link IAnnotatedSpecification}s.
*
* @author diewald
* @author $Author$
......@@ -35,14 +43,16 @@ import org.fortiss.tooling.base.model.element.IModelElement;
*/
public class AnnotationUtils {
// TODO This function is a workaround which might no longer be required. Before the IDE dev
// tools update on 2014/12/01, the EMF code generator failed when generic abstract base
// classes are referenced from another ecore file.
/**
* Wrapper for returning derived values of {@link IDerivedAnnotation}s. If the user input is
* preferred by the concrete annotation, this function returns the user input if any is set.
* Otherwise it determines the calculated value, if the value can be calculated. If the value
* cannot be calculated, and a user input is given which is not preferred it is returned
* instead. If none of the previously mentioned cases applies, null is returned.
* This function is a workaround, since the EMF code generator fails when generic abstract base
* classes are referenced from another ecore file.
*
* @param annotation
* The annotation to which this wrapper shall be applied to.
......@@ -94,4 +104,25 @@ public class AnnotationUtils {
return null;
}
/**
* Returns the {@link IAnnotatedSpecification}s matching the type 'T' of
* the parents of the given {@link IModelElement}.
*
* @param startElement
* The {@link IModelElement} to start the search from.
* @param clazz
* The {@link IAnnotatedSpecification}s to be searched for.
* @return {@link EList} of found annotations.
*/
public static <T extends IAnnotatedSpecification> EList<T> getParentAnnotationsWithType(
IModelElement startElement, Class<T> clazz) {
EList<T> parentSpecificationList = new BasicEList<T>();
List<IModelElement> parentsElements = getParentsWithType(startElement, IModelElement.class);
for(IModelElement currentParentElement : parentsElements) {
parentSpecificationList.add(pickFirstInstanceOf(clazz,
currentParentElement.getSpecifications()));
}
return parentSpecificationList;
}
}
......@@ -217,6 +217,32 @@ public class EcoreUtils {
return children;
}
/**
* Returns a list of all parent {@link EObject}s that have the type 'T'. If
* none are found, an empty list is returned.
*
* @param startElement
* The EObject from which the "upwards" search shall begin.
* @param type
* The class type which is used to filter the search.
* @return List of parent {EObject}s of type 'T'.
*/
@SuppressWarnings("unchecked")
public static <T extends EObject> EList<T> getParentsWithType(EObject startElement,
Class<T> type) {
EList<T> parentElements = new BasicEList<T>();
EObject currentParent = startElement;
while(currentParent != null) {
currentParent = currentParent.eContainer();
if(type.isAssignableFrom(currentParent.getClass())) {
parentElements.add((T)currentParent);
}
}
return parentElements;
}
/**
* Posts a {@link Notification} with an event type ID outside the EMF
* default notification IDs to cause UI refreshes.
......
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