org.eclipse.xtext.common.types.JvmAnnotationType Java Examples

The following examples show how to use org.eclipse.xtext.common.types.JvmAnnotationType. 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: AnnotationReferenceBuildContextImpl.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected JvmOperation findOperation(final String name) {
  ConditionUtils.checkJavaIdentifier(name, "name");
  final JvmAnnotationType annotationType = this.delegate.getAnnotation();
  final Function1<JvmOperation, Boolean> _function = (JvmOperation it) -> {
    String _simpleName = it.getSimpleName();
    return Boolean.valueOf(Objects.equal(_simpleName, name));
  };
  final JvmOperation jvmOperation = IterableExtensions.<JvmOperation>findFirst(annotationType.getDeclaredOperations(), _function);
  if ((jvmOperation == null)) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("The annotation property \'");
    _builder.append(name);
    _builder.append("\' is not declared on the annotation type \'");
    String _identifier = annotationType.getIdentifier();
    _builder.append(_identifier);
    _builder.append("\'.");
    throw new IllegalArgumentException(_builder.toString());
  }
  return jvmOperation;
}
 
Example #2
Source File: ActiveAnnotationContextProvider.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected void registerMacroAnnotations(final XtendAnnotationTarget candidate, final IAcceptor<Pair<JvmAnnotationType, XAnnotation>> acceptor) {
  final Function1<XAnnotation, Boolean> _function = (XAnnotation it) -> {
    return Boolean.valueOf(this._xAnnotationExtensions.isProcessed(it));
  };
  Iterable<XAnnotation> _filter = IterableExtensions.<XAnnotation>filter(candidate.getAnnotations(), _function);
  for (final XAnnotation annotation : _filter) {
    {
      final JvmAnnotationType activeAnnotationDeclaration = this._xAnnotationExtensions.tryFindAnnotationType(annotation);
      if ((activeAnnotationDeclaration != null)) {
        boolean _isValid = this.isValid(annotation, activeAnnotationDeclaration);
        if (_isValid) {
          Pair<JvmAnnotationType, XAnnotation> _mappedTo = Pair.<JvmAnnotationType, XAnnotation>of(activeAnnotationDeclaration, annotation);
          acceptor.accept(_mappedTo);
        }
      }
    }
  }
}
 
Example #3
Source File: XbaseWithAnnotationsValidator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Check
public void checkAllAttributesConfigured(XAnnotation annotation) {
	JvmType annotationType = annotation.getAnnotationType();
	if (annotationType == null || annotationType.eIsProxy() || !(annotationType instanceof JvmAnnotationType))
		return;
	Iterable<JvmOperation> attributes = ((JvmAnnotationType) annotationType).getDeclaredOperations();
	for (JvmOperation jvmOperation : attributes) {
		XExpression value = annotationUtil.findValue(annotation, jvmOperation);
		if(value == null) {
			if (jvmOperation.getDefaultValue() == null) {
				error("The annotation must define the attribute '"+jvmOperation.getSimpleName()+"'.", annotation, null, 
						ValidationMessageAcceptor.INSIGNIFICANT_INDEX, ANNOTATIONS_MISSING_ATTRIBUTE_DEFINITION);
			}
		} else
			annotationValueValidator.validateAnnotationValue(value, this);
	}
}
 
Example #4
Source File: XtendAnnotationReferenceImpl.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public AnnotationTypeDeclaration getAnnotationTypeDeclaration() {
  AnnotationTypeDeclaration _switchResult = null;
  JvmType _annotationType = this.getAnnotationType();
  final JvmType type = _annotationType;
  boolean _matched = false;
  if (type instanceof JvmAnnotationType) {
    _matched=true;
    TypeDeclaration _typeDeclaration = this.getCompilationUnit().toTypeDeclaration(((JvmDeclaredType)type));
    _switchResult = ((AnnotationTypeDeclaration) _typeDeclaration);
  }
  if (!_matched) {
    _switchResult = null;
  }
  return _switchResult;
}
 
Example #5
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 #6
Source File: JvmModelGeneratorTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testNestedAnnotationType() {
  try {
    final XExpression expression = this.expression("42");
    final JvmGenericType outerClass = this.builder.toClass(expression, "my.outer.Clazz");
    EList<JvmMember> _members = outerClass.getMembers();
    final Procedure1<JvmAnnotationType> _function = (JvmAnnotationType it) -> {
      EList<JvmMember> _members_1 = it.getMembers();
      final Procedure1<JvmOperation> _function_1 = (JvmOperation it_1) -> {
        this.builder.setBody(it_1, expression);
      };
      JvmOperation _method = this.builder.toMethod(expression, "theTruth", this.references.getTypeForName(int.class, expression), _function_1);
      this.builder.<JvmOperation>operator_add(_members_1, _method);
    };
    JvmAnnotationType _annotationType = this.builder.toAnnotationType(expression, "MyAnnotation", _function);
    this.builder.<JvmAnnotationType>operator_add(_members, _annotationType);
    final Class<?> compiled = IterableExtensions.<Class<?>>head(((Iterable<Class<?>>)Conversions.doWrapArray(this.compile(expression.eResource(), outerClass).getDeclaredClasses())));
    Assert.assertEquals("my.outer.Clazz.MyAnnotation", compiled.getCanonicalName());
    Assert.assertEquals(Integer.valueOf(42), IterableExtensions.<Method>head(((Iterable<Method>)Conversions.doWrapArray(compiled.getDeclaredMethods()))).getDefaultValue());
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #7
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 #8
Source File: JvmAnnotationReferenceImpl.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@SuppressWarnings("unchecked")
@Override
public void eSet(int featureID, Object newValue)
{
	switch (featureID)
	{
		case TypesPackage.JVM_ANNOTATION_REFERENCE__ANNOTATION:
			setAnnotation((JvmAnnotationType)newValue);
			return;
		case TypesPackage.JVM_ANNOTATION_REFERENCE__EXPLICIT_VALUES:
			getExplicitValues().clear();
			getExplicitValues().addAll((Collection<? extends JvmAnnotationValue>)newValue);
			return;
	}
	super.eSet(featureID, newValue);
}
 
Example #9
Source File: TypeInsteadOfConstructorLinkingCandidate.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public boolean validate(IAcceptor<? super AbstractDiagnostic> result) {
	JvmType type = (JvmType) description.getElementOrProxy();
	String typeKind = "";
	if (type instanceof JvmPrimitiveType || type instanceof JvmVoid) {
		typeKind = "primitive type";
	} else if (type instanceof JvmAnnotationType) {
		typeKind = "annotation type";
	} else if (type instanceof JvmEnumerationType) {
		typeKind = "enum type";
	} else if (type instanceof JvmGenericType && ((JvmGenericType) type).isInterface()) {
		typeKind = "interface type";
	} else if (type instanceof JvmTypeParameter) {
		typeKind = "type parameter";
	}
	String message = String.format("Cannot instantiate the %s %s", typeKind, type.getSimpleName());
	AbstractDiagnostic diagnostic = new EObjectDiagnosticImpl(Severity.ERROR,
			IssueCodes.ILLEGAL_CLASS_INSTANTIATION, message, getExpression(),
			XbasePackage.Literals.XCONSTRUCTOR_CALL__CONSTRUCTOR, -1, null);
	result.accept(diagnostic);
	return false;
}
 
Example #10
Source File: ResourceDescriptionProviderTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testStubGeneration_04() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("public @interface MyTest {");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("String value();");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  final Procedure1<IResourceDescription> _function = (IResourceDescription it) -> {
    Assert.assertEquals("MyTest", IterableExtensions.<IEObjectDescription>head(it.getExportedObjects()).getQualifiedName().toString());
    EObject _eObjectOrProxy = IterableExtensions.<IEObjectDescription>head(it.getExportedObjects()).getEObjectOrProxy();
    Assert.assertTrue((_eObjectOrProxy instanceof JvmAnnotationType));
    Assert.assertEquals(1, IterableExtensions.size(it.getExportedObjects()));
  };
  this.resultsIn(_builder, _function);
}
 
Example #11
Source File: JvmTypesBuilderTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testAnnotationDefaultValue() {
  try {
    final XAnnotationsFactory f = XAnnotationsFactory.eINSTANCE;
    final XExpression e = this.expression("\'Foo\'");
    final XAnnotation anno = f.createXAnnotation();
    JvmType _findDeclaredType = this.references.findDeclaredType(Named.class, e);
    anno.setAnnotationType(((JvmAnnotationType) _findDeclaredType));
    anno.setValue(e);
    final JvmGenericType type = this.typesFactory.createJvmGenericType();
    this._jvmTypesBuilder.addAnnotation(type, anno);
    Assert.assertEquals(anno.getAnnotationType(), IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getAnnotation());
    JvmAnnotationValue _head = IterableExtensions.<JvmAnnotationValue>head(IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getValues());
    EObject _head_1 = IterableExtensions.<EObject>head(((JvmCustomAnnotationValue) _head).getValues());
    Assert.assertTrue((_head_1 instanceof XStringLiteral));
    Assert.assertNull(IterableExtensions.<JvmAnnotationValue>head(IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getValues()).getOperation());
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #12
Source File: JvmTypesBuilderTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testStringAnnotation() {
  try {
    final XAnnotationsFactory f = XAnnotationsFactory.eINSTANCE;
    final XExpression e = this.expression("\'Foo\'");
    final XAnnotation anno = f.createXAnnotation();
    JvmType _findDeclaredType = this.references.findDeclaredType(Inject.class, e);
    anno.setAnnotationType(((JvmAnnotationType) _findDeclaredType));
    anno.setValue(e);
    final JvmGenericType type = this.typesFactory.createJvmGenericType();
    this._jvmTypesBuilder.addAnnotation(type, anno);
    Assert.assertEquals(anno.getAnnotationType(), IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getAnnotation());
    JvmAnnotationValue _head = IterableExtensions.<JvmAnnotationValue>head(IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getValues());
    EObject _head_1 = IterableExtensions.<EObject>head(((JvmCustomAnnotationValue) _head).getValues());
    Assert.assertTrue((_head_1 instanceof XStringLiteral));
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #13
Source File: XAnnotationExtensions.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
public JvmAnnotationType tryFindAnnotationType(final XAnnotation it) {
  Object _eGet = it.eGet(XAnnotationsPackage.Literals.XANNOTATION__ANNOTATION_TYPE, false);
  final Object proxy = _eGet;
  boolean _matched = false;
  if (proxy instanceof EObject) {
    boolean _eIsProxy = ((EObject)proxy).eIsProxy();
    if (_eIsProxy) {
      _matched=true;
      final URI uri = ((InternalEObject) proxy).eProxyURI();
      EObject _eObject = it.eResource().getResourceSet().getEObject(uri, true);
      return ((JvmAnnotationType) _eObject);
    }
  }
  if (!_matched) {
    if (proxy instanceof JvmAnnotationType) {
      _matched=true;
      return ((JvmAnnotationType)proxy);
    }
  }
  return null;
}
 
Example #14
Source File: XtendHighlightingCalculator.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected void highlightDeprecatedXtendAnnotationTarget(IHighlightedPositionAcceptor acceptor, XtendAnnotationTarget target, XAnnotation annotation){
	JvmType annotationType = annotation.getAnnotationType();
	if(annotationType instanceof JvmAnnotationType && DeprecationUtil.isDeprecatedAnnotation((JvmAnnotationType) annotationType)){
		if (target instanceof XtendConstructor) {
			ICompositeNode compositeNode = NodeModelUtils.getNode(target);
			for(ILeafNode leaf: compositeNode.getLeafNodes()) {
				if (leaf.getGrammarElement() == xtendGrammarAccess.getMemberAccess().getNewKeyword_2_2_2()) {
					highlightNode(acceptor, leaf, XbaseHighlightingStyles.DEPRECATED_MEMBERS);
					highlightNode(acceptor, leaf, HighlightingStyles.KEYWORD_ID);
					return;
				}
			}
		} else {
			EStructuralFeature nameFeature = target.eClass().getEStructuralFeature("name");
			if (nameFeature!=null) {
				highlightFeature(acceptor, target, nameFeature, XbaseHighlightingStyles.DEPRECATED_MEMBERS);
			}
		}
	}
}
 
Example #15
Source File: ReflectionTypeFactory.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected JvmAnnotationType createAnnotationType(Class<?> clazz) {
	JvmAnnotationType result = TypesFactory.eINSTANCE.createJvmAnnotationType();
	result.internalSetIdentifier(clazz.getName());
	result.setSimpleName(clazz.getSimpleName());
	if (clazz.getDeclaringClass() == null && clazz.getPackage() != null)
		result.setPackageName(clazz.getPackage().getName());
	setVisibility(clazz, result);
	setTypeModifiers(clazz, result);
	createNestedTypes(clazz, result);
	createMethods(clazz, result);
	createFields(clazz, result);
	setSuperTypes(clazz, result);
	createAnnotationValues(clazz, result);
	return result;
}
 
Example #16
Source File: AbstractTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public JvmAnnotationValue getDefaultAnnotationValue(String name) {
	String typeName = TestAnnotationWithDefaults.class.getName();
	JvmAnnotationType type = (JvmAnnotationType) getTypeProvider().findTypeByName(typeName);
	JvmOperation operation = getMethodFromType(type, TestAnnotationWithDefaults.class, name + "()");
	JvmAnnotationValue result = operation.getDefaultValue();
	assertNotNull(result);
	assertSame(operation, result.getOperation());
	return result;
}
 
Example #17
Source File: AbstractTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private void doTestAnnotation_01(JvmAnnotationTarget target) {
	JvmAnnotationType annotationType = (JvmAnnotationType) getTypeProvider()
			.findTypeByName(TestAnnotation.class.getName());
	assertEquals(1, target.getAnnotations().size());
	JvmAnnotationReference annotationReference = target.getAnnotations().get(0);
	assertSame(annotationType, annotationReference.getAnnotation());
	if (isDefaultValueSupported())
		assertEquals(14, annotationReference.getExplicitValues().size());
	assertEquals(15, annotationReference.getValues().size());
}
 
Example #18
Source File: JvmModelTests.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testAnnotationImplicitSuperType() {
  try {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("annotation Foo {");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final JvmAnnotationType inferred = this._iXtendJvmAssociations.getInferredAnnotationType(this.annotationType(_builder.toString()));
    Assert.assertEquals(1, inferred.getSuperTypes().size());
    Assert.assertEquals("java.lang.annotation.Annotation", IterableExtensions.<JvmTypeReference>head(inferred.getSuperTypes()).getIdentifier());
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #19
Source File: AnnotationValidation.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public boolean isValidAnnotationValueType(final JvmTypeReference reference) {
  JvmTypeReference _switchResult = null;
  boolean _matched = false;
  if (reference instanceof JvmGenericArrayTypeReference) {
    _matched=true;
    _switchResult = ((JvmGenericArrayTypeReference)reference).getComponentType();
  }
  if (!_matched) {
    _switchResult = reference;
  }
  final JvmTypeReference toCheck = _switchResult;
  if ((toCheck == null)) {
    return true;
  }
  JvmType _type = toCheck.getType();
  if ((_type instanceof JvmPrimitiveType)) {
    return true;
  }
  JvmType _type_1 = toCheck.getType();
  if ((_type_1 instanceof JvmEnumerationType)) {
    return true;
  }
  JvmType _type_2 = toCheck.getType();
  if ((_type_2 instanceof JvmAnnotationType)) {
    return true;
  }
  if ((Objects.equal(toCheck.getType().getQualifiedName(), "java.lang.String") || Objects.equal(toCheck.getType().getQualifiedName(), "java.lang.Class"))) {
    return true;
  }
  return false;
}
 
Example #20
Source File: AbstractTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testAnnotatedParameter_03() throws Exception {
	String typeName = TestAnnotation.Annotated.class.getName();
	JvmAnnotationType annotationType = (JvmAnnotationType) getTypeProvider()
			.findTypeByName(TestAnnotation.NestedAnnotation.class.getName());
	JvmDeclaredType type = (JvmDeclaredType) getTypeProvider().findTypeByName(typeName);
	JvmConstructor constructor = getConstructorFromType(type, TestAnnotation.Annotated.class,
			"Annotated(java.lang.String,java.lang.String,java.lang.String)");
	JvmAnnotationTarget target = constructor.getParameters().get(2);
	assertEquals(1, target.getAnnotations().size());
	JvmAnnotationReference annotationReference = target.getAnnotations().get(0);
	assertSame(annotationType, annotationReference.getAnnotation());
}
 
Example #21
Source File: AnnotationLookup.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
private JvmAnnotationType findAnnotationType(Class<? extends Annotation> type, Notifier context) {
	ResourceSet resourceSet = EcoreUtil2.getResourceSet(context);
	if (resourceSet == null)
		return null;
	IJvmTypeProvider typeProvider = typeProviderFactory.findOrCreateTypeProvider(resourceSet);
	try {
		final JvmType result = typeProvider.findTypeByName(type.getName());
		if (result instanceof JvmAnnotationType)
			return (JvmAnnotationType) result;
		return null;
	} catch (RuntimeException e) {
		return null;
	}
}
 
Example #22
Source File: DeprecationUtil.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public static boolean isDeprecatedMember(JvmMember member) {
	if (member.isSetDeprecated()) {
		return member.isDeprecated();
	}
	List<JvmAnnotationReference> annotations = member.getAnnotations();
	for(JvmAnnotationReference annotation: annotations) {
		JvmAnnotationType annotationType = annotation.getAnnotation();
		if (annotationType != null && Deprecated.class.getName().equals(annotationType.getIdentifier())) {
			return true;
		}
	}
	return false;
}
 
Example #23
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testAnnotatedParameter_03() throws Exception {
	String typeName = TestAnnotation.Annotated.class.getName();
	JvmAnnotationType annotationType = (JvmAnnotationType) getTypeProvider()
			.findTypeByName(TestAnnotation.NestedAnnotation.class.getName());
	JvmDeclaredType type = (JvmDeclaredType) getTypeProvider().findTypeByName(typeName);
	JvmConstructor constructor = getConstructorFromType(type, TestAnnotation.Annotated.class,
			"Annotated(java.lang.String,java.lang.String,java.lang.String)");
	JvmAnnotationTarget target = constructor.getParameters().get(2);
	assertEquals(1, target.getAnnotations().size());
	JvmAnnotationReference annotationReference = target.getAnnotations().get(0);
	assertSame(annotationType, annotationReference.getAnnotation());
}
 
Example #24
Source File: XtendAnnotationReferenceImpl.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected Object translateAnnotationValue(final XExpression value, final String property) {
  final JvmType annotationType = this.getDelegate().getAnnotationType();
  if ((annotationType instanceof JvmAnnotationType)) {
    final Function1<JvmOperation, Boolean> _function = (JvmOperation it) -> {
      String _simpleName = it.getSimpleName();
      return Boolean.valueOf(Objects.equal(_simpleName, property));
    };
    final JvmOperation operation = IterableExtensions.<JvmOperation>findFirst(Iterables.<JvmOperation>filter(((JvmAnnotationType)annotationType).getMembers(), JvmOperation.class), _function);
    if ((operation != null)) {
      final boolean array = this.getCompilationUnit().getTypeReferences().isArray(operation.getReturnType());
      return this.getCompilationUnit().translateAnnotationValue(value, operation.getReturnType(), array);
    }
  }
  return this.getCompilationUnit().translateAnnotationValue(value, null, false);
}
 
Example #25
Source File: AnnotationReferenceProviderImpl.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean isArrayOfAnnotations(final JvmAnnotationValue annotationValue) {
  final JvmTypeReference returnType = annotationValue.getOperation().getReturnType();
  if ((returnType instanceof JvmGenericArrayTypeReference)) {
    JvmType _type = ((JvmGenericArrayTypeReference)returnType).getComponentType().getType();
    return (_type instanceof JvmAnnotationType);
  }
  return false;
}
 
Example #26
Source File: ReflectionTypeFactory.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected JvmAnnotationType createAnnotationProxy(Class<? extends Annotation> type) {
	JvmAnnotationType proxy = annotationProxies.get(type);
	if (proxy == null)  {
		proxy = TypesFactory.eINSTANCE.createJvmAnnotationType();
		URI uri = uriHelper.getFullURI(type);
		((InternalEObject)proxy).eSetProxyURI(uri);
		annotationProxies.put(type, proxy);
	}
	return proxy;
}
 
Example #27
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public JvmAnnotationValue getDefaultAnnotationValue(String name) {
	String typeName = TestAnnotationWithDefaults.class.getName();
	JvmAnnotationType type = (JvmAnnotationType) getTypeProvider().findTypeByName(typeName);
	JvmOperation operation = getMethodFromType(type, TestAnnotationWithDefaults.class, name + "()");
	JvmAnnotationValue result = operation.getDefaultValue();
	assertNotNull(result);
	assertSame(operation, result.getOperation());
	return result;
}
 
Example #28
Source File: JvmTypesBuilderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testEmptyAnnotation() {
  try {
    final XAnnotationsFactory f = XAnnotationsFactory.eINSTANCE;
    final XExpression e = this.expression("\'Foo\'");
    final XAnnotation anno = f.createXAnnotation();
    JvmType _findDeclaredType = this.references.findDeclaredType(Inject.class, e);
    anno.setAnnotationType(((JvmAnnotationType) _findDeclaredType));
    final JvmGenericType type = this.typesFactory.createJvmGenericType();
    this._jvmTypesBuilder.addAnnotations(type, Collections.<XAnnotation>unmodifiableList(CollectionLiterals.<XAnnotation>newArrayList(anno)));
    Assert.assertEquals(anno.getAnnotationType(), IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getAnnotation());
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #29
Source File: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected JvmAnnotationType createAnnotationProxy(/* @NonNull */ ITypeBinding annotation) {
	JvmAnnotationType proxy = annotationProxies.get(annotation);
	if (proxy == null) {
		URI uri = uriHelper.getFullURI(annotation);
		proxy = ANNOTATION_PROXIES.get(uri.fragment());
		if (proxy == null) {
			proxy = TypesFactory.eINSTANCE.createJvmAnnotationType();
			((InternalEObject)proxy).eSetProxyURI(uri);
		}
		annotationProxies.put(annotation, proxy);
	}
	return proxy;
}
 
Example #30
Source File: JvmTypesBuilderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testInitializeSafely_4() {
  final Runnable _function = () -> {
    final Function3<EObject, String, Procedure1<? super JvmAnnotationType>, EObject> _function_1 = (EObject expr, String name, Procedure1<? super JvmAnnotationType> init) -> {
      return this._jvmTypesBuilder.toAnnotationType(expr, name, init);
    };
    this.<JvmAnnotationType>genericTestInitializeSafely(_function_1);
  };
  this.expectErrorLogging(2, _function);
}