Search in sources :

Example 1 with ParameterDeclaration

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

the class JvmExecutableDeclarationImpl method addParameter.

public MutableParameterDeclaration addParameter(final String name, final TypeReference type) {
    this.checkMutable();
    ConditionUtils.checkJavaIdentifier(name, "name");
    Preconditions.checkArgument((type != null), "type cannot be null");
    boolean _isInferred = type.isInferred();
    if (_isInferred) {
        throw new IllegalArgumentException("Cannot use inferred type as parameter type.");
    }
    final JvmFormalParameter param = TypesFactory.eINSTANCE.createJvmFormalParameter();
    param.setName(name);
    param.setParameterType(this.getCompilationUnit().toJvmTypeReference(type));
    this.getDelegate().getParameters().add(param);
    ParameterDeclaration _parameterDeclaration = this.getCompilationUnit().toParameterDeclaration(param);
    return ((MutableParameterDeclaration) _parameterDeclaration);
}
Also used : JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) ParameterDeclaration(org.eclipse.xtend.lib.macro.declaration.ParameterDeclaration) MutableTypeParameterDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableTypeParameterDeclaration) MutableParameterDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableParameterDeclaration) TypeParameterDeclaration(org.eclipse.xtend.lib.macro.declaration.TypeParameterDeclaration)

Example 2 with ParameterDeclaration

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

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

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

the class ResolvedExecutableImpl method getResolvedParameters.

@Override
public Iterable<? extends ResolvedParameter> getResolvedParameters() {
    Iterable<? extends ResolvedParameter> _xblockexpression = null;
    {
        if ((this.resolvedParameters == null)) {
            int _size = this.getDelegate().getDeclaration().getParameters().size();
            final Function1<Integer, ResolvedParameterImpl> _function = (Integer i) -> {
                ParameterDeclaration _parameterDeclaration = this.getCompilationUnit().toParameterDeclaration(this.getDelegate().getDeclaration().getParameters().get((i).intValue()));
                TypeReference _typeReference = this.getCompilationUnit().toTypeReference(this.getDelegate().getResolvedParameterTypes().get((i).intValue()));
                return new ResolvedParameterImpl(_parameterDeclaration, _typeReference);
            };
            this.resolvedParameters = IterableExtensions.<Integer, ResolvedParameterImpl>map(new ExclusiveRange(0, _size, true), _function);
        }
        _xblockexpression = this.resolvedParameters;
    }
    return _xblockexpression;
}
Also used : Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) ResolvedParameterImpl(org.eclipse.xtend.core.macro.declaration.ResolvedParameterImpl) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) TypeReference(org.eclipse.xtend.lib.macro.declaration.TypeReference) ExclusiveRange(org.eclipse.xtext.xbase.lib.ExclusiveRange) ParameterDeclaration(org.eclipse.xtend.lib.macro.declaration.ParameterDeclaration)

Aggregations

ParameterDeclaration (org.eclipse.xtend.lib.macro.declaration.ParameterDeclaration)4 ConstructorDeclaration (org.eclipse.xtend.lib.macro.declaration.ConstructorDeclaration)2 TypeReference (org.eclipse.xtend.lib.macro.declaration.TypeReference)2 Function1 (org.eclipse.xtext.xbase.lib.Functions.Function1)2 AccessibleObject (java.lang.reflect.AccessibleObject)1 CompilationUnitImpl (org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl)1 ResolvedParameterImpl (org.eclipse.xtend.core.macro.declaration.ResolvedParameterImpl)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 MutableParameterDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableParameterDeclaration)1 MutableTypeParameterDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableTypeParameterDeclaration)1 TypeDeclaration (org.eclipse.xtend.lib.macro.declaration.TypeDeclaration)1 TypeParameterDeclaration (org.eclipse.xtend.lib.macro.declaration.TypeParameterDeclaration)1 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)1