Search in sources :

Example 6 with XtendAnnotationType

use of org.eclipse.xtend.core.xtend.XtendAnnotationType in project xtext-xtend by eclipse.

the class XtendValidator method checkAnnotationTarget.

@Check
public void checkAnnotationTarget(XAnnotation annotation) {
    JvmType annotationType = annotation.getAnnotationType();
    if (annotationType == null || annotationType.eIsProxy() || !(annotationType instanceof JvmAnnotationType)) {
        return;
    }
    Set<ElementType> targets = annotationUtil.getAnnotationTargets((JvmAnnotationType) annotationType);
    if (targets.isEmpty())
        return;
    final EObject eContainer = getContainingAnnotationTarget(annotation);
    Class<? extends EObject> clazz = eContainer.getClass();
    if (eContainer instanceof XtendField && eContainer.eContainer() instanceof XtendAnnotationType) {
        clazz = XtendFunction.class;
    }
    for (Entry<Class<?>, Collection<ElementType>> mapping : targetInfos.asMap().entrySet()) {
        if (mapping.getKey().isAssignableFrom(clazz)) {
            targets.retainAll(mapping.getValue());
            if (targets.isEmpty()) {
                error("The annotation @" + annotation.getAnnotationType().getSimpleName() + " is disallowed for this location.", annotation, null, INSIGNIFICANT_INDEX, ANNOTATION_WRONG_TARGET);
            }
        }
    }
}
Also used : JvmAnnotationType(org.eclipse.xtext.common.types.JvmAnnotationType) ElementType(java.lang.annotation.ElementType) XtendAnnotationType(org.eclipse.xtend.core.xtend.XtendAnnotationType) EObject(org.eclipse.emf.ecore.EObject) Collection(java.util.Collection) AnonymousClass(org.eclipse.xtend.core.xtend.AnonymousClass) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) JvmType(org.eclipse.xtext.common.types.JvmType) XtendField(org.eclipse.xtend.core.xtend.XtendField) Check(org.eclipse.xtext.validation.Check)

Example 7 with XtendAnnotationType

use of org.eclipse.xtend.core.xtend.XtendAnnotationType in project xtext-xtend by eclipse.

the class XtendJvmModelInferrer method doInferTypeSceleton.

protected JvmDeclaredType doInferTypeSceleton(final XtendTypeDeclaration declaration, final IJvmDeclaredTypeAcceptor acceptor, boolean preIndexingPhase, XtendFile xtendFile, List<Runnable> doLater) {
    if (Strings.isEmpty(declaration.getName()))
        return null;
    if (declaration instanceof XtendAnnotationType) {
        final JvmAnnotationType annotation = typesFactory.createJvmAnnotationType();
        if (!preIndexingPhase) {
            doLater.add(new Runnable() {

                @Override
                public void run() {
                    initialize((XtendAnnotationType) declaration, annotation);
                }
            });
        }
        return annotation;
    } else if (declaration instanceof XtendClass) {
        XtendClass xtendClass = (XtendClass) declaration;
        final JvmGenericType javaType = typesFactory.createJvmGenericType();
        copyTypeParameters(xtendClass.getTypeParameters(), javaType);
        if (!preIndexingPhase) {
            doLater.add(new Runnable() {

                @Override
                public void run() {
                    initialize((XtendClass) declaration, javaType);
                }
            });
        }
        return javaType;
    } else if (declaration instanceof XtendInterface) {
        XtendInterface xtendInterface = (XtendInterface) declaration;
        final JvmGenericType javaType = typesFactory.createJvmGenericType();
        javaType.setInterface(true);
        copyTypeParameters(xtendInterface.getTypeParameters(), javaType);
        if (!preIndexingPhase) {
            doLater.add(new Runnable() {

                @Override
                public void run() {
                    initialize((XtendInterface) declaration, javaType);
                }
            });
        }
        return javaType;
    } else if (declaration instanceof XtendEnum) {
        final JvmEnumerationType javaType = typesFactory.createJvmEnumerationType();
        if (!preIndexingPhase) {
            doLater.add(new Runnable() {

                @Override
                public void run() {
                    initialize((XtendEnum) declaration, javaType);
                }
            });
        }
        return javaType;
    } else {
        return null;
    }
}
Also used : JvmAnnotationType(org.eclipse.xtext.common.types.JvmAnnotationType) XtendAnnotationType(org.eclipse.xtend.core.xtend.XtendAnnotationType) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) XtendInterface(org.eclipse.xtend.core.xtend.XtendInterface) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) XtendEnum(org.eclipse.xtend.core.xtend.XtendEnum) JvmEnumerationType(org.eclipse.xtext.common.types.JvmEnumerationType)

Example 8 with XtendAnnotationType

use of org.eclipse.xtend.core.xtend.XtendAnnotationType in project xtext-xtend by eclipse.

the class JavaConverterTest method testAnnotationDeclaration.

@Test
public void testAnnotationDeclaration() throws Exception {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("import java.lang.annotation.Documented;");
    _builder.newLine();
    _builder.append("import java.lang.annotation.ElementType;");
    _builder.newLine();
    _builder.append("import java.lang.annotation.Target;");
    _builder.newLine();
    _builder.append(" ");
    _builder.newLine();
    _builder.append("@Documented");
    _builder.newLine();
    _builder.append("@Target(ElementType.METHOD)");
    _builder.newLine();
    _builder.append("public @interface MyAnno{");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("String author() default \"me\";");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("String date();");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("int revision() default 1;");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("String comments();");
    _builder.newLine();
    _builder.append("}");
    final String javaBody = _builder.toString();
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append(javaBody);
    final XtendAnnotationType clazz = this.toValidXtendAnnotation(_builder_1.toString());
    Assert.assertNotNull(clazz);
    String body = this.toXtendCode(javaBody);
    StringConcatenation _builder_2 = new StringConcatenation();
    _builder_2.append("import java.lang.annotation.Documented");
    _builder_2.newLine();
    _builder_2.append("import java.lang.annotation.ElementType");
    _builder_2.newLine();
    _builder_2.append("import java.lang.annotation.Target");
    _builder_2.newLine();
    _builder_2.append("@Documented @Target(ElementType.METHOD)public annotation MyAnno {");
    _builder_2.newLine();
    _builder_2.append("String author = \"me\"");
    _builder_2.newLine();
    _builder_2.append("String date");
    _builder_2.newLine();
    _builder_2.append("int revision = 1");
    _builder_2.newLine();
    _builder_2.append("String comments");
    _builder_2.newLine();
    _builder_2.append("}");
    Assert.assertEquals(_builder_2.toString(), body);
}
Also used : XtendAnnotationType(org.eclipse.xtend.core.xtend.XtendAnnotationType) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) RichString(org.eclipse.xtend.core.xtend.RichString) Test(org.junit.Test)

Example 9 with XtendAnnotationType

use of org.eclipse.xtend.core.xtend.XtendAnnotationType in project xtext-xtend by eclipse.

the class CompilationUnitImpl method toXtendTypeDeclaration.

public XtendTypeDeclarationImpl<? extends XtendTypeDeclaration> toXtendTypeDeclaration(final XtendTypeDeclaration delegate) {
    final Function1<XtendTypeDeclaration, XtendTypeDeclarationImpl<? extends XtendTypeDeclaration>> _function = (XtendTypeDeclaration it) -> {
        XtendTypeDeclarationImpl<? extends XtendTypeDeclaration> _switchResult = null;
        boolean _matched = false;
        if (delegate instanceof XtendClass) {
            _matched = true;
            XtendClassDeclarationImpl _xtendClassDeclarationImpl = new XtendClassDeclarationImpl();
            final Procedure1<XtendClassDeclarationImpl> _function_1 = (XtendClassDeclarationImpl it_1) -> {
                it_1.setDelegate(((XtendClass) delegate));
                it_1.setCompilationUnit(this);
            };
            _switchResult = ObjectExtensions.<XtendClassDeclarationImpl>operator_doubleArrow(_xtendClassDeclarationImpl, _function_1);
        }
        if (!_matched) {
            if (delegate instanceof XtendInterface) {
                _matched = true;
                XtendInterfaceDeclarationImpl _xtendInterfaceDeclarationImpl = new XtendInterfaceDeclarationImpl();
                final Procedure1<XtendInterfaceDeclarationImpl> _function_1 = (XtendInterfaceDeclarationImpl it_1) -> {
                    it_1.setDelegate(((XtendInterface) delegate));
                    it_1.setCompilationUnit(this);
                };
                _switchResult = ObjectExtensions.<XtendInterfaceDeclarationImpl>operator_doubleArrow(_xtendInterfaceDeclarationImpl, _function_1);
            }
        }
        if (!_matched) {
            if (delegate instanceof XtendAnnotationType) {
                _matched = true;
                XtendAnnotationTypeDeclarationImpl _xtendAnnotationTypeDeclarationImpl = new XtendAnnotationTypeDeclarationImpl();
                final Procedure1<XtendAnnotationTypeDeclarationImpl> _function_1 = (XtendAnnotationTypeDeclarationImpl it_1) -> {
                    it_1.setDelegate(((XtendAnnotationType) delegate));
                    it_1.setCompilationUnit(this);
                };
                _switchResult = ObjectExtensions.<XtendAnnotationTypeDeclarationImpl>operator_doubleArrow(_xtendAnnotationTypeDeclarationImpl, _function_1);
            }
        }
        if (!_matched) {
            if (delegate instanceof XtendEnum) {
                _matched = true;
                XtendEnumerationDeclarationImpl _xtendEnumerationDeclarationImpl = new XtendEnumerationDeclarationImpl();
                final Procedure1<XtendEnumerationDeclarationImpl> _function_1 = (XtendEnumerationDeclarationImpl it_1) -> {
                    it_1.setDelegate(((XtendEnum) delegate));
                    it_1.setCompilationUnit(this);
                };
                _switchResult = ObjectExtensions.<XtendEnumerationDeclarationImpl>operator_doubleArrow(_xtendEnumerationDeclarationImpl, _function_1);
            }
        }
        return _switchResult;
    };
    return this.<XtendTypeDeclaration, XtendTypeDeclarationImpl<? extends XtendTypeDeclaration>>getOrCreate(delegate, _function);
}
Also used : XtendTypeDeclarationImpl(org.eclipse.xtend.core.macro.declaration.XtendTypeDeclarationImpl) XtendClassDeclarationImpl(org.eclipse.xtend.core.macro.declaration.XtendClassDeclarationImpl) XtendAnnotationType(org.eclipse.xtend.core.xtend.XtendAnnotationType) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) XtendInterface(org.eclipse.xtend.core.xtend.XtendInterface) XtendInterfaceDeclarationImpl(org.eclipse.xtend.core.macro.declaration.XtendInterfaceDeclarationImpl) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) XtendEnum(org.eclipse.xtend.core.xtend.XtendEnum) XtendAnnotationTypeDeclarationImpl(org.eclipse.xtend.core.macro.declaration.XtendAnnotationTypeDeclarationImpl) XtendEnumerationDeclarationImpl(org.eclipse.xtend.core.macro.declaration.XtendEnumerationDeclarationImpl)

Example 10 with XtendAnnotationType

use of org.eclipse.xtend.core.xtend.XtendAnnotationType in project xtext-xtend by eclipse.

the class XtendValidationTest method testAnnotationTarget_11.

@Test
public void testAnnotationTarget_11() throws Exception {
    XtendAnnotationType annotationType = annotationType("annotation A { @testdata.Annotation6 String value = 'Issue206'}");
    helper.assertNoErrors(annotationType);
}
Also used : XtendAnnotationType(org.eclipse.xtend.core.xtend.XtendAnnotationType) Test(org.junit.Test)

Aggregations

XtendAnnotationType (org.eclipse.xtend.core.xtend.XtendAnnotationType)11 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)5 Test (org.junit.Test)5 XtendEnum (org.eclipse.xtend.core.xtend.XtendEnum)4 XtendField (org.eclipse.xtend.core.xtend.XtendField)4 XtendInterface (org.eclipse.xtend.core.xtend.XtendInterface)4 EObject (org.eclipse.emf.ecore.EObject)3 RichString (org.eclipse.xtend.core.xtend.RichString)2 XtendConstructor (org.eclipse.xtend.core.xtend.XtendConstructor)2 XtendEnumLiteral (org.eclipse.xtend.core.xtend.XtendEnumLiteral)2 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)2 XtendMember (org.eclipse.xtend.core.xtend.XtendMember)2 XtendParameter (org.eclipse.xtend.core.xtend.XtendParameter)2 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)2 JvmAnnotationType (org.eclipse.xtext.common.types.JvmAnnotationType)2 ElementType (java.lang.annotation.ElementType)1 Collection (java.util.Collection)1 EPackage (org.eclipse.emf.ecore.EPackage)1 InternalEObject (org.eclipse.emf.ecore.InternalEObject)1 XtendAnnotationTypeDeclarationImpl (org.eclipse.xtend.core.macro.declaration.XtendAnnotationTypeDeclarationImpl)1