Skip to content
Snippets Groups Projects
Commit dadda0ef authored by Alexander Diewald's avatar Alexander Diewald
Browse files

Annotations: Remove optional instantiation during retrieval

The migration service takes care to create annotations on demand when
projects are loaded. Moreover, compositors also consider annotation
instantiation already. Thus, we should consolidate the code now right
after the release.

Issue-Ref: 4008
Issue-Url: https://af3-developer.fortiss.org/issues/4008


Signed-off-by: default avatarAlexander Diewald <diewald@fortiss.org>
parent eaa578de
No related branches found
No related tags found
1 merge request!117Test case generator for DSE problems
AngleUtils.java 462551eae71738ff51f92c9906bff9a21a375d2b GREEN
AnnotationUtils.java ea90ec75a20df3a616f21611f1b0bce165795cfe YELLOW
AnnotationUtils.java 58a2a4706b9d2823022e8278fc9a9cd2692d68f8 YELLOW
BaseMathUtils.java 65f6c39b641cba3c984a38f42b1bbf7dbf3287a3 GREEN
BaseModelElementUtils.java b8775b7a462efc168cf79a017aa3377a782d10f6 GREEN
DimensionUtils.java 0dc67f9de11a84e6e4c6e1eb627817dee91ff30a GREEN
......
......@@ -17,7 +17,6 @@ package org.fortiss.tooling.base.utils;
import static org.fortiss.tooling.base.utils.BaseMathUtils.convertNumber;
import static org.fortiss.tooling.kernel.utils.EcoreUtils.getChildrenWithType;
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 static org.fortiss.tooling.kernel.utils.EcoreUtils.pickInstanceOf;
......@@ -30,10 +29,8 @@ import java.util.stream.DoubleStream;
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.EClassifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.fortiss.tooling.base.ToolingBaseActivator;
import org.fortiss.tooling.base.annotation.AnnotationEntry;
import org.fortiss.tooling.base.annotation.AnnotationValueService;
......@@ -51,8 +48,6 @@ import org.fortiss.tooling.base.annotation.valueprovider.IAnnotationValueProvide
import org.fortiss.tooling.base.compose.HierarchicElementCompositorBase;
import org.fortiss.tooling.base.model.element.IAnnotatedSpecification;
import org.fortiss.tooling.base.model.element.IModelElement;
import org.fortiss.tooling.kernel.extension.data.ITopLevelElement;
import org.fortiss.tooling.kernel.service.IPersistencyService;
/**
* Utility methods for dealing with {@link IAnnotatedSpecification}s.
......@@ -60,31 +55,6 @@ import org.fortiss.tooling.kernel.service.IPersistencyService;
* @author diewald
*/
public class AnnotationUtils {
/**
* Returns the annotation of the particular {@code clazz} from a given {@code modelElement}.
*/
private static <T extends IAnnotatedSpecification> T
getAnnotationInternal(final IModelElement modelElement, Class<T> clazz) {
return pickFirstInstanceOf(clazz, modelElement.getSpecifications());
}
/**
* Creates an annotation of a particular {@code eClass} and adds it the specification list of
* the {@code modelElement}.
*/
@SuppressWarnings("unchecked")
private static <T extends IAnnotatedSpecification> void
createAnnotationInternal(final IModelElement modelElement, Class<T> clazz) {
final EClass eClass = getEClassForClass(clazz);
if(eClass == null) {
return;
}
final T newAnnotation = (T)EcoreUtil.create(eClass);
modelElement.getSpecifications().add(newAnnotation);
}
/**
* Returns the value of type {@code U} specified for the given {@link IModelElement} by the
......@@ -126,59 +96,10 @@ public class AnnotationUtils {
return as.getAnnotationEntry(modelElement).getSpecification(type) != null;
}
/**
* <p>
* 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}.
* </p>
*
* <p>
* <b>Note: </b>The access to the model is wrapped into a command in order to allow for
* transparent creation of the annotation object from other contexts (e.g., GUI). If this is not
* desirable, use the {@link #getAnnotation(IModelElement, Class, boolean)} variant of this
* method.
* </p>
*/
public static <T extends IAnnotatedSpecification> T getAnnotation(IModelElement modelElement,
Class<T> clazz) {
return getAnnotation(modelElement, clazz, true);
}
/**
* 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}. Depending on the caller's choice, the model modification is wrapped
* into a command.
*/
public static <T extends IAnnotatedSpecification> T getAnnotation(
final IModelElement modelElement, final Class<T> clazz, boolean wrapIntoCommand) {
T annotation = getAnnotationInternal(modelElement, clazz);
if(annotation != null) {
return annotation;
}
if(wrapIntoCommand) {
ITopLevelElement modelContext =
IPersistencyService.getInstance().getTopLevelElementFor(modelElement);
modelContext.runAsCommand(() -> {
// TODO (see #2228 / #2208) Keep this code that ensures that non-existing
// annotations are lazily instantiated until #2228 is resolved. It should be
// noted that each time an annotation is constructed from this context, an
// undo/redo command will be created that is not comprehensible for the user
// (see #2208).
createAnnotationInternal(modelElement, clazz);
});
} else {
// Create if the creation does not need to be wrapped into a command.
createAnnotationInternal(modelElement, clazz);
}
// Return new annotation (be it created in a command, or directly). This will return null in
// case the creation of the new annotation was not successful.
return getAnnotationInternal(modelElement, clazz);
/** Returns the annotation of a given {@code Class<T>} from a given {@code modelElement}. */
public static <T extends IAnnotatedSpecification> T
getAnnotation(final IModelElement modelElement, final Class<T> clazz) {
return pickFirstInstanceOf(clazz, modelElement.getSpecifications());
}
/**
......
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