Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restrictions do not apply to class literals, instanceof, casts, or method references #166

Open
dwnusbaum opened this issue Aug 21, 2023 · 6 comments
Labels

Comments

@dwnusbaum
Copy link
Member

dwnusbaum commented Aug 21, 2023

I am not sure if this is intended, but restrictions do not apply to class literals (XYZ.class) (EDIT: and a few other cases, see #166 (comment)). For example, access-modifier-checker does not fail given this in one module:

@Restricted(NoExternalUse.class)
class Foo { }

And this in another module:

class Bar {
  Bar() {
    Foo.class.getName();
  }
}

I think it is straightforward to fix this if we do consider it a bug by expanding Checker.RestrictedMethodVisitor to override visitLdcInsn, although I would not be surprised if plugins are inadvertently relying on the current behavior, making this a source-incompatible change.

@daniel-beck
Copy link
Member

I would not be surprised if plugins are inadvertently relying on the current behavior, making this a source-incompatible change

Given how we use it in core, e.g. freely renaming/refactoring/removing things, this seems like a net positive change, +1 from me. (Compare restricting generated Messages.)

@jglick
Copy link
Member

jglick commented Aug 21, 2023

I would not be surprised if plugins are inadvertently relying on the current behavior

I am pretty sure I have relied on the current behavior.

@dwnusbaum
Copy link
Member Author

For what it's worth, looking a little deeper, restrictions also don't apply when casting (Foo) foo, with instanceof (foo instanceof Foo), or with method references (Runnable r = Foo::restrictedMethod; r.run()).

foo.class, foo instanceof Foo, and (Foo) foo seem more or less the same, so I think we should probably either block all of them or none of them.

Method references seem distinct and for those I don't really see any reason to allow them. It is a bit awkward to work with them though using ASM.

I am pretty sure I have relied on the current behavior.

Are you just noting that as far as concern about potential incompatibilities, or do you mean that you think we should preserve the current behavior?

@dwnusbaum dwnusbaum changed the title Restrictions do not apply to class literals Restrictions do not apply to class literals, instanceof, casts, or method references Aug 21, 2023
@basil
Copy link
Member

basil commented Aug 21, 2023

I don't think we should be making breaking changes without proactively identifying and adapting consumers.

@jglick
Copy link
Member

jglick commented Aug 21, 2023

I am not too concerned since this is only a build-time error and you can always suppress this tool on a per-class level.

@basil
Copy link
Member

basil commented Aug 21, 2023

To be absolutely clear, I intend to block this change unless consumers are proactively identified and adapted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants