Search in sources :

Example 96 with MutableClassDeclaration

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

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

MutableClassDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration)97 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)94 Test (org.junit.Test)91 CompilationUnitImpl (org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl)87 MutableMethodDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration)24 MutableFieldDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration)17 AnnotationReference (org.eclipse.xtend.lib.macro.declaration.AnnotationReference)14 EObject (org.eclipse.emf.ecore.EObject)10 Type (org.eclipse.xtend.lib.macro.declaration.Type)10 TypeReference (org.eclipse.xtend.lib.macro.declaration.TypeReference)10 List (java.util.List)9 Element (org.eclipse.xtend.lib.macro.declaration.Element)8 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)8 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)7 AnnotationTypeDeclaration (org.eclipse.xtend.lib.macro.declaration.AnnotationTypeDeclaration)6 ClassDeclaration (org.eclipse.xtend.lib.macro.declaration.ClassDeclaration)6 MutableInterfaceDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableInterfaceDeclaration)6 AccessibleObject (java.lang.reflect.AccessibleObject)5 Consumer (java.util.function.Consumer)4 XtendCompilerTester (org.eclipse.xtend.core.compiler.batch.XtendCompilerTester)4