Search in sources :

Example 1 with MutableEnumerationTypeDeclaration

use of org.eclipse.xtend.lib.macro.declaration.MutableEnumerationTypeDeclaration in project xtext-xtend by eclipse.

the class AbstractReusableActiveAnnotationTests method testAccessAndModifyEnumerationValueDeclaration.

@Test
public void testAccessAndModifyEnumerationValueDeclaration() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package myannotation");
    _builder.newLine();
    _builder.newLine();
    _builder.append("import java.util.List");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.Active");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.CodeGenerationContext");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.CodeGenerationParticipant");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.RegisterGlobalsContext");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.RegisterGlobalsParticipant");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.TransformationContext");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.TransformationParticipant");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.EnumerationTypeDeclaration");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.EnumerationValueDeclaration");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableEnumerationTypeDeclaration");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.TypeDeclaration");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.Visibility");
    _builder.newLine();
    _builder.newLine();
    _builder.append("import static com.google.common.base.Preconditions.*");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(MyAnnotationProcessor)");
    _builder.newLine();
    _builder.append("annotation MyAnnotation {");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class MyAnnotationProcessor implements RegisterGlobalsParticipant<EnumerationTypeDeclaration>, TransformationParticipant<MutableEnumerationTypeDeclaration>, CodeGenerationParticipant<EnumerationTypeDeclaration> {");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doGenerateCode(List<? extends EnumerationTypeDeclaration> annotatedSourceElements,");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("extension CodeGenerationContext context) {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doRegisterGlobals(List<? extends EnumerationTypeDeclaration> annotatedSourceElements,");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("RegisterGlobalsContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("for (enumeration : annotatedSourceElements) {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("enumeration.checkState");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def checkState(EnumerationTypeDeclaration enumeration) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val values = enumeration.declaredValues");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("checkState(values.size == 3, \"enumeration.declaredValues.size != 3\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("enumeration.findDeclaredValue(values.get(0).simpleName).checkState(\"A\", enumeration)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("enumeration.findDeclaredValue(values.get(1).simpleName).checkState(\"B\", enumeration)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("enumeration.findDeclaredValue(values.get(2).simpleName).checkState(\"C\", enumeration)");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def checkState(EnumerationValueDeclaration value, String expectedSimpleName, TypeDeclaration expectedType) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("checkState(value.simpleName == expectedSimpleName, \"value.simpleName != expectedSimpleName\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("checkState(value.declaringType == expectedType, \"value.declaringType != expectedType\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("checkState(value.visibility == Visibility.PUBLIC, \"value.visibility != Visibility.PUBLIC\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("checkState(value.annotations.size == 0, \"value.annotations.size != 0\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("checkState(value.findAnnotation(null) == null, \"value.findAnnotation(null) != null\")");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(List<? extends MutableEnumerationTypeDeclaration> annotatedTargetElements,");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("for (enumeration : annotatedTargetElements) {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("enumeration.checkState");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("for (value : enumeration.declaredValues) {");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("checkState(value.annotations.size == 0, value.annotations.size != 0)");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("value.addAnnotation(Deprecated.newAnnotationReference)");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("checkState(value.annotations.size == 1, value.annotations.size != 1)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("enumeration.addValue(\"D\") [");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("addAnnotation(Deprecated.newAnnotationReference)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("]");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/MyAnnotation.xtend", _builder.toString());
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package myusercode");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("import myannotation.MyAnnotation");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("enum UserCode {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("A, ");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("B,");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("C");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    Pair<String, String> _mappedTo_1 = Pair.<String, String>of("myusercode/UserCode.xtend", _builder_1.toString());
    final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
        final MutableEnumerationTypeDeclaration enumerationType = it.getTypeLookup().findEnumerationType("myusercode.UserCode");
        Assert.assertEquals(4, IterableExtensions.size(enumerationType.getDeclaredValues()));
        final Type deprecatedAnnotationType = it.getTypeReferenceProvider().newTypeReference(Deprecated.class).getType();
        Iterable<? extends MutableEnumerationValueDeclaration> _declaredValues = enumerationType.getDeclaredValues();
        for (final MutableEnumerationValueDeclaration value : _declaredValues) {
            {
                Assert.assertEquals(1, IterableExtensions.size(value.getAnnotations()));
                Assert.assertNotNull(value.findAnnotation(deprecatedAnnotationType));
            }
        }
        Assert.assertEquals("D", IterableExtensions.last(enumerationType.getDeclaredValues()).getSimpleName());
        Assert.assertNotNull(enumerationType.findDeclaredValue("D"));
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Also used : MutableEnumerationTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableEnumerationTypeDeclaration) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) Type(org.eclipse.xtend.lib.macro.declaration.Type) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) MutableEnumerationValueDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableEnumerationValueDeclaration) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Test(org.junit.Test)

Example 2 with MutableEnumerationTypeDeclaration

use of org.eclipse.xtend.lib.macro.declaration.MutableEnumerationTypeDeclaration in project xtext-xtend by eclipse.

the class CheckMutableEnumerationTypeDeclarationProcessor method doTransform.

@Override
public void doTransform(final List<? extends MutableEnumerationTypeDeclaration> annotatedTargetElements, @Extension final TransformationContext context) {
    for (final MutableEnumerationTypeDeclaration enumeration : annotatedTargetElements) {
        {
            final Procedure1<String> _function = (String identifier) -> {
                final Procedure1<MutableEnumerationValueDeclaration> _function_1 = (MutableEnumerationValueDeclaration it) -> {
                };
                enumeration.addValue(identifier, _function_1).remove();
            };
            MutableAssert.assertValidJavaIdentifier("name", _function);
            final Procedure0 _function_1 = () -> {
                enumeration.addValue("foo", null);
            };
            MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "initializer cannot be null", _function_1);
            final Procedure0 _function_2 = () -> {
                final Procedure1<MutableEnumerationValueDeclaration> _function_3 = (MutableEnumerationValueDeclaration it) -> {
                };
                enumeration.addValue("A", _function_3).setVisibility(Visibility.PRIVATE);
            };
            MutableAssert.<UnsupportedOperationException>assertThrowable(UnsupportedOperationException.class, "It is not possible to change visibility of enumeration value.", _function_2);
        }
    }
}
Also used : MutableEnumerationTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableEnumerationTypeDeclaration) Procedure0(org.eclipse.xtext.xbase.lib.Procedures.Procedure0) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) MutableEnumerationValueDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableEnumerationValueDeclaration)

Example 3 with MutableEnumerationTypeDeclaration

use of org.eclipse.xtend.lib.macro.declaration.MutableEnumerationTypeDeclaration in project xtext-xtend by eclipse.

the class TypeLookupImpl method findEnumerationType.

@Override
public MutableEnumerationTypeDeclaration findEnumerationType(final String qualifiedName) {
    final Type type = this.findType(qualifiedName);
    MutableEnumerationTypeDeclaration _switchResult = null;
    boolean _matched = false;
    if (type instanceof MutableEnumerationTypeDeclaration) {
        _matched = true;
        _switchResult = ((MutableEnumerationTypeDeclaration) type);
    }
    return _switchResult;
}
Also used : MutableEnumerationTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableEnumerationTypeDeclaration) JvmType(org.eclipse.xtext.common.types.JvmType) Type(org.eclipse.xtend.lib.macro.declaration.Type) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType)

Example 4 with MutableEnumerationTypeDeclaration

use of org.eclipse.xtend.lib.macro.declaration.MutableEnumerationTypeDeclaration in project xtext-xtend by eclipse.

the class AbstractReusableActiveAnnotationTests method testRemove.

@Test
public void testRemove() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package myannotation");
    _builder.newLine();
    _builder.newLine();
    _builder.append("import com.google.common.base.Preconditions");
    _builder.newLine();
    _builder.append("import java.util.List");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.Active");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.TransformationContext");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.TransformationParticipant");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableNamedElement");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableTypeDeclaration");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(MyAnnotationProcessor)");
    _builder.newLine();
    _builder.append("annotation MyAnnotation {");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class MyAnnotationProcessor implements TransformationParticipant<MutableNamedElement> {");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(List<? extends MutableNamedElement> annotatedTargetElements,");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("for (annotatedTargetElement : annotatedTargetElements) {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("remove(annotatedTargetElement, context)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def dispatch void remove(MutableNamedElement annotatedTargetElement, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val sourceElement = annotatedTargetElement.primarySourceElement");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("Preconditions.checkState(sourceElement != null,");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("\'");
    _builder.append("\'\'a source element should not be null before removing, but: ");
    _builder.append("�", "\t\t\t");
    _builder.append("sourceElement");
    _builder.append("�", "\t\t\t");
    _builder.append("\'");
    _builder.append("\'\')");
    _builder.newLineIfNotEmpty();
    _builder.append("\t\t");
    _builder.append("Preconditions.checkState(sourceElement.primaryGeneratedJavaElement == annotatedTargetElement,");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("\'");
    _builder.append("\'\'expected: ");
    _builder.append("�", "\t\t\t");
    _builder.append("annotatedTargetElement");
    _builder.append("�", "\t\t\t");
    _builder.append(", but: ");
    _builder.append("�", "\t\t\t");
    _builder.append("sourceElement.primaryGeneratedJavaElement");
    _builder.append("�", "\t\t\t");
    _builder.append("\'");
    _builder.append("\'\')");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotatedTargetElement.remove");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("Preconditions.checkState(annotatedTargetElement.primarySourceElement == null,");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("\'");
    _builder.append("\'\'a source element should be null after removing, but: ");
    _builder.append("�", "\t\t\t");
    _builder.append("annotatedTargetElement.primarySourceElement");
    _builder.append("�", "\t\t\t");
    _builder.append("\'");
    _builder.append("\'\')");
    _builder.newLineIfNotEmpty();
    _builder.append("\t\t");
    _builder.append("Preconditions.checkState(sourceElement.primaryGeneratedJavaElement == null,");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("\'");
    _builder.append("\'\'a target element should be null after removing, but: ");
    _builder.append("�", "\t\t\t");
    _builder.append("sourceElement.primaryGeneratedJavaElement");
    _builder.append("�", "\t\t\t");
    _builder.append("\'");
    _builder.append("\'\')");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("try {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("annotatedTargetElement.remove");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("Preconditions.checkState(false, \'");
    _builder.append("\'\'");
    _builder.append("�", "\t\t\t");
    _builder.append("IllegalArgumentException");
    _builder.append("�", "\t\t\t");
    _builder.append(" is expected\'");
    _builder.append("\'\')");
    _builder.newLineIfNotEmpty();
    _builder.append("\t\t");
    _builder.append("} catch (IllegalStateException e) {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("Preconditions.checkState(e.message.startsWith(\"This element has already been removed: \"),");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("\'");
    _builder.append("\'\'Wrong error message: ");
    _builder.append("�", "\t\t\t\t");
    _builder.append("e.message");
    _builder.append("�", "\t\t\t\t");
    _builder.append("\'");
    _builder.append("\'\')");
    _builder.newLineIfNotEmpty();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def dispatch void remove(MutableTypeDeclaration annotatedTargetElement, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("try {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("annotatedTargetElement.remove");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("Preconditions.checkState(false, \'");
    _builder.append("\'\'");
    _builder.append("�", "\t\t\t");
    _builder.append("UnsupportedOperationException");
    _builder.append("�", "\t\t\t");
    _builder.append(" is expected\'");
    _builder.append("\'\')");
    _builder.newLineIfNotEmpty();
    _builder.append("\t\t");
    _builder.append("} catch (UnsupportedOperationException e) {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("Preconditions.checkState(e.message == \"The type cannot be removed.\", \'\'");
    _builder.append("\'Wrong error message: ");
    _builder.append("�", "\t\t\t");
    _builder.append("e.message");
    _builder.append("�", "\t\t\t");
    _builder.append("\'");
    _builder.append("\'\')");
    _builder.newLineIfNotEmpty();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/MyAnnotation.xtend", _builder.toString());
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package myusercode");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("import myannotation.MyAnnotation");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("class UserClass {");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("String fieldToRemove");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("new(String arg) {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("new(@MyAnnotation Integer argToRemove) {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("def methodToRemove() {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("static class NestedClass {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("static interface NestedInteface {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("static annotation NestedAnnotation {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("static enum NestedEnum {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("enum UserEnum {");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("interface UserInterface {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("def String methodToRemove();");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("annotation UserAnnotation {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("String fieldToRemove");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    Pair<String, String> _mappedTo_1 = Pair.<String, String>of("myusercode/UserCode.xtend", _builder_1.toString());
    final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
        final MutableClassDeclaration clazz = it.getTypeLookup().findClass("myusercode.UserClass");
        Assert.assertNotNull(clazz);
        Assert.assertEquals(5, IterableExtensions.size(clazz.getDeclaredMembers()));
        final MutableConstructorDeclaration constructor = IterableExtensions.head(clazz.getDeclaredConstructors());
        Assert.assertNotNull(clazz);
        Assert.assertEquals(0, IterableExtensions.size(constructor.getParameters()));
        Assert.assertEquals(1, IterableExtensions.size(Iterables.<ClassDeclaration>filter(clazz.getDeclaredMembers(), ClassDeclaration.class)));
        Assert.assertEquals(1, IterableExtensions.size(Iterables.<InterfaceDeclaration>filter(clazz.getDeclaredMembers(), InterfaceDeclaration.class)));
        Assert.assertEquals(1, IterableExtensions.size(Iterables.<EnumerationTypeDeclaration>filter(clazz.getDeclaredMembers(), EnumerationTypeDeclaration.class)));
        Assert.assertEquals(1, IterableExtensions.size(Iterables.<AnnotationTypeDeclaration>filter(clazz.getDeclaredMembers(), AnnotationTypeDeclaration.class)));
        final MutableEnumerationTypeDeclaration enum_ = it.getTypeLookup().findEnumerationType("myusercode.UserEnum");
        Assert.assertNotNull(enum_);
        final MutableInterfaceDeclaration interface_ = it.getTypeLookup().findInterface("myusercode.UserInterface");
        Assert.assertNotNull(interface_);
        Assert.assertEquals(0, IterableExtensions.size(interface_.getDeclaredMembers()));
        final MutableAnnotationTypeDeclaration annotation = it.getTypeLookup().findAnnotationType("myusercode.UserAnnotation");
        Assert.assertNotNull(annotation);
        Assert.assertEquals(0, IterableExtensions.size(annotation.getDeclaredMembers()));
        Element _primarySourceElement = it.getTracability().getPrimarySourceElement(clazz);
        final MethodDeclaration removedMethod = ((ClassDeclaration) _primarySourceElement).findDeclaredMethod("methodToRemove");
        Expression _body = removedMethod.getBody();
        final XExpression expression = ((ExpressionImpl) _body).getDelegate();
        Assert.assertNull(this.logicalContainerProvider.getLogicalContainer(expression));
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Also used : CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) MethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MethodDeclaration) Element(org.eclipse.xtend.lib.macro.declaration.Element) InterfaceDeclaration(org.eclipse.xtend.lib.macro.declaration.InterfaceDeclaration) MutableInterfaceDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableInterfaceDeclaration) MutableAnnotationTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableAnnotationTypeDeclaration) AnnotationTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.AnnotationTypeDeclaration) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) EnumerationTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.EnumerationTypeDeclaration) MutableEnumerationTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableEnumerationTypeDeclaration) MutableEnumerationTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableEnumerationTypeDeclaration) MutableInterfaceDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableInterfaceDeclaration) ClassDeclaration(org.eclipse.xtend.lib.macro.declaration.ClassDeclaration) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Expression(org.eclipse.xtend.lib.macro.expression.Expression) XExpression(org.eclipse.xtext.xbase.XExpression) MutableConstructorDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableConstructorDeclaration) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XExpression(org.eclipse.xtext.xbase.XExpression) MutableAnnotationTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableAnnotationTypeDeclaration) Test(org.junit.Test)

Example 5 with MutableEnumerationTypeDeclaration

use of org.eclipse.xtend.lib.macro.declaration.MutableEnumerationTypeDeclaration in project xtext-xtend by eclipse.

the class AbstractReusableActiveAnnotationTests method testRemove.

@Test
public void testRemove() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package myannotation");
    _builder.newLine();
    _builder.newLine();
    _builder.append("import com.google.common.base.Preconditions");
    _builder.newLine();
    _builder.append("import java.util.List");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.Active");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.TransformationContext");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.TransformationParticipant");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableNamedElement");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableTypeDeclaration");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(MyAnnotationProcessor)");
    _builder.newLine();
    _builder.append("annotation MyAnnotation {");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class MyAnnotationProcessor implements TransformationParticipant<MutableNamedElement> {");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(List<? extends MutableNamedElement> annotatedTargetElements,");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("for (annotatedTargetElement : annotatedTargetElements) {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("remove(annotatedTargetElement, context)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def dispatch void remove(MutableNamedElement annotatedTargetElement, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val sourceElement = annotatedTargetElement.primarySourceElement");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("Preconditions.checkState(sourceElement != null,");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("\'");
    _builder.append("\'\'a source element should not be null before removing, but: ");
    _builder.append("�", "\t\t\t");
    _builder.append("sourceElement");
    _builder.append("�", "\t\t\t");
    _builder.append("\'");
    _builder.append("\'\')");
    _builder.newLineIfNotEmpty();
    _builder.append("\t\t");
    _builder.append("Preconditions.checkState(sourceElement.primaryGeneratedJavaElement == annotatedTargetElement,");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("\'");
    _builder.append("\'\'expected: ");
    _builder.append("�", "\t\t\t");
    _builder.append("annotatedTargetElement");
    _builder.append("�", "\t\t\t");
    _builder.append(", but: ");
    _builder.append("�", "\t\t\t");
    _builder.append("sourceElement.primaryGeneratedJavaElement");
    _builder.append("�", "\t\t\t");
    _builder.append("\'");
    _builder.append("\'\')");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotatedTargetElement.remove");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("Preconditions.checkState(annotatedTargetElement.primarySourceElement == null,");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("\'");
    _builder.append("\'\'a source element should be null after removing, but: ");
    _builder.append("�", "\t\t\t");
    _builder.append("annotatedTargetElement.primarySourceElement");
    _builder.append("�", "\t\t\t");
    _builder.append("\'");
    _builder.append("\'\')");
    _builder.newLineIfNotEmpty();
    _builder.append("\t\t");
    _builder.append("Preconditions.checkState(sourceElement.primaryGeneratedJavaElement == null,");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("\'");
    _builder.append("\'\'a target element should be null after removing, but: ");
    _builder.append("�", "\t\t\t");
    _builder.append("sourceElement.primaryGeneratedJavaElement");
    _builder.append("�", "\t\t\t");
    _builder.append("\'");
    _builder.append("\'\')");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("try {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("annotatedTargetElement.remove");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("Preconditions.checkState(false, \'");
    _builder.append("\'\'");
    _builder.append("�", "\t\t\t");
    _builder.append("IllegalArgumentException");
    _builder.append("�", "\t\t\t");
    _builder.append(" is expected\'");
    _builder.append("\'\')");
    _builder.newLineIfNotEmpty();
    _builder.append("\t\t");
    _builder.append("} catch (IllegalStateException e) {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("Preconditions.checkState(e.message.startsWith(\"This element has already been removed: \"),");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("\'");
    _builder.append("\'\'Wrong error message: ");
    _builder.append("�", "\t\t\t\t");
    _builder.append("e.message");
    _builder.append("�", "\t\t\t\t");
    _builder.append("\'");
    _builder.append("\'\')");
    _builder.newLineIfNotEmpty();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def dispatch void remove(MutableTypeDeclaration annotatedTargetElement, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("try {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("annotatedTargetElement.remove");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("Preconditions.checkState(false, \'");
    _builder.append("\'\'");
    _builder.append("�", "\t\t\t");
    _builder.append("UnsupportedOperationException");
    _builder.append("�", "\t\t\t");
    _builder.append(" is expected\'");
    _builder.append("\'\')");
    _builder.newLineIfNotEmpty();
    _builder.append("\t\t");
    _builder.append("} catch (UnsupportedOperationException e) {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("Preconditions.checkState(e.message == \"The type cannot be removed.\", \'\'");
    _builder.append("\'Wrong error message: ");
    _builder.append("�", "\t\t\t");
    _builder.append("e.message");
    _builder.append("�", "\t\t\t");
    _builder.append("\'");
    _builder.append("\'\')");
    _builder.newLineIfNotEmpty();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/MyAnnotation.xtend", _builder.toString());
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package myusercode");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("import myannotation.MyAnnotation");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("class UserClass {");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("String fieldToRemove");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("new(String arg) {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("new(@MyAnnotation Integer argToRemove) {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("def methodToRemove() {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("static class NestedClass {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("static interface NestedInteface {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("static annotation NestedAnnotation {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("static enum NestedEnum {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("enum UserEnum {");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("interface UserInterface {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("def String methodToRemove();");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("annotation UserAnnotation {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("String fieldToRemove");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    Pair<String, String> _mappedTo_1 = Pair.<String, String>of("myusercode/UserCode.xtend", _builder_1.toString());
    final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
        final MutableClassDeclaration clazz = it.getTypeLookup().findClass("myusercode.UserClass");
        Assert.assertNotNull(clazz);
        Assert.assertEquals(5, IterableExtensions.size(clazz.getDeclaredMembers()));
        final MutableConstructorDeclaration constructor = IterableExtensions.head(clazz.getDeclaredConstructors());
        Assert.assertNotNull(clazz);
        Assert.assertEquals(0, IterableExtensions.size(constructor.getParameters()));
        Assert.assertEquals(1, IterableExtensions.size(Iterables.<ClassDeclaration>filter(clazz.getDeclaredMembers(), ClassDeclaration.class)));
        Assert.assertEquals(1, IterableExtensions.size(Iterables.<InterfaceDeclaration>filter(clazz.getDeclaredMembers(), InterfaceDeclaration.class)));
        Assert.assertEquals(1, IterableExtensions.size(Iterables.<EnumerationTypeDeclaration>filter(clazz.getDeclaredMembers(), EnumerationTypeDeclaration.class)));
        Assert.assertEquals(1, IterableExtensions.size(Iterables.<AnnotationTypeDeclaration>filter(clazz.getDeclaredMembers(), AnnotationTypeDeclaration.class)));
        final MutableEnumerationTypeDeclaration enum_ = it.getTypeLookup().findEnumerationType("myusercode.UserEnum");
        Assert.assertNotNull(enum_);
        final MutableInterfaceDeclaration interface_ = it.getTypeLookup().findInterface("myusercode.UserInterface");
        Assert.assertNotNull(interface_);
        Assert.assertEquals(0, IterableExtensions.size(interface_.getDeclaredMembers()));
        final MutableAnnotationTypeDeclaration annotation = it.getTypeLookup().findAnnotationType("myusercode.UserAnnotation");
        Assert.assertNotNull(annotation);
        Assert.assertEquals(0, IterableExtensions.size(annotation.getDeclaredMembers()));
        Element _primarySourceElement = it.getTracability().getPrimarySourceElement(clazz);
        final MethodDeclaration removedMethod = ((ClassDeclaration) _primarySourceElement).findDeclaredMethod("methodToRemove");
        Expression _body = removedMethod.getBody();
        final XExpression expression = ((ExpressionImpl) _body).getDelegate();
        Assert.assertNull(this.logicalContainerProvider.getLogicalContainer(expression));
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Also used : CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) MethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MethodDeclaration) Element(org.eclipse.xtend.lib.macro.declaration.Element) InterfaceDeclaration(org.eclipse.xtend.lib.macro.declaration.InterfaceDeclaration) MutableInterfaceDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableInterfaceDeclaration) MutableAnnotationTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableAnnotationTypeDeclaration) AnnotationTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.AnnotationTypeDeclaration) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) EnumerationTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.EnumerationTypeDeclaration) MutableEnumerationTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableEnumerationTypeDeclaration) MutableEnumerationTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableEnumerationTypeDeclaration) MutableInterfaceDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableInterfaceDeclaration) ClassDeclaration(org.eclipse.xtend.lib.macro.declaration.ClassDeclaration) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Expression(org.eclipse.xtend.lib.macro.expression.Expression) XExpression(org.eclipse.xtext.xbase.XExpression) MutableConstructorDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableConstructorDeclaration) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XExpression(org.eclipse.xtext.xbase.XExpression) MutableAnnotationTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableAnnotationTypeDeclaration) Test(org.junit.Test)

Aggregations

MutableEnumerationTypeDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableEnumerationTypeDeclaration)6 CompilationUnitImpl (org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl)4 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)4 Test (org.junit.Test)4 MutableEnumerationValueDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableEnumerationValueDeclaration)3 Type (org.eclipse.xtend.lib.macro.declaration.Type)3 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)3 AnnotationTypeDeclaration (org.eclipse.xtend.lib.macro.declaration.AnnotationTypeDeclaration)2 ClassDeclaration (org.eclipse.xtend.lib.macro.declaration.ClassDeclaration)2 Element (org.eclipse.xtend.lib.macro.declaration.Element)2 EnumerationTypeDeclaration (org.eclipse.xtend.lib.macro.declaration.EnumerationTypeDeclaration)2 InterfaceDeclaration (org.eclipse.xtend.lib.macro.declaration.InterfaceDeclaration)2 MethodDeclaration (org.eclipse.xtend.lib.macro.declaration.MethodDeclaration)2 MutableAnnotationTypeDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableAnnotationTypeDeclaration)2 MutableClassDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration)2 MutableConstructorDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableConstructorDeclaration)2 MutableInterfaceDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableInterfaceDeclaration)2 MutableMethodDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration)2 Expression (org.eclipse.xtend.lib.macro.expression.Expression)2 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)2