Search in sources :

Example 1 with HashtableOfArrayToObject

use of org.eclipse.jdt.internal.core.util.HashtableOfArrayToObject in project che by eclipse.

the class JavaModelManager method determineIfOnClasspath.

/**
     * 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.
     */
public static IJavaElement determineIfOnClasspath(IResource resource, IJavaProject project) {
    IPath resourcePath = resource.getFullPath();
    boolean isExternal = ExternalFoldersManager.isExternalFolderPath(resourcePath);
    if (isExternal)
        resourcePath = resource.getLocation();
    try {
        JavaProjectElementInfo projectInfo = (JavaProjectElementInfo) org.eclipse.jdt.internal.core.JavaModelManager.getJavaModelManager().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 = // JAVA file can only live inside SRC folder (on the raw path)
        isJavaLike ? // JAVA file can only live inside SRC folder (on the raw path)
        project.getRawClasspath() : ((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.getType() == IResource.FILE) {
                            // 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();
                        // (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;
}
Also used : IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) JavaModelException(org.eclipse.jdt.core.JavaModelException) IPath(org.eclipse.core.runtime.IPath) HashtableOfArrayToObject(org.eclipse.jdt.internal.core.util.HashtableOfArrayToObject) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry)

Aggregations

IPath (org.eclipse.core.runtime.IPath)1 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)1 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 HashtableOfArrayToObject (org.eclipse.jdt.internal.core.util.HashtableOfArrayToObject)1