Search in sources :

Example 31 with MutableMethodDeclaration

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

the class DeclarationsTest method testXtendClassWithMethodFieldAndConstructor.

@Test
public void testXtendClassWithMethodFieldAndConstructor() {
    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) -> {
        Assert.assertEquals("foo", it.getPackageName());
        TypeDeclaration _head = IterableExtensions.head(it.getSourceTypeDeclarations());
        final ClassDeclaration clazz = ((ClassDeclaration) _head);
        final MutableClassDeclaration genClazz = it.getTypeLookup().findClass("foo.MyClass");
        Assert.assertEquals("foo.MyClass", clazz.getQualifiedName());
        Assert.assertNull(clazz.getExtendedClass());
        Assert.assertTrue(IterableExtensions.isEmpty(clazz.getImplementedInterfaces()));
        Assert.assertEquals(3, IterableExtensions.size(clazz.getDeclaredMembers()));
        Assert.assertEquals("T", IterableExtensions.head(clazz.getTypeParameters()).getSimpleName());
        Assert.assertEquals("CharSequence", IterableExtensions.head(IterableExtensions.head(clazz.getTypeParameters()).getUpperBounds()).toString());
        Assert.assertSame(clazz, IterableExtensions.head(clazz.getTypeParameters()).getTypeParameterDeclarator());
        final FieldDeclaration field = IterableExtensions.head(clazz.getDeclaredFields());
        Assert.assertSame(clazz, field.getDeclaringType());
        Assert.assertEquals("myField", field.getSimpleName());
        Assert.assertEquals("String", field.getType().toString());
        Assert.assertFalse(field.isFinal());
        final ConstructorDeclaration constructor = IterableExtensions.head(clazz.getDeclaredConstructors());
        Assert.assertSame(clazz, constructor.getDeclaringType());
        Assert.assertEquals("MyClass", constructor.getSimpleName());
        Assert.assertEquals("initial", IterableExtensions.head(constructor.getParameters()).getSimpleName());
        Assert.assertEquals("String", IterableExtensions.head(constructor.getParameters()).getType().toString());
        final MethodDeclaration method = IterableExtensions.head(clazz.getDeclaredMethods());
        final MutableMethodDeclaration genMethod = IterableExtensions.head(genClazz.getDeclaredMethods());
        Assert.assertSame(clazz, method.getDeclaringType());
        Assert.assertEquals("a", IterableExtensions.head(method.getParameters()).getSimpleName());
        Assert.assertEquals("T2", IterableExtensions.head(method.getParameters()).getType().toString());
        Assert.assertSame(IterableExtensions.head(genMethod.getTypeParameters()), IterableExtensions.head(method.getParameters()).getType().getType());
        Assert.assertEquals("T", ((ParameterDeclaration[]) Conversions.unwrapArray(method.getParameters(), ParameterDeclaration.class))[1].getType().toString());
        Assert.assertSame(IterableExtensions.head(genClazz.getTypeParameters()), ((ParameterDeclaration[]) Conversions.unwrapArray(method.getParameters(), ParameterDeclaration.class))[1].getType().getType());
        Assert.assertSame(genClazz, method.getReturnType().getType());
        Assert.assertEquals("T2", IterableExtensions.head(method.getTypeParameters()).getSimpleName());
        Assert.assertEquals("CharSequence", IterableExtensions.head(IterableExtensions.head(method.getTypeParameters()).getUpperBounds()).toString());
        Assert.assertSame(IterableExtensions.head(method.getTypeParameters()), IterableExtensions.head(method.getTypeParameters()));
        Assert.assertSame(method, IterableExtensions.head(method.getTypeParameters()).getTypeParameterDeclarator());
        Assert.assertSame(field, ((Object[]) Conversions.unwrapArray(clazz.getDeclaredMembers(), Object.class))[0]);
        Assert.assertSame(constructor, ((Object[]) Conversions.unwrapArray(clazz.getDeclaredMembers(), Object.class))[1]);
        Assert.assertSame(method, ((Object[]) Conversions.unwrapArray(clazz.getDeclaredMembers(), Object.class))[2]);
    };
    this.asCompilationUnit(this.validFile(_builder), _function);
}
Also used : ClassDeclaration(org.eclipse.xtend.lib.macro.declaration.ClassDeclaration) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) ConstructorDeclaration(org.eclipse.xtend.lib.macro.declaration.ConstructorDeclaration) MethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MethodDeclaration) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) AccessibleObject(java.lang.reflect.AccessibleObject) AnnotationTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.AnnotationTypeDeclaration) TypeDeclaration(org.eclipse.xtend.lib.macro.declaration.TypeDeclaration) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) FieldDeclaration(org.eclipse.xtend.lib.macro.declaration.FieldDeclaration) ParameterDeclaration(org.eclipse.xtend.lib.macro.declaration.ParameterDeclaration) Test(org.junit.Test)

Example 32 with MutableMethodDeclaration

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

the class JvmTypeDeclarationImpl method addMethod.

public MutableMethodDeclaration addMethod(final String name, final Procedure1<MutableMethodDeclaration> initializer) {
    this.checkMutable();
    ConditionUtils.checkJavaIdentifier(name, "name");
    Preconditions.checkArgument((initializer != null), "initializer cannot be null");
    final JvmOperation newMethod = TypesFactory.eINSTANCE.createJvmOperation();
    newMethod.setVisibility(JvmVisibility.PUBLIC);
    newMethod.setSimpleName(name);
    newMethod.setReturnType(this.getCompilationUnit().toJvmTypeReference(this.getCompilationUnit().getTypeReferenceProvider().getPrimitiveVoid()));
    this.getDelegate().getMembers().add(newMethod);
    MemberDeclaration _memberDeclaration = this.getCompilationUnit().toMemberDeclaration(newMethod);
    final MutableMethodDeclaration mutableMethodDeclaration = ((MutableMethodDeclaration) _memberDeclaration);
    initializer.apply(mutableMethodDeclaration);
    return mutableMethodDeclaration;
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) MemberDeclaration(org.eclipse.xtend.lib.macro.declaration.MemberDeclaration) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration)

Example 33 with MutableMethodDeclaration

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

the class TracedAccessorsProcessor method doTransform.

@Override
public void doTransform(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
    annotatedClass.setExtendedClass(context.newTypeReference(TracingSugar.class));
    final TypeReference iterableType = context.newTypeReference(Iterable.class, context.newWildcardTypeReference());
    final TypeReference annotationType = context.newTypeReference(TracedAccessors.class);
    AnnotationReference _findAnnotation = annotatedClass.findAnnotation(annotationType.getType());
    TypeReference[] _classArrayValue = null;
    if (_findAnnotation != null) {
        _classArrayValue = _findAnnotation.getClassArrayValue("value");
    }
    final TypeReference[] factories = _classArrayValue;
    if ((factories == null)) {
        return;
    }
    final Function1<TypeReference, Type> _function = (TypeReference it) -> {
        return it.getType();
    };
    Iterable<InterfaceDeclaration> _filter = Iterables.<InterfaceDeclaration>filter(ListExtensions.<TypeReference, Type>map(((List<TypeReference>) Conversions.doWrapArray(factories)), _function), InterfaceDeclaration.class);
    for (final InterfaceDeclaration f : _filter) {
        final Function1<MethodDeclaration, Boolean> _function_1 = (MethodDeclaration it) -> {
            return Boolean.valueOf((it.getSimpleName().startsWith("create") && IterableExtensions.isEmpty(it.getParameters())));
        };
        final Function1<MethodDeclaration, TypeReference> _function_2 = (MethodDeclaration it) -> {
            return it.getReturnType();
        };
        Iterable<TypeReference> _map = IterableExtensions.map(IterableExtensions.filter(f.getDeclaredMethods(), _function_1), _function_2);
        for (final TypeReference t : _map) {
            final Function1<ResolvedMethod, Boolean> _function_3 = (ResolvedMethod it) -> {
                return Boolean.valueOf(this.isSupportedGetter(it));
            };
            final Function1<ResolvedMethod, Boolean> _function_4 = (ResolvedMethod it) -> {
                boolean _isAssignableFrom = iterableType.isAssignableFrom(it.getDeclaration().getReturnType());
                return Boolean.valueOf((!_isAssignableFrom));
            };
            Iterable<? extends ResolvedMethod> _filter_1 = IterableExtensions.filter(IterableExtensions.filter(t.getAllResolvedMethods(), _function_3), _function_4);
            for (final ResolvedMethod getter : _filter_1) {
                {
                    final TypeReference rt = getter.getResolvedReturnType();
                    boolean _contains = TracedAccessorsProcessor.TYPES_WITH_GOOD_TO_STRING.contains(rt.getType().getSimpleName().toLowerCase());
                    if (_contains) {
                        final Procedure1<MutableMethodDeclaration> _function_5 = (MutableMethodDeclaration it) -> {
                            it.setReturnType(context.newTypeReference(IGeneratorNode.class));
                            it.addParameter("target", t);
                            StringConcatenationClient _client = new StringConcatenationClient() {

                                @Override
                                protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                                    _builder.append(EStructuralFeature.class);
                                    _builder.append(" feature = target.eClass().getEStructuralFeature(\"");
                                    String _featureName = TracedAccessorsProcessor.this.featureName(getter);
                                    _builder.append(_featureName);
                                    _builder.append("\");");
                                    _builder.newLineIfNotEmpty();
                                    _builder.append(ILocationData.class);
                                    _builder.append(" location = this.location(target, feature, -1);");
                                    _builder.newLineIfNotEmpty();
                                    _builder.append(CompositeGeneratorNode.class);
                                    _builder.append(" trace = this.trace(location);");
                                    _builder.newLineIfNotEmpty();
                                    _builder.append("this.append(trace, target.");
                                    String _simpleName = getter.getDeclaration().getSimpleName();
                                    _builder.append(_simpleName);
                                    _builder.append("());");
                                    _builder.newLineIfNotEmpty();
                                    _builder.append("return trace;");
                                    _builder.newLine();
                                }
                            };
                            it.setBody(_client);
                        };
                        annotatedClass.addMethod(this.tracerName(getter), _function_5);
                        final Procedure1<MutableMethodDeclaration> _function_6 = (MutableMethodDeclaration it) -> {
                            it.setReturnType(context.newTypeReference(IGeneratorNode.class));
                            it.addParameter("target", t);
                            it.addParameter("useForDebugging", context.newTypeReference(Boolean.TYPE));
                            StringConcatenationClient _client = new StringConcatenationClient() {

                                @Override
                                protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                                    _builder.append(EStructuralFeature.class);
                                    _builder.append(" feature = target.eClass().getEStructuralFeature(\"");
                                    String _featureName = TracedAccessorsProcessor.this.featureName(getter);
                                    _builder.append(_featureName);
                                    _builder.append("\");");
                                    _builder.newLineIfNotEmpty();
                                    _builder.append(ILocationData.class);
                                    _builder.append(" location = this.location(target, feature, -1);");
                                    _builder.newLineIfNotEmpty();
                                    _builder.append(CompositeGeneratorNode.class);
                                    _builder.append(" trace = this.trace(location, useForDebugging);");
                                    _builder.newLineIfNotEmpty();
                                    _builder.append("this.append(trace, target.");
                                    String _simpleName = getter.getDeclaration().getSimpleName();
                                    _builder.append(_simpleName);
                                    _builder.append("());");
                                    _builder.newLineIfNotEmpty();
                                    _builder.append("return trace;");
                                    _builder.newLine();
                                }
                            };
                            it.setBody(_client);
                        };
                        annotatedClass.addMethod(this.tracerName(getter), _function_6);
                    }
                    final Procedure1<MutableMethodDeclaration> _function_7 = (MutableMethodDeclaration it) -> {
                        it.setReturnType(context.newTypeReference(IGeneratorNode.class));
                        it.addParameter("target", t);
                        final TypeReference stringProvider = context.newTypeReference(Function.class, rt, context.getString());
                        it.addParameter("stringProvider", stringProvider);
                        StringConcatenationClient _client = new StringConcatenationClient() {

                            @Override
                            protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                                _builder.append(EStructuralFeature.class);
                                _builder.append(" feature = target.eClass().getEStructuralFeature(\"");
                                String _featureName = TracedAccessorsProcessor.this.featureName(getter);
                                _builder.append(_featureName);
                                _builder.append("\");");
                                _builder.newLineIfNotEmpty();
                                _builder.append(ILocationData.class);
                                _builder.append(" location = this.location(target, feature, -1);");
                                _builder.newLineIfNotEmpty();
                                _builder.append(CompositeGeneratorNode.class);
                                _builder.append(" trace = this.trace(location);");
                                _builder.newLineIfNotEmpty();
                                _builder.append("this.append(trace, stringProvider.apply(target.");
                                String _simpleName = getter.getDeclaration().getSimpleName();
                                _builder.append(_simpleName);
                                _builder.append("()));");
                                _builder.newLineIfNotEmpty();
                                _builder.append("return trace;");
                                _builder.newLine();
                            }
                        };
                        it.setBody(_client);
                    };
                    annotatedClass.addMethod(this.tracerName(getter), _function_7);
                }
            }
        }
    }
}
Also used : MethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MethodDeclaration) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) InterfaceDeclaration(org.eclipse.xtend.lib.macro.declaration.InterfaceDeclaration) IGeneratorNode(org.eclipse.xtext.generator.trace.node.IGeneratorNode) Type(org.eclipse.xtend.lib.macro.declaration.Type) ResolvedMethod(org.eclipse.xtend.lib.macro.declaration.ResolvedMethod) TracingSugar(org.eclipse.xtext.generator.trace.node.TracingSugar) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) AnnotationReference(org.eclipse.xtend.lib.macro.declaration.AnnotationReference) TypeReference(org.eclipse.xtend.lib.macro.declaration.TypeReference)

Example 34 with MutableMethodDeclaration

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

the class TracedProcessor method doTransform.

@Override
public void doTransform(final MutableMethodDeclaration annotatedMethod, @Extension final TransformationContext context) {
    final boolean useForDebugging = annotatedMethod.findAnnotation(context.findTypeGlobally(Traced.class)).getBooleanValue("useForDebugging");
    final TypeReference traceSugar = context.newTypeReference(TracingSugar.class);
    final TypeReference templateClient = context.newTypeReference(StringConcatenationClient.class);
    final TypeReference nodeType = context.newTypeReference(IGeneratorNode.class);
    final TypeReference eobjectType = context.newTypeReference(EObject.class);
    MutableTypeDeclaration _declaringType = annotatedMethod.getDeclaringType();
    final MutableClassDeclaration clazz = ((MutableClassDeclaration) _declaringType);
    final Function1<MutableFieldDeclaration, Boolean> _function = (MutableFieldDeclaration it) -> {
        return Boolean.valueOf(traceSugar.isAssignableFrom(it.getType()));
    };
    final MutableFieldDeclaration field = IterableExtensions.findFirst(clazz.getDeclaredFields(), _function);
    if ((field == null)) {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("@");
        String _simpleName = Traced.class.getSimpleName();
        _builder.append(_simpleName);
        _builder.append(" methods can only declared in a class with a field of type ");
        _builder.append(TracingSugar.class);
        context.addError(annotatedMethod, _builder.toString());
        return;
    }
    final Function1<MutableParameterDeclaration, Boolean> _function_1 = (MutableParameterDeclaration it) -> {
        return Boolean.valueOf(eobjectType.isAssignableFrom(it.getType()));
    };
    final MutableParameterDeclaration traceParam = IterableExtensions.findFirst(annotatedMethod.getParameters(), _function_1);
    if ((traceParam == null)) {
        StringConcatenation _builder_1 = new StringConcatenation();
        _builder_1.append("@");
        String _simpleName_1 = Traced.class.getSimpleName();
        _builder_1.append(_simpleName_1);
        _builder_1.append(" methods need at least one parameter of type ");
        _builder_1.append(EObject.class);
        _builder_1.append(".");
        context.addError(annotatedMethod, _builder_1.toString());
        return;
    }
    String _simpleName_2 = annotatedMethod.getSimpleName();
    String _plus = ("_" + _simpleName_2);
    final Procedure1<MutableMethodDeclaration> _function_2 = (MutableMethodDeclaration it) -> {
        Iterable<? extends MutableParameterDeclaration> _parameters = annotatedMethod.getParameters();
        for (final MutableParameterDeclaration p : _parameters) {
            it.addParameter(p.getSimpleName(), p.getType());
        }
        it.setReturnType(templateClient);
        it.setBody(annotatedMethod.getBody());
    };
    clazz.addMethod(_plus, _function_2);
    annotatedMethod.setReturnType(nodeType);
    StringConcatenationClient _client = new StringConcatenationClient() {

        @Override
        protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
            _builder.append(ILocationData.class);
            _builder.append(" _location = this.");
            String _simpleName = field.getSimpleName();
            _builder.append(_simpleName);
            _builder.append(".location(");
            String _simpleName_1 = traceParam.getSimpleName();
            _builder.append(_simpleName_1);
            _builder.append(");");
            _builder.newLineIfNotEmpty();
            _builder.append(CompositeGeneratorNode.class);
            _builder.append(" _traceNode = this.");
            String _simpleName_2 = field.getSimpleName();
            _builder.append(_simpleName_2);
            _builder.append(".trace(_location, ");
            _builder.append(useForDebugging);
            _builder.append(");");
            _builder.newLineIfNotEmpty();
            _builder.append("this.");
            String _simpleName_3 = field.getSimpleName();
            _builder.append(_simpleName_3);
            _builder.append(".appendTemplate(_traceNode, _");
            String _simpleName_4 = annotatedMethod.getSimpleName();
            _builder.append(_simpleName_4);
            _builder.append("(");
            final Function1<MutableParameterDeclaration, CharSequence> _function = (MutableParameterDeclaration it) -> {
                return it.getSimpleName();
            };
            String _join = IterableExtensions.join(annotatedMethod.getParameters(), ",", _function);
            _builder.append(_join);
            _builder.append("));");
            _builder.newLineIfNotEmpty();
            _builder.append("return _traceNode;");
            _builder.newLine();
        }
    };
    annotatedMethod.setBody(_client);
}
Also used : MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) MutableParameterDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableParameterDeclaration) MutableTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableTypeDeclaration) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) TypeReference(org.eclipse.xtend.lib.macro.declaration.TypeReference)

Aggregations

MutableMethodDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration)34 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)24 MutableClassDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration)22 CompilationUnitImpl (org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl)19 Test (org.junit.Test)19 MutableFieldDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration)12 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)9 TypeReference (org.eclipse.xtend.lib.macro.declaration.TypeReference)8 Element (org.eclipse.xtend.lib.macro.declaration.Element)6 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)6 CompilationStrategy (org.eclipse.xtend.lib.macro.declaration.CompilationStrategy)5 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)4 AnnotationReference (org.eclipse.xtend.lib.macro.declaration.AnnotationReference)3 MutableInterfaceDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableInterfaceDeclaration)3 AccessibleObject (java.lang.reflect.AccessibleObject)2 List (java.util.List)2 Set (java.util.Set)2 EObject (org.eclipse.emf.ecore.EObject)2 MemberDeclaration (org.eclipse.xtend.lib.macro.declaration.MemberDeclaration)2 MethodDeclaration (org.eclipse.xtend.lib.macro.declaration.MethodDeclaration)2