Search in sources :

Example 96 with Procedure1

use of org.eclipse.xtext.xbase.lib.Procedures.Procedure1 in project xtext-xtend by eclipse.

the class AccessorsProcessor method tryAddMethod.

public MutableMethodDeclaration tryAddMethod(final MutableTypeDeclaration it, final String name, final Procedure1<? super MutableMethodDeclaration> initializer) {
    MutableMethodDeclaration _elvis = null;
    MutableMethodDeclaration _findDeclaredMethod = it.findDeclaredMethod(name);
    if (_findDeclaredMethod != null) {
        _elvis = _findDeclaredMethod;
    } else {
        MutableMethodDeclaration _addMethod = it.addMethod(name, ((Procedure1<MutableMethodDeclaration>) initializer));
        _elvis = _addMethod;
    }
    return _elvis;
}
Also used : MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1)

Example 97 with Procedure1

use of org.eclipse.xtext.xbase.lib.Procedures.Procedure1 in project xtext-xtend by eclipse.

the class AccessorsProcessor method doTransform.

@Override
public void doTransform(final List<? extends MutableFieldDeclaration> javaFields, @Extension final TransformationContext context) {
    for (final MutableFieldDeclaration f : javaFields) {
        {
            String _firstUpper = StringExtensions.toFirstUpper(f.getSimpleName());
            final String getterName = ("get" + _firstUpper);
            String _firstUpper_1 = StringExtensions.toFirstUpper(f.getSimpleName());
            final String setterName = ("set" + _firstUpper_1);
            final Procedure1<MutableMethodDeclaration> _function = (MutableMethodDeclaration it) -> {
                it.setReturnType(f.getType());
                final CompilationStrategy _function_1 = (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_1);
            };
            this.tryAddMethod(f.getDeclaringType(), getterName, _function);
            boolean _isFinal = f.isFinal();
            boolean _not = (!_isFinal);
            if (_not) {
                final Procedure1<MutableMethodDeclaration> _function_1 = (MutableMethodDeclaration it) -> {
                    it.setReturnType(context.getPrimitiveVoid());
                    it.addParameter(f.getSimpleName(), f.getType());
                    final CompilationStrategy _function_2 = (CompilationStrategy.CompilationContext it_1) -> {
                        StringConcatenation _builder = new StringConcatenation();
                        _builder.append("this.");
                        String _simpleName = f.getSimpleName();
                        _builder.append(_simpleName);
                        _builder.append(" = ");
                        String _simpleName_1 = f.getSimpleName();
                        _builder.append(_simpleName_1);
                        _builder.append(";");
                        _builder.newLineIfNotEmpty();
                        return _builder;
                    };
                    it.setBody(_function_2);
                };
                this.tryAddMethod(f.getDeclaringType(), setterName, _function_1);
            }
        }
    }
}
Also used : MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) CompilationStrategy(org.eclipse.xtend.lib.macro.declaration.CompilationStrategy)

Example 98 with Procedure1

use of org.eclipse.xtext.xbase.lib.Procedures.Procedure1 in project xtext-xtend by eclipse.

the class CheckMutableClassDeclarationProcessor method doTransform.

@Override
public void doTransform(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
    final Procedure0 _function = () -> {
        annotatedClass.setImplementedInterfaces(null);
    };
    MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "superIntefaces cannot be null", _function);
    final Procedure0 _function_1 = () -> {
        annotatedClass.setImplementedInterfaces(Collections.<TypeReference>unmodifiableList(CollectionLiterals.<TypeReference>newArrayList((TypeReference) null)));
    };
    MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "superIntefaces cannot contain null", _function_1);
    annotatedClass.setImplementedInterfaces(CollectionLiterals.<TypeReference>emptyList());
    final Procedure1<String> _function_2 = (String identifier) -> {
        annotatedClass.addTypeParameter(identifier).remove();
    };
    MutableAssert.assertValidJavaIdentifier("name", _function_2);
    final Procedure0 _function_3 = () -> {
        annotatedClass.addTypeParameter("T", ((TypeReference[]) null));
    };
    MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "upperBounds cannot be null", _function_3);
    final Procedure0 _function_4 = () -> {
        annotatedClass.addTypeParameter("T", new TypeReference[] { null });
    };
    MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "upperBounds cannot contain null", _function_4);
    final MutableTypeParameterDeclaration typeParameter = annotatedClass.addTypeParameter("T");
    final Procedure1<String> _function_5 = (String identifier) -> {
        typeParameter.setSimpleName(identifier);
    };
    MutableAssert.assertValidJavaIdentifier("name", _function_5);
    final Procedure0 _function_6 = () -> {
        typeParameter.setUpperBounds(null);
    };
    MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "upperBounds cannot be null", _function_6);
    final Procedure0 _function_7 = () -> {
        typeParameter.setUpperBounds(Collections.<TypeReference>unmodifiableList(CollectionLiterals.<TypeReference>newArrayList((TypeReference) null)));
    };
    MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "upperBounds cannot contain null", _function_7);
    typeParameter.setUpperBounds(CollectionLiterals.<TypeReference>emptyList());
    final Procedure0 _function_8 = () -> {
        annotatedClass.setSimpleName(null);
    };
    MutableAssert.<UnsupportedOperationException>assertThrowable(UnsupportedOperationException.class, "The type cannot be renamed.", _function_8);
    final Procedure1<String> _function_9 = (String identifier) -> {
        final Procedure1<MutableFieldDeclaration> _function_10 = (MutableFieldDeclaration it) -> {
            it.setType(context.newTypeReference(String.class));
        };
        annotatedClass.addField(identifier, _function_10).remove();
    };
    MutableAssert.assertValidJavaIdentifier("name", _function_9);
    final Procedure0 _function_10 = () -> {
        annotatedClass.addField("foo", null);
    };
    MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "initializer cannot be null", _function_10);
    final Procedure1<String> _function_11 = (String identifier) -> {
        final Procedure1<MutableMethodDeclaration> _function_12 = (MutableMethodDeclaration it) -> {
        };
        annotatedClass.addMethod(identifier, _function_12).remove();
    };
    MutableAssert.assertValidJavaIdentifier("name", _function_11);
    final Procedure0 _function_12 = () -> {
        annotatedClass.addMethod("foo", null);
    };
    MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "initializer cannot be null", _function_12);
    final Procedure0 _function_13 = () -> {
        annotatedClass.addConstructor(null);
    };
    MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "initializer cannot be null", _function_13);
    final Procedure1<MutableMethodDeclaration> _function_14 = (MutableMethodDeclaration it) -> {
    };
    final MutableMethodDeclaration method = annotatedClass.addMethod("foo", _function_14);
    annotatedClass.findDeclaredMethod(null);
    annotatedClass.findDeclaredMethod("foo");
    final Procedure0 _function_15 = () -> {
        annotatedClass.findDeclaredMethod("foo", ((TypeReference[]) null));
    };
    MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "parameterTypes cannot be null", _function_15);
    final Procedure0 _function_16 = () -> {
        annotatedClass.findDeclaredMethod("foo", new TypeReference[] { null });
    };
    MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "parameterTypes cannot contain null", _function_16);
    annotatedClass.findDeclaredMethod("foo", ((TypeReference[]) Conversions.unwrapArray(CollectionLiterals.<TypeReference>emptyList(), TypeReference.class)));
    method.remove();
    final Procedure0 _function_17 = () -> {
        annotatedClass.findDeclaredConstructor(((TypeReference[]) null));
    };
    MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "parameterTypes cannot be null", _function_17);
    final Procedure0 _function_18 = () -> {
        annotatedClass.findDeclaredConstructor(new TypeReference[] { null });
    };
    MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "parameterTypes cannot contain null", _function_18);
    annotatedClass.findDeclaredConstructor(((TypeReference[]) Conversions.unwrapArray(CollectionLiterals.<TypeReference>emptyList(), TypeReference.class)));
}
Also used : Procedure0(org.eclipse.xtext.xbase.lib.Procedures.Procedure0) MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) TypeReference(org.eclipse.xtend.lib.macro.declaration.TypeReference) MutableTypeParameterDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableTypeParameterDeclaration)

Example 99 with Procedure1

use of org.eclipse.xtext.xbase.lib.Procedures.Procedure1 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 100 with Procedure1

use of org.eclipse.xtext.xbase.lib.Procedures.Procedure1 in project xtext-xtend by eclipse.

the class ImmutableProcessor method tryAddMethod.

public MutableMethodDeclaration tryAddMethod(final MutableClassDeclaration it, final String name, final Procedure1<? super MutableMethodDeclaration> initializer) {
    MutableMethodDeclaration _elvis = null;
    MutableMethodDeclaration _findDeclaredMethod = it.findDeclaredMethod(name);
    if (_findDeclaredMethod != null) {
        _elvis = _findDeclaredMethod;
    } else {
        MutableMethodDeclaration _addMethod = it.addMethod(name, ((Procedure1<MutableMethodDeclaration>) initializer));
        _elvis = _addMethod;
    }
    return _elvis;
}
Also used : MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1)

Aggregations

Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)173 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)99 Test (org.junit.Test)98 IHiddenRegionFormatter (org.eclipse.xtext.formatting2.IHiddenRegionFormatter)28 IFormattableDocument (org.eclipse.xtext.formatting2.IFormattableDocument)20 GenericFormatter (org.eclipse.xtext.formatting2.internal.GenericFormatter)20 ITextRegionExtensions (org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions)20 GenericFormatterTestRequest (org.eclipse.xtext.formatting2.internal.GenericFormatterTestRequest)18 AbstractXtendCompilerTest (org.eclipse.xtend.core.tests.compiler.AbstractXtendCompilerTest)16 CompilationTestHelper (org.eclipse.xtext.xbase.testing.CompilationTestHelper)16 EList (org.eclipse.emf.common.util.EList)14 IChangeSerializer (org.eclipse.xtext.ide.serializer.IChangeSerializer)14 MapBasedPreferenceValues (org.eclipse.xtext.preferences.MapBasedPreferenceValues)14 IDList (org.eclipse.xtext.formatting2.internal.formattertestlanguage.IDList)13 EObject (org.eclipse.emf.ecore.EObject)11 Method (java.lang.reflect.Method)10 AbstractBuilderTest (org.eclipse.xtend.ide.tests.codebuilder.AbstractBuilderTest)10 ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)10 MandatoryValue (org.eclipse.xtext.ide.tests.testlanguage.partialSerializationTestLanguage.MandatoryValue)10 Field (java.lang.reflect.Field)9