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

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

Introduction

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

Prototype

@Override
public IPackageFragmentRoot getPackageFragmentRoot(String externalLibraryPath) 

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.j a v  a2 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.che.jdt.internal.core.JavaModelManager.java

License:Open Source License

/**
 * Returns the package fragment root represented by the resource, or
 * the package fragment the given resource is located in, or <code>null</code>
 * if the given resource is not on the classpath of the given project.
 *//*from ww  w  .  j av a2s .  c om*/
public static IJavaElement determineIfOnClasspath(File resource, JavaProject project) {
    IPath resourcePath = new Path(resource.getAbsolutePath());
    boolean isExternal = false; //ExternalFoldersManager.isInternalPathForExternalFolder(resourcePath);
    //        if (isExternal)
    //            resourcePath = resource.getLocation();

    try {
        JavaProjectElementInfo projectInfo = (JavaProjectElementInfo) ((JavaProject) project).manager
                .getInfo(project);
        JavaProjectElementInfo.ProjectCache projectCache = projectInfo == null ? null
                : projectInfo.projectCache;
        HashtableOfArrayToObject allPkgFragmentsCache = projectCache == null ? null
                : projectCache.allPkgFragmentsCache;
        boolean isJavaLike = Util.isJavaLikeFileName(resourcePath.lastSegment());
        IClasspathEntry[] entries = isJavaLike ? project.getRawClasspath() // JAVA file can only live inside SRC folder (on the raw path)
                : ((JavaProject) project).getResolvedClasspath();

        int length = entries.length;
        if (length > 0) {
            String sourceLevel = project.getOption(JavaCore.COMPILER_SOURCE, true);
            String complianceLevel = project.getOption(JavaCore.COMPILER_COMPLIANCE, true);
            for (int i = 0; i < length; i++) {
                IClasspathEntry entry = entries[i];
                if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT)
                    continue;
                IPath rootPath = entry.getPath();
                if (rootPath.equals(resourcePath)) {
                    if (isJavaLike)
                        return null;
                    return project.getPackageFragmentRoot(resource);
                } else if (rootPath.isPrefixOf(resourcePath)) {
                    // allow creation of package fragment if it contains a .java file that is included
                    if (!Util.isExcluded(resourcePath, ((ClasspathEntry) entry).fullInclusionPatternChars(),
                            ((ClasspathEntry) entry).fullExclusionPatternChars(), true)) {
                        // given we have a resource child of the root, it cannot be a JAR pkg root
                        PackageFragmentRoot root =
                                //                                    isExternal ?
                                //                                    new ExternalPackageFragmentRoot(rootPath, (JavaProject) project) :
                                (PackageFragmentRoot) ((JavaProject) project)
                                        .getFolderPackageFragmentRoot(rootPath);
                        if (root == null)
                            return null;
                        IPath pkgPath = resourcePath.removeFirstSegments(rootPath.segmentCount());

                        if (resource.isFile()) {
                            // if the resource is a file, then remove the last segment which
                            // is the file name in the package
                            pkgPath = pkgPath.removeLastSegments(1);
                        }
                        String[] pkgName = pkgPath.segments();

                        // if package name is in the cache, then it has already been validated
                        // (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=133141)
                        if (allPkgFragmentsCache != null && allPkgFragmentsCache.containsKey(pkgName))
                            return root.getPackageFragment(pkgName);

                        if (pkgName.length != 0 && JavaConventions
                                .validatePackageName(Util.packageName(pkgPath, sourceLevel, complianceLevel),
                                        sourceLevel, complianceLevel)
                                .getSeverity() == IStatus.ERROR) {
                            return null;
                        }
                        return root.getPackageFragment(pkgName);
                    }
                }
            }
        }
    } catch (JavaModelException npe) {
        return null;
    }
    return null;
}

From source file:org.eclipse.che.jdt.internal.core.JavaModelManager.java

License:Open Source License

/**
 * Creates and returns a handle for the given JAR file, its project being the given project.
 * The Java model associated with the JAR's project may be
 * created as a side effect./*from   w w  w.j  a v  a  2s  .  com*/
 * Returns <code>null</code> if unable to create a JAR package fragment root.
 * (for example, if the JAR file represents a non-Java resource)
 */
public static IPackageFragmentRoot createJarPackageFragmentRootFrom(File file, JavaProject project) {
    if (file == null) {
        return null;
    }
    //        if (project == null) {
    //            project = JavaCore.create(file.getProject());
    //        }

    // Create a jar package fragment root only if on the classpath
    IPath resourcePath = new Path(file.getPath());
    try {
        IClasspathEntry entry = project.getClasspathEntryFor(resourcePath);
        if (entry != null) {
            return project.getPackageFragmentRoot(file);
        }
    } catch (JavaModelException e) {
        // project doesn't exist: return null
    }
    return null;
}