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

The following examples show how to use org.eclipse.xtext.common.types.JvmAnnotationReference#getExplicitValues() . 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: LogicalContainerAwareReentrantTypeResolver.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void recordAnnotationExpressions(List<JvmAnnotationReference> annotations) {
	for(JvmAnnotationReference annotation: annotations) {
		EObject sourceElement = getSourceElement(annotation);
		if (sourceElement != annotation) {
			rootedInstances.add(sourceElement);
		} else {
			for(JvmAnnotationValue value: annotation.getExplicitValues()) {
				if (value instanceof JvmCustomAnnotationValue) {
					JvmCustomAnnotationValue custom = (JvmCustomAnnotationValue) value;
					for(Object object: custom.getValues()) {
						if (object instanceof XExpression) {
							rootedInstances.add(sourceElement);
						}
					}
				} else if (value instanceof JvmAnnotationAnnotationValue) {
					recordAnnotationExpressions(((JvmAnnotationAnnotationValue) value).getValues());
				}
			}
		}
	}
}
 
Example 2
Source File: LogicalContainerAwareReentrantTypeResolver.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void computeAnnotationTypes(ResolvedTypes resolvedTypes, IFeatureScopeSession featureScopeSession, List<JvmAnnotationReference> annotations) {
	for(JvmAnnotationReference annotation: annotations) {
		EObject sourceElement = getSourceElement(annotation);
		if (sourceElement != annotation) {
			computeTypes(resolvedTypes, featureScopeSession, sourceElement);
		} else {
			for(JvmAnnotationValue value: annotation.getExplicitValues()) {
				if (value instanceof JvmCustomAnnotationValue) {
					JvmCustomAnnotationValue custom = (JvmCustomAnnotationValue) value;
					for(Object object: custom.getValues()) {
						if (object instanceof XExpression) {
							AnnotationValueTypeComputationState state = new AnnotationValueTypeComputationState(resolvedTypes, featureScopeSession, value, (XExpression) object);
							state.computeTypes();
						}
					}
				} else if (value instanceof JvmAnnotationAnnotationValue) {
					computeAnnotationTypes(resolvedTypes, featureScopeSession, ((JvmAnnotationAnnotationValue) value).getValues());
				}
			}
		}
	}
}
 
Example 3
Source File: JvmAnnotationReferencePrinter.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected String _internalToString(JvmAnnotationReference reference) {
	StringBuilder buffer = new StringBuilder();
	buffer.append("@");
	buffer.append(createLinkWithLabel(XtextElementLinks.XTEXTDOC_SCHEME, EcoreUtil.getURI(reference.getAnnotation()), reference.getAnnotation().getSimpleName()));

	List<JvmAnnotationValue> explicitValues = reference.getExplicitValues();
	boolean needsExplicitProperties = explicitValues.size() > 1 //
			|| (!explicitValues.isEmpty() && explicitValues.get(0).getOperation() != null //
					&& !"value".equals(explicitValues.get(0).getOperation().getSimpleName()));

	if (!explicitValues.isEmpty()) {
		buffer.append("(");
		buffer.append(explicitValues.stream().map(explicitValue -> {
			StringBuilder builder = new StringBuilder();
			if (needsExplicitProperties) {
				Iterable<JvmOperation> declaredOperations = reference.getAnnotation().getDeclaredOperations();
				builder.append(createLinkToOperation(explicitValue.getOperation(), declaredOperations));
				builder.append("=");
			}
			builder.append(internalToString(explicitValue));
			return builder.toString();
		}).collect(Collectors.joining(", ")));
		buffer.append(")");
	}
	return buffer.toString();
}
 
Example 4
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 5
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public JvmAnnotationValue getExplicitAnnotationValue(String name, JvmAnnotationTarget target) {
	JvmAnnotationReference annotationReference = target.getAnnotations().get(0);
	for (JvmAnnotationValue value : annotationReference.getExplicitValues()) {
		if (name.equals(value.getValueName()))
			return value;
	}
	fail("Cannot find annotationValue " + name);
	return null;
}
 
Example 6
Source File: ReflectionTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected JvmTypeAnnotationValue getClassArrayAnnotationValue(JvmAnnotationReference annotationReference) {
	for(JvmAnnotationValue candidate: annotationReference.getExplicitValues()) {
		if (candidate instanceof JvmTypeAnnotationValue && candidate.getValueName().equals("classArray")) {
			return (JvmTypeAnnotationValue) candidate;
		}
	}
	fail("Cannot find annotation value 'classArray'");
	return null;
}
 
Example 7
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public JvmAnnotationValue getExplicitAnnotationValue(String name, JvmAnnotationTarget target) {
	JvmAnnotationReference annotationReference = target.getAnnotations().get(0);
	for (JvmAnnotationValue value : annotationReference.getExplicitValues()) {
		if (name.equals(value.getValueName()))
			return value;
	}
	fail("Cannot find annotationValue " + name);
	return null;
}
 
Example 8
Source File: JvmModelGeneratorTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testBug380754() {
  try {
    final XExpression expression = this.expression("null");
    final JvmAnnotationReferenceBuilder jvmAnnotationReferenceBuilder = this.jvmAnnotationReferenceBuilderFactory.create(expression.eResource().getResourceSet());
    final Procedure1<JvmGenericType> _function = (JvmGenericType it) -> {
      EList<JvmMember> _members = it.getMembers();
      final Procedure1<JvmOperation> _function_1 = (JvmOperation it_1) -> {
        this.builder.setBody(it_1, expression);
        final JvmAnnotationReference annotation = jvmAnnotationReferenceBuilder.annotationRef(TestAnnotations.class);
        final JvmAnnotationAnnotationValue annotationAnnotationValue = this.typesFactory.createJvmAnnotationAnnotationValue();
        EList<JvmAnnotationReference> _values = annotationAnnotationValue.getValues();
        JvmAnnotationReference _annotationRef = jvmAnnotationReferenceBuilder.annotationRef(TestAnnotation.class);
        this.builder.<JvmAnnotationReference>operator_add(_values, _annotationRef);
        EList<JvmAnnotationReference> _values_1 = annotationAnnotationValue.getValues();
        JvmAnnotationReference _annotationRef_1 = jvmAnnotationReferenceBuilder.annotationRef(TestAnnotation.class);
        this.builder.<JvmAnnotationReference>operator_add(_values_1, _annotationRef_1);
        EList<JvmAnnotationReference> _values_2 = annotationAnnotationValue.getValues();
        JvmAnnotationReference _annotationRef_2 = jvmAnnotationReferenceBuilder.annotationRef(TestAnnotation.class);
        this.builder.<JvmAnnotationReference>operator_add(_values_2, _annotationRef_2);
        EList<JvmAnnotationValue> _explicitValues = annotation.getExplicitValues();
        this.builder.<JvmAnnotationAnnotationValue>operator_add(_explicitValues, annotationAnnotationValue);
        EList<JvmAnnotationReference> _annotations = it_1.getAnnotations();
        this.builder.<JvmAnnotationReference>operator_add(_annotations, annotation);
      };
      JvmOperation _method = this.builder.toMethod(expression, "doStuff", this.references.getTypeForName("java.lang.Object", expression), _function_1);
      this.builder.<JvmOperation>operator_add(_members, _method);
    };
    final JvmGenericType clazz = this.builder.toClass(expression, "my.test.Foo", _function);
    this.compile(expression.eResource(), clazz);
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example 9
Source File: JvmModelGeneratorTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testBug419430() {
  try {
    final XExpression expression = this.expression("null");
    final JvmAnnotationReferenceBuilder jvmAnnotationReferenceBuilder = this.jvmAnnotationReferenceBuilderFactory.create(expression.eResource().getResourceSet());
    final Procedure1<JvmGenericType> _function = (JvmGenericType it) -> {
      EList<JvmMember> _members = it.getMembers();
      final Procedure1<JvmOperation> _function_1 = (JvmOperation it_1) -> {
        this.builder.setBody(it_1, expression);
        final JvmAnnotationReference annotation = jvmAnnotationReferenceBuilder.annotationRef(TestAnnotations.class);
        final JvmAnnotationAnnotationValue annotationAnnotationValue = this.typesFactory.createJvmAnnotationAnnotationValue();
        EList<JvmAnnotationReference> _values = annotationAnnotationValue.getValues();
        JvmAnnotationReference _annotationRef = jvmAnnotationReferenceBuilder.annotationRef(TestAnnotation.class);
        this.builder.<JvmAnnotationReference>operator_add(_values, _annotationRef);
        EList<JvmAnnotationReference> _values_1 = annotationAnnotationValue.getValues();
        JvmAnnotationReference _annotationRef_1 = jvmAnnotationReferenceBuilder.annotationRef(TestAnnotation.class);
        this.builder.<JvmAnnotationReference>operator_add(_values_1, _annotationRef_1);
        EList<JvmAnnotationReference> _values_2 = annotationAnnotationValue.getValues();
        JvmAnnotationReference _annotationRef_2 = jvmAnnotationReferenceBuilder.annotationRef(TestAnnotation.class);
        this.builder.<JvmAnnotationReference>operator_add(_values_2, _annotationRef_2);
        EList<JvmAnnotationValue> _explicitValues = annotation.getExplicitValues();
        this.builder.<JvmAnnotationAnnotationValue>operator_add(_explicitValues, annotationAnnotationValue);
        EList<JvmAnnotationReference> _annotations = it_1.getAnnotations();
        this.builder.<JvmAnnotationReference>operator_add(_annotations, annotation);
      };
      JvmOperation _method = this.builder.toMethod(expression, "doStuff", this.references.getTypeForName("java.lang.Object", expression), _function_1);
      this.builder.<JvmOperation>operator_add(_members, _method);
    };
    final JvmGenericType clazz = this.builder.toClass(expression, "my.test.Foo", _function);
    final String code = this.generate(expression.eResource(), clazz);
    Assert.assertTrue(code, code.contains("@TestAnnotations({ @TestAnnotation, @TestAnnotation, @TestAnnotation })"));
    final Class<?> compiledClazz = this.compileToClass(expression.eResource(), clazz, code);
    final Method method = compiledClazz.getMethod("doStuff");
    final TestAnnotations methodAnnotation = method.<TestAnnotations>getAnnotation(TestAnnotations.class);
    Assert.assertEquals(3, ((List<TestAnnotation>)Conversions.doWrapArray(methodAnnotation.value())).size());
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example 10
Source File: AbstractTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public JvmAnnotationValue getExplicitAnnotationValue(String name, JvmAnnotationTarget target) {
	JvmAnnotationReference annotationReference = target.getAnnotations().get(0);
	for (JvmAnnotationValue value : annotationReference.getExplicitValues()) {
		if (name.equals(value.getValueName()))
			return value;
	}
	fail("Cannot find annotationValue " + name);
	return null;
}