Search in sources :

Example 31 with XImportDeclaration

use of org.eclipse.xtext.xtype.XImportDeclaration in project xtext-extras by eclipse.

the class RewritableImportSection method addStaticImport.

/**
 * @param typeFqn
 *            The fully qualified name of the type to import. E.g. <code>java.util.List</code>. May not be
 *            <code>null</code>.
 * @param member
 *            member name to import. May not be <code>null</code>. For wildcard use <code>*</code>
 */
public boolean addStaticImport(final String typeFqn, final String member) {
    if (typeFqn == null || member == null) {
        throw new IllegalArgumentException("Type name " + typeFqn + ". Member name: " + member);
    }
    if (hasStaticImport(typeFqn, member, false)) {
        return false;
    }
    XImportDeclaration importDecl = createImport(typeFqn, member);
    importDecl.setStatic(true);
    return addedImportDeclarations.add(importDecl);
}
Also used : XImportDeclaration(org.eclipse.xtext.xtype.XImportDeclaration)

Example 32 with XImportDeclaration

use of org.eclipse.xtext.xtype.XImportDeclaration in project xtext-extras by eclipse.

the class XbaseValidator method populateMaps.

protected void populateMaps(XImportSection importSection, final Map<String, List<XImportDeclaration>> imports, final Map<String, List<XImportDeclaration>> staticImports, final Map<String, List<XImportDeclaration>> extensionImports, final Map<String, JvmType> importedNames) {
    for (XImportDeclaration imp : importSection.getImportDeclarations()) {
        if (imp.getImportedNamespace() != null) {
            addIssue("The use of wildcard imports is deprecated.", imp, IMPORT_WILDCARD_DEPRECATED);
            continue;
        }
        JvmType importedType = imp.getImportedType();
        if (importedType == null || importedType.eIsProxy()) {
            continue;
        }
        Map<String, List<XImportDeclaration>> map = imp.isStatic() ? (imp.isExtension() ? extensionImports : staticImports) : imports;
        List<XImportDeclaration> list = map.get(importedType.getIdentifier());
        if (list != null) {
            list.add(imp);
            continue;
        }
        list = Lists.newArrayListWithCapacity(2);
        list.add(imp);
        map.put(importedType.getIdentifier(), list);
        if (!imp.isStatic()) {
            JvmType currentType = importedType;
            String currentSuffix = currentType.getSimpleName();
            JvmType collidingImport = importedNames.put(currentSuffix, importedType);
            if (collidingImport != null)
                error("The import '" + importedType.getIdentifier() + "' collides with the import '" + collidingImport.getIdentifier() + "'.", imp, null, IssueCodes.IMPORT_COLLISION);
            while (currentType.eContainer() instanceof JvmType) {
                currentType = (JvmType) currentType.eContainer();
                currentSuffix = currentType.getSimpleName() + "$" + currentSuffix;
                JvmType collidingImport2 = importedNames.put(currentSuffix, importedType);
                if (collidingImport2 != null)
                    error("The import '" + importedType.getIdentifier() + "' collides with the import '" + collidingImport2.getIdentifier() + "'.", imp, null, IssueCodes.IMPORT_COLLISION);
            }
        } else if (!imp.isWildcard()) {
            Iterable<JvmFeature> allFeatures = staticallyImportedMemberProvider.getAllFeatures(imp);
            if (!allFeatures.iterator().hasNext()) {
                addIssue("The import " + imp.getImportedName() + " cannot be resolved.", imp, IMPORT_UNRESOLVED);
                continue;
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) JvmType(org.eclipse.xtext.common.types.JvmType) XImportDeclaration(org.eclipse.xtext.xtype.XImportDeclaration)

Example 33 with XImportDeclaration

use of org.eclipse.xtext.xtype.XImportDeclaration in project xtext-extras by eclipse.

the class XbaseValidator method removeStaticImport.

private boolean removeStaticImport(Map<String, List<XImportDeclaration>> staticImports, JvmMember member) {
    JvmDeclaredType declaringType = member.getDeclaringType();
    String identifier = declaringType.getIdentifier();
    List<XImportDeclaration> list = staticImports.get(identifier);
    if (list == null) {
        return false;
    }
    if (list.size() == 1) {
        staticImports.remove(identifier);
        return true;
    }
    int indexToRemove = -1;
    for (int i = 0; i < list.size(); i++) {
        XImportDeclaration staticImportDeclaration = list.get(i);
        if (staticImportDeclaration.isWildcard()) {
            if (indexToRemove == -1) {
                indexToRemove = i;
            }
            continue;
        }
        if (Objects.equal(member.getSimpleName(), staticImportDeclaration.getMemberName())) {
            indexToRemove = i;
            break;
        }
    }
    if (indexToRemove == -1) {
        indexToRemove = 0;
    }
    list.remove(indexToRemove);
    return true;
}
Also used : JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) XImportDeclaration(org.eclipse.xtext.xtype.XImportDeclaration) JvmTypeConstraint(org.eclipse.xtext.common.types.JvmTypeConstraint)

Example 34 with XImportDeclaration

use of org.eclipse.xtext.xtype.XImportDeclaration in project xtext-extras by eclipse.

the class SerializerScopeProvider method doGetTypeScope.

protected IScope doGetTypeScope(XFeatureCall call, JvmType type) {
    if (call.isPackageFragment()) {
        if (type instanceof JvmDeclaredType) {
            String packageName = ((JvmDeclaredType) type).getPackageName();
            int dot = packageName.indexOf('.');
            if (dot == -1) {
                return new SingletonScope(EObjectDescription.create(packageName, type), IScope.NULLSCOPE);
            } else {
                String firstSegment = packageName.substring(0, dot);
                return new SingletonScope(EObjectDescription.create(firstSegment, type), IScope.NULLSCOPE);
            }
        }
        return IScope.NULLSCOPE;
    } else {
        if (type instanceof JvmDeclaredType && ((JvmDeclaredType) type).getDeclaringType() != null) {
            Resource resource = call.eResource();
            if (resource instanceof XtextResource) {
                XImportSection importSection = importsConfiguration.getImportSection((XtextResource) resource);
                if (importSection != null) {
                    List<XImportDeclaration> importDeclarations = importSection.getImportDeclarations();
                    List<IEObjectDescription> descriptions = Lists.newArrayList();
                    for (XImportDeclaration importDeclaration : importDeclarations) {
                        if (!importDeclaration.isStatic() && !importDeclaration.isWildcard() && !importDeclaration.isExtension()) {
                            JvmDeclaredType importedType = importDeclaration.getImportedType();
                            if (importedType == type) {
                                String syntax = importsConfiguration.getLegacyImportSyntax(importDeclaration);
                                if (syntax != null && /* no node model attached */
                                syntax.equals(type.getQualifiedName())) {
                                    String packageName = importedType.getPackageName();
                                    descriptions.add(EObjectDescription.create(syntax.substring(packageName.length() + 1), type));
                                }
                            }
                            if (EcoreUtil.isAncestor(importedType, type)) {
                                String name = type.getSimpleName();
                                JvmType worker = type;
                                while (worker != importedType) {
                                    worker = (JvmType) worker.eContainer();
                                    name = worker.getSimpleName() + "$" + name;
                                }
                                descriptions.add(EObjectDescription.create(name, type));
                            }
                        }
                    }
                    return new SimpleScope(descriptions);
                }
            }
            return new SingletonScope(EObjectDescription.create(type.getSimpleName(), type), IScope.NULLSCOPE);
        } else {
            return new SingletonScope(EObjectDescription.create(type.getSimpleName(), type), IScope.NULLSCOPE);
        }
    }
}
Also used : SingletonScope(org.eclipse.xtext.scoping.impl.SingletonScope) XImportSection(org.eclipse.xtext.xtype.XImportSection) SimpleScope(org.eclipse.xtext.scoping.impl.SimpleScope) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) XtextResource(org.eclipse.xtext.resource.XtextResource) JvmType(org.eclipse.xtext.common.types.JvmType) XImportDeclaration(org.eclipse.xtext.xtype.XImportDeclaration) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Aggregations

XImportDeclaration (org.eclipse.xtext.xtype.XImportDeclaration)34 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)8 XImportSection (org.eclipse.xtext.xtype.XImportSection)8 JvmType (org.eclipse.xtext.common.types.JvmType)6 JvmTypeParameter (org.eclipse.xtext.common.types.JvmTypeParameter)6 Test (org.junit.Test)6 JvmParameterizedTypeReference (org.eclipse.xtext.common.types.JvmParameterizedTypeReference)5 EObject (org.eclipse.emf.ecore.EObject)4 EPackage (org.eclipse.emf.ecore.EPackage)4 Action (org.eclipse.xtext.Action)4 Parameter (org.eclipse.xtext.Parameter)4 ParserRule (org.eclipse.xtext.ParserRule)4 JvmLowerBound (org.eclipse.xtext.common.types.JvmLowerBound)4 JvmUpperBound (org.eclipse.xtext.common.types.JvmUpperBound)4 ITextRegion (org.eclipse.xtext.util.ITextRegion)4 XBlockExpression (org.eclipse.xtext.xbase.XBlockExpression)4 XClosure (org.eclipse.xtext.xbase.XClosure)4 XFunctionTypeRef (org.eclipse.xtext.xtype.XFunctionTypeRef)4 JvmFormalParameter (org.eclipse.xtext.common.types.JvmFormalParameter)3 List (java.util.List)2