Search in sources :

Example 1 with MutableConstructorDeclaration

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

the class JvmTypeDeclarationImpl method addConstructor.

public MutableConstructorDeclaration addConstructor(final Procedure1<MutableConstructorDeclaration> initializer) {
    this.checkMutable();
    Preconditions.checkArgument((initializer != null), "initializer cannot be null");
    final Function1<JvmConstructor, Boolean> _function = (JvmConstructor it) -> {
        return Boolean.valueOf(this.getCompilationUnit().getTypeExtensions().isSingleSyntheticDefaultConstructor(it));
    };
    final JvmConstructor constructor = IterableExtensions.<JvmConstructor>findFirst(Iterables.<JvmConstructor>filter(this.getDelegate().getMembers(), JvmConstructor.class), _function);
    if ((constructor != null)) {
        MemberDeclaration _memberDeclaration = this.getCompilationUnit().toMemberDeclaration(constructor);
        final MutableConstructorDeclaration mutableConstructorDeclaration = ((MutableConstructorDeclaration) _memberDeclaration);
        mutableConstructorDeclaration.remove();
    }
    final JvmConstructor newConstructor = TypesFactory.eINSTANCE.createJvmConstructor();
    newConstructor.setVisibility(JvmVisibility.PUBLIC);
    newConstructor.setSimpleName(this.getSimpleName());
    this.getDelegate().getMembers().add(newConstructor);
    MemberDeclaration _memberDeclaration_1 = this.getCompilationUnit().toMemberDeclaration(newConstructor);
    final MutableConstructorDeclaration mutableConstructorDeclaration_1 = ((MutableConstructorDeclaration) _memberDeclaration_1);
    initializer.apply(mutableConstructorDeclaration_1);
    return mutableConstructorDeclaration_1;
}
Also used : MemberDeclaration(org.eclipse.xtend.lib.macro.declaration.MemberDeclaration) MutableConstructorDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableConstructorDeclaration) JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor)

Example 2 with MutableConstructorDeclaration

use of org.eclipse.xtend.lib.macro.declaration.MutableConstructorDeclaration 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 3 with MutableConstructorDeclaration

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

the class ImmutableProcessor method doTransform.

@Override
public void doTransform(final List<? extends MutableClassDeclaration> annotatedTargetElements, @Extension final TransformationContext context) {
    for (final MutableClassDeclaration clazz : annotatedTargetElements) {
        {
            final Iterable<? extends MutableFieldDeclaration> fields = clazz.getDeclaredFields();
            final Procedure1<MutableConstructorDeclaration> _function = (MutableConstructorDeclaration it) -> {
                for (final MutableFieldDeclaration f : fields) {
                    {
                        f.markAsInitializedBy(it);
                        it.addParameter(f.getSimpleName(), f.getType());
                    }
                }
                final CompilationStrategy _function_1 = (CompilationStrategy.CompilationContext it_1) -> {
                    StringConcatenation _builder = new StringConcatenation();
                    {
                        for (final MutableFieldDeclaration f_1 : fields) {
                            _builder.append("this.");
                            String _simpleName = f_1.getSimpleName();
                            _builder.append(_simpleName);
                            _builder.append(" = ");
                            String _simpleName_1 = f_1.getSimpleName();
                            _builder.append(_simpleName_1);
                            _builder.append(";");
                            _builder.newLineIfNotEmpty();
                        }
                    }
                    return _builder;
                };
                it.setBody(_function_1);
            };
            clazz.addConstructor(_function);
            for (final MutableFieldDeclaration f : fields) {
                {
                    f.setFinal(true);
                    String _firstUpper = StringExtensions.toFirstUpper(f.getSimpleName());
                    final String getterName = ("get" + _firstUpper);
                    final Procedure1<MutableMethodDeclaration> _function_1 = (MutableMethodDeclaration it) -> {
                        f.markAsRead();
                        it.setReturnType(f.getType());
                        final CompilationStrategy _function_2 = (CompilationStrategy.CompilationContext it_1) -> {
                            StringConcatenation _builder = new StringConcatenation();
                            _builder.append("return ");
                            String _simpleName = f.getSimpleName();
                            _builder.append(_simpleName);
                            _builder.append(";");
                            _builder.newLineIfNotEmpty();
                            return _builder;
                        };
                        it.setBody(_function_2);
                    };
                    this.tryAddMethod(clazz, getterName, _function_1);
                }
            }
            final Procedure1<MutableMethodDeclaration> _function_1 = (MutableMethodDeclaration it) -> {
                it.setReturnType(context.getPrimitiveInt());
                it.addAnnotation(context.newAnnotationReference(Override.class));
                final CompilationStrategy _function_2 = (CompilationStrategy.CompilationContext it_1) -> {
                    StringConcatenation _builder = new StringConcatenation();
                    _builder.append("final int prime = 31;");
                    _builder.newLine();
                    {
                        boolean _extendsSomethingWithProperHashCode = this.getExtendsSomethingWithProperHashCode(clazz);
                        if (_extendsSomethingWithProperHashCode) {
                            _builder.append("int result = super.hashCode();");
                            _builder.newLine();
                        } else {
                            _builder.append("int result = 1;");
                            _builder.newLine();
                        }
                    }
                    {
                        for (final MutableFieldDeclaration f_1 : fields) {
                            {
                                TypeReference _type = f_1.getType();
                                TypeReference _primitiveBoolean = context.getPrimitiveBoolean();
                                boolean _equals = Objects.equal(_type, _primitiveBoolean);
                                if (_equals) {
                                    _builder.append("result = prime * result + (");
                                    String _simpleName = f_1.getSimpleName();
                                    _builder.append(_simpleName);
                                    _builder.append(" ? 1231 : 1237);");
                                    _builder.newLineIfNotEmpty();
                                } else {
                                    TypeReference _primitiveInt = context.getPrimitiveInt();
                                    TypeReference _primitiveChar = context.getPrimitiveChar();
                                    TypeReference _primitiveByte = context.getPrimitiveByte();
                                    TypeReference _primitiveShort = context.getPrimitiveShort();
                                    boolean _contains = Collections.<TypeReference>unmodifiableSet(CollectionLiterals.<TypeReference>newHashSet(_primitiveInt, _primitiveChar, _primitiveByte, _primitiveShort)).contains(f_1.getType());
                                    if (_contains) {
                                        _builder.append("result = prime * result + ");
                                        String _simpleName_1 = f_1.getSimpleName();
                                        _builder.append(_simpleName_1);
                                        _builder.append(";");
                                        _builder.newLineIfNotEmpty();
                                    } else {
                                        TypeReference _primitiveLong = context.getPrimitiveLong();
                                        TypeReference _type_1 = f_1.getType();
                                        boolean _equals_1 = Objects.equal(_primitiveLong, _type_1);
                                        if (_equals_1) {
                                            _builder.append("result = prime * result + (int) (");
                                            String _simpleName_2 = f_1.getSimpleName();
                                            _builder.append(_simpleName_2);
                                            _builder.append(" ^ (");
                                            String _simpleName_3 = f_1.getSimpleName();
                                            _builder.append(_simpleName_3);
                                            _builder.append(" >>> 32));");
                                            _builder.newLineIfNotEmpty();
                                        } else {
                                            TypeReference _primitiveFloat = context.getPrimitiveFloat();
                                            TypeReference _type_2 = f_1.getType();
                                            boolean _equals_2 = Objects.equal(_primitiveFloat, _type_2);
                                            if (_equals_2) {
                                                _builder.append("result = prime * result + Float.floatToIntBits(");
                                                String _simpleName_4 = f_1.getSimpleName();
                                                _builder.append(_simpleName_4);
                                                _builder.append(");");
                                                _builder.newLineIfNotEmpty();
                                            } else {
                                                TypeReference _primitiveDouble = context.getPrimitiveDouble();
                                                TypeReference _type_3 = f_1.getType();
                                                boolean _equals_3 = Objects.equal(_primitiveDouble, _type_3);
                                                if (_equals_3) {
                                                    _builder.append("result = prime * result + (int) (Double.doubleToLongBits(");
                                                    String _simpleName_5 = f_1.getSimpleName();
                                                    _builder.append(_simpleName_5);
                                                    _builder.append(") ^ (Double.doubleToLongBits(");
                                                    String _simpleName_6 = f_1.getSimpleName();
                                                    _builder.append(_simpleName_6);
                                                    _builder.append(") >>> 32));");
                                                    _builder.newLineIfNotEmpty();
                                                } else {
                                                    _builder.append("result = prime * result + ((");
                                                    String _simpleName_7 = f_1.getSimpleName();
                                                    _builder.append(_simpleName_7);
                                                    _builder.append("== null) ? 0 : ");
                                                    String _simpleName_8 = f_1.getSimpleName();
                                                    _builder.append(_simpleName_8);
                                                    _builder.append(".hashCode());");
                                                    _builder.newLineIfNotEmpty();
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    _builder.append("return result;");
                    _builder.newLine();
                    return _builder;
                };
                it.setBody(_function_2);
            };
            this.tryAddMethod(clazz, "hashCode", _function_1);
        }
    }
}
Also used : MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) MutableConstructorDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableConstructorDeclaration) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) TypeReference(org.eclipse.xtend.lib.macro.declaration.TypeReference) CompilationStrategy(org.eclipse.xtend.lib.macro.declaration.CompilationStrategy)

Example 4 with MutableConstructorDeclaration

use of org.eclipse.xtend.lib.macro.declaration.MutableConstructorDeclaration 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

MutableConstructorDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableConstructorDeclaration)4 MutableClassDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration)3 MutableMethodDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration)3 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)3 CompilationUnitImpl (org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl)2 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 MutableEnumerationTypeDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableEnumerationTypeDeclaration)2 MutableInterfaceDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableInterfaceDeclaration)2 Expression (org.eclipse.xtend.lib.macro.expression.Expression)2 XExpression (org.eclipse.xtext.xbase.XExpression)2 Test (org.junit.Test)2 CompilationStrategy (org.eclipse.xtend.lib.macro.declaration.CompilationStrategy)1 MemberDeclaration (org.eclipse.xtend.lib.macro.declaration.MemberDeclaration)1 MutableFieldDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration)1