Search in sources :

Example 36 with LightweightTypeReference

use of org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference in project xtext-xtend by eclipse.

the class XtendValidator method createExceptionMismatchError.

protected void createExceptionMismatchError(IResolvedOperation operation, EObject sourceElement, List<IResolvedOperation> exceptionMismatch) {
    List<LightweightTypeReference> exceptions = operation.getIllegallyDeclaredExceptions();
    StringBuilder message = new StringBuilder(100);
    message.append("The declared exception");
    if (exceptions.size() > 1) {
        message.append('s');
    }
    message.append(' ');
    for (int i = 0; i < exceptions.size(); i++) {
        if (i != 0) {
            if (i != exceptions.size() - 1)
                message.append(", ");
            else
                message.append(" and ");
        }
        message.append(exceptions.get(i).getHumanReadableName());
    }
    if (exceptions.size() > 1) {
        message.append(" are");
    } else {
        message.append(" is");
    }
    message.append(" not compatible with throws clause in ");
    for (int i = 0; i < exceptionMismatch.size(); i++) {
        if (i != 0) {
            if (i != exceptionMismatch.size() - 1)
                message.append(", ");
            else
                message.append(" and ");
        }
        IResolvedOperation resolvedOperation = exceptionMismatch.get(i);
        message.append(getDeclaratorName(resolvedOperation));
        message.append('.');
        message.append(exceptionMismatch.get(i).getSimpleSignature());
    }
    error(message.toString(), sourceElement, exceptionsFeature(sourceElement), INCOMPATIBLE_THROWS_CLAUSE);
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) ToStringBuilder(org.eclipse.xtext.xbase.lib.util.ToStringBuilder) IResolvedOperation(org.eclipse.xtext.xbase.typesystem.override.IResolvedOperation)

Example 37 with LightweightTypeReference

use of org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference in project xtext-xtend by eclipse.

the class XtendValidator method checkExceptions.

private void checkExceptions(EObject context, List<JvmTypeReference> exceptions, EReference reference) {
    Set<String> declaredExceptionNames = Sets.newHashSet();
    JvmTypeReference throwableType = getServices().getTypeReferences().getTypeForName(Throwable.class, context);
    if (throwableType == null) {
        return;
    }
    ITypeReferenceOwner owner = new StandardTypeReferenceOwner(getServices(), context);
    LightweightTypeReference throwableReference = owner.toLightweightTypeReference(throwableType);
    for (int i = 0; i < exceptions.size(); i++) {
        JvmTypeReference exception = exceptions.get(i);
        // throwables may not carry generics thus the raw comparison is sufficient
        if (exception.getType() != null && !exception.getType().eIsProxy()) {
            if (!throwableReference.isAssignableFrom(exception.getType()))
                error("No exception of type " + exception.getSimpleName() + " can be thrown; an exception type must be a subclass of Throwable", reference, i, EXCEPTION_NOT_THROWABLE);
            if (!declaredExceptionNames.add(exception.getQualifiedName()))
                error("Exception " + exception.getSimpleName() + " is declared twice", reference, i, EXCEPTION_DECLARED_TWICE);
        }
    }
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) RichString(org.eclipse.xtend.core.xtend.RichString) ITypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner) StandardTypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner)

Example 38 with LightweightTypeReference

use of org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference in project xtext-xtend by eclipse.

the class CacheMethodCompileStrategy method apply.

@Override
public void apply(ITreeAppendable appendable) {
    JvmOperation cacheMethod = (JvmOperation) logicalContainerProvider.getLogicalContainer(createExtensionInfo.getCreateExpression());
    JvmDeclaredType containerType = cacheMethod.getDeclaringType();
    IResolvedTypes resolvedTypes = typeResolver.resolveTypes(containerType);
    final ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, containerType);
    LightweightTypeReference listType = owner.newReferenceTo(ArrayList.class, new TypeReferenceInitializer<ParameterizedTypeReference>() {

        @Override
        public LightweightTypeReference enhance(ParameterizedTypeReference reference) {
            reference.addTypeArgument(owner.newWildcardTypeReference());
            return reference;
        }
    });
    String cacheVarName = cacheField.getSimpleName();
    String cacheKeyVarName = appendable.declareSyntheticVariable("CacheKey", "_cacheKey");
    appendable.append("final ").append(listType).append(" ").append(cacheKeyVarName).append(" = ").append(CollectionLiterals.class).append(".newArrayList(");
    List<JvmFormalParameter> list = cacheMethod.getParameters();
    for (Iterator<JvmFormalParameter> iterator = list.iterator(); iterator.hasNext(); ) {
        JvmFormalParameter jvmFormalParameter = iterator.next();
        appendable.append(getVarName(jvmFormalParameter));
        if (iterator.hasNext()) {
            appendable.append(", ");
        }
    }
    appendable.append(");");
    // declare result variable
    LightweightTypeReference returnType = resolvedTypes.getActualType(initializerMethod.getParameters().get(0));
    if (returnType != null) {
        appendable.newLine().append("final ").append(returnType);
    } else {
        appendable.newLine().append("final Object");
    }
    String resultVarName = "_result";
    appendable.append(" ").append(resultVarName).append(";");
    // open synchronize block
    appendable.newLine().append("synchronized (").append(cacheVarName).append(") {");
    appendable.increaseIndentation();
    // if the cache contains the key return the previously created object.
    appendable.newLine().append("if (").append(cacheVarName).append(".containsKey(").append(cacheKeyVarName).append(")) {");
    appendable.increaseIndentation();
    appendable.newLine().append("return ").append(cacheVarName).append(".get(").append(cacheKeyVarName).append(");");
    appendable.decreaseIndentation().newLine().append("}");
    // execute the creation
    compiler.toJavaStatement(createExtensionInfo.getCreateExpression(), appendable, true);
    appendable.newLine();
    appendable.append(resultVarName).append(" = ");
    compiler.toJavaExpression(createExtensionInfo.getCreateExpression(), appendable);
    appendable.append(";");
    // store the newly created object in the cache
    appendable.newLine().append(cacheVarName).append(".put(").append(cacheKeyVarName).append(", ");
    LightweightTypeReference fieldType = resolvedTypes.getActualType(cacheField);
    LightweightTypeReference declaredResultType = fieldType.getTypeArguments().get(1);
    boolean castRequired = false;
    if (!declaredResultType.isAssignableFrom(returnType)) {
        castRequired = true;
        appendable.append("(").append(declaredResultType).append(")");
    }
    appendable.append(resultVarName).append(");");
    // close synchronize block
    appendable.decreaseIndentation();
    appendable.newLine().append("}");
    appendable.newLine().append(initializerMethod.getSimpleName()).append("(").append(resultVarName);
    for (JvmFormalParameter parameter : cacheMethod.getParameters()) {
        appendable.append(", ").append(parameter.getName());
    }
    appendable.append(");");
    // return the result
    appendable.newLine().append("return ");
    if (castRequired) {
        appendable.append("(").append(declaredResultType).append(")");
    }
    appendable.append(resultVarName).append(";");
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) CollectionLiterals(org.eclipse.xtext.xbase.lib.CollectionLiterals) IResolvedTypes(org.eclipse.xtext.xbase.typesystem.IResolvedTypes) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) ITypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) ParameterizedTypeReference(org.eclipse.xtext.xbase.typesystem.references.ParameterizedTypeReference) StandardTypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner)

Example 39 with LightweightTypeReference

use of org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference in project xtext-xtend by eclipse.

the class DispatchMethodCompileStrategy method generateActualDispatchCall.

protected void generateActualDispatchCall(JvmOperation dispatchOperation, JvmOperation actualOperationToCall, ITreeAppendable a, ITypeReferenceOwner owner) {
    a.append(actualOperationToCall.getSimpleName()).append("(");
    Iterator<JvmFormalParameter> iter1 = dispatchOperation.getParameters().iterator();
    for (Iterator<JvmFormalParameter> iter2 = actualOperationToCall.getParameters().iterator(); iter2.hasNext(); ) {
        JvmFormalParameter p1 = iter1.next();
        JvmFormalParameter p2 = iter2.next();
        LightweightTypeReference type1 = owner.toLightweightTypeReference(p1.getParameterType());
        LightweightTypeReference type2 = owner.toLightweightTypeReference(p2.getParameterType());
        if (!type2.isAssignableFrom(type1, new TypeConformanceComputationArgument(true, false, true, true, false, false))) {
            a.append("(").append(type2.getWrapperTypeIfPrimitive()).append(")");
        }
        if (typeReferences.is(p2.getParameterType(), Void.class)) {
            a.append("null");
        } else {
            a.append(getVarName(p1, a));
        }
        if (iter2.hasNext()) {
            a.append(", ");
        }
    }
    a.append(")");
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) TypeConformanceComputationArgument(org.eclipse.xtext.xbase.typesystem.conformance.TypeConformanceComputationArgument)

Example 40 with LightweightTypeReference

use of org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference in project xtext-xtend by eclipse.

the class ReorderedFeatureCallArgumentsTest method testBug457779_01.

@Test
public void testBug457779_01() {
    final IFeatureCallArguments arguments = this.toArgumentsWithReceiver("String s, int i", "[], 1");
    final XExpression first = arguments.getArgument(0);
    Assert.assertNull(first);
    final LightweightTypeReference firstType = arguments.getDeclaredTypeForLambda(0);
    Assert.assertNull(firstType);
    final XExpression third = arguments.getArgument(1);
    Assert.assertTrue((third instanceof XNumberLiteral));
    final LightweightTypeReference thirdType = arguments.getDeclaredTypeForLambda(1);
    Assert.assertEquals("int", thirdType.getSimpleName());
    final XExpression second = arguments.getArgument(2);
    Assert.assertTrue((second instanceof XClosure));
    final LightweightTypeReference secondType = arguments.getDeclaredTypeForLambda(2);
    Assert.assertEquals("String", secondType.getSimpleName());
    try {
        arguments.getArgument(3);
        Assert.fail("Expected exception");
    } catch (final Throwable _t) {
        if (_t instanceof IndexOutOfBoundsException) {
        } else {
            throw Exceptions.sneakyThrow(_t);
        }
    }
    try {
        arguments.getDeclaredTypeForLambda(3);
        Assert.fail("Expected exception");
    } catch (final Throwable _t_1) {
        if (_t_1 instanceof IndexOutOfBoundsException) {
        } else {
            throw Exceptions.sneakyThrow(_t_1);
        }
    }
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) XClosure(org.eclipse.xtext.xbase.XClosure) XExpression(org.eclipse.xtext.xbase.XExpression) XNumberLiteral(org.eclipse.xtext.xbase.XNumberLiteral) IFeatureCallArguments(org.eclipse.xtext.xbase.typesystem.arguments.IFeatureCallArguments) Test(org.junit.Test)

Aggregations

LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)83 Test (org.junit.Test)28 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)23 XExpression (org.eclipse.xtext.xbase.XExpression)22 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)20 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)19 JvmFormalParameter (org.eclipse.xtext.common.types.JvmFormalParameter)16 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)13 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)10 XBlockExpression (org.eclipse.xtext.xbase.XBlockExpression)10 IFeatureCallArguments (org.eclipse.xtext.xbase.typesystem.arguments.IFeatureCallArguments)10 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)9 XNumberLiteral (org.eclipse.xtext.xbase.XNumberLiteral)9 JvmType (org.eclipse.xtext.common.types.JvmType)8 ITypeReferenceOwner (org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner)8 EObject (org.eclipse.emf.ecore.EObject)7 JvmIdentifiableElement (org.eclipse.xtext.common.types.JvmIdentifiableElement)7 StandardTypeReferenceOwner (org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner)7 RichString (org.eclipse.xtend.core.xtend.RichString)6 JvmTypeParameter (org.eclipse.xtext.common.types.JvmTypeParameter)6