You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A useful addition to MoreElements or Visibility could be a way to check whether a given Element can see another one; the expected most-common use-case being to check whether some method can be called from another class (particularly when enumerating methods of a class including inherited methods from class hierarchy). The method would return false if the target element is private, true if it's public (and all enclosing elements are also visible to the source element; i.e. if the effective visibility of the element is public), and would check whether the source and target elements are in the same package otherwise.
For example, in Bullet•, the generated class will call methods from the processed Dagger‡ component. Components can be abstract classes and therefore inherit methods from a base class. The Bullet• processor thus needs to check which methods the generated class will be able to call without causing a compilation error, and similarly for types for member injections (either to avoid generating such code, or to possibly generate helper code in the appropriate package, to get access to the type or method).
The API could look like:
static boolean isVisibleFrom(Element target, Element from);
or
static boolean isAccessibleFrom(Element target, Element from);
In the case of Bullet•, the from will likely be the package (as a PackageElement) where the class is being generated.
When generating a subclass, one could want to know which methods from the parent class are callable from the subclass, so there could be an option to get a true when the target field or method (or nested type) is protected, even if not in the same package. Not sure what the API would look like though.
Absolutely. The only problem is that that is a large and somewhat complicated part of the JLS to implement. :\ It's certainly on the TODO list, but like GWT, we've just been limping along until a sufficiently tricky issue forces us into solving this problem.
A useful addition to
MoreElements
orVisibility
could be a way to check whether a givenElement
can see another one; the expected most-common use-case being to check whether some method can be called from another class (particularly when enumerating methods of a class including inherited methods from class hierarchy). The method would returnfalse
if the target element isprivate
,true
if it'spublic
(and all enclosing elements are also visible to the source element; i.e. if the effective visibility of the element ispublic
), and would check whether the source and target elements are in the same package otherwise.For example, in Bullet•, the generated class will call methods from the processed Dagger‡ component. Components can be abstract classes and therefore inherit methods from a base class. The Bullet• processor thus needs to check which methods the generated class will be able to call without causing a compilation error, and similarly for types for member injections (either to avoid generating such code, or to possibly generate helper code in the appropriate package, to get access to the type or method).
The API could look like:
or
In the case of Bullet•, the from will likely be the package (as a
PackageElement
) where the class is being generated.When generating a subclass, one could want to know which methods from the parent class are callable from the subclass, so there could be an option to get a
true
when the target field or method (or nested type) isprotected
, even if not in the same package. Not sure what the API would look like though.FWIW, GWT's
JClassType
hasgetInheritableMethods
for methods a subclass could call, andgetOverridableMethods
for those you can override in a subclass (that would be the subset of inheritable methods that's notfinal
). And there actually is a TODO in the code to add anisCallable
: https://github.com/gwtproject/gwt/blob/a6da588fadc17f9ae5b77204d25bad70f3846e4f/user/src/com/google/gwt/place/rebind/PlaceHistoryGeneratorContext.java#L288-L290 But while GWT limits it to methods, this is applicable to fields, nested types, and types in general (e.g. when looking at return types or parameter types of methods, or types of fields: not only the method or field needs to be accessible, but also the referenced type(s)).The text was updated successfully, but these errors were encountered: