Search in sources :

Example 6 with JvmParameterizedTypeReference

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

the class XtendReentrantTypeResolver method doPrepareLocalTypes.

protected void doPrepareLocalTypes(final ResolvedTypes resolvedTypes, IFeatureScopeSession featureScopeSession, JvmFeature container, Map<JvmIdentifiableElement, ResolvedTypes> resolvedTypesByContext) {
    List<JvmGenericType> localClasses = container.getLocalClasses();
    for (final JvmGenericType localClass : localClasses) {
        JvmTypeReference superType = localClass.getSuperTypes().get(0);
        final IFeatureScopeSession nestedSession = featureScopeSession;
        if (InferredTypeIndicator.isInferred(superType)) {
            final XComputedTypeReference casted = (XComputedTypeReference) superType;
            InferredTypeIndicator typeProvider = (InferredTypeIndicator) casted.getTypeProvider();
            final AnonymousClass anonymousClass = (AnonymousClass) typeProvider.getExpression();
            XConstructorCall constructorCall = anonymousClass.getConstructorCall();
            IScope typeScope = featureScopeSession.getScope(constructorCall, TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE, resolvedTypes);
            final JvmDeclaredType type = anonymousClassUtil.getSuperTypeNonResolving(anonymousClass, typeScope);
            if (type == null) {
                JvmUnknownTypeReference superTypeReference = TypesFactory.eINSTANCE.createJvmUnknownTypeReference();
                requestCapturedLocalVariables(superTypeReference, localClass, resolvedTypes, resolvedTypesByContext, new IAcceptor<JvmTypeReference>() {

                    @Override
                    public void accept(JvmTypeReference capturingTypeReference) {
                        casted.setEquivalent(capturingTypeReference);
                        inferAnonymousClassConstructor(anonymousClass, localClass);
                    }
                });
            } else {
                final JvmParameterizedTypeReference superTypeReference = createSuperTypeReference(type, constructorCall);
                requestCapturedLocalVariables(superTypeReference, localClass, resolvedTypes, resolvedTypesByContext, new IAcceptor<JvmTypeReference>() {

                    @Override
                    @SuppressWarnings("deprecation")
                    public void accept(JvmTypeReference capturingTypeReference) {
                        casted.setEquivalent(capturingTypeReference);
                        IFeatureScopeSession mySession = addThisAndSuper(nestedSession, resolvedTypes.getReferenceOwner(), localClass, superTypeReference, false);
                        if (type.eClass() == TypesPackage.Literals.JVM_GENERIC_TYPE && ((JvmGenericType) type).isInterface()) {
                            localClass.getSuperTypes().add(0, typesBuilder.newTypeRef(localClass, Object.class));
                            inferAnonymousClassConstructor(anonymousClass, localClass);
                        } else {
                            for (JvmMember superMember : type.getMembers()) {
                                if (superMember instanceof JvmConstructor) {
                                    JvmConstructor superTypeConstructor = (JvmConstructor) superMember;
                                    boolean visible = mySession.isVisible(superTypeConstructor);
                                    inferAnonymousClassConstructor(anonymousClass, localClass, superTypeConstructor, visible);
                                }
                            }
                        }
                    }
                });
            }
        }
    }
}
Also used : IFeatureScopeSession(org.eclipse.xtext.xbase.scoping.batch.IFeatureScopeSession) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) XComputedTypeReference(org.eclipse.xtext.xtype.XComputedTypeReference) XConstructorCall(org.eclipse.xtext.xbase.XConstructorCall) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) JvmUnknownTypeReference(org.eclipse.xtext.common.types.JvmUnknownTypeReference) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) AnonymousClass(org.eclipse.xtend.core.xtend.AnonymousClass) InferredTypeIndicator(org.eclipse.xtext.xbase.typesystem.InferredTypeIndicator) JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor) IScope(org.eclipse.xtext.scoping.IScope) JvmMember(org.eclipse.xtext.common.types.JvmMember) JvmParameterizedTypeReference(org.eclipse.xtext.common.types.JvmParameterizedTypeReference)

Example 7 with JvmParameterizedTypeReference

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

the class XtendReentrantTypeResolver method createSuperTypeReference.

protected JvmParameterizedTypeReference createSuperTypeReference(JvmType superType, XConstructorCall constructorCall) {
    JvmParameterizedTypeReference result = TypesFactory.eINSTANCE.createJvmParameterizedTypeReference();
    result.setType(superType);
    for (JvmTypeReference typeReference : constructorCall.getTypeArguments()) {
        result.getArguments().add(typesBuilder.cloneWithProxies(typeReference));
    }
    return result;
}
Also used : JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) JvmParameterizedTypeReference(org.eclipse.xtext.common.types.JvmParameterizedTypeReference)

Example 8 with JvmParameterizedTypeReference

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

the class XtendValidator method validateInferredType.

protected void validateInferredType(JvmTypeReference inferredType, XtendMember member, String messagePrefix, EAttribute location) {
    if (inferredType != null) {
        TreeIterator<EObject> iterator = EcoreUtil2.eAll(inferredType);
        while (iterator.hasNext()) {
            EObject next = iterator.next();
            if (next instanceof JvmParameterizedTypeReference) {
                JvmParameterizedTypeReference candidate = (JvmParameterizedTypeReference) next;
                JvmType type = candidate.getType();
                if (type instanceof JvmGenericType && !((JvmGenericType) type).getTypeParameters().isEmpty()) {
                    if (candidate.getArguments().isEmpty()) {
                        StringBuilder message = new StringBuilder(messagePrefix);
                        message = proxyAwareUIStrings.visit(inferredType, message);
                        if (message != null) {
                            message.append(" uses the raw type ");
                            message.append(type.getSimpleName());
                            message.append(". References to generic type ");
                            message = proxyAwareUIStrings.appendTypeSignature(type, message);
                            message.append(" should be parameterized");
                            warning(message.toString(), member, location, org.eclipse.xtext.xbase.validation.IssueCodes.RAW_TYPE);
                        }
                        return;
                    }
                }
            } else if (next instanceof XComputedTypeReference) {
                validateInferredType(((XComputedTypeReference) next).getEquivalent(), member, messagePrefix, location);
                iterator.prune();
            }
        }
    }
}
Also used : ToStringBuilder(org.eclipse.xtext.xbase.lib.util.ToStringBuilder) EObject(org.eclipse.emf.ecore.EObject) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) XComputedTypeReference(org.eclipse.xtext.xtype.XComputedTypeReference) JvmType(org.eclipse.xtext.common.types.JvmType) JvmParameterizedTypeReference(org.eclipse.xtext.common.types.JvmParameterizedTypeReference)

Example 9 with JvmParameterizedTypeReference

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

the class XtendJvmModelInferrer method computeFieldName.

/* @Nullable */
protected String computeFieldName(XtendField field) {
    if (field.getName() != null)
        return field.getName();
    JvmTypeReference type = field.getType();
    String name = null;
    if (type != null) {
        while (type instanceof JvmGenericArrayTypeReference) {
            type = ((JvmGenericArrayTypeReference) type).getComponentType();
        }
        if (type instanceof JvmParameterizedTypeReference) {
            List<INode> nodes = NodeModelUtils.findNodesForFeature(type, TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE);
            if (!nodes.isEmpty()) {
                String typeName = nodes.get(0).getText().trim();
                int lastDot = typeName.lastIndexOf('.');
                if (lastDot != -1) {
                    typeName = typeName.substring(lastDot + 1);
                }
                name = "_" + Strings.toFirstLower(typeName);
            }
        }
    }
    return name;
}
Also used : JvmGenericArrayTypeReference(org.eclipse.xtext.common.types.JvmGenericArrayTypeReference) INode(org.eclipse.xtext.nodemodel.INode) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) JvmParameterizedTypeReference(org.eclipse.xtext.common.types.JvmParameterizedTypeReference) JvmTypeConstraint(org.eclipse.xtext.common.types.JvmTypeConstraint)

Example 10 with JvmParameterizedTypeReference

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

the class InferredJvmModelTest method testInferredSuperTypeTypeArgument.

@Test
public void testInferredSuperTypeTypeArgument() throws Exception {
    XtendFile xtendFile = file("class Foo extends Iterable<Object> { }");
    JvmGenericType inferredType = getInferredType(xtendFile);
    XtendClass xtendClass = (XtendClass) xtendFile.getXtendTypes().get(0);
    assertEquals(1, inferredType.getSuperTypes().size());
    JvmTypeReference xtendSuperType = xtendClass.getExtends();
    JvmTypeReference jvmSuperType = inferredType.getSuperTypes().get(0);
    assertFalse(xtendSuperType == jvmSuperType);
    assertTrue(jvmSuperType instanceof JvmParameterizedTypeReference);
    assertTrue(xtendSuperType instanceof JvmParameterizedTypeReference);
    EList<JvmTypeReference> xtendTypeArguments = ((JvmParameterizedTypeReference) xtendSuperType).getArguments();
    EList<JvmTypeReference> jvmTypeArguments = ((JvmParameterizedTypeReference) jvmSuperType).getArguments();
    assertEquals(1, jvmTypeArguments.size());
    JvmTypeReference xtendTypeArgument = xtendTypeArguments.get(0);
    JvmTypeReference jvmTypeArgument = jvmTypeArguments.get(0);
    assertFalse(xtendTypeArgument == jvmTypeArgument);
    assertEquals(xtendTypeArgument.getType(), jvmTypeArgument.getType());
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) JvmParameterizedTypeReference(org.eclipse.xtext.common.types.JvmParameterizedTypeReference) Test(org.junit.Test)

Aggregations

JvmParameterizedTypeReference (org.eclipse.xtext.common.types.JvmParameterizedTypeReference)10 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)8 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)4 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)4 Test (org.junit.Test)4 JvmGenericArrayTypeReference (org.eclipse.xtext.common.types.JvmGenericArrayTypeReference)3 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)2 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)2 XtendMember (org.eclipse.xtend.core.xtend.XtendMember)2 XtendParameter (org.eclipse.xtend.core.xtend.XtendParameter)2 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)2 JvmType (org.eclipse.xtext.common.types.JvmType)2 JvmTypeParameter (org.eclipse.xtext.common.types.JvmTypeParameter)2 XComputedTypeReference (org.eclipse.xtext.xtype.XComputedTypeReference)2 XFunctionTypeRef (org.eclipse.xtext.xtype.XFunctionTypeRef)2 IFile (org.eclipse.core.resources.IFile)1 EObject (org.eclipse.emf.ecore.EObject)1 EPackage (org.eclipse.emf.ecore.EPackage)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1