Example usage for org.eclipse.jdt.internal.core.util HashtableOfArrayToObject containsKey

List of usage examples for org.eclipse.jdt.internal.core.util HashtableOfArrayToObject containsKey

Introduction

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

Prototype

public boolean containsKey(Object[] key) 

Source Link

Usage

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   w  w  w .j a  v  a 2s. c o m*/
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.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  www .  ja v  a2  s . c  o m
public static IJavaElement determineIfOnClasspath(IResource resource, IJavaProject project) {
    IPath resourcePath = resource.getFullPath();
    boolean isExternal = ExternalFoldersManager.isInternalPathForExternalFolder(resourcePath);
    if (isExternal)
        resourcePath = resource.getLocation();

    try {
        JavaProjectElementInfo projectInfo = (JavaProjectElementInfo) getJavaModelManager().getInfo(project);
        ProjectCache projectCache = projectInfo == null ? null : projectInfo.projectCache;
        HashtableOfArrayToObject allPkgFragmentsCache = projectCache == null ? null
                : projectCache.allPkgFragmentsCache;
        boolean isJavaLike = org.eclipse.jdt.internal.core.util.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(resource, ((ClasspathEntry) entry).fullInclusionPatternChars(),
                            ((ClasspathEntry) entry).fullExclusionPatternChars())) {
                        // 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();

                        // 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;
}