Search in sources :

Example 6 with IPackageFragmentRoot

use of org.eclipse.jdt.core.IPackageFragmentRoot in project che by eclipse.

the class JavaModelManager method secondaryTypesSearching.

/*
     * Perform search request to get all secondary types of a given project.
     * If not waiting for indexes and indexing is running, will return types found in current built indexes...
     */
private Map secondaryTypesSearching(IJavaProject project, boolean waitForIndexes, IProgressMonitor monitor, final PerProjectInfo projectInfo) throws JavaModelException {
    if (VERBOSE || BasicSearchEngine.VERBOSE) {
        //$NON-NLS-1$
        StringBuffer buffer = new StringBuffer("JavaModelManager.secondaryTypesSearch(");
        buffer.append(project.getElementName());
        buffer.append(',');
        buffer.append(waitForIndexes);
        buffer.append(')');
        Util.verbose(buffer.toString());
    }
    final Hashtable secondaryTypes = new Hashtable(3);
    IRestrictedAccessTypeRequestor nameRequestor = new IRestrictedAccessTypeRequestor() {

        public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path, AccessRestriction access) {
            //$NON-NLS-1$
            String key = packageName == null ? "" : new String(packageName);
            HashMap types = (HashMap) secondaryTypes.get(key);
            if (types == null)
                types = new HashMap(3);
            types.put(new String(simpleTypeName), path);
            secondaryTypes.put(key, types);
        }
    };
    // Build scope using prereq projects but only source folders
    IPackageFragmentRoot[] allRoots = project.getAllPackageFragmentRoots();
    int length = allRoots.length, size = 0;
    IPackageFragmentRoot[] allSourceFolders = new IPackageFragmentRoot[length];
    for (int i = 0; i < length; i++) {
        if (allRoots[i].getKind() == IPackageFragmentRoot.K_SOURCE) {
            allSourceFolders[size++] = allRoots[i];
        }
    }
    if (size < length) {
        System.arraycopy(allSourceFolders, 0, allSourceFolders = new IPackageFragmentRoot[size], 0, size);
    }
    // Search all secondary types on scope
    new BasicSearchEngine().searchAllSecondaryTypeNames(allSourceFolders, nameRequestor, waitForIndexes, monitor);
    // Build types from paths
    Iterator packages = secondaryTypes.values().iterator();
    while (packages.hasNext()) {
        HashMap types = (HashMap) packages.next();
        HashMap tempTypes = new HashMap(types.size());
        Iterator names = types.entrySet().iterator();
        while (names.hasNext()) {
            Map.Entry entry = (Map.Entry) names.next();
            String typeName = (String) entry.getKey();
            String path = (String) entry.getValue();
            names.remove();
            if (Util.isJavaLikeFileName(path)) {
                IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(path));
                ICompilationUnit unit = org.eclipse.jdt.internal.core.JavaModelManager.createCompilationUnitFrom(file, null);
                IType type = unit.getType(typeName);
                tempTypes.put(typeName, type);
            }
        }
        types.putAll(tempTypes);
    }
    // Store result in per project info cache if still null or there's still an indexing cache (may have been set by another thread...)
    if (projectInfo.secondaryTypes == null || projectInfo.secondaryTypes.get(INDEXED_SECONDARY_TYPES) != null) {
        projectInfo.secondaryTypes = secondaryTypes;
        if (VERBOSE || BasicSearchEngine.VERBOSE) {
            //$NON-NLS-1$
            System.out.print(Thread.currentThread() + "	-> secondary paths stored in cache: ");
            System.out.println();
            Iterator entries = secondaryTypes.entrySet().iterator();
            while (entries.hasNext()) {
                Map.Entry entry = (Map.Entry) entries.next();
                String qualifiedName = (String) entry.getKey();
                //$NON-NLS-1$
                Util.verbose("		- " + qualifiedName + '-' + entry.getValue());
            }
        }
    }
    return projectInfo.secondaryTypes;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) AccessRestriction(org.eclipse.jdt.internal.compiler.env.AccessRestriction) IFile(org.eclipse.core.resources.IFile) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) Hashtable(java.util.Hashtable) IRestrictedAccessTypeRequestor(org.eclipse.jdt.internal.core.search.IRestrictedAccessTypeRequestor) BasicSearchEngine(org.eclipse.jdt.internal.core.search.BasicSearchEngine) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) IType(org.eclipse.jdt.core.IType) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) Iterator(java.util.Iterator) Map(java.util.Map) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap)

Example 7 with IPackageFragmentRoot

use of org.eclipse.jdt.core.IPackageFragmentRoot in project che by eclipse.

the class JavaProject method computePackageFragmentRoots.

/**
     * Returns (local/all) the package fragment roots identified by the given project's classpath.
     * Note: this follows project classpath references to find required project contributions,
     * eliminating duplicates silently.
     * Only works with resolved entries
     *
     * @param resolvedClasspath
     *         IClasspathEntry[]
     * @param retrieveExportedRoots
     *         boolean
     * @return IPackageFragmentRoot[]
     * @throws JavaModelException
     */
public IPackageFragmentRoot[] computePackageFragmentRoots(IClasspathEntry[] resolvedClasspath, boolean retrieveExportedRoots, Map rootToResolvedEntries) throws JavaModelException {
    ObjectVector accumulatedRoots = new ObjectVector();
    computePackageFragmentRoots(resolvedClasspath, accumulatedRoots, // rootIDs
    new HashSet(5), // inside original project
    null, retrieveExportedRoots, rootToResolvedEntries);
    IPackageFragmentRoot[] rootArray = new IPackageFragmentRoot[accumulatedRoots.size()];
    accumulatedRoots.copyInto(rootArray);
    return rootArray;
}
Also used : ObjectVector(org.eclipse.jdt.internal.compiler.util.ObjectVector) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 8 with IPackageFragmentRoot

use of org.eclipse.jdt.core.IPackageFragmentRoot in project che by eclipse.

the class JavaProject method computePackageFragmentRoots.

/**
     * Returns the package fragment roots identified by the given entry. In case it refers to
     * a project, it will follow its classpath so as to find exported roots as well.
     * Only works with resolved entry
     *
     * @param resolvedEntry
     *         IClasspathEntry
     * @param accumulatedRoots
     *         ObjectVector
     * @param rootIDs
     *         HashSet
     * @param referringEntry
     *         the CP entry (project) referring to this entry, or null if initial project
     * @param retrieveExportedRoots
     *         boolean
     * @throws JavaModelException
     */
public void computePackageFragmentRoots(IClasspathEntry resolvedEntry, ObjectVector accumulatedRoots, HashSet rootIDs, IClasspathEntry referringEntry, boolean retrieveExportedRoots, Map rootToResolvedEntries) throws JavaModelException {
    String rootID = ((ClasspathEntry) resolvedEntry).rootID();
    if (rootIDs.contains(rootID))
        return;
    IPath projectPath = this.project.getFullPath();
    IPath entryPath = resolvedEntry.getPath();
    IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
    IPackageFragmentRoot root = null;
    switch(resolvedEntry.getEntryKind()) {
        // source folder
        case IClasspathEntry.CPE_SOURCE:
            if (projectPath.isPrefixOf(entryPath)) {
                Object target = getTarget(entryPath, true);
                if (target == null)
                    return;
                if (target instanceof IFolder || target instanceof IProject) {
                    root = getPackageFragmentRoot((IResource) target);
                }
            }
            break;
        // internal/external JAR or folder
        case IClasspathEntry.CPE_LIBRARY:
            if (referringEntry != null && !resolvedEntry.isExported())
                return;
            Object target = getTarget(entryPath, true);
            if (target == null)
                return;
            if (target instanceof IResource) {
                //                    // internal target
                root = getPackageFragmentRoot((IResource) target, entryPath);
            } else if (target instanceof File) {
                // external target
                if (isFile(target)) {
                    root = new JarPackageFragmentRoot(entryPath, this);
                } else if (((File) target).isDirectory()) {
                    //                        root = new ExternalPackageFragmentRoot(entryPath, this);
                    throw new UnsupportedOperationException();
                }
            }
            break;
        // recurse into required project
        case IClasspathEntry.CPE_PROJECT:
            if (!retrieveExportedRoots)
                return;
            if (referringEntry != null && !resolvedEntry.isExported())
                return;
            IResource member = workspaceRoot.findMember(entryPath);
            if (member != null && member.getType() == IResource.PROJECT) {
                // double check if bound to project (23977)
                IProject requiredProjectRsc = (IProject) member;
                if (org.eclipse.jdt.internal.core.JavaProject.hasJavaNature(requiredProjectRsc)) {
                    // special builder binary output
                    rootIDs.add(rootID);
                    org.eclipse.jdt.internal.core.JavaProject requiredProject = (org.eclipse.jdt.internal.core.JavaProject) JavaCore.create(requiredProjectRsc);
                    requiredProject.computePackageFragmentRoots(requiredProject.getResolvedClasspath(), accumulatedRoots, rootIDs, rootToResolvedEntries == null ? resolvedEntry : ((ClasspathEntry) resolvedEntry).combineWith((ClasspathEntry) referringEntry), // only combine if need to build the reverse map
                    retrieveExportedRoots, rootToResolvedEntries);
                }
                break;
            }
    }
    if (root != null) {
        accumulatedRoots.add(root);
        rootIDs.add(rootID);
        if (rootToResolvedEntries != null)
            rootToResolvedEntries.put(root, ((ClasspathEntry) resolvedEntry).combineWith((ClasspathEntry) referringEntry));
    }
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject) IPath(org.eclipse.core.runtime.IPath) IProject(org.eclipse.core.resources.IProject) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) JavaModelManager.isFile(org.eclipse.jdt.internal.core.JavaModelManager.isFile) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IResource(org.eclipse.core.resources.IResource) IFolder(org.eclipse.core.resources.IFolder)

Example 9 with IPackageFragmentRoot

use of org.eclipse.jdt.core.IPackageFragmentRoot in project che by eclipse.

the class DeltaProcessor method popUntilPrefixOf.

private void popUntilPrefixOf(IPath path) {
    while (this.currentElement != null) {
        IPath currentElementPath = null;
        if (this.currentElement instanceof IPackageFragmentRoot) {
            currentElementPath = ((IPackageFragmentRoot) this.currentElement).getPath();
        } else {
            IResource currentElementResource = this.currentElement.resource();
            if (currentElementResource != null) {
                currentElementPath = currentElementResource.getFullPath();
            }
        }
        if (currentElementPath != null) {
            if (this.currentElement instanceof IPackageFragment && ((IPackageFragment) this.currentElement).isDefaultPackage() && currentElementPath.segmentCount() != path.segmentCount() - 1) {
                // default package and path is not a direct child
                this.currentElement = (Openable) this.currentElement.getParent();
            }
            if (currentElementPath.isPrefixOf(path)) {
                return;
            }
        }
        this.currentElement = (Openable) this.currentElement.getParent();
    }
}
Also used : IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IPath(org.eclipse.core.runtime.IPath) IResource(org.eclipse.core.resources.IResource) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 10 with IPackageFragmentRoot

use of org.eclipse.jdt.core.IPackageFragmentRoot in project che by eclipse.

the class ImportOrganizeTest method testImportToStar.

@Test
public void testImportToStar() throws Exception {
    IPackageFragmentRoot sourceFolder = JavaProjectHelper.addSourceContainer(fJProject1, "src");
    IPackageFragment pack2 = sourceFolder.createPackageFragment("pack", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package pack;\n");
    buf.append("public class List {\n");
    buf.append("}\n");
    pack2.createCompilationUnit("List.java", buf.toString(), false, null);
    IPackageFragment pack1 = sourceFolder.createPackageFragment("pack1", false, null);
    buf = new StringBuffer();
    buf.append("package pack1;\n");
    buf.append("\n");
    buf.append("import java.util.Set;\n");
    buf.append("import java.util.Vector;\n");
    buf.append("import java.util.Map;\n");
    buf.append("\n");
    buf.append("import pack.List;\n");
    buf.append("\n");
    buf.append("public class C {\n");
    buf.append("    Vector v;\n");
    buf.append("    Set v2;\n");
    buf.append("    Map v3;\n");
    buf.append("    List v4;\n");
    buf.append("    String v5;\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("C.java", buf.toString(), false, null);
    String[] order = new String[] { "java", "pack" };
    IChooseImportQuery query = createQuery("C", new String[] {}, new int[] {});
    OrganizeImportsOperation op = createOperation(cu, order, 2, false, true, true, query);
    op.run(null);
    buf = new StringBuffer();
    buf.append("package pack1;\n");
    buf.append("\n");
    buf.append("import java.util.*;\n");
    buf.append("\n");
    buf.append("import pack.List;\n");
    buf.append("\n");
    buf.append("public class C {\n");
    buf.append("    Vector v;\n");
    buf.append("    Set v2;\n");
    buf.append("    Map v3;\n");
    buf.append("    List v4;\n");
    buf.append("    String v5;\n");
    buf.append("}\n");
    assertEqualString(cu.getSource(), buf.toString());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) OrganizeImportsOperation(org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation) IChooseImportQuery(org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) Test(org.junit.Test)

Aggregations

IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)171 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)94 Test (org.junit.Test)91 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)77 OrganizeImportsOperation (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation)67 IChooseImportQuery (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery)67 IJavaProject (org.eclipse.jdt.core.IJavaProject)30 File (java.io.File)26 IJavaElement (org.eclipse.jdt.core.IJavaElement)20 JarEntry (org.eclipse.che.ide.ext.java.shared.JarEntry)16 IResource (org.eclipse.core.resources.IResource)16 IPath (org.eclipse.core.runtime.IPath)13 ArrayList (java.util.ArrayList)12 Path (org.eclipse.core.runtime.Path)11 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)11 CoreException (org.eclipse.core.runtime.CoreException)10 IFile (org.eclipse.core.resources.IFile)8 IType (org.eclipse.jdt.core.IType)8 JavaModelException (org.eclipse.jdt.core.JavaModelException)8 IFolder (org.eclipse.core.resources.IFolder)7