Example usage for org.eclipse.jdt.internal.core.util Messages path_mustBeAbsolute

List of usage examples for org.eclipse.jdt.internal.core.util Messages path_mustBeAbsolute

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.core.util Messages path_mustBeAbsolute.

Prototype

String path_mustBeAbsolute

To view the source code for org.eclipse.jdt.internal.core.util Messages path_mustBeAbsolute.

Click Source Link

Usage

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://w w w .jav a 2  s .  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.jdt.internal.core.JavaProject.java

License:Open Source License

public IPackageFragmentRoot findPackageFragmentRoot0(IPath path) throws JavaModelException {

    IPackageFragmentRoot[] allRoots = this.getAllPackageFragmentRoots();
    if (!path.isAbsolute()) {
        throw new IllegalArgumentException(Messages.path_mustBeAbsolute);
    }/*  w  w w.  j  a  va  2 s  . c o  m*/
    for (int i = 0; i < allRoots.length; i++) {
        IPackageFragmentRoot classpathRoot = allRoots[i];
        if (classpathRoot.getPath().equals(path)) {
            return classpathRoot;
        }
    }
    return null;
}