Search in sources :

Example 1 with CompilationStrategy

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

the class AccessObjectProcessor method doTransform.

@Override
public void doTransform(final List<? extends MutableClassDeclaration> annotatedSourceClasses, @Extension final TransformationContext ctx) {
    final Consumer<MutableClassDeclaration> _function = (MutableClassDeclaration it) -> {
        final Consumer<MutableFieldDeclaration> _function_1 = (MutableFieldDeclaration field) -> {
            MutableTypeDeclaration _declaringType = field.getDeclaringType();
            String _firstUpper = StringExtensions.toFirstUpper(field.getSimpleName());
            String _plus = ("get" + _firstUpper);
            final Procedure1<MutableMethodDeclaration> _function_2 = (MutableMethodDeclaration it_1) -> {
                it_1.setReturnType(field.getType());
                final CompilationStrategy _function_3 = (CompilationStrategy.CompilationContext it_2) -> {
                    StringConcatenation _builder = new StringConcatenation();
                    _builder.append("return this.");
                    String _simpleName = field.getSimpleName();
                    _builder.append(_simpleName);
                    _builder.append(";");
                    _builder.newLineIfNotEmpty();
                    return _builder;
                };
                it_1.setBody(_function_3);
            };
            _declaringType.addMethod(_plus, _function_2);
        };
        it.getDeclaredFields().forEach(_function_1);
        String _qualifiedName = it.getQualifiedName();
        int _length = it.getQualifiedName().length();
        int _length_1 = it.getSimpleName().length();
        int _minus = (_length - _length_1);
        final String pkg = _qualifiedName.substring(0, _minus);
        final TypeReference ser = ctx.newTypeReference(Serializable.class);
        if ((ser == null)) {
            ctx.addError(it, "Cannot find Serializable");
        }
        String _simpleName = it.getSimpleName();
        final String PVersionName = ((pkg + "P") + _simpleName);
        final MutableClassDeclaration p = ctx.findClass(PVersionName);
        if ((p == null)) {
            ctx.addError(it, (("Class " + PVersionName) + " not found"));
        }
        if (((p != null) && (ser != null))) {
            final LinkedList<TypeReference> pIfcs = new LinkedList<TypeReference>();
            pIfcs.add(ser);
            p.setImplementedInterfaces(pIfcs);
        }
        String _simpleName_1 = it.getSimpleName();
        final String GVersionName = ((pkg + "G") + _simpleName_1);
        final MutableClassDeclaration g = ctx.findClass(GVersionName);
        if ((g == null)) {
            ctx.addError(it, (("Class " + GVersionName) + " not found"));
        }
        if (((g != null) && (ser != null))) {
            final LinkedList<TypeReference> gIfcs = new LinkedList<TypeReference>();
            gIfcs.add(ser);
            g.setImplementedInterfaces(gIfcs);
        }
    };
    annotatedSourceClasses.forEach(_function);
}
Also used : Serializable(java.io.Serializable) MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) MutableTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableTypeDeclaration) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) LinkedList(java.util.LinkedList) Consumer(java.util.function.Consumer) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) TypeReference(org.eclipse.xtend.lib.macro.declaration.TypeReference) CompilationStrategy(org.eclipse.xtend.lib.macro.declaration.CompilationStrategy)

Example 2 with CompilationStrategy

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

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

the class ArtificialMethodsProcessor method doTransform.

@Override
public void doTransform(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
    final Procedure1<MutableMethodDeclaration> _function = (MutableMethodDeclaration it) -> {
        final CompilationStrategy _function_1 = (CompilationStrategy.CompilationContext it_1) -> {
            StringConcatenation _builder = new StringConcatenation();
            return _builder;
        };
        it.setBody(_function_1);
    };
    annotatedClass.addMethod("blank", _function);
    final Procedure1<MutableMethodDeclaration> _function_1 = (MutableMethodDeclaration it) -> {
        final CompilationStrategy _function_2 = (CompilationStrategy.CompilationContext it_1) -> {
            StringConcatenation _builder = new StringConcatenation();
            _builder.append("\t\t\t\t");
            _builder.newLine();
            return _builder;
        };
        it.setBody(_function_2);
    };
    annotatedClass.addMethod("blank_1", _function_1);
    final Procedure1<MutableMethodDeclaration> _function_2 = (MutableMethodDeclaration it) -> {
        final CompilationStrategy _function_3 = (CompilationStrategy.CompilationContext it_1) -> {
            return "";
        };
        it.setBody(_function_3);
    };
    annotatedClass.addMethod("blank_2", _function_2);
    final Procedure1<MutableMethodDeclaration> _function_3 = (MutableMethodDeclaration it) -> {
        final CompilationStrategy _function_4 = (CompilationStrategy.CompilationContext it_1) -> {
            return "\n";
        };
        it.setBody(_function_4);
    };
    annotatedClass.addMethod("blank_3", _function_3);
    final Procedure1<MutableMethodDeclaration> _function_4 = (MutableMethodDeclaration it) -> {
        final CompilationStrategy _function_5 = (CompilationStrategy.CompilationContext it_1) -> {
            StringConcatenation _builder = new StringConcatenation();
            _builder.append("int foo = 42;");
            _builder.newLine();
            return _builder;
        };
        it.setBody(_function_5);
    };
    annotatedClass.addMethod("blank_4", _function_4);
    final Procedure1<MutableMethodDeclaration> _function_5 = (MutableMethodDeclaration it) -> {
        StringConcatenationClient _client = new StringConcatenationClient() {

            @Override
            protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                _builder.append("int foo = 42;");
                _builder.newLine();
            }
        };
        it.setBody(_client);
    };
    annotatedClass.addMethod("blank_5", _function_5);
    final Procedure1<MutableMethodDeclaration> _function_6 = (MutableMethodDeclaration it) -> {
        StringConcatenationClient _client = new StringConcatenationClient() {

            @Override
            protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                _builder.append("int foo = 42;");
            }
        };
        it.setBody(_client);
    };
    annotatedClass.addMethod("blank_6", _function_6);
    final Procedure1<MutableMethodDeclaration> _function_7 = (MutableMethodDeclaration it) -> {
        StringConcatenationClient _client = new StringConcatenationClient() {

            @Override
            protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                _builder.append(List.class);
                _builder.append("<? extends Object> list = new ");
                _builder.append(ArrayList.class);
                _builder.append("<");
                _builder.append(BigDecimal.class);
                _builder.append(">();");
            }
        };
        it.setBody(_client);
    };
    annotatedClass.addMethod("blank_7", _function_7);
}
Also used : StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) CompilationStrategy(org.eclipse.xtend.lib.macro.declaration.CompilationStrategy) BigDecimal(java.math.BigDecimal)

Example 4 with CompilationStrategy

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

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

the class DeclarationsTest method testMutableClassDeclaration.

@Test
public void testMutableClassDeclaration() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package foo");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class MyClass<T extends CharSequence> {");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("String myField");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("new(String initial) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("this.myField = initial");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def <T2 extends CharSequence> MyClass myMethod(T2 a, T b) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("myField = myField + a + b");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("return this");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
        final MutableClassDeclaration genClazz = it.getTypeLookup().findClass("foo.MyClass");
        final Procedure1<MutableMethodDeclaration> _function_1 = (MutableMethodDeclaration it_1) -> {
            CompilationUnit _compilationUnit = genClazz.getCompilationUnit();
            it_1.setReturnType(((CompilationUnitImpl) _compilationUnit).getTypeReferenceProvider().getString());
            it_1.setVisibility(Visibility.PRIVATE);
            final CompilationStrategy _function_2 = (CompilationStrategy.CompilationContext it_2) -> {
                StringConcatenation _builder_1 = new StringConcatenation();
                _builder_1.append("return \"foo\";");
                _builder_1.newLine();
                return _builder_1;
            };
            it_1.setBody(_function_2);
        };
        genClazz.addMethod("newMethod", _function_1);
        final MutableMethodDeclaration mutableMethod = genClazz.findDeclaredMethod("newMethod");
        Assert.assertSame(mutableMethod, ((Object[]) Conversions.unwrapArray(genClazz.getDeclaredMethods(), Object.class))[1]);
        Assert.assertEquals("String", mutableMethod.getReturnType().toString());
        Assert.assertEquals(Visibility.PRIVATE, mutableMethod.getVisibility());
    };
    this.asCompilationUnit(this.validFile(_builder), _function);
}
Also used : CompilationUnit(org.eclipse.xtend.lib.macro.declaration.CompilationUnit) CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) AccessibleObject(java.lang.reflect.AccessibleObject) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) CompilationStrategy(org.eclipse.xtend.lib.macro.declaration.CompilationStrategy) Test(org.junit.Test)

Aggregations

CompilationStrategy (org.eclipse.xtend.lib.macro.declaration.CompilationStrategy)5 MutableMethodDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration)5 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)5 MutableClassDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration)3 MutableFieldDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration)3 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)3 TypeReference (org.eclipse.xtend.lib.macro.declaration.TypeReference)2 Serializable (java.io.Serializable)1 AccessibleObject (java.lang.reflect.AccessibleObject)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Consumer (java.util.function.Consumer)1 CompilationUnitImpl (org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl)1 CompilationUnit (org.eclipse.xtend.lib.macro.declaration.CompilationUnit)1 MutableConstructorDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableConstructorDeclaration)1 MutableTypeDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableTypeDeclaration)1 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)1 Test (org.junit.Test)1