Example usage for org.eclipse.jdt.core IJavaElement getResource

List of usage examples for org.eclipse.jdt.core IJavaElement getResource

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaElement getResource.

Prototype

IResource getResource();

Source Link

Document

Returns the innermost resource enclosing this element.

Usage

From source file:at.bestsolution.fxide.jdt.corext.javadoc.JavaDocLocations.java

License:Open Source License

/**
 * Returns the location of the Javadoc./*from ww w . j  a  v  a 2  s . c  om*/
 *
 * @param element whose Javadoc location has to be found
 * @param isBinary <code>true</code> if the Java element is from a binary container
 * @return the location URL of the Javadoc or <code>null</code> if the location cannot be found
 * @throws JavaModelException thrown when the Java element cannot be accessed
 * @since 3.9
 */
public static String getBaseURL(IJavaElement element, boolean isBinary) throws JavaModelException {
    if (isBinary) {
        // TODO not ported
        //         // Source attachment usually does not include Javadoc resources
        //         // => Always use the Javadoc location as base:
        //         URL baseURL= JavaUI.getJavadocLocation(element, false);
        //         if (baseURL != null) {
        //            if (baseURL.getProtocol().equals(JAR_PROTOCOL)) {
        //               // It's a JarURLConnection, which is not known to the browser widget.
        //               // Let's start the help web server:
        //               URL baseURL2= PlatformUI.getWorkbench().getHelpSystem().resolve(baseURL.toExternalForm(), true);
        //               if (baseURL2 != null) { // can be null if org.eclipse.help.ui is not available
        //                  baseURL= baseURL2;
        //               }
        //            }
        //            return baseURL.toExternalForm();
        //         }
    } else {
        IResource resource = element.getResource();
        if (resource != null) {
            /*
             * Too bad: Browser widget knows nothing about EFS and custom URL handlers,
             * so IResource#getLocationURI() does not work in all cases.
             * We only support the local file system for now.
             * A solution could be https://bugs.eclipse.org/bugs/show_bug.cgi?id=149022 .
             */
            IPath location = resource.getLocation();
            if (location != null)
                return location.toFile().toURI().toString();
        }
    }
    return null;
}

From source file:ca.mcgill.sable.soot.attributes.JavaAttributesComputer.java

License:Open Source License

/**
 * initialize rec and proj/*w w  w.j  a v a 2 s. c  o m*/
 */
protected void init(AbstractTextEditor editor) {
    IJavaElement jElem = getJavaElement(editor);
    setProj(jElem.getResource().getProject());
    setRec(jElem.getResource());
}

From source file:ca.mcgill.sable.soot.attributes.VisManLauncher.java

License:Open Source License

public void selectionChanged(IAction action, ISelection selection) {
    if (selection instanceof IStructuredSelection) {
        IStructuredSelection struct = (IStructuredSelection) selection;
        Iterator it = struct.iterator();
        while (it.hasNext()) {
            Object next = it.next();
            if (next instanceof IResource) {
                setProj(((IResource) next).getProject());
                setRec((IResource) next);
            } else if (next instanceof IJavaElement) {
                IJavaElement jElem = (IJavaElement) next;
                setProj(jElem.getJavaProject().getProject());
                setRec(jElem.getResource());
            }//from   ww  w  .j  a v a  2s  . c o  m
        }
    }
}

From source file:com.aliyun.odps.eclipse.launch.shortcut.udf.UDFLaunchShortcuts1.java

License:Apache License

/**
 * Returns the resource containing the Java element associated with the given adaptable, or
 * <code>null</code>.//from www.  jav  a 2s  .c o  m
 * 
 * @param adaptable adaptable object
 * @return containing resource or <code>null</code>
 */
private IResource getLaunchableResource(IAdaptable adaptable) {
    IJavaElement je = (IJavaElement) adaptable.getAdapter(IJavaElement.class);
    if (je != null) {
        return je.getResource();
    }
    return null;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MatchLocator.java

License:Open Source License

/**
 * Locates the package declarations corresponding to the search pattern.
 */// w  ww.ja  va  2  s. c  o m
protected void locatePackageDeclarations(SearchPattern searchPattern, SearchParticipant participant,
        IJavaProject[] projects) throws CoreException {
    if (this.progressMonitor != null && this.progressMonitor.isCanceled()) {
        throw new OperationCanceledException();
    }
    if (searchPattern instanceof OrPattern) {
        SearchPattern[] patterns = ((OrPattern) searchPattern).patterns;
        for (int i = 0, length = patterns.length; i < length; i++) {
            locatePackageDeclarations(patterns[i], participant, projects);
        }
    } else if (searchPattern instanceof PackageDeclarationPattern) {
        IJavaElement focus = searchPattern.focus;
        if (focus != null) {
            if (encloses(focus)) {
                SearchMatch match = new PackageDeclarationMatch(
                        focus.getAncestor(IJavaElement.PACKAGE_FRAGMENT), SearchMatch.A_ACCURATE, -1, -1,
                        participant, focus.getResource());
                report(match);
            }
            return;
        }
        PackageDeclarationPattern pkgPattern = (PackageDeclarationPattern) searchPattern;
        boolean isWorkspaceScope = this.scope == JavaModelManager.getJavaModelManager().getWorkspaceScope();
        IPath[] scopeProjectsAndJars = isWorkspaceScope ? null : this.scope.enclosingProjectsAndJars();
        int scopeLength = isWorkspaceScope ? 0 : scopeProjectsAndJars.length;
        SimpleSet packages = new SimpleSet();
        for (int i = 0, length = projects.length; i < length; i++) {
            IJavaProject javaProject = projects[i];
            if (this.progressMonitor != null) {
                if (this.progressMonitor.isCanceled())
                    throw new OperationCanceledException();
                this.progressWorked++;
                if ((this.progressWorked % this.progressStep) == 0)
                    this.progressMonitor.worked(this.progressStep);
            }
            // Verify that project belongs to the scope
            if (!isWorkspaceScope) {
                boolean found = false;
                for (int j = 0; j < scopeLength; j++) {
                    if (javaProject.getPath().equals(scopeProjectsAndJars[j])) {
                        found = true;
                        break;
                    }
                }
                if (!found)
                    continue;
            }
            // Get all project package fragment names
            this.nameLookup = ((JavaProject) projects[i]).newNameLookup(this.workingCopies);
            IPackageFragment[] packageFragments = this.nameLookup
                    .findPackageFragments(new String(pkgPattern.pkgName), false, true);
            int pLength = packageFragments == null ? 0 : packageFragments.length;
            // Report matches avoiding duplicate names
            for (int p = 0; p < pLength; p++) {
                IPackageFragment fragment = packageFragments[p];
                if (packages.addIfNotIncluded(fragment) == null)
                    continue;
                if (encloses(fragment)) {
                    IResource resource = fragment.getResource();
                    if (resource == null) // case of a file in an external jar
                        resource = javaProject.getProject();
                    try {
                        if (encloses(fragment)) {
                            SearchMatch match = new PackageDeclarationMatch(fragment, SearchMatch.A_ACCURATE,
                                    -1, -1, participant, resource);
                            report(match);
                        }
                    } catch (JavaModelException e) {
                        throw e;
                    } catch (CoreException e) {
                        throw new JavaModelException(e);
                    }
                }
            }
        }
    }
}

From source file:com.ebmwebsourcing.petals.common.internal.provisional.projectscnf.PetalsProjectContentProvider.java

License:Open Source License

@Override
public Object getParent(Object element) {

    if (element instanceof PetalsProjectCategory)
        return null;

    // What do we have?
    IResource res = null;//from w  w  w .  j av  a 2 s . com
    IJavaElement javaElement = null;
    boolean isInJavaProject = false;

    if (element instanceof IResource) {
        res = (IResource) element;
        try {
            if (((IResource) element).exists())
                isInJavaProject = res.getProject().hasNature(JavaCore.NATURE_ID);

            if (isInJavaProject)
                javaElement = JavaCore.create(res);

        } catch (CoreException e) {
            PetalsCommonPlugin.log(e, IStatus.ERROR);
        }

    } else if (element instanceof IJavaElement) {
        isInJavaProject = true;
        javaElement = (IJavaElement) element;
        res = javaElement.getResource();
    }

    // Projects should return a category
    // Otherwise, selection won't work when we want to reveal a new project
    if (res instanceof IProject) {
        List<PetalsProjectCategory> cats = PetalsProjectManager.INSTANCE.getCategories((IProject) res);
        return cats != null && cats.size() > 0 ? cats.get(0) : null;
    }

    // If the parent is a project and this root project is not displayed, then return the Petals category
    if (res != null && res.getParent() instanceof IProject) {
        Object parent = getParent(res.getParent());
        if (parent instanceof PetalsProjectCategory && !((PetalsProjectCategory) parent).isRootProjectVisible())
            return parent;
    }

    // Java elements
    if (javaElement != null) {
        // PETALSSTUD-165: Selection of Java resources fails for JSR-181
        if (element instanceof IPackageFragment) {
            PetalsCnfPackageFragment ppf = PetalsProjectManager.INSTANCE.dirtyViewerMap.get(javaElement);
            if (ppf != null) {
                if (ppf.getParent() instanceof PetalsCnfPackageFragment)
                    return ((PetalsCnfPackageFragment) ppf.getParent()).getFragment();

                return ppf.getParent();
            }
        }

        IJavaElement elt = javaElement.getParent();
        if (elt instanceof IJavaProject)
            return ((IJavaProject) elt).getProject();

        return elt;
        // PETALSSTUD-165: Selection of Java resources fails for JSR-181
    }

    // Otherwise, return the parent
    if (res != null) {
        // Be careful, the parent of a resource may sometimes be a Java element (e.g. a file in a package)
        res = res.getParent();
        javaElement = JavaCore.create(res);
        if (javaElement instanceof IJavaProject)
            return res.getProject();

        if (javaElement instanceof IPackageFragment || javaElement instanceof IPackageFragmentRoot)
            return javaElement;

        return res;
    }

    return null;
}

From source file:com.feup.contribution.druid.data.DruidFeature.java

License:Open Source License

public void removeClass(IJavaElement javaElement) {
    ArrayList<DruidMethod> toRemove = new ArrayList<DruidMethod>();
    for (DruidMethod method : methods)
        if (method.getMethod().getResource().equals(javaElement.getResource()))
            toRemove.add(method);/*from w  w w  .  j  av a  2s.  com*/
    for (DruidMethod method : toRemove)
        methods.remove(method);
}

From source file:com.google.gwt.eclipse.core.wizards.NewEntryPointWizardPage.java

License:Open Source License

public void init(IStructuredSelection selection) {
    IJavaElement initialJavaElement = getInitialJavaElement(selection);
    initContainerPage(initialJavaElement);
    initTypePage(initialJavaElement);//from w w  w .  ja  va  2s  . c om

    // Set the initial resource, based either on the selection or on the
    // initial Java element returned by NewContainerWizardPage
    IResource initialResource = AbstractNewFileWizard.getSelectedResource(selection);
    if (initialResource.getType() == IResource.ROOT && initialJavaElement != null) {
        initialResource = initialJavaElement.getResource();
    }

    initEntryPointPage(initialResource);
    doStatusUpdate();

    setMethodStubSelection(false, true);
}

From source file:com.javapathfinder.vjp.verify.topics.link.LinkParser.java

License:Open Source License

private IFile findFile(String pathString) {
    IPath path = new Path(pathString);
    IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
    try {/*from   w  w  w  .j  a  v  a 2 s. com*/
        for (IProject project : projects) {
            if (project.exists() && project.isOpen() && project.hasNature(JavaCore.NATURE_ID)) {
                IJavaProject jproject = (IJavaProject) JavaCore.create(project);
                IJavaElement element = jproject.findElement(path);
                if (element != null) {
                    if (element.getResource() instanceof IFile) {
                        IFile file = (IFile) element.getResource();
                        return file;
                    }
                }
            }
        }
    } catch (CoreException e) {
        VJP.logError(e.getMessage(), e);
    }
    return null;
}

From source file:com.jstar.eclipse.services.Utils.java

License:BSD License

private void makeImportReady(final JavaFile selectedFile, final String importLine) throws IOException {
    final IPath sourcePath = new Path(StringUtils.replace(importLine, ".", File.separator));

    try {//from  w w w  .  ja  v  a2 s . c o  m
        final IJavaElement element = selectedFile.getJavaProject().getProject()
                .findElement(sourcePath.addFileExtension("java"));

        if (element == null) {
            throw new NullPointerException("Could not import class: " + importLine
                    + ". Please check if it is written in the correct way, e.g. java.lang.Object");
        }

        final IResource resource = element.getResource();

        if (resource == null) {
            checkFiles(selectedFile, sourcePath, importLine);
            return;
        }

        if (resource != null && resource instanceof IFile) {
            final boolean specInSource = JavaFilePersistentProperties.isSpecInSourceFile(resource);

            if (specInSource) {
                final IFile file = (IFile) resource;
                checkGeneratedFiles(file, selectedFile, sourcePath, importLine);
                makeImportsReady(new JavaFile(file));
            } else {
                checkFiles(selectedFile, sourcePath, importLine);
            }

            return;
        }

        throw new RuntimeException("Could not import class: " + importLine
                + ". Please check if it is written in the correct way, e.g. java.lang.Object");

    } catch (JavaModelException jme) {
        jme.printStackTrace(ConsoleService.getInstance().getConsoleStream());
    }

}