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
  • From: "Milles, Eric (TR Tech, Content & Ops)" <eric.milles@xxxxxxxxxxxxxxxxxx>
  • Date: Tue, 3 Mar 2020 22:46:55 +0000
  • Accept-language: en-US
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=thomsonreuters.com; dmarc=pass action=none header.from=thomsonreuters.com; dkim=pass header.d=thomsonreuters.com; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=vL3peYoqXGIwMpeBzTxcRKBLLgvbGZnrDJKMWGqYP6o=; b=mTvBHvvIJrugpxtfKZdRB3MJD2gZGtQ5ovEvBh4yTjE587tKahh8VqPNnlhCoUTH1eZ+U8Ep4BW+uX1st+x/12VhX2R0U7BgA4iBfUbWHtb6omFICoTdzbFzJKkg6Ndzhf4P4VGdSu+e4PvlZJezvDXUFB56D8rngefp+t0si2a2i1qw6ofFCTf1mA8inRu0vSpGeF2P3fh0eu7Wz0aCMl+2GXcyKXHa4hcmp6xOnWC1OTq3HcDtvArfY2oy+Fq6CT65g39uSPWJ2ephUhx6F4M/a/fYKBv8FWz/AifIEjJQF9Yh5KK7R3rWXcZgDIfdRKqPxGbYP5kMU0wZtLsIeQ==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=laQ68S5eVIybcvYAw+RT8LfSvIxNB9pdxCRcUfTF3Vg2r55P197xVnN5SISsgVAIkxe3e/gwiVJVQLkm+WSdrBsOWaEoScezTIOgiyN2hB+ZDTtzbeUn+gYQWRBQBtCOen4G+FzgI3inLb0EjB9UNdSbJX+5dnsWM7J9Jx3eAPyDX8bN4UtrllPZR7jj749WPrixkvP4Aayr5WX9cPU/tQPmKh54Vd9peN6Rs0efpiOAWUGaQwO+CaCALnj4v0X5pJ4/AOxia7miEJeuXv6P620Bs2Zdt+VGBedBHUHErlLFVeIhC9kqyupBVK9r/jKcbJVv8C6nVhJMucpXsP9SzQ==
  • Delivered-to: jdt-dev@xxxxxxxxxxx
  • List-archive: <https://www.eclipse.org/mailman/private/jdt-dev>
  • List-help: <mailto:jdt-dev-request@eclipse.org?subject=help>
  • List-subscribe: <https://www.eclipse.org/mailman/listinfo/jdt-dev>, <mailto:jdt-dev-request@eclipse.org?subject=subscribe>
  • List-unsubscribe: <https://www.eclipse.org/mailman/options/jdt-dev>, <mailto:jdt-dev-request@eclipse.org?subject=unsubscribe>
  • Thread-index: AQHV8Y0EXF75F/yzMU2PqCmZmhE3fag3c6GAgAADxlA=
  • Thread-topic: [jdt-dev] Figuring out a Type is forbidden or not

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=https%3A%2F%2Fwww.
> eclipse.org%2Fmailman%2Flistinfo%2Fjdt-dev&amp;data=02%7C01%7Ceric.mil
> 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=https%3A%2F%2Fwww.eclipse.org%2Fmailman%2Flistinfo%2Fjdt-dev&amp;data=02%7C01%7Ceric.milles%40thomsonreuters.com%7Cf8e61ed7757d4b356fd808d7bfc2d129%7C62ccb8646a1a4b5d8e1c397dec1a8258%7C0%7C0%7C637188715768696394&amp;sdata=bkPHY9ZESjY93OURRAiTJO2A1UhEMTOe1L3noP0x56A%3D&amp;reserved=0


Back to the top