Java Code Examples for org.eclipse.xtext.common.types.JvmAnnotationReference#setAnnotation()

The following examples show how to use org.eclipse.xtext.common.types.JvmAnnotationReference#setAnnotation() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JvmTypesBuilder.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Creates and returns an annotation reference of the given annotation type's name and the given value.
 * 
 * @param sourceElement
 *            the source element to associate the created element with.
 * @param annotationTypeName
 *            the type name of the created annotation.
 * @param value
 *            the value of the annotation reference. Can be <code>null</code> if the reference doesn't have any value.
 *            
 * @return a result representing an annotation reference to the given annotation type, <code>null<code> if 
 * 		sourceElement or annotationType are <code>null</code>.
 * 
 * @deprecated use {@link JvmAnnotationReferenceBuilder#annotationRef(String, String...)} instead
 */
//TODO Move up the code used in Xtend's CompilationUnitImpl so we can reuse it here.
/* @Nullable */ 
@Deprecated
public JvmAnnotationReference toAnnotation(/* @Nullable */ EObject sourceElement, /* @Nullable */ String annotationTypeName, /* @Nullable */ Object value) {
	JvmAnnotationReference result = typesFactory.createJvmAnnotationReference();
	JvmType jvmType = references.findDeclaredType(annotationTypeName, sourceElement);
	if (jvmType == null) {
		throw new IllegalArgumentException("The type "+annotationTypeName +" is not on the classpath.");
	}
	if (!(jvmType instanceof JvmAnnotationType)) {
		throw new IllegalArgumentException("The given class " + annotationTypeName + " is not an annotation type.");
	}
	result.setAnnotation((JvmAnnotationType) jvmType);
	if (value != null) {
		if (value instanceof String) {
			JvmStringAnnotationValue annotationValue = typesFactory.createJvmStringAnnotationValue();
			annotationValue.getValues().add((String) value);
			result.getExplicitValues().add(annotationValue);
		}
	}
	return result;
}
 
Example 2
Source File: JvmAnnotationReferenceBuilder.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Creates and returns an annotation reference of the given annotation type's name and the given value.
 * 
 * @param annotationTypeName
 *            the type name of the created annotation.
 * @param values
 *            the string value of the annotation's 'values' property.
 *            
 * @return a result representing an annotation reference to the given annotation type, <code>null<code> if 
 * 		annotationType are <code>null</code>.  
 */
//TODO Move up the code used in Xtend's CompilationUnitImpl so we can reuse it here.
//TODO Support other types and setting non default properties
/* @Nullable */ 
public JvmAnnotationReference annotationRef(/* @Nullable */ String annotationTypeName, /* @Nullable */ String... values) {
	if (context == null || annotationTypeName == null)
		return null;
	JvmAnnotationReference result = typesFactory.createJvmAnnotationReference();
	JvmType jvmType = references.findDeclaredType(annotationTypeName, context);
	if (jvmType == null) {
		throw new IllegalArgumentException("The type "+annotationTypeName +" is not on the classpath.");
	}
	if (!(jvmType instanceof JvmAnnotationType)) {
		throw new IllegalArgumentException("The given class " + annotationTypeName + " is not an annotation type.");
	}
	result.setAnnotation((JvmAnnotationType) jvmType);
	if (values != null && values.length>0) {
		JvmStringAnnotationValue annotationValue = typesFactory.createJvmStringAnnotationValue();
		for (String value : values) {
			annotationValue.getValues().add(value);
		}
		result.getExplicitValues().add(annotationValue);
	}
	return result;
}
 
Example 3
Source File: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @since 2.4
 */
protected JvmAnnotationReference createAnnotationReference(/* @NonNull */ IAnnotationBinding annotation) {
	JvmAnnotationReference annotationReference = TypesFactory.eINSTANCE.createJvmAnnotationReference();
	ITypeBinding annotationType = annotation.getAnnotationType();
	annotationReference.setAnnotation(createAnnotationProxy(annotationType));
	InternalEList<JvmAnnotationValue> values = (InternalEList<JvmAnnotationValue>)annotationReference.getExplicitValues();
	IMemberValuePairBinding[] allMemberValuePairs = annotation.getDeclaredMemberValuePairs();
	for (IMemberValuePairBinding memberValuePair : allMemberValuePairs) {
		IMethodBinding methodBinding = memberValuePair.getMethodBinding();
		if (methodBinding != null) {
			try {
				values.addUnique(createAnnotationValue(annotationType, memberValuePair.getValue(), methodBinding));
			} catch(NullPointerException npe) {
				// memberValuePair#getValue may throw an NPE if the methodBinding has no return type
				if (methodBinding.getReturnType() != null) {
					throw npe;
				} else {
					if (log.isDebugEnabled()) {
						log.debug(npe.getMessage(), npe);
					}
				}
			}
		}
	}
	return annotationReference;
}
 
Example 4
Source File: AnnotationReferenceProviderImpl.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected JvmAnnotationReference createJvmAnnotationReference(final JvmType type) {
  JvmAnnotationReference _switchResult = null;
  boolean _matched = false;
  if (type instanceof JvmAnnotationType) {
    _matched=true;
    JvmAnnotationReference _createJvmAnnotationReference = TypesFactory.eINSTANCE.createJvmAnnotationReference();
    final Procedure1<JvmAnnotationReference> _function = (JvmAnnotationReference it) -> {
      it.setAnnotation(((JvmAnnotationType)type));
    };
    _switchResult = ObjectExtensions.<JvmAnnotationReference>operator_doubleArrow(_createJvmAnnotationReference, _function);
  }
  if (!_matched) {
    _switchResult = null;
  }
  return _switchResult;
}
 
Example 5
Source File: JvmTypesBuilder.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Translates a single {@link XAnnotation} to {@link JvmAnnotationReference} that can be added to a {@link JvmAnnotationTarget}.
 * 
 * @param anno the source annotation
 * 
 * @return a {@link JvmAnnotationReference} that can be attached to some {@link JvmAnnotationTarget}
 */
/* @Nullable */ 
public JvmAnnotationReference getJvmAnnotationReference(/* @Nullable */ XAnnotation anno) {
	if(anno == null)
		return null;
	JvmAnnotationReference reference = typesFactory.createJvmAnnotationReference();
	final JvmType annotation = (JvmType) anno.eGet(
			XAnnotationsPackage.Literals.XANNOTATION__ANNOTATION_TYPE, false);
	if (annotation.eIsProxy()) {
		JvmAnnotationType copiedProxy = TypesFactory.eINSTANCE.createJvmAnnotationType();
		((InternalEObject)copiedProxy).eSetProxyURI(EcoreUtil.getURI(annotation));
		reference.setAnnotation(copiedProxy);
	} else if (annotation instanceof JvmAnnotationType){
		reference.setAnnotation((JvmAnnotationType) annotation);
	}
	for (XAnnotationElementValuePair val : anno.getElementValuePairs()) {
		XExpression valueExpression = val.getValue();
		JvmAnnotationValue annotationValue = toJvmAnnotationValue(valueExpression);
		if (annotationValue != null) {
			JvmOperation op = (JvmOperation) val.eGet(
					XAnnotationsPackage.Literals.XANNOTATION_ELEMENT_VALUE_PAIR__ELEMENT, false);
			annotationValue.setOperation(op);
			reference.getExplicitValues().add(annotationValue);
		}
	}
	if (anno.getValue() != null) {
		JvmAnnotationValue value = toJvmAnnotationValue(anno.getValue());
		if (value != null) {
			reference.getExplicitValues().add(value);
		}
	}
	associate(anno, reference);
	return reference;
}
 
Example 6
Source File: AnnotationLookup.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public JvmAnnotationReference findOrAddAnnotation(/* @NonNull */ JvmAnnotationTarget annotationTarget, /* @NonNull */ Notifier context, /* @NonNull */ Class<? extends Annotation> type) {
	JvmAnnotationReference result = findAnnotation(annotationTarget, type);
	if (result != null)
		return result;
	JvmAnnotationType annotationType = findAnnotationType(type, context);
	if (annotationType != null) {
		result = typesFactory.createJvmAnnotationReference();
		result.setAnnotation(annotationType);
		return result;
	}
	return null;
}