Search in sources :

Example 6 with StandardTypeReferenceOwner

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

the class DeferredTypeParameterHintCollectorTest method createOwner.

@Override
protected StandardTypeReferenceOwner createOwner() {
    CommonTypeComputationServices _services = this.getServices();
    ResourceSet _contextResourceSet = this.getContextResourceSet();
    return new StandardTypeReferenceOwner(_services, _contextResourceSet) {

        @Override
        public void acceptHint(final Object handle, final LightweightBoundTypeArgument boundTypeArgument) {
            DeferredTypeParameterHintCollectorTest.this.hints.put(handle, boundTypeArgument);
        }

        @Override
        public List<LightweightBoundTypeArgument> getAllHints(final Object handle) {
            return DeferredTypeParameterHintCollectorTest.this.hints.get(handle);
        }

        @Override
        public boolean isResolved(final Object handle) {
            return false;
        }
    };
}
Also used : CommonTypeComputationServices(org.eclipse.xtext.xbase.typesystem.util.CommonTypeComputationServices) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) StandardTypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner) LightweightBoundTypeArgument(org.eclipse.xtext.xbase.typesystem.references.LightweightBoundTypeArgument)

Example 7 with StandardTypeReferenceOwner

use of org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner 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 8 with StandardTypeReferenceOwner

use of org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner 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 9 with StandardTypeReferenceOwner

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

the class AbstractCodeBuilder method appendTypeParameters.

protected ISourceAppender appendTypeParameters(final ISourceAppender appendable, final List<JvmTypeParameter> typeParameters) {
    ISourceAppender _xblockexpression = null;
    {
        final Iterator<JvmTypeParameter> iterator = typeParameters.iterator();
        boolean _hasNext = iterator.hasNext();
        if (_hasNext) {
            appendable.append("<");
            do {
                {
                    final JvmTypeParameter typeParameter = iterator.next();
                    appendable.append(typeParameter.getName());
                    final Function1<JvmUpperBound, Boolean> _function = (JvmUpperBound it) -> {
                        String _identifier = it.getTypeReference().getIdentifier();
                        return Boolean.valueOf((!Objects.equal(_identifier, "java.lang.Object")));
                    };
                    final Iterable<JvmUpperBound> upperBounds = IterableExtensions.<JvmUpperBound>filter(Iterables.<JvmUpperBound>filter(typeParameter.getConstraints(), JvmUpperBound.class), _function);
                    boolean _isEmpty = IterableExtensions.isEmpty(upperBounds);
                    boolean _not = (!_isEmpty);
                    if (_not) {
                        appendable.append(" extends ");
                        boolean isFirst = true;
                        final StandardTypeReferenceOwner owner = new StandardTypeReferenceOwner(this.services, this.context);
                        for (final JvmUpperBound upperBound : upperBounds) {
                            {
                                if ((!isFirst)) {
                                    appendable.append(" & ");
                                }
                                isFirst = false;
                                this.appendType(appendable, owner.toLightweightTypeReference(upperBound.getTypeReference()), "Object");
                            }
                        }
                    }
                    boolean _hasNext_1 = iterator.hasNext();
                    if (_hasNext_1) {
                        appendable.append(",");
                    }
                }
            } while (iterator.hasNext());
            appendable.append("> ");
        }
        _xblockexpression = appendable;
    }
    return _xblockexpression;
}
Also used : JvmUpperBound(org.eclipse.xtext.common.types.JvmUpperBound) JvmTypeParameter(org.eclipse.xtext.common.types.JvmTypeParameter) Iterator(java.util.Iterator) ISourceAppender(org.eclipse.xtext.xbase.compiler.ISourceAppender) StandardTypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner)

Example 10 with StandardTypeReferenceOwner

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

the class CreateMemberQuickfixes method getResolvedArgumentTypes.

protected List<LightweightTypeReference> getResolvedArgumentTypes(EObject context, JvmIdentifiableElement logicalContainer, List<XExpression> arguments) {
    List<LightweightTypeReference> argumentTypes = newArrayList();
    IResolvedTypes resolvedTypes = typeResolver.resolveTypes(context);
    for (XExpression argument : arguments) {
        LightweightTypeReference resolved = resolvedTypes.getActualType(argument);
        if (resolved == null) {
            StandardTypeReferenceOwner owner = new StandardTypeReferenceOwner(services, context);
            argumentTypes.add(owner.newReferenceToObject());
        } else {
            LocalTypeSubstitutor substitutor = new LocalTypeSubstitutor(resolved.getOwner(), logicalContainer);
            argumentTypes.add(substitutor.withoutLocalTypes(resolved));
        }
    }
    return argumentTypes;
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) IResolvedTypes(org.eclipse.xtext.xbase.typesystem.IResolvedTypes) XExpression(org.eclipse.xtext.xbase.XExpression) LocalTypeSubstitutor(org.eclipse.xtext.xbase.typesystem.util.LocalTypeSubstitutor) StandardTypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner)

Aggregations

StandardTypeReferenceOwner (org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner)22 ITypeReferenceOwner (org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner)8 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)8 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)5 XExpression (org.eclipse.xtext.xbase.XExpression)5 ParameterizedTypeReference (org.eclipse.xtext.xbase.typesystem.references.ParameterizedTypeReference)5 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)4 CommonTypeComputationServices (org.eclipse.xtext.xbase.typesystem.util.CommonTypeComputationServices)4 Injector (com.google.inject.Injector)3 List (java.util.List)3 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)3 JvmType (org.eclipse.xtext.common.types.JvmType)3 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)3 ClasspathTypeProvider (org.eclipse.xtext.common.types.access.impl.ClasspathTypeProvider)3 XtextResource (org.eclipse.xtext.resource.XtextResource)3 XtextResourceSet (org.eclipse.xtext.resource.XtextResourceSet)3 XAbstractFeatureCall (org.eclipse.xtext.xbase.XAbstractFeatureCall)3 XbaseStandaloneSetup (org.eclipse.xtext.xbase.XbaseStandaloneSetup)3 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)2 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)2