Search in sources :

Example 16 with AbstractMethodBuilder

use of org.eclipse.xtend.ide.codebuilder.AbstractMethodBuilder in project xtext-xtend by eclipse.

the class MethodBuilderTest method testXtendExceptions.

@Test
public void testXtendExceptions() {
    AbstractMethodBuilder _createMethodBuilder = this._codeBuilderFactory.createMethodBuilder(this.getXtendClass());
    final Procedure1<AbstractMethodBuilder> _function = (AbstractMethodBuilder it) -> {
        it.setContext(this.getXtendClass());
        it.setMethodName("foo");
        LightweightTypeReference _createTypeRef = this.createTypeRef(Exception.class, this.getXtendClass());
        LightweightTypeReference _createTypeRef_1 = this.createTypeRef(RuntimeException.class, this.getXtendClass());
        it.setExceptions(Collections.<LightweightTypeReference>unmodifiableList(CollectionLiterals.<LightweightTypeReference>newArrayList(_createTypeRef, _createTypeRef_1)));
    };
    AbstractMethodBuilder _doubleArrow = ObjectExtensions.<AbstractMethodBuilder>operator_doubleArrow(_createMethodBuilder, _function);
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("def foo() throws Exception, RuntimeException {");
    _builder.newLine();
    _builder.append("  ");
    _builder.append(AbstractBuilderTest.DEFAULT_BODY, "  ");
    _builder.newLineIfNotEmpty();
    _builder.append("}");
    this.assertBuilds(_doubleArrow, _builder.toString());
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) AbstractMethodBuilder(org.eclipse.xtend.ide.codebuilder.AbstractMethodBuilder) Test(org.junit.Test) AbstractBuilderTest(org.eclipse.xtend.ide.tests.codebuilder.AbstractBuilderTest)

Example 17 with AbstractMethodBuilder

use of org.eclipse.xtend.ide.codebuilder.AbstractMethodBuilder in project xtext-xtend by eclipse.

the class MemberFromSuperImplementor method appendOverrideFunction.

public void appendOverrideFunction(final XtendTypeDeclaration overrider, final IResolvedOperation overriddenOperation, final ISourceAppender appendable) {
    final JvmDeclaredType inferredType = this.associations.getInferredType(overrider);
    final AbstractMethodBuilder methodBuilder = this.codeBuilderFactory.createMethodBuilder(inferredType);
    this.initializeExecutableBuilder(methodBuilder, inferredType, overriddenOperation);
    methodBuilder.setOverrideFlag(true);
    methodBuilder.setMethodName(overriddenOperation.getDeclaration().getSimpleName());
    methodBuilder.setReturnType(overriddenOperation.getResolvedReturnType());
    boolean _isSynchronized = overriddenOperation.getDeclaration().isSynchronized();
    if (_isSynchronized) {
        methodBuilder.setSynchronizedFlag(true);
    }
    boolean _isEmpty = overriddenOperation.getResolvedTypeParameters().isEmpty();
    boolean _not = (!_isEmpty);
    if (_not) {
        final ArrayList<JvmTypeParameter> typeParameters = CollectionLiterals.<JvmTypeParameter>newArrayList();
        final Procedure2<JvmTypeParameter, Integer> _function = (JvmTypeParameter typeParam, Integer idx) -> {
            final JvmTypeParameter newTypeParam = this.typesFactory.createJvmTypeParameter();
            newTypeParam.setName(typeParam.getName());
            final Consumer<LightweightTypeReference> _function_1 = (LightweightTypeReference it) -> {
                final JvmUpperBound upperBound = this.typesFactory.createJvmUpperBound();
                upperBound.setTypeReference(it.toJavaCompliantTypeReference());
                EList<JvmTypeConstraint> _constraints = newTypeParam.getConstraints();
                _constraints.add(upperBound);
            };
            overriddenOperation.getResolvedTypeParameterConstraints((idx).intValue()).forEach(_function_1);
            typeParameters.add(newTypeParam);
        };
        IterableExtensions.<JvmTypeParameter>forEach(overriddenOperation.getResolvedTypeParameters(), _function);
        methodBuilder.setTypeParameters(typeParameters);
    }
    boolean _isAbstract = overriddenOperation.getDeclaration().isAbstract();
    boolean _not_1 = (!_isAbstract);
    if (_not_1) {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("super.");
        {
            List<JvmTypeParameter> _resolvedTypeParameters = overriddenOperation.getResolvedTypeParameters();
            boolean _hasElements = false;
            for (final JvmTypeParameter typeParam : _resolvedTypeParameters) {
                if (!_hasElements) {
                    _hasElements = true;
                    _builder.append("<");
                } else {
                    _builder.appendImmediate(", ", "");
                }
                String _simpleName = typeParam.getSimpleName();
                _builder.append(_simpleName);
            }
            if (_hasElements) {
                _builder.append(">");
            }
        }
        String _simpleName_1 = overriddenOperation.getDeclaration().getSimpleName();
        _builder.append(_simpleName_1);
        _builder.append("(");
        final Function1<JvmFormalParameter, String> _function_1 = (JvmFormalParameter it) -> {
            return it.getSimpleName();
        };
        String _join = IterableExtensions.join(ListExtensions.<JvmFormalParameter, String>map(overriddenOperation.getDeclaration().getParameters(), _function_1), ", ");
        _builder.append(_join);
        _builder.append(")");
        final String body = _builder.toString();
        final Procedure1<? super ISourceAppender> superTypeRef = this.getImplementedInterface(inferredType, overriddenOperation.getDeclaration().getDeclaringType());
        final Procedure1<ISourceAppender> _function_2 = (ISourceAppender it) -> {
            if (superTypeRef != null) {
                superTypeRef.apply(it);
            }
            it.append(body);
        };
        methodBuilder.setBodyGenerator(_function_2);
    }
    boolean _isValid = methodBuilder.isValid();
    if (_isValid) {
        methodBuilder.build(appendable);
    }
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmTypeParameter(org.eclipse.xtext.common.types.JvmTypeParameter) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) ISourceAppender(org.eclipse.xtext.xbase.compiler.ISourceAppender) JvmUpperBound(org.eclipse.xtext.common.types.JvmUpperBound) Consumer(java.util.function.Consumer) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) JvmTypeConstraint(org.eclipse.xtext.common.types.JvmTypeConstraint) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) List(java.util.List) AbstractMethodBuilder(org.eclipse.xtend.ide.codebuilder.AbstractMethodBuilder)

Example 18 with AbstractMethodBuilder

use of org.eclipse.xtend.ide.codebuilder.AbstractMethodBuilder in project xtext-xtend by eclipse.

the class CreateMemberQuickfixes method newMethodQuickfix.

protected void newMethodQuickfix(JvmDeclaredType containerType, String name, /* @Nullable */
LightweightTypeReference returnType, List<LightweightTypeReference> parameterTypes, boolean isStatic, boolean isAbstract, boolean isExtension, boolean isLocal, XAbstractFeatureCall call, final Issue issue, final IssueResolutionAcceptor issueResolutionAcceptor) {
    AbstractMethodBuilder methodBuilder = codeBuilderFactory.createMethodBuilder(containerType);
    methodBuilder.setMethodName(name);
    methodBuilder.setReturnType(returnType);
    for (LightweightTypeReference parameterType : parameterTypes) methodBuilder.newParameterBuilder().setType(parameterType);
    methodBuilder.setContext(call);
    methodBuilder.setVisibility(JvmVisibility.PUBLIC);
    methodBuilder.setStaticFlag(isStatic);
    methodBuilder.setAbstractFlag(isAbstract);
    StringBuffer label = new StringBuffer("Create ");
    if (isStatic)
        label.append("static ");
    if (isExtension)
        label.append("extension ");
    label.append("method '").append(name).append("(");
    boolean isFirst = true;
    for (LightweightTypeReference parameterType : parameterTypes) {
        if (!isFirst)
            label.append(", ");
        isFirst = false;
        label.append(parameterType.getSimpleName());
    }
    label.append(")'");
    if (!isLocal)
        label.append(" in '" + containerType.getSimpleName() + "'");
    quickfixFactory.addQuickfix(methodBuilder, label.toString(), issue, issueResolutionAcceptor);
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) AbstractMethodBuilder(org.eclipse.xtend.ide.codebuilder.AbstractMethodBuilder)

Example 19 with AbstractMethodBuilder

use of org.eclipse.xtend.ide.codebuilder.AbstractMethodBuilder in project xtext-xtend by eclipse.

the class MethodBuilderTest method testJavaNamedParameter.

@Test
public void testJavaNamedParameter() {
    AbstractMethodBuilder _createMethodBuilder = this._codeBuilderFactory.createMethodBuilder(this.getJavaClass());
    final Procedure1<AbstractMethodBuilder> _function = (AbstractMethodBuilder it) -> {
        it.setContext(this.getJavaClass());
        it.setMethodName("foo");
        AbstractParameterBuilder _newParameterBuilder = it.newParameterBuilder();
        final Procedure1<AbstractParameterBuilder> _function_1 = (AbstractParameterBuilder it_1) -> {
            it_1.setName("bar");
            it_1.setType(this.createTypeRef(this.getXtendClass()));
        };
        ObjectExtensions.<AbstractParameterBuilder>operator_doubleArrow(_newParameterBuilder, _function_1);
        AbstractParameterBuilder _newParameterBuilder_1 = it.newParameterBuilder();
        final Procedure1<AbstractParameterBuilder> _function_2 = (AbstractParameterBuilder it_1) -> {
            it_1.setName("baz");
            it_1.setType(this.createTypeRef(this.getXtendClass()));
        };
        ObjectExtensions.<AbstractParameterBuilder>operator_doubleArrow(_newParameterBuilder_1, _function_2);
    };
    AbstractMethodBuilder _doubleArrow = ObjectExtensions.<AbstractMethodBuilder>operator_doubleArrow(_createMethodBuilder, _function);
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("void foo(Foo bar, Foo baz) {");
    _builder.newLine();
    _builder.append("  ");
    _builder.append(AbstractBuilderTest.DEFAULT_BODY, "  ");
    _builder.append(";");
    _builder.newLineIfNotEmpty();
    _builder.append("}");
    this.assertBuilds(_doubleArrow, _builder.toString());
}
Also used : AbstractParameterBuilder(org.eclipse.xtend.ide.codebuilder.AbstractParameterBuilder) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) AbstractMethodBuilder(org.eclipse.xtend.ide.codebuilder.AbstractMethodBuilder) Test(org.junit.Test) AbstractBuilderTest(org.eclipse.xtend.ide.tests.codebuilder.AbstractBuilderTest)

Example 20 with AbstractMethodBuilder

use of org.eclipse.xtend.ide.codebuilder.AbstractMethodBuilder in project xtext-xtend by eclipse.

the class MethodBuilderTest method testJavaOverride.

@Test
public void testJavaOverride() {
    AbstractMethodBuilder _createMethodBuilder = this._codeBuilderFactory.createMethodBuilder(this.getJavaClass());
    final Procedure1<AbstractMethodBuilder> _function = (AbstractMethodBuilder it) -> {
        it.setContext(this.getJavaClass());
        it.setMethodName("foo");
        it.setOverrideFlag(true);
    };
    AbstractMethodBuilder _doubleArrow = ObjectExtensions.<AbstractMethodBuilder>operator_doubleArrow(_createMethodBuilder, _function);
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("@Override");
    _builder.newLine();
    _builder.append("void foo() {");
    _builder.newLine();
    _builder.append("  ");
    _builder.append(AbstractBuilderTest.DEFAULT_BODY, "  ");
    _builder.append(";");
    _builder.newLineIfNotEmpty();
    _builder.append("}");
    this.assertBuilds(_doubleArrow, _builder.toString());
}
Also used : StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) AbstractMethodBuilder(org.eclipse.xtend.ide.codebuilder.AbstractMethodBuilder) Test(org.junit.Test) AbstractBuilderTest(org.eclipse.xtend.ide.tests.codebuilder.AbstractBuilderTest)

Aggregations

AbstractMethodBuilder (org.eclipse.xtend.ide.codebuilder.AbstractMethodBuilder)21 AbstractBuilderTest (org.eclipse.xtend.ide.tests.codebuilder.AbstractBuilderTest)19 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)19 Test (org.junit.Test)19 AbstractParameterBuilder (org.eclipse.xtend.ide.codebuilder.AbstractParameterBuilder)7 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)6 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)4 JvmTypeParameter (org.eclipse.xtext.common.types.JvmTypeParameter)3 ISourceAppender (org.eclipse.xtext.xbase.compiler.ISourceAppender)3 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Consumer (java.util.function.Consumer)1 EList (org.eclipse.emf.common.util.EList)1 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)1 JvmFormalParameter (org.eclipse.xtext.common.types.JvmFormalParameter)1 JvmTypeConstraint (org.eclipse.xtext.common.types.JvmTypeConstraint)1 JvmUpperBound (org.eclipse.xtext.common.types.JvmUpperBound)1