Search in sources :

Example 1 with JvmTypeConstraint

use of org.eclipse.xtext.common.types.JvmTypeConstraint in project xtext-xtend by eclipse.

the class InferredJvmModelTest method testInferredTypeWithSelfReferringTypeParameter.

@Test
public void testInferredTypeWithSelfReferringTypeParameter() throws Exception {
    XtendFile xtendFile = file("package foo class Foo <T extends Foo> {}");
    JvmGenericType inferredType = getInferredType(xtendFile);
    assertEquals(1, inferredType.getTypeParameters().size());
    JvmTypeParameter typeParameter = inferredType.getTypeParameters().get(0);
    assertEquals(1, typeParameter.getConstraints().size());
    JvmTypeConstraint typeConstraint = typeParameter.getConstraints().get(0);
    assertTrue(typeConstraint instanceof JvmUpperBound);
    assertEquals(inferredType, ((JvmUpperBound) typeConstraint).getTypeReference().getType());
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) JvmUpperBound(org.eclipse.xtext.common.types.JvmUpperBound) JvmTypeParameter(org.eclipse.xtext.common.types.JvmTypeParameter) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) JvmTypeConstraint(org.eclipse.xtext.common.types.JvmTypeConstraint) Test(org.junit.Test)

Example 2 with JvmTypeConstraint

use of org.eclipse.xtext.common.types.JvmTypeConstraint in project xtext-xtend by eclipse.

the class ParserTest method testTypeParams_3.

@Test
public void testTypeParams_3() throws Exception {
    XtendFunction func = function("def <T extends CharSequence & java.io.Serializable> foo(T t) { t}");
    assertEquals(1, func.getTypeParameters().size());
    JvmTypeParameter tp = func.getTypeParameters().get(0);
    assertEquals("T", tp.getName());
    assertEquals(2, tp.getConstraints().size());
    for (JvmTypeConstraint constraint : tp.getConstraints()) {
        assertTrue(constraint instanceof JvmUpperBound);
    }
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) JvmUpperBound(org.eclipse.xtext.common.types.JvmUpperBound) JvmTypeParameter(org.eclipse.xtext.common.types.JvmTypeParameter) JvmTypeConstraint(org.eclipse.xtext.common.types.JvmTypeConstraint) Test(org.junit.Test)

Example 3 with JvmTypeConstraint

use of org.eclipse.xtext.common.types.JvmTypeConstraint 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 4 with JvmTypeConstraint

use of org.eclipse.xtext.common.types.JvmTypeConstraint in project xtext-xtend by eclipse.

the class XtendJvmModelInferrer method fixTypeParameters.

protected void fixTypeParameters(JvmTypeParameterDeclarator target) {
    for (JvmTypeParameter typeParameter : target.getTypeParameters()) {
        boolean upperBoundSeen = false;
        for (JvmTypeConstraint constraint : typeParameter.getConstraints()) {
            if (constraint instanceof JvmUpperBound) {
                upperBoundSeen = true;
                break;
            }
        }
        if (!upperBoundSeen) {
            JvmUpperBound upperBound = typesFactory.createJvmUpperBound();
            upperBound.setTypeReference(typeReferences.getTypeForName(Object.class, target));
            typeParameter.getConstraints().add(upperBound);
        }
    }
}
Also used : JvmUpperBound(org.eclipse.xtext.common.types.JvmUpperBound) JvmTypeParameter(org.eclipse.xtext.common.types.JvmTypeParameter) JvmTypeConstraint(org.eclipse.xtext.common.types.JvmTypeConstraint) EObject(org.eclipse.emf.ecore.EObject)

Aggregations

JvmTypeConstraint (org.eclipse.xtext.common.types.JvmTypeConstraint)4 JvmTypeParameter (org.eclipse.xtext.common.types.JvmTypeParameter)4 JvmUpperBound (org.eclipse.xtext.common.types.JvmUpperBound)4 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Consumer (java.util.function.Consumer)1 EList (org.eclipse.emf.common.util.EList)1 EObject (org.eclipse.emf.ecore.EObject)1 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)1 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)1 AbstractMethodBuilder (org.eclipse.xtend.ide.codebuilder.AbstractMethodBuilder)1 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)1 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)1 JvmFormalParameter (org.eclipse.xtext.common.types.JvmFormalParameter)1 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)1 ISourceAppender (org.eclipse.xtext.xbase.compiler.ISourceAppender)1 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)1