Search in sources :

Example 16 with IJavaElement

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

the class JavaSearchScope method packageFragmentRoot.

/**
     * @see AbstractJavaSearchScope#packageFragmentRoot(String, int, String)
     */
public IPackageFragmentRoot packageFragmentRoot(String resourcePathString, int jarSeparatorIndex, String jarPath) {
    int index = -1;
    boolean isJarFile = jarSeparatorIndex != -1;
    if (isJarFile) {
        // internal or external jar (case 3, 4, or 5)
        String relativePath = resourcePathString.substring(jarSeparatorIndex + 1);
        index = indexOf(jarPath, relativePath);
    } else {
        // resource in workspace (case 1 or 2)
        index = indexOf(resourcePathString);
    }
    if (index >= 0) {
        int idx = this.projectIndexes[index];
        String projectPath = idx == -1 ? null : (String) this.projectPaths.get(idx);
        if (projectPath != null) {
            IJavaProject project = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot().getProject(projectPath));
            if (isJarFile) {
                IResource resource = JavaModel.getWorkspaceTarget(new Path(jarPath));
                if (resource != null)
                    return project.getPackageFragmentRoot(resource);
                return project.getPackageFragmentRoot(jarPath);
            }
            Object target = JavaModel.getWorkspaceTarget(new Path(this.containerPaths[index] + '/' + this.relativePaths[index]));
            if (target != null) {
                if (target instanceof IProject) {
                    return project.getPackageFragmentRoot((IProject) target);
                }
                IJavaElement element = JavaModelManager.create((IResource) target, project);
                return (IPackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
            }
        }
    }
    return null;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IJavaElement(org.eclipse.jdt.core.IJavaElement) IJavaProject(org.eclipse.jdt.core.IJavaProject) IResource(org.eclipse.core.resources.IResource) IProject(org.eclipse.core.resources.IProject) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 17 with IJavaElement

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

the class JavaSearchScope method encloses.

/* (non-Javadoc)
     * @see IJavaSearchScope#encloses(IJavaElement)
     */
public boolean encloses(IJavaElement element) {
    if (this.elements != null) {
        for (int i = 0, length = this.elements.size(); i < length; i++) {
            IJavaElement scopeElement = (IJavaElement) this.elements.get(i);
            IJavaElement searchedElement = element;
            while (searchedElement != null) {
                if (searchedElement.equals(scopeElement))
                    return true;
                searchedElement = searchedElement.getParent();
            }
        }
        return false;
    }
    IPackageFragmentRoot root = (IPackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
    if (root != null && root.isArchive()) {
        // external or internal jar
        IPath rootPath = root.getPath();
        String rootPathToString = rootPath.getDevice() == null ? rootPath.toString() : rootPath.toOSString();
        IPath relativePath = getPath(element, true);
        return indexOf(rootPathToString, relativePath.toString()) >= 0;
    }
    // resource in workspace
    String fullResourcePathString = getPath(element, false).toString();
    return indexOf(fullResourcePathString) >= 0;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IPath(org.eclipse.core.runtime.IPath) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 18 with IJavaElement

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

the class JavaSearchScope method add.

/**
     * Add an element to the java search scope.
     * @param element The element we want to add to current java search scope
     * @throws JavaModelException May happen if some Java Model info are not available
     */
public void add(IJavaElement element) throws JavaModelException {
    IPath containerPath = null;
    String containerPathToString = null;
    PackageFragmentRoot root = null;
    int includeMask = SOURCES | APPLICATION_LIBRARIES | SYSTEM_LIBRARIES;
    switch(element.getElementType()) {
        case IJavaElement.JAVA_MODEL:
            // a workspace sope should be used
            break;
        case IJavaElement.JAVA_PROJECT:
            add((JavaProject) element, null, includeMask, new HashSet(2), new HashSet(2), null);
            break;
        case IJavaElement.PACKAGE_FRAGMENT_ROOT:
            root = (PackageFragmentRoot) element;
            IPath rootPath = root.internalPath();
            containerPath = root.getKind() == IPackageFragmentRoot.K_SOURCE ? root.getParent().getPath() : rootPath;
            containerPathToString = containerPath.getDevice() == null ? containerPath.toString() : containerPath.toOSString();
            IResource rootResource = root.resource();
            String projectPath = root.getJavaProject().getPath().toString();
            if (rootResource != null && rootResource.isAccessible()) {
                String relativePath = Util.relativePath(rootResource.getFullPath(), containerPath.segmentCount());
                add(projectPath, relativePath, containerPathToString, false, /*not a package*/
                null);
            } else {
                //$NON-NLS-1$
                add(projectPath, "", containerPathToString, false, /*not a package*/
                null);
            }
            break;
        case IJavaElement.PACKAGE_FRAGMENT:
            root = (PackageFragmentRoot) element.getParent();
            projectPath = root.getJavaProject().getPath().toString();
            if (root.isArchive()) {
                String relativePath = Util.concatWith(((PackageFragment) element).names, '/');
                containerPath = root.getPath();
                containerPathToString = containerPath.getDevice() == null ? containerPath.toString() : containerPath.toOSString();
                add(projectPath, relativePath, containerPathToString, true, /*package*/
                null);
            } else {
                IResource resource = ((JavaElement) element).resource();
                if (resource != null) {
                    if (resource.isAccessible()) {
                        containerPath = root.getKind() == IPackageFragmentRoot.K_SOURCE ? root.getParent().getPath() : root.internalPath();
                    } else {
                        // for working copies, get resource container full path
                        containerPath = resource.getParent().getFullPath();
                    }
                    containerPathToString = containerPath.getDevice() == null ? containerPath.toString() : containerPath.toOSString();
                    String relativePath = Util.relativePath(resource.getFullPath(), containerPath.segmentCount());
                    add(projectPath, relativePath, containerPathToString, true, /*package*/
                    null);
                }
            }
            break;
        default:
            // remember sub-cu (or sub-class file) java elements
            if (element instanceof IMember) {
                if (this.elements == null) {
                    this.elements = new ArrayList();
                }
                this.elements.add(element);
            }
            root = (PackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
            projectPath = root.getJavaProject().getPath().toString();
            String relativePath;
            if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
                containerPath = root.getParent().getPath();
                relativePath = Util.relativePath(getPath(element, false), root.getJavaProject().getPath().segmentCount());
            } else {
                containerPath = root.internalPath();
                relativePath = getPath(element, true).toString();
            }
            containerPathToString = containerPath.getDevice() == null ? containerPath.toString() : containerPath.toOSString();
            add(projectPath, relativePath, containerPathToString, false, /*not a package*/
            null);
    }
    if (root != null)
        addEnclosingProjectOrJar(root.getKind() == IPackageFragmentRoot.K_SOURCE ? root.getParent().getPath() : root.getPath());
}
Also used : PackageFragmentRoot(org.eclipse.jdt.internal.core.PackageFragmentRoot) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) JavaElement(org.eclipse.jdt.internal.core.JavaElement) IJavaElement(org.eclipse.jdt.core.IJavaElement) IPath(org.eclipse.core.runtime.IPath) ArrayList(java.util.ArrayList) IMember(org.eclipse.jdt.core.IMember) IResource(org.eclipse.core.resources.IResource) HashSet(java.util.HashSet)

Example 19 with IJavaElement

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

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

the class JavaModelOperation method run.

/**
     * Runs this operation and registers any deltas created.
     *
     * @throws CoreException
     *         if the operation fails
     * @see IWorkspaceRunnable
     */
public void run(IProgressMonitor monitor) throws CoreException {
    JavaModelManager manager = JavaModelManager.getJavaModelManager();
    DeltaProcessor deltaProcessor = manager.getDeltaProcessor();
    int previousDeltaCount = deltaProcessor.javaModelDeltas.size();
    try {
        this.progressMonitor = monitor;
        pushOperation(this);
        try {
            if (canModifyRoots()) {
                // computes the root infos before executing the operation
                // noop if aready initialized
                manager.deltaState.initializeRoots(false);
            }
            executeOperation();
        } finally {
            if (isTopLevelOperation()) {
                runPostActions();
            }
        }
    } finally {
        try {
            // reacquire delta processor as it can have been reset during executeOperation()
            deltaProcessor = manager.getDeltaProcessor();
            // update JavaModel using deltas that were recorded during this operation
            for (int i = previousDeltaCount, size = deltaProcessor.javaModelDeltas.size(); i < size; i++) {
                deltaProcessor.updateJavaModel((IJavaElementDelta) deltaProcessor.javaModelDeltas.get(i));
            }
            // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=83646
            for (int i = 0, length = this.resultElements.length; i < length; i++) {
                IJavaElement element = this.resultElements[i];
                Openable openable = (Openable) element.getOpenable();
                if (!(openable instanceof CompilationUnit) || !((CompilationUnit) openable).isWorkingCopy()) {
                    // a working copy must remain a child of its parent even after a move
                    ((JavaElement) openable.getParent()).close();
                }
                switch(element.getElementType()) {
                    case IJavaElement.PACKAGE_FRAGMENT_ROOT:
                    case IJavaElement.PACKAGE_FRAGMENT:
                        deltaProcessor.projectCachesToReset.add(element.getJavaProject());
                        break;
                }
            }
            deltaProcessor.resetProjectCaches();
            // - but the operation has not modified any resource
            if (isTopLevelOperation()) {
                if ((deltaProcessor.javaModelDeltas.size() > previousDeltaCount || !deltaProcessor.reconcileDeltas.isEmpty()) && !hasModifiedResource()) {
                    deltaProcessor.fire(null, DeltaProcessor.DEFAULT_CHANGE_EVENT);
                }
            // else deltas are fired while processing the resource delta
            }
        } finally {
            popOperation();
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IJavaElement(org.eclipse.jdt.core.IJavaElement) IJavaElement(org.eclipse.jdt.core.IJavaElement)

Aggregations

IJavaElement (org.eclipse.jdt.core.IJavaElement)208 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)46 IType (org.eclipse.jdt.core.IType)41 ArrayList (java.util.ArrayList)34 IJavaProject (org.eclipse.jdt.core.IJavaProject)32 JavaModelException (org.eclipse.jdt.core.JavaModelException)31 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)30 IResource (org.eclipse.core.resources.IResource)29 IMethod (org.eclipse.jdt.core.IMethod)28 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)28 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)21 IField (org.eclipse.jdt.core.IField)14 IMember (org.eclipse.jdt.core.IMember)14 CoreException (org.eclipse.core.runtime.CoreException)13 StringTokenizer (java.util.StringTokenizer)11 ISourceRange (org.eclipse.jdt.core.ISourceRange)11 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)11 IPath (org.eclipse.core.runtime.IPath)10 HashMap (java.util.HashMap)9 HashSet (java.util.HashSet)8