Search in sources :

Example 1 with IJavaModel

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

the class IndexSelector method canSeeFocus.

/**
	 * Returns whether elements of the given project or jar can see the given focus (an IJavaProject or
	 * a JarPackageFragmentRot) either because the focus is part of the project or the jar, or because it is
	 * accessible throught the project's classpath
	 */
public static int canSeeFocus(SearchPattern pattern, IPath projectOrJarPath) {
    try {
        IJavaModel model = JavaModelManager.getJavaModelManager().getJavaModel();
        IJavaProject project = getJavaProject(projectOrJarPath, model);
        IJavaElement[] focuses = getFocusedElementsAndTypes(pattern, project, null);
        if (focuses.length == 0)
            return PROJECT_CAN_NOT_SEE_FOCUS;
        if (project != null) {
            return canSeeFocus(focuses, (JavaProject) project, null);
        }
        // projectOrJarPath is a jar
        // it can see the focus only if it is on the classpath of a project that can see the focus
        int result = PROJECT_CAN_NOT_SEE_FOCUS;
        IJavaProject[] allProjects = model.getJavaProjects();
        for (int i = 0, length = allProjects.length; i < length; i++) {
            JavaProject otherProject = (JavaProject) allProjects[i];
            IClasspathEntry entry = otherProject.getClasspathEntryFor(projectOrJarPath);
            if (entry != null && entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                int canSeeFocus = canSeeFocus(focuses, otherProject, null);
                if (canSeeFocus == PROJECT_CAN_SEE_FOCUS)
                    return PROJECT_CAN_SEE_FOCUS;
                if (canSeeFocus == PROJECT_SOURCE_CAN_NOT_SEE_FOCUS)
                    result = PROJECT_SOURCE_CAN_NOT_SEE_FOCUS;
            }
        }
        return result;
    } catch (JavaModelException e) {
        return PROJECT_CAN_NOT_SEE_FOCUS;
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaProject(org.eclipse.jdt.internal.core.JavaProject) IJavaProject(org.eclipse.jdt.core.IJavaProject) JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaProject(org.eclipse.jdt.core.IJavaProject) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) IJavaModel(org.eclipse.jdt.core.IJavaModel)

Example 2 with IJavaModel

use of org.eclipse.jdt.core.IJavaModel 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 3 with IJavaModel

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

the class DeltaProcessor method processResourceDelta.

/*
     * Converts a <code>IResourceDelta</code> rooted in a <code>Workspace</code> into
     * the corresponding set of <code>IJavaElementDelta</code>, rooted in the
     * relevant <code>JavaModel</code>s.
     */
private IJavaElementDelta processResourceDelta(IResourceDelta changes) {
    try {
        IJavaModel model = this.manager.getJavaModel();
        if (!model.isOpen()) {
            // force opening of java model so that java element delta are reported
            try {
                model.open(null);
            } catch (JavaModelException e) {
                if (VERBOSE) {
                    e.printStackTrace();
                }
                return null;
            }
        }
        this.state.initializeRoots(false);
        this.currentElement = null;
        // get the workspace delta, and start processing there.
        //			IResourceDelta[] deltas = (IResourceDelta[])changes.getAffectedChildren(IResourceDelta.ADDED | IResourceDelta.REMOVED |
        // IResourceDelta.CHANGED, IContainer.INCLUDE_HIDDEN);
        //			for (int i = 0; i < deltas.length; i++) {
        //				IResourceDelta delta = deltas[i];
        //				File res = delta.getFile();
        //
        //				// find out the element type
        //				RootInfo rootInfo = null;
        //				int elementType;
        //				IProject proj = (IProject)res;
        //				boolean wasJavaProject = this.state.findJavaProject(proj.getName()) != null;
        //				boolean isJavaProject = JavaProject.hasJavaNature(proj);
        //				if (!wasJavaProject && !isJavaProject) {
        //					elementType = NON_JAVA_RESOURCE;
        //				} else {
        //					IPath rootPath = externalPath(res);
        //					rootInfo = enclosingRootInfo(rootPath, delta.getKind());
        //					if (rootInfo != null && rootInfo.isRootOfProject(rootPath)) {
        //						elementType = IJavaElement.PACKAGE_FRAGMENT_ROOT;
        //					} else {
        //						elementType = IJavaElement.JAVA_PROJECT;
        //					}
        //				}
        //
        //				// traverse delta
        //				traverseDelta(changes, IJavaElement.JAVA_PROJECT, null, null);
        updateCurrentDeltaAndIndex(changes, IJavaElement.COMPILATION_UNIT, null);
        //
        //				if (elementType == NON_JAVA_RESOURCE
        //						|| (wasJavaProject != isJavaProject && (delta.getKind()) == IResourceDelta.CHANGED)) { // project has changed
        // nature (description or open/closed)
        //					try {
        //						// add child as non java resource
        //						nonJavaResourcesChanged((JavaModel)model, delta);
        //					} catch (JavaModelException e) {
        //						// java model could not be opened
        //					}
        //				}
        //
        //			}
        resetProjectCaches();
        return this.currentDelta;
    } finally {
        this.currentDelta = null;
    }
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaModel(org.eclipse.jdt.core.IJavaModel)

Example 4 with IJavaModel

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

the class RenamePackageTest method test1.

//-------
@Test
public void test1() throws Exception {
    fIsPreDeltaTest = true;
    RenamePackageProcessor proc = helper2(new String[] { "r" }, new String[][] { { "A" } }, "p1");
    IJavaElementMapper jm = (IJavaElementMapper) proc.getAdapter(IJavaElementMapper.class);
    IResourceMapper rm = (IResourceMapper) proc.getAdapter(IResourceMapper.class);
    IJavaModel javaModel = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
    IJavaProject project = getRoot().getJavaProject();
    IFile _project = project.getProject().getFile(".project");
    checkMappingUnchanged(jm, rm, new Object[] { project, _project, getRoot(), getPackageP(), getRoot().getPackageFragment("inexistent"), getRoot().getPackageFragment("r.inexistent"), getRoot().getPackageFragment("p1.inexistent") });
    IPackageFragment r = getRoot().getPackageFragment("p1");
    ICompilationUnit r_A = r.getCompilationUnit("A.java");
    IType r_A_A = r_A.getType("A");
    IField r_A_A_a = r_A_A.getField("a");
    IPackageFragment p1 = getRoot().getPackageFragment("p1");
    ICompilationUnit p1_A = p1.getCompilationUnit("A.java");
    IType p1_A_A = p1_A.getType("A");
    IField p1_A_A_a = p1_A_A.getField("a");
    checkMappingChanged(jm, rm, new Object[][] { { r, p1 }, { r_A, p1_A }, { r_A_A, p1_A_A }, { r_A_A_a, p1_A_A_a } });
}
Also used : IResourceMapper(org.eclipse.ltk.core.refactoring.IResourceMapper) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) RenamePackageProcessor(org.eclipse.jdt.internal.corext.refactoring.rename.RenamePackageProcessor) IJavaProject(org.eclipse.jdt.core.IJavaProject) IFile(org.eclipse.core.resources.IFile) IJavaElementMapper(org.eclipse.jdt.core.refactoring.IJavaElementMapper) IField(org.eclipse.jdt.core.IField) IJavaModel(org.eclipse.jdt.core.IJavaModel) IType(org.eclipse.jdt.core.IType) Test(org.junit.Test)

Example 5 with IJavaModel

use of org.eclipse.jdt.core.IJavaModel in project xtext-eclipse by eclipse.

the class Storage2UriMapperJavaImpl method getProjectDeltas.

private Set<IJavaElementDelta> getProjectDeltas(IJavaElementDelta delta) {
    IJavaElement element = delta.getElement();
    if (delta.getElement().getElementType() == IJavaElement.JAVA_PROJECT) {
        return Collections.singleton(delta);
    }
    Set<IJavaElementDelta> result = null;
    if (element instanceof IJavaModel) {
        for (IJavaElementDelta child : delta.getAffectedChildren()) {
            Set<IJavaElementDelta> projectDeltas = getProjectDeltas(child);
            if (!projectDeltas.isEmpty()) {
                if (result == null)
                    result = newLinkedHashSet();
                result.addAll(projectDeltas);
            }
        }
    }
    return result == null ? Collections.<IJavaElementDelta>emptySet() : result;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IJavaElementDelta(org.eclipse.jdt.core.IJavaElementDelta) IJavaModel(org.eclipse.jdt.core.IJavaModel)

Aggregations

IJavaModel (org.eclipse.jdt.core.IJavaModel)12 IJavaProject (org.eclipse.jdt.core.IJavaProject)9 JavaModelException (org.eclipse.jdt.core.JavaModelException)5 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)4 IProject (org.eclipse.core.resources.IProject)3 IPath (org.eclipse.core.runtime.IPath)3 IJavaElement (org.eclipse.jdt.core.IJavaElement)3 JavaProject (org.eclipse.jdt.internal.core.JavaProject)3 ArrayList (java.util.ArrayList)2 IFolder (org.eclipse.core.resources.IFolder)2 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 IFile (org.eclipse.core.resources.IFile)1 Path (org.eclipse.core.runtime.Path)1 QualifiedName (org.eclipse.core.runtime.QualifiedName)1 IClasspathContainer (org.eclipse.jdt.core.IClasspathContainer)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 IField (org.eclipse.jdt.core.IField)1 IJavaElementDelta (org.eclipse.jdt.core.IJavaElementDelta)1