Example usage for org.eclipse.jdt.internal.core JavaProject getResource

List of usage examples for org.eclipse.jdt.internal.core JavaProject getResource

Introduction

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

Prototype

IResource getResource();

Source Link

Document

Returns the innermost resource enclosing this element.

Usage

From source file:com.codenvy.ide.ext.java.server.internal.core.JavaProjectElementInfo.java

License:Open Source License

/**
 * Compute the non-java resources contained in this java project.
 *//*w  w  w  . j  ava 2 s.  co  m*/
private Object[] computeNonJavaResources(JavaProject project) {

    // determine if src == project and/or if bin == project
    IPath projectPath = project.getProject().getFullPath();
    boolean srcIsProject = false;
    boolean binIsProject = false;
    char[][] inclusionPatterns = null;
    char[][] exclusionPatterns = null;
    IPath projectOutput = null;
    boolean isClasspathResolved = true;
    try {
        IClasspathEntry entry = project.getClasspathEntryFor(projectPath);
        if (entry != null) {
            srcIsProject = true;
            inclusionPatterns = ((ClasspathEntry) entry).fullInclusionPatternChars();
            exclusionPatterns = ((ClasspathEntry) entry).fullExclusionPatternChars();
        }
        projectOutput = project.getOutputLocation();
        binIsProject = projectPath.equals(projectOutput);
    } catch (JavaModelException e) {
        isClasspathResolved = false;
    }

    Object[] resources = new IResource[5];
    int resourcesCounter = 0;
    try {
        IResource[] members = ((IContainer) project.getResource()).members();
        int length = members.length;
        if (length > 0) {
            String sourceLevel = project.getOption(JavaCore.COMPILER_SOURCE, true);
            String complianceLevel = project.getOption(JavaCore.COMPILER_COMPLIANCE, true);
            IClasspathEntry[] classpath = project.getResolvedClasspath();
            for (int i = 0; i < length; i++) {
                IResource res = members[i];
                switch (res.getType()) {
                case IResource.FILE:
                    IPath resFullPath = res.getFullPath();
                    String resName = res.getName();

                    // ignore a jar file on the classpath
                    if (isClasspathResolved && isClasspathEntryOrOutputLocation(resFullPath,
                            res.getLocation()/* see https://bugs.eclipse
                                             .org/bugs/show_bug.cgi?id=244406 */, classpath, projectOutput)) {
                        break;
                    }
                    // ignore .java file if src == project
                    if (srcIsProject && Util.isValidCompilationUnitName(resName, sourceLevel, complianceLevel)
                            && !Util.isExcluded(res, inclusionPatterns, exclusionPatterns)) {
                        break;
                    }
                    // ignore .class file if bin == project
                    if (binIsProject && Util.isValidClassFileName(resName, sourceLevel, complianceLevel)) {
                        break;
                    }
                    // else add non java resource
                    if (resources.length == resourcesCounter) {
                        // resize
                        System.arraycopy(resources, 0, (resources = new IResource[resourcesCounter * 2]), 0,
                                resourcesCounter);
                    }
                    resources[resourcesCounter++] = res;
                    break;
                case IResource.FOLDER:
                    resFullPath = res.getFullPath();

                    // ignore non-excluded folders on the classpath or that correspond to an output location
                    if ((srcIsProject && !Util.isExcluded(res, inclusionPatterns, exclusionPatterns)
                            && Util.isValidFolderNameForPackage(res.getName(), sourceLevel, complianceLevel))
                            || (isClasspathResolved && isClasspathEntryOrOutputLocation(resFullPath,
                                    res.getLocation(), classpath, projectOutput))) {
                        break;
                    }
                    // else add non java resource
                    if (resources.length == resourcesCounter) {
                        // resize
                        System.arraycopy(resources, 0, (resources = new IResource[resourcesCounter * 2]), 0,
                                resourcesCounter);
                    }
                    resources[resourcesCounter++] = res;
                }
            }
        }
        if (resources.length != resourcesCounter) {
            System.arraycopy(resources, 0, (resources = new IResource[resourcesCounter]), 0, resourcesCounter);
        }
    } catch (CoreException e) {
        resources = NO_NON_JAVA_RESOURCES;
        resourcesCounter = 0;
    }
    return resources;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.NameLookup.java

License:Open Source License

/**
 * Returns the package fragment whose path matches the given
 * (absolute) path, or <code>null</code> if none exist. The domain of
 * the search is bounded by the classpath of the <code>IJavaProject</code>
 * this <code>NameLookup</code> was obtained from.
 * The path can be://from   www  . j  av  a 2s  .c  o m
 * - internal to the workbench: "/Project/src"
 * - external to the workbench: "c:/jdk/classes.zip/java/lang"
 */
public IPackageFragment findPackageFragment(IPath path) {
    if (!path.isAbsolute()) {
        throw new IllegalArgumentException(Messages.path_mustBeAbsolute);
    }
    /*
     * TODO (jerome) this code should rather use the package fragment map to find the candidate package, then
     * check if the respective enclosing root maps to the one on this given IPath.
     */
    IResource possibleFragment = ResourcesPlugin.getWorkspace().getRoot().findMember(path);
    if (possibleFragment == null) {
        //external jar
        for (int i = 0; i < this.packageFragmentRoots.length; i++) {
            IPackageFragmentRoot root = this.packageFragmentRoots[i];
            if (!root.isExternal()) {
                continue;
            }
            IPath rootPath = root.getPath();
            if (rootPath.isPrefixOf(path)) {
                String name = path.toOSString();
                // + 1 is for the File.separatorChar
                name = name.substring(rootPath.toOSString().length() + 1, name.length());
                name = name.replace(File.separatorChar, '.');
                IJavaElement[] list = null;
                try {
                    list = root.getChildren();
                } catch (JavaModelException npe) {
                    continue; // the package fragment root is not present;
                }
                int elementCount = list.length;
                for (int j = 0; j < elementCount; j++) {
                    IPackageFragment packageFragment = (IPackageFragment) list[j];
                    if (nameMatches(name, packageFragment, false)) {
                        return packageFragment;
                    }
                }
            }
        }
    } else {
        IJavaElement fromFactory = JavaCore.create(possibleFragment);
        if (fromFactory == null) {
            return null;
        }
        switch (fromFactory.getElementType()) {
        case IJavaElement.PACKAGE_FRAGMENT:
            return (IPackageFragment) fromFactory;
        case IJavaElement.JAVA_PROJECT:
            // default package in a default root
            JavaProject project = (JavaProject) fromFactory;
            try {
                IClasspathEntry entry = project.getClasspathEntryFor(path);
                if (entry != null) {
                    IPackageFragmentRoot root = project.getPackageFragmentRoot(project.getResource());
                    Object defaultPkgRoot = this.packageFragments.get(CharOperation.NO_STRINGS);
                    if (defaultPkgRoot == null) {
                        return null;
                    }
                    if (defaultPkgRoot instanceof PackageFragmentRoot && defaultPkgRoot.equals(root))
                        return ((PackageFragmentRoot) root).getPackageFragment(CharOperation.NO_STRINGS);
                    else {
                        IPackageFragmentRoot[] roots = (IPackageFragmentRoot[]) defaultPkgRoot;
                        for (int i = 0; i < roots.length; i++) {
                            if (roots[i].equals(root)) {
                                return ((PackageFragmentRoot) root)
                                        .getPackageFragment(CharOperation.NO_STRINGS);
                            }
                        }
                    }
                }
            } catch (JavaModelException e) {
                return null;
            }
            return null;
        case IJavaElement.PACKAGE_FRAGMENT_ROOT:
            return ((PackageFragmentRoot) fromFactory).getPackageFragment(CharOperation.NO_STRINGS);
        }
    }
    return null;
}

From source file:org.eclipse.m2e.pr.internal.wizard.ProblemReportingWizard.java

License:Open Source License

List<File> saveData(File bundleDir, Set<Data> dataSet, IProgressMonitor monitor) throws IOException {
    //refresh the projects in case they're out of date
    Set<IProject> projects = new LinkedHashSet<IProject>();
    List<?> list = descriptionPage.getSelectedProjects().toList();
    int numTicks = list == null ? 0 : list.size();
    SubProgressMonitor sub = new SubProgressMonitor(monitor, numTicks);
    sub.beginTask(Messages.ProblemReportingWizard_monitor_reading, numTicks);
    for (Iterator<?> i = descriptionPage.getSelectedProjects().iterator(); i.hasNext();) {
        try {//ww w  . j a v a 2  s .  c  o  m
            Object o = i.next();
            if (o instanceof JavaProject) {

                JavaProject jp = (JavaProject) o;
                projects.add(jp.getProject());
                if (jp.getResource() != null) {
                    jp.getResource().refreshLocal(IResource.DEPTH_INFINITE, sub);
                }

            } else if (o instanceof IProject) {
                IProject project = (IProject) o;
                projects.add(project);
                if (project.getWorkspace() != null && project.getWorkspace().getRoot() != null) {
                    project.getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, sub);
                }

            }
        } catch (CoreException e) {
            MavenLogger.log(e);
        }
        sub.worked(1);
    }
    sub.done();

    MavenPlugin mavenPlugin = MavenPlugin.getDefault();
    DataGatherer gatherer = new DataGatherer(MavenPlugin.getDefault().getMavenConfiguration(), //
            mavenPlugin.getMavenProjectManager(), mavenPlugin.getConsole(), //
            ResourcesPlugin.getWorkspace(), projects, getClass().getResource("/apr/public-key.txt")); //$NON-NLS-1$

    return gatherer.gather(bundleDir, dataSet, monitor);
}