Search in sources :

Example 1 with ConstructorDeclaration

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

the class JvmTypeDeclarationImpl method findDeclaredConstructor.

public ConstructorDeclaration findDeclaredConstructor(final TypeReference... parameterTypes) {
    ConstructorDeclaration _xblockexpression = null;
    {
        ConditionUtils.checkIterable(((Iterable<?>) Conversions.doWrapArray(parameterTypes)), "parameterTypes");
        final Function1<ConstructorDeclaration, Boolean> _function = (ConstructorDeclaration constructor) -> {
            final Function1<ParameterDeclaration, TypeReference> _function_1 = (ParameterDeclaration it) -> {
                return it.getType();
            };
            List<TypeReference> _list = IterableExtensions.<TypeReference>toList(IterableExtensions.map(constructor.getParameters(), _function_1));
            List<TypeReference> _list_1 = IterableExtensions.<TypeReference>toList(((Iterable<TypeReference>) Conversions.doWrapArray(parameterTypes)));
            return Boolean.valueOf(Objects.equal(_list, _list_1));
        };
        _xblockexpression = IterableExtensions.findFirst(this.getDeclaredConstructors(), _function);
    }
    return _xblockexpression;
}
Also used : MutableConstructorDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableConstructorDeclaration) ConstructorDeclaration(org.eclipse.xtend.lib.macro.declaration.ConstructorDeclaration) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) TypeReference(org.eclipse.xtend.lib.macro.declaration.TypeReference) ParameterDeclaration(org.eclipse.xtend.lib.macro.declaration.ParameterDeclaration)

Example 2 with ConstructorDeclaration

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

Aggregations

ConstructorDeclaration (org.eclipse.xtend.lib.macro.declaration.ConstructorDeclaration)2 ParameterDeclaration (org.eclipse.xtend.lib.macro.declaration.ParameterDeclaration)2 AccessibleObject (java.lang.reflect.AccessibleObject)1 CompilationUnitImpl (org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl)1 AnnotationTypeDeclaration (org.eclipse.xtend.lib.macro.declaration.AnnotationTypeDeclaration)1 ClassDeclaration (org.eclipse.xtend.lib.macro.declaration.ClassDeclaration)1 FieldDeclaration (org.eclipse.xtend.lib.macro.declaration.FieldDeclaration)1 MethodDeclaration (org.eclipse.xtend.lib.macro.declaration.MethodDeclaration)1 MutableClassDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration)1 MutableConstructorDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableConstructorDeclaration)1 MutableFieldDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration)1 MutableMethodDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration)1 TypeDeclaration (org.eclipse.xtend.lib.macro.declaration.TypeDeclaration)1 TypeReference (org.eclipse.xtend.lib.macro.declaration.TypeReference)1 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)1 Function1 (org.eclipse.xtext.xbase.lib.Functions.Function1)1 Test (org.junit.Test)1