Search in sources :

Example 6 with JvmType

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

the class KnownTypesScope method getExactMatch.

/*
	 * If we know java.util.Map$Entry exists and we query for the FQN, we assume things are valid.
	 */
protected JvmType getExactMatch(JvmType type, int index, QualifiedName name) {
    String qn = type.getQualifiedName();
    if (Strings.isEmpty(qn)) {
        return null;
    }
    QualifiedName typeName = QualifiedName.create(Strings.split(qn, '.'));
    if (name.equals(typeName)) {
        return type;
    }
    if (name.startsWith(typeName)) {
        JvmType result = findNestedType(type, index, name.skipFirst(typeName.getSegmentCount() - 1));
        return result;
    }
    if (name.getSegmentCount() > typeName.getSegmentCount()) {
        if (typeName.skipLast(1).equals(name.skipLast(1))) {
            if (typeName.getLastSegment().equals(name.skipFirst(typeName.getSegmentCount() - 1).toString("$"))) {
                return type;
            }
        }
    }
    return null;
}
Also used : QualifiedName(org.eclipse.xtext.naming.QualifiedName) JvmType(org.eclipse.xtext.common.types.JvmType)

Example 7 with JvmType

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

the class TypeScopeWithWildcardImports method doGetElements.

@Override
protected void doGetElements(JvmType type, List<IEObjectDescription> result) {
    if (!(type instanceof JvmDeclaredType)) {
        return;
    }
    JvmDeclaredType declaredType = (JvmDeclaredType) type;
    String packageName = declaredType.getPackageName();
    if (!Strings.isEmpty(packageName)) {
        QualifiedName qualifiedPackageName = QualifiedName.create(Strings.split(packageName, '.'));
        QualifiedName withDot = null;
        String withDollar = null;
        for (int i = 0; i < imports.length; i++) {
            ImportNormalizer[] chunk = imports[i];
            for (int j = 0; j < chunk.length; j++) {
                ImportNormalizer normalizer = chunk[j];
                QualifiedName namespacePrefix = normalizer.getImportedNamespacePrefix();
                if (namespacePrefix.equals(qualifiedPackageName)) {
                    if (withDot == null) {
                        withDot = QualifiedName.create(Strings.split(type.getQualifiedName('.'), '.'));
                        withDollar = type.eContainer() instanceof JvmType ? type.getQualifiedName('$').substring(packageName.length() + 1) : null;
                    }
                    result.add(EObjectDescription.create(withDot.skipFirst(qualifiedPackageName.getSegmentCount()), type));
                    if (withDollar != null) {
                        result.add(EObjectDescription.create(withDollar, type));
                    }
                }
            }
        }
    }
    if (parent != null) {
        parent.doGetElements(type, result);
    } else {
        Iterables.addAll(result, typeScope.getElements(type));
    }
}
Also used : ImportNormalizer(org.eclipse.xtext.scoping.impl.ImportNormalizer) QualifiedName(org.eclipse.xtext.naming.QualifiedName) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) JvmType(org.eclipse.xtext.common.types.JvmType)

Example 8 with JvmType

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

the class XtendImportedNamespaceScopeProvider method getImportScope.

private AbstractScope getImportScope(XImportSection importSection, AbstractScope parent, RecordingTypeScope typeScope) {
    if (importSection == null)
        return parent;
    List<XImportDeclaration> importDeclarations = importSection.getImportDeclarations();
    if (importDeclarations.isEmpty()) {
        return parent;
    }
    List<ImportNormalizer> wildcardImports = null;
    List<JvmType> concreteImports = null;
    List<QualifiedName> importedNames = null;
    boolean hasLegacyImport = false;
    for (XImportDeclaration importDeclaration : importDeclarations) {
        if (!importDeclaration.isStatic()) {
            if (importDeclaration.isWildcard()) {
                if (wildcardImports == null) {
                    wildcardImports = Lists.newArrayListWithCapacity(4);
                }
                String importedNamespace = importDeclaration.getImportedNamespace();
                importedNamespace = importedNamespace.substring(0, importedNamespace.length() - 2);
                QualifiedName qualifiedImportedNamespace = qualifiedNameConverter.toQualifiedName(importedNamespace);
                wildcardImports.add(AbstractNestedTypeAwareImportNormalizer.createNestedTypeAwareImportNormalizer(qualifiedImportedNamespace, true, false));
            } else {
                JvmDeclaredType importedType = importDeclaration.getImportedType();
                if (importedType != null && !importedType.eIsProxy()) {
                    if (concreteImports == null || importedNames == null) /* to make JDT happy */
                    {
                        concreteImports = Lists.newArrayListWithCapacity(10);
                        importedNames = Lists.newArrayListWithCapacity(10);
                    }
                    concreteImports.add(importedType);
                    if (importedType.eContainer() instanceof JvmDeclaredType) {
                        String importSyntax = getImportsConfiguration().getLegacyImportSyntax(importDeclaration);
                        if (importSyntax != null) {
                            hasLegacyImport = true;
                            importedNames.add(getQualifiedNameConverter().toQualifiedName(importSyntax));
                        } else
                            importedNames.add(null);
                    } else {
                        importedNames.add(null);
                    }
                }
            }
        }
    }
    return getImportScope(wildcardImports, concreteImports, hasLegacyImport ? importedNames : null, parent, typeScope);
}
Also used : AbstractNestedTypeAwareImportNormalizer(org.eclipse.xtext.xbase.scoping.AbstractNestedTypeAwareImportNormalizer) ImportNormalizer(org.eclipse.xtext.scoping.impl.ImportNormalizer) QualifiedName(org.eclipse.xtext.naming.QualifiedName) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) JvmType(org.eclipse.xtext.common.types.JvmType) XImportDeclaration(org.eclipse.xtext.xtype.XImportDeclaration)

Example 9 with JvmType

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

the class LocalClassAwareTypeNames method doVisitParameterizedTypeReference.

@Override
protected void doVisitParameterizedTypeReference(ParameterizedTypeReference reference, StringBuilder param) {
    JvmType type = reference.getType();
    if (type instanceof JvmDeclaredType) {
        boolean local = ((JvmDeclaredType) type).isLocal();
        if (local) {
            param.append("new ");
            Iterables.getLast(reference.getSuperTypes()).accept(this, param);
            param.append("(){}");
            return;
        }
    }
    super.doVisitParameterizedTypeReference(reference, param);
}
Also used : JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) JvmType(org.eclipse.xtext.common.types.JvmType)

Example 10 with JvmType

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

the class XtendValidator method checkDispatchFunctions.

@Check
public void checkDispatchFunctions(XtendClass clazz) {
    JvmGenericType type = associations.getInferredType(clazz);
    if (type != null) {
        Multimap<DispatchHelper.DispatchSignature, JvmOperation> dispatchMethods = dispatchHelper.getDeclaredOrEnhancedDispatchMethods(type);
        checkDispatchNonDispatchConflict(clazz, dispatchMethods);
        for (DispatchHelper.DispatchSignature signature : dispatchMethods.keySet()) {
            Collection<JvmOperation> dispatchOperations = dispatchMethods.get(signature);
            JvmOperation syntheticDispatchMethod = dispatchHelper.getDispatcherOperation(type, signature);
            if (syntheticDispatchMethod != null) {
                JvmOperation overriddenOperation = overrideHelper.findOverriddenOperation(syntheticDispatchMethod);
                Boolean expectStatic = null;
                if (overriddenOperation != null) {
                    if (isMorePrivateThan(syntheticDispatchMethod.getVisibility(), overriddenOperation.getVisibility())) {
                        String msg = "Synthetic dispatch method reduces visibility of overridden method " + overriddenOperation.getIdentifier();
                        addDispatchError(type, dispatchOperations, msg, null, OVERRIDE_REDUCES_VISIBILITY);
                    }
                    expectStatic = overriddenOperation.isStatic();
                }
                LightweightTypeReference dispatchMethodReturnType = getActualType(clazz, syntheticDispatchMethod);
                if (dispatchOperations.size() == 1) {
                    JvmOperation singleOp = dispatchOperations.iterator().next();
                    XtendFunction function = associations.getXtendFunction(singleOp);
                    addIssue("Single dispatch method.", function, XTEND_MEMBER__MODIFIERS, function.getModifiers().indexOf("dispatch"), SINGLE_DISPATCH_FUNCTION);
                } else {
                    Multimap<List<JvmType>, JvmOperation> signatures = HashMultimap.create();
                    boolean[] allPrimitive = new boolean[signature.getArity()];
                    Arrays.fill(allPrimitive, true);
                    boolean isFirstLocalOperation = true;
                    JvmVisibility commonVisibility = null;
                    Boolean commonStatic = null;
                    for (JvmOperation jvmOperation : dispatchOperations) {
                        signatures.put(getParamTypes(jvmOperation, true), jvmOperation);
                        for (int i = 0; i < jvmOperation.getParameters().size(); i++) {
                            JvmFormalParameter parameter = jvmOperation.getParameters().get(i);
                            if (!(parameter.getParameterType().getType() instanceof JvmPrimitiveType)) {
                                allPrimitive[i] = false;
                            }
                        }
                        if (jvmOperation.getDeclaringType() == type) {
                            if (expectStatic != null) {
                                if (expectStatic && !jvmOperation.isStatic()) {
                                    String msg = "The dispatch method must be static because the dispatch methods in the superclass are static.";
                                    addDispatchError(jvmOperation, msg, "static", DISPATCH_FUNCTIONS_STATIC_EXPECTED);
                                }
                                if (!expectStatic && jvmOperation.isStatic()) {
                                    String msg = "The dispatch method must not be static because the dispatch methods in the superclass are not static.";
                                    addDispatchError(jvmOperation, msg, "static", DISPATCH_FUNCTIONS_NON_STATIC_EXPECTED);
                                }
                            }
                            if (isFirstLocalOperation) {
                                commonVisibility = jvmOperation.getVisibility();
                                commonStatic = jvmOperation.isStatic();
                                isFirstLocalOperation = false;
                            } else {
                                if (jvmOperation.getVisibility() != commonVisibility) {
                                    commonVisibility = null;
                                }
                                if (commonStatic != null && commonStatic != jvmOperation.isStatic()) {
                                    commonStatic = null;
                                }
                            }
                            // TODO move validation to type computation
                            if (dispatchMethodReturnType != null) {
                                XtendFunction function = associations.getXtendFunction(jvmOperation);
                                if (function != null) {
                                    LightweightTypeReference operationType = getActualType(function.getExpression(), jvmOperation);
                                    if (!dispatchMethodReturnType.isAssignableFrom(operationType)) {
                                        error("Incompatible return type of dispatch method. Expected " + dispatchMethodReturnType.getHumanReadableName() + " but was " + operationType.getHumanReadableName(), function, XtendPackage.Literals.XTEND_FUNCTION__RETURN_TYPE, ValidationMessageAcceptor.INSIGNIFICANT_INDEX, INCOMPATIBLE_RETURN_TYPE);
                                    }
                                }
                            }
                        }
                    }
                    if (commonVisibility == null) {
                        addDispatchError(type, dispatchOperations, "All local dispatch methods must have the same visibility.", null, DISPATCH_FUNCTIONS_WITH_DIFFERENT_VISIBILITY);
                    }
                    if (expectStatic == null && commonStatic == null) {
                        addDispatchError(type, dispatchOperations, "Static and non-static dispatch methods can not be mixed.", "static", DISPATCH_FUNCTIONS_MIXED_STATIC_AND_NON_STATIC);
                    }
                    for (final List<JvmType> paramTypes : signatures.keySet()) {
                        Collection<JvmOperation> ops = signatures.get(paramTypes);
                        if (ops.size() > 1) {
                            if (Iterables.any(ops, new Predicate<JvmOperation>() {

                                @Override
                                public boolean apply(JvmOperation input) {
                                    return !getParamTypes(input, false).equals(paramTypes);
                                }
                            })) {
                                for (JvmOperation jvmOperation : ops) {
                                    XtendFunction function = associations.getXtendFunction(jvmOperation);
                                    error("Duplicate dispatch methods. Primitives cannot overload their wrapper types in dispatch methods.", function, null, DUPLICATE_METHOD);
                                }
                            }
                        }
                    }
                    for (int i = 0; i < allPrimitive.length; i++) {
                        if (allPrimitive[i]) {
                            Iterator<JvmOperation> operationIter = dispatchOperations.iterator();
                            JvmType paramType1 = operationIter.next().getParameters().get(i).getParameterType().getType();
                            while (operationIter.hasNext()) {
                                JvmType paramType2 = operationIter.next().getParameters().get(i).getParameterType().getType();
                                if (!paramType2.equals(paramType1)) {
                                    for (JvmOperation jvmOperation : dispatchOperations) {
                                        XtendFunction function = associations.getXtendFunction(jvmOperation);
                                        addIssue("Dispatch methods have arguments with different primitive types.", function, XTEND_EXECUTABLE__PARAMETERS, i, DISPATCH_FUNCTIONS_DIFFERENT_PRIMITIVE_ARGS);
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) RichString(org.eclipse.xtend.core.xtend.RichString) JvmType(org.eclipse.xtext.common.types.JvmType) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) JvmPrimitiveType(org.eclipse.xtext.common.types.JvmPrimitiveType) JvmVisibility(org.eclipse.xtext.common.types.JvmVisibility) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) DispatchHelper(org.eclipse.xtend.core.jvmmodel.DispatchHelper) Check(org.eclipse.xtext.validation.Check)

Aggregations

JvmType (org.eclipse.xtext.common.types.JvmType)73 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)29 EObject (org.eclipse.emf.ecore.EObject)18 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)17 Test (org.junit.Test)17 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)14 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)12 XExpression (org.eclipse.xtext.xbase.XExpression)12 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)11 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)11 JvmAnnotationType (org.eclipse.xtext.common.types.JvmAnnotationType)10 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)9 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)9 AnonymousClass (org.eclipse.xtend.core.xtend.AnonymousClass)8 XtendMember (org.eclipse.xtend.core.xtend.XtendMember)8 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)8 Resource (org.eclipse.emf.ecore.resource.Resource)7 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)7 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)6 JvmEnumerationType (org.eclipse.xtext.common.types.JvmEnumerationType)6