Search in sources :

Example 1 with JvmAnnotationReference

use of org.eclipse.xtext.common.types.JvmAnnotationReference in project xtext-eclipse by eclipse.

the class JdtBasedTypeFactory method createAnnotationReference.

/**
 * @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;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) InternalEList(org.eclipse.emf.ecore.util.InternalEList) IMemberValuePairBinding(org.eclipse.jdt.core.dom.IMemberValuePairBinding) JvmAnnotationReference(org.eclipse.xtext.common.types.JvmAnnotationReference) JvmAnnotationValue(org.eclipse.xtext.common.types.JvmAnnotationValue)

Example 2 with JvmAnnotationReference

use of org.eclipse.xtext.common.types.JvmAnnotationReference in project xtext-eclipse by eclipse.

the class AbstractTypeProviderTest method testAnnotationWithStringDefault_01.

@Test
public void testAnnotationWithStringDefault_01() throws Exception {
    String typeName = TestAnnotationWithStringDefault.Annotated.class.getName();
    JvmDeclaredType type = (JvmDeclaredType) getTypeProvider().findTypeByName(typeName);
    List<JvmAnnotationReference> annotations = type.getAnnotations();
    assertEquals(1, annotations.size());
    JvmAnnotationReference annotationReference = annotations.get(0);
    assertEquals(TestAnnotationWithStringDefault.class.getName(), annotationReference.getAnnotation().getIdentifier());
    checkDefaultAnnotationValues(annotationReference);
}
Also used : TestAnnotationWithStringDefault(org.eclipse.xtext.common.types.testSetups.TestAnnotationWithStringDefault) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) JvmAnnotationReference(org.eclipse.xtext.common.types.JvmAnnotationReference) Test(org.junit.Test)

Example 3 with JvmAnnotationReference

use of org.eclipse.xtext.common.types.JvmAnnotationReference in project xtext-eclipse by eclipse.

the class AbstractTypeProviderTest method testAnnotationWithStringDefault_02.

@Test
public void testAnnotationWithStringDefault_02() throws Exception {
    String typeName = AnnotatedInterfaceWithStringDefault.class.getName();
    JvmDeclaredType type = (JvmDeclaredType) getTypeProvider().findTypeByName(typeName);
    List<JvmAnnotationReference> annotations = type.getAnnotations();
    assertEquals(1, annotations.size());
    JvmAnnotationReference annotationReference = annotations.get(0);
    assertEquals(TestAnnotationWithStringDefault.class.getName(), annotationReference.getAnnotation().getIdentifier());
    checkDefaultAnnotationValuesAnnotatedExternalClass(annotationReference);
}
Also used : TestAnnotationWithStringDefault(org.eclipse.xtext.common.types.testSetups.TestAnnotationWithStringDefault) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) JvmAnnotationReference(org.eclipse.xtext.common.types.JvmAnnotationReference) Test(org.junit.Test)

Example 4 with JvmAnnotationReference

use of org.eclipse.xtext.common.types.JvmAnnotationReference in project xtext-eclipse by eclipse.

the class AbstractTypeProviderTest method testAnnotatedParameter_06.

@Test
public void testAnnotatedParameter_06() throws Exception {
    String typeName = TestAnnotation.Annotated.class.getName();
    JvmAnnotationType annotationType = (JvmAnnotationType) getTypeProvider().findTypeByName(TestAnnotation.NestedAnnotation.class.getName());
    JvmDeclaredType type = (JvmDeclaredType) getTypeProvider().findTypeByName(typeName);
    JvmOperation method = getMethodFromType(type, TestAnnotation.Annotated.class, "method(java.lang.String,java.lang.String,java.lang.String)");
    JvmAnnotationTarget target = method.getParameters().get(2);
    assertEquals(1, target.getAnnotations().size());
    JvmAnnotationReference annotationReference = target.getAnnotations().get(0);
    assertSame(annotationType, annotationReference.getAnnotation());
}
Also used : JvmAnnotationType(org.eclipse.xtext.common.types.JvmAnnotationType) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) JvmAnnotationTarget(org.eclipse.xtext.common.types.JvmAnnotationTarget) TestAnnotation(org.eclipse.xtext.common.types.testSetups.TestAnnotation) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) JvmAnnotationReference(org.eclipse.xtext.common.types.JvmAnnotationReference) Test(org.junit.Test)

Example 5 with JvmAnnotationReference

use of org.eclipse.xtext.common.types.JvmAnnotationReference in project xtext-eclipse by eclipse.

the class AbstractTypeProviderTest method getDefaultOrExplicitAnnotationValue.

public JvmAnnotationValue getDefaultOrExplicitAnnotationValue(String name, JvmAnnotationTarget target) {
    JvmAnnotationReference annotationReference = target.getAnnotations().get(0);
    for (JvmAnnotationValue value : annotationReference.getValues()) {
        if (name.equals(value.getValueName()))
            return value;
    }
    fail("Cannot find annotationValue " + name);
    return null;
}
Also used : JvmAnnotationReference(org.eclipse.xtext.common.types.JvmAnnotationReference) JvmAnnotationValue(org.eclipse.xtext.common.types.JvmAnnotationValue)

Aggregations

JvmAnnotationReference (org.eclipse.xtext.common.types.JvmAnnotationReference)47 Test (org.junit.Test)23 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)15 JvmAnnotationValue (org.eclipse.xtext.common.types.JvmAnnotationValue)11 JvmAnnotationType (org.eclipse.xtext.common.types.JvmAnnotationType)9 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)7 JvmAnnotationAnnotationValue (org.eclipse.xtext.common.types.JvmAnnotationAnnotationValue)6 JvmAnnotationTarget (org.eclipse.xtext.common.types.JvmAnnotationTarget)6 JvmTypeAnnotationValue (org.eclipse.xtext.common.types.JvmTypeAnnotationValue)6 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)6 EObject (org.eclipse.emf.ecore.EObject)5 AnnotationReference (org.eclipse.xtend.lib.macro.declaration.AnnotationReference)5 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)5 JvmStringAnnotationValue (org.eclipse.xtext.common.types.JvmStringAnnotationValue)5 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)5 TestAnnotation (org.eclipse.xtext.common.types.testSetups.TestAnnotation)4 TestAnnotationWithStringDefault (org.eclipse.xtext.common.types.testSetups.TestAnnotationWithStringDefault)4 Extension (org.eclipse.xtext.xbase.lib.Extension)4 IFile (org.eclipse.core.resources.IFile)3 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)3