Search in sources :

Example 56 with JavaModelException

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

the class IndexSelector method initializeIndexLocations.

/*
 *  Compute the list of paths which are keying index files.
 */
private void initializeIndexLocations() {
    IPath[] projectsAndJars = this.searchScope.enclosingProjectsAndJars();
    IndexManager manager = JavaModelManager.getIndexManager();
    // use a linked set to preserve the order during search: see bug 348507
    LinkedHashSet locations = new LinkedHashSet();
    IJavaElement focus = MatchLocator.projectOrJarFocus(this.pattern);
    if (focus == null) {
        for (int i = 0; i < projectsAndJars.length; i++) {
            IPath path = projectsAndJars[i];
            Object target = JavaModel.getTarget(path, false);
            if (// case of an external folder
            target instanceof IFolder)
                path = ((IFolder) target).getFullPath();
            locations.add(manager.computeIndexLocation(path));
        }
    } else {
        try {
            // See whether the state builder might be used to reduce the number of index locations
            // find the projects from projectsAndJars that see the focus then walk those projects looking for the jars from projectsAndJars
            int length = projectsAndJars.length;
            JavaProject[] projectsCanSeeFocus = new JavaProject[length];
            SimpleSet visitedProjects = new SimpleSet(length);
            int projectIndex = 0;
            SimpleSet externalLibsToCheck = new SimpleSet(length);
            ObjectVector superTypes = new ObjectVector();
            IJavaElement[] focuses = getFocusedElementsAndTypes(this.pattern, focus, superTypes);
            char[][][] focusQualifiedNames = null;
            boolean isAutoBuilding = ResourcesPlugin.getWorkspace().getDescription().isAutoBuilding();
            if (isAutoBuilding && focus instanceof IJavaProject) {
                focusQualifiedNames = getQualifiedNames(superTypes);
            }
            IJavaModel model = JavaModelManager.getJavaModelManager().getJavaModel();
            for (int i = 0; i < length; i++) {
                IPath path = projectsAndJars[i];
                JavaProject project = (JavaProject) getJavaProject(path, model);
                if (project != null) {
                    visitedProjects.add(project);
                    /*We are adding all modules to the locations for searching in each one.
					Now the location contains not only current module and Jars on which depends, but also all modules from the workspace.*/
                    locations.add(manager.computeIndexLocation(path));
                /*int canSeeFocus = canSeeFocus(focuses, project, focusQualifiedNames);
					if (canSeeFocus == PROJECT_CAN_SEE_FOCUS) {
						locations.add(manager.computeIndexLocation(path));
					}
					if (canSeeFocus != PROJECT_CAN_NOT_SEE_FOCUS) {
						projectsCanSeeFocus[projectIndex++] = project;
					}*/
                } else {
                    externalLibsToCheck.add(path);
                }
            }
            for (int i = 0; i < projectIndex && externalLibsToCheck.elementSize > 0; i++) {
                IClasspathEntry[] entries = projectsCanSeeFocus[i].getResolvedClasspath();
                for (int j = entries.length; --j >= 0; ) {
                    IClasspathEntry entry = entries[j];
                    if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                        IPath path = entry.getPath();
                        if (externalLibsToCheck.remove(path) != null) {
                            Object target = JavaModel.getTarget(path, false);
                            if (// case of an external folder
                            target instanceof IFolder)
                                path = ((IFolder) target).getFullPath();
                            locations.add(manager.computeIndexLocation(path));
                        }
                    }
                }
            }
            // jar files can be included in the search scope without including one of the projects that references them, so scan all projects that have not been visited
            if (externalLibsToCheck.elementSize > 0) {
                IJavaProject[] allProjects = model.getJavaProjects();
                for (int i = 0, l = allProjects.length; i < l && externalLibsToCheck.elementSize > 0; i++) {
                    JavaProject project = (JavaProject) allProjects[i];
                    if (!visitedProjects.includes(project)) {
                        IClasspathEntry[] entries = project.getResolvedClasspath();
                        for (int j = entries.length; --j >= 0; ) {
                            IClasspathEntry entry = entries[j];
                            if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                                IPath path = entry.getPath();
                                if (externalLibsToCheck.remove(path) != null) {
                                    Object target = JavaModel.getTarget(path, false);
                                    if (// case of an external folder
                                    target instanceof IFolder)
                                        path = ((IFolder) target).getFullPath();
                                    locations.add(manager.computeIndexLocation(path));
                                }
                            }
                        }
                    }
                }
            }
        } catch (JavaModelException e) {
        // ignored
        }
    }
    // Ensure no nulls
    locations.remove(null);
    this.indexLocations = (IndexLocation[]) locations.toArray(new IndexLocation[locations.size()]);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaProject(org.eclipse.jdt.internal.core.JavaProject) IJavaProject(org.eclipse.jdt.core.IJavaProject) SimpleSet(org.eclipse.jdt.internal.compiler.util.SimpleSet) JavaModelException(org.eclipse.jdt.core.JavaModelException) IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) ObjectVector(org.eclipse.jdt.internal.compiler.util.ObjectVector) IndexManager(org.eclipse.jdt.internal.core.search.indexing.IndexManager) IJavaProject(org.eclipse.jdt.core.IJavaProject) IndexLocation(org.eclipse.jdt.internal.core.index.IndexLocation) IFolder(org.eclipse.core.resources.IFolder) IJavaModel(org.eclipse.jdt.core.IJavaModel)

Example 57 with JavaModelException

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

the class JavaModelManager method create.

/**
     * Returns the package fragment or package fragment root corresponding to the given folder,
     * its parent or great parent being the given project.
     * or <code>null</code> if unable to associate the given folder with a Java element.
     * <p>
     * Note that a package fragment root is returned rather than a default package.
     * <p>
     * Creating a Java element has the side effect of creating and opening all of the
     * element's parents if they are not yet open.
     */
public static IJavaElement create(IFolder folder, IJavaProject project) {
    if (folder == null) {
        return null;
    }
    IJavaElement element;
    if (project == null) {
        project = JavaCore.create(folder.getProject());
        element = determineIfOnClasspath(folder, project);
        if (element == null) {
            // walk all projects and find one that have the given folder on its classpath
            IJavaProject[] projects;
            try {
                projects = org.eclipse.jdt.internal.core.JavaModelManager.getJavaModelManager().getJavaModel().getJavaProjects();
            } catch (JavaModelException e) {
                return null;
            }
            for (int i = 0, length = projects.length; i < length; i++) {
                project = projects[i];
                element = determineIfOnClasspath(folder, project);
                if (element != null)
                    break;
            }
        }
    } else {
        element = determineIfOnClasspath(folder, project);
    }
    return element;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaProject(org.eclipse.jdt.core.IJavaProject)

Example 58 with JavaModelException

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

the class JavaModelManager method determineIfOnClasspath.

/**
     * Returns the package fragment root represented by the resource, or
     * the package fragment the given resource is located in, or <code>null</code>
     * if the given resource is not on the classpath of the given project.
     */
public static IJavaElement determineIfOnClasspath(IResource resource, IJavaProject project) {
    IPath resourcePath = resource.getFullPath();
    boolean isExternal = ExternalFoldersManager.isExternalFolderPath(resourcePath);
    if (isExternal)
        resourcePath = resource.getLocation();
    try {
        JavaProjectElementInfo projectInfo = (JavaProjectElementInfo) org.eclipse.jdt.internal.core.JavaModelManager.getJavaModelManager().getInfo(project);
        JavaProjectElementInfo.ProjectCache projectCache = projectInfo == null ? null : projectInfo.projectCache;
        HashtableOfArrayToObject allPkgFragmentsCache = projectCache == null ? null : projectCache.allPkgFragmentsCache;
        boolean isJavaLike = Util.isJavaLikeFileName(resourcePath.lastSegment());
        IClasspathEntry[] entries = // JAVA file can only live inside SRC folder (on the raw path)
        isJavaLike ? // JAVA file can only live inside SRC folder (on the raw path)
        project.getRawClasspath() : ((JavaProject) project).getResolvedClasspath();
        int length = entries.length;
        if (length > 0) {
            String sourceLevel = project.getOption(JavaCore.COMPILER_SOURCE, true);
            String complianceLevel = project.getOption(JavaCore.COMPILER_COMPLIANCE, true);
            for (int i = 0; i < length; i++) {
                IClasspathEntry entry = entries[i];
                if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT)
                    continue;
                IPath rootPath = entry.getPath();
                if (rootPath.equals(resourcePath)) {
                    if (isJavaLike)
                        return null;
                    return project.getPackageFragmentRoot(resource);
                } else if (rootPath.isPrefixOf(resourcePath)) {
                    // allow creation of package fragment if it contains a .java file that is included
                    if (!Util.isExcluded(resourcePath, ((ClasspathEntry) entry).fullInclusionPatternChars(), ((ClasspathEntry) entry).fullExclusionPatternChars(), true)) {
                        // given we have a resource child of the root, it cannot be a JAR pkg root
                        PackageFragmentRoot root = isExternal ? new ExternalPackageFragmentRoot(rootPath, (JavaProject) project) : (PackageFragmentRoot) ((JavaProject) project).getFolderPackageFragmentRoot(rootPath);
                        if (root == null)
                            return null;
                        IPath pkgPath = resourcePath.removeFirstSegments(rootPath.segmentCount());
                        if (resource.getType() == IResource.FILE) {
                            // if the resource is a file, then remove the last segment which
                            // is the file name in the package
                            pkgPath = pkgPath.removeLastSegments(1);
                        }
                        String[] pkgName = pkgPath.segments();
                        // (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=133141)
                        if (allPkgFragmentsCache != null && allPkgFragmentsCache.containsKey(pkgName))
                            return root.getPackageFragment(pkgName);
                        if (pkgName.length != 0 && JavaConventions.validatePackageName(Util.packageName(pkgPath, sourceLevel, complianceLevel), sourceLevel, complianceLevel).getSeverity() == IStatus.ERROR) {
                            return null;
                        }
                        return root.getPackageFragment(pkgName);
                    }
                }
            }
        }
    } catch (JavaModelException npe) {
        return null;
    }
    return null;
}
Also used : IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) JavaModelException(org.eclipse.jdt.core.JavaModelException) IPath(org.eclipse.core.runtime.IPath) HashtableOfArrayToObject(org.eclipse.jdt.internal.core.util.HashtableOfArrayToObject) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry)

Example 59 with JavaModelException

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

the class JavaModelManager method initializeContainer.

private IClasspathContainer initializeContainer(IJavaProject project, IPath containerPath) throws JavaModelException {
    ClasspathContainerInitializer initializer = containerInitializersCache.get(containerPath.segment(0));
    IClasspathContainer container = null;
    if (initializer != null) {
        // avoid initialization cycles
        containerPut(project, containerPath, CONTAINER_INITIALIZATION_IN_PROGRESS);
        try {
            initializer.initialize(containerPath, project);
            //            if (monitor != null)
            //                monitor.subTask(""); //$NON-NLS-1$
            // retrieve value (if initialization was successful)
            container = containerBeingInitializedGet(project, containerPath);
            if (container == null && containerGet(project, containerPath) == null) {
                // initializer failed to do its job: redirect to the failure container
                container = initializer.getFailureContainer(containerPath, project);
                //                if (container == null) {
                //                    if (CP_RESOLVE_VERBOSE || CP_RESOLVE_VERBOSE_FAILURE)
                //                        verbose_container_null_failure_container(project, containerPath, initializer);
                //                    return null; // break cycle
                //                }
                //                if (CP_RESOLVE_VERBOSE || CP_RESOLVE_VERBOSE_FAILURE)
                //                    verbose_container_using_failure_container(project, containerPath, initializer);
                containerPut(project, containerPath, container);
            }
        } catch (CoreException e) {
            if (e instanceof JavaModelException) {
                throw (JavaModelException) e;
            } else {
                throw new JavaModelException(e);
            }
        }
    } else {
        // create a dummy initializer and get the default failure container
        container = (new ClasspathContainerInitializer() {

            public void initialize(IPath path, IJavaProject javaProject) throws CoreException {
            // not used
            }
        }).getFailureContainer(containerPath, project);
    }
    return container;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaProject(org.eclipse.jdt.core.IJavaProject) CoreException(org.eclipse.core.runtime.CoreException) IPath(org.eclipse.core.runtime.IPath) IClasspathContainer(org.eclipse.jdt.core.IClasspathContainer) ClasspathContainerInitializer(org.eclipse.jdt.core.ClasspathContainerInitializer)

Example 60 with JavaModelException

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

the class JavaModelOperation method deleteEmptyPackageFragment.

/**
	 * Convenience method to delete an empty package fragment
	 */
protected void deleteEmptyPackageFragment(IPackageFragment fragment, boolean forceFlag, IResource rootResource) throws JavaModelException {
    IContainer resource = (IContainer) ((JavaElement) fragment).resource();
    try {
        resource.delete(forceFlag ? IResource.FORCE | IResource.KEEP_HISTORY : IResource.KEEP_HISTORY, getSubProgressMonitor(1));
        setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
        while (resource instanceof IFolder) {
            // deleting a package: delete the parent if it is empty (e.g. deleting x.y where folder x doesn't have resources but y)
            // without deleting the package fragment root
            resource = resource.getParent();
            if (!resource.equals(rootResource) && resource.members().length == 0) {
                resource.delete(forceFlag ? IResource.FORCE | IResource.KEEP_HISTORY : IResource.KEEP_HISTORY, getSubProgressMonitor(1));
                setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
            }
        }
    } catch (CoreException e) {
        throw new JavaModelException(e);
    }
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) CoreException(org.eclipse.core.runtime.CoreException) IContainer(org.eclipse.core.resources.IContainer) IFolder(org.eclipse.core.resources.IFolder)

Aggregations

JavaModelException (org.eclipse.jdt.core.JavaModelException)172 IJavaProject (org.eclipse.jdt.core.IJavaProject)44 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)41 IType (org.eclipse.jdt.core.IType)40 IJavaElement (org.eclipse.jdt.core.IJavaElement)35 CoreException (org.eclipse.core.runtime.CoreException)30 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)25 IPath (org.eclipse.core.runtime.IPath)24 ArrayList (java.util.ArrayList)21 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)17 IFile (org.eclipse.core.resources.IFile)15 IResource (org.eclipse.core.resources.IResource)15 HashMap (java.util.HashMap)14 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)13 IMethod (org.eclipse.jdt.core.IMethod)12 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)12 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)11 SimpleName (org.eclipse.jdt.core.dom.SimpleName)9 Iterator (java.util.Iterator)8 ISourceRange (org.eclipse.jdt.core.ISourceRange)8