Example usage for org.eclipse.jdt.internal.core PackageFragmentRoot getResolvedClasspathEntry

List of usage examples for org.eclipse.jdt.internal.core PackageFragmentRoot getResolvedClasspathEntry

Introduction

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

Prototype

@Override
    public IClasspathEntry getResolvedClasspathEntry() throws JavaModelException 

Source Link

Usage

From source file:org.codehaus.groovy.eclipse.codeassist.ProposalUtils.java

License:Apache License

/**
 * Can be null if access restriction cannot be resolved for given type
 *
 * @param type//  w  ww.  ja  va 2 s . c om
 * @param project
 * @return
 */
public static AccessRestriction getTypeAccessibility(IType type) {

    PackageFragmentRoot root = (PackageFragmentRoot) type.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);

    try {
        IClasspathEntry entry = root.getResolvedClasspathEntry();
        // Alternative:
        // entry = ((JavaProject) typeProject).getClasspathEntryFor(root
        // .getPath());
        if (entry instanceof ClasspathEntry) {
            AccessRuleSet accessRuleSet = ((ClasspathEntry) entry).getAccessRuleSet();
            if (accessRuleSet != null) {
                char[] packageName = type.getPackageFragment().getElementName().toCharArray();
                char[][] packageChars = CharOperation.splitOn('.', packageName);
                char[] fileWithoutExtension = type.getElementName().toCharArray();

                return accessRuleSet.getViolatedRestriction(
                        CharOperation.concatWith(packageChars, fileWithoutExtension, '/'));

            }
        }
    } catch (JavaModelException e) {
        // nothing
    }

    return null;
}