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

Annotations: Simplify value retrieval

* The intended way to obtain annotation values by providers requires
  lots of boilerplate code.
* Provide a util method therefore.
* Remove the exception marker for the getAnnotatedValue method as it
  has not been used since ~3 years...

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


Signed-off-by: default avatarAlexander Diewald <diewald@fortiss.org>
parent 2d2909e2
No related branches found
No related tags found
1 merge request!57Annotations: Simplify value retrieval
AnnotationInstSpec.java b4f2ed47a8984e751e04049de5bdb3cad2c0a933 GREEN
DerivedAnnotationValueProviderBase.java 15da44b7b92b7fd351aa48422ff5957a2ce34e35 GREEN
DerivedAnnotationValueProviderBase.java afedd21d3469127bbb20adb34c191b5c9c980f6c YELLOW
EStructuralFeatureDescriptor.java 2e14df3830d854bc1693382727b2033b23d0051c GREEN
EStructuralFeatureValueProviderBase.java 71c418486bc6c46bc468e18c92b21f6b537706db GREEN
IAnnotationValueProvider.java d093cb522e7c484420331f0c77690bebe7e131b4 GREEN
EStructuralFeatureValueProviderBase.java 287facbbce47c16d892bae82a214f64ceeef2263 YELLOW
IAnnotationValueProvider.java 08e0e5f66dc97865e9ac03e1ac646af332845e14 YELLOW
ValueProviderBase.java e4e866840845346ec99a4304048f5327c4890996 GREEN
......@@ -98,7 +98,7 @@ public abstract class DerivedAnnotationValueProviderBase<T extends IDerivedAnnot
/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override
public <U> U getAnnotationValue(T specification) throws Exception {
public <U> U getAnnotationValue(T specification) {
U calculatedValue = (U)specification.getValue();
if(calculatedValue != null) {
return calculatedValue;
......
......@@ -280,7 +280,7 @@ public abstract class EStructuralFeatureValueProviderBase<T extends IAnnotatedSp
/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override
public <U> U getAnnotationValue(T specification) throws Exception {
public <U> U getAnnotationValue(T specification) {
return (U)specification
.eGet(getEStructuralFeatureDescriptor().getEStructuralFeature(specification));
}
......
......@@ -64,10 +64,10 @@ public interface IAnnotationValueProvider<T extends IAnnotatedSpecification>
public class AnnotationActionEntry {
/** Scope defining the {@link AnnotationEntry}s to apply the corresponding action to. */
public enum ActionScope {
/** All {@link AnnotationEntry}s that are matching the selected filter. */
ALL_VISIBLE_ITEMS,
/** Apply the action only to the selected item. */
SINGLE_ITEM
/** All {@link AnnotationEntry}s that are matching the selected filter. */
ALL_VISIBLE_ITEMS,
/** Apply the action only to the selected item. */
SINGLE_ITEM
}
/** Name of the associated action, i.e. its entry. */
......@@ -292,7 +292,7 @@ public interface IAnnotationValueProvider<T extends IAnnotatedSpecification>
public boolean canEdit(T specification);
/** Returns the value of the annotation. */
public <U> U getAnnotationValue(T specification) throws Exception;
public <U> U getAnnotationValue(T specification);
/**
* Sets a new value for the annotation.
......
AngleUtils.java 462551eae71738ff51f92c9906bff9a21a375d2b GREEN
AnnotationUtils.java 41e25bcdd2906f7fa511223a20cbd09a1391a23c GREEN
AnnotationUtils.java 1da750d19d92658b0bdf6bc18ceb18aa667e557b YELLOW
BaseMathUtils.java 65f6c39b641cba3c984a38f42b1bbf7dbf3287a3 GREEN
BaseModelElementUtils.java b8775b7a462efc168cf79a017aa3377a782d10f6 GREEN
ConstraintsBaseUtils.java bba938b43756ce6f35c338f6cef21c3ab5d49a9d GREEN
......
......@@ -85,6 +85,24 @@ public class AnnotationUtils {
modelElement.getSpecifications().add(newAnnotation);
}
/**
* Returns the value of type {@code U} specified for the given {@link IModelElement} by the
* given annotation {@code clazz}.
*/
public static <T extends IAnnotatedSpecification, U> U
getAnnotationValue(IModelElement modelElement, Class<T> clazz, Class<U> valueType) {
IAnnotationValueProvider<T> annValProvider =
getAnnotationValueProvider(modelElement, clazz);
T annotation = getAnnotation(modelElement, clazz);
U value = annValProvider.getAnnotationValue(annotation);
if(value != null && !valueType.isAssignableFrom(value.getClass())) {
throw new IllegalArgumentException("The value of the annotation " +
clazz.getSimpleName() + " of the model element " + modelElement +
" is not of the expected type " + valueType.getSimpleName());
}
return value;
}
/**
* <p>
* Returns the annotation of a given {@code Class<T>} from a given {@code modelElement}. If the
......@@ -150,10 +168,11 @@ public class AnnotationUtils {
* Annotation type.
* @return The corresponding value provider or {@code null} if none is found.
*/
public static IAnnotationValueProvider<IAnnotatedSpecification> getAnnotationValueProvider(
IModelElement element, Class<IAnnotatedSpecification> annotationType) {
@SuppressWarnings("unchecked")
public static <T extends IAnnotatedSpecification> IAnnotationValueProvider<T>
getAnnotationValueProvider(IModelElement element, Class<T> annotationType) {
AnnotationEntry annEntry = AnnotationValueService.getInstance().getAnnotationEntry(element);
return annEntry.getAnnotationValueProvider(annotationType);
return (IAnnotationValueProvider<T>)annEntry.getAnnotationValueProvider(annotationType);
}
/**
......
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