Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jdt-dev] Figuring out a Type is forbidden or not

Thanks Eric, for chiming in.

Let me just add that several types used in that snippet are not API.

In some cases you can perhaps simply replace JavaProject (internal) with IJavaProject (api), but it will not work for all of them -- notable AccessRuleSet is internal.

Stephan

Am 2020-03-03 23:46, schrieb Milles, Eric (TR Tech, Content & Ops):

If you are trying to check for the build access rules (defined in project classpath) that Stephan mentioned, you can check like this:

    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 ignore) {
        }
        return null;
    }


Then it is common in the codeassist space to check accessibility like this:

        int accessibility = IAccessRule.K_ACCESSIBLE;
        if (accessRestriction != null) {
            switch (accessRestriction.getProblemId()) {
            case IProblem.DiscouragedReference:
                if (options.checkDiscouragedReference) {
                    return;
                }
                accessibility = IAccessRule.K_DISCOURAGED;
                break;
            case IProblem.ForbiddenReference:
                if (options.checkForbiddenReference) {
                    return;
                }
                accessibility = IAccessRule.K_NON_ACCESSIBLE;
                break;
            }
        }

-----Original Message-----
From: jdt-dev-bounces@xxxxxxxxxxx <jdt-dev-bounces@xxxxxxxxxxx> On Behalf Of Stephan Herrmann
Sent: Tuesday, March 3, 2020 4:31 PM
To: jdt-dev@xxxxxxxxxxx
Subject: Re: [jdt-dev] Figuring out a Type is forbidden or not

it depends on which rule defines "forbidden"

In the olden days, we only had our own access rules defined as patterns attached to classpath entries. The key abstraction here is org.eclipse.jdt.core.IAccessRule. I'm not aware of any API that actually interprets these rules for a given type.

In a modular world (JRE 9+), classes are "forbidden", when their package is not exported from the enclosing module, or exported with restrictions. This information can be accessed similar to this snippet:

IType type ...
IPackageFragmentRoot pfr = type.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT)
IModuleDescription mod = pfr.getModuleDescription(); String[] exported = mod.getExportedPackageNames(null); // exported to all String given = type.getPackageFragment().getElementName();
// does exported contain given?

HTH,
Stephan


On 03.03.20 19:52, Gayan Perera wrote:
Hi All,

On a plugin i'm working on i want to check if the given type is
forbidden or not, like a class from com.sun package. Is there a JDT API for this ?

Br
Gayan.

_______________________________________________
jdt-dev mailing list
jdt-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or
unsubscribe from this list, visit
https://nam02.safelinks.protection.outlook.com/?url="">.
eclipse.org%2Fmailman%2Flistinfo%2Fjdt-dev&amp;data="" /> les%40thomsonreuters.com%7Cf8e61ed7757d4b356fd808d7bfc2d129%7C62ccb864
6a1a4b5d8e1c397dec1a8258%7C0%7C0%7C637188715768696394&amp;sdata=bkPHY9
ZESjY93OURRAiTJO2A1UhEMTOe1L3noP0x56A%3D&amp;reserved=0

_______________________________________________
jdt-dev mailing list
jdt-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://nam02.safelinks.protection.outlook.com/?url="">
_______________________________________________
jdt-dev mailing list
jdt-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://www.eclipse.org/mailman/listinfo/jdt-dev



Back to the top