google / google/auto

AutoCommon: check visibility of one method (or type or field) from another.

Open
#226 2 comments 0 reactions 0 assignees View on GitHub
Component: common P3 type=addition
Dominant language
Java
Stars
10.6k
Forks
1.2k
Avg merge
6h 32m
Merged PRs (30d)
13

Description

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•](https://github.com/tbroyer/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.

FWIW, GWT's `JClassType` has [`getInheritableMethods`](http://www.gwtproject.org/javadoc/latest/com/google/gwt/core/ext/typeinfo/JClassType.html#getInheritableMethods%28%29) for methods a subclass could _call_, and [`getOverridableMethods`](http://www.gwtproject.org/javadoc/latest/com/google/gwt/core/ext/typeinfo/JClassType.html#getOverridableMethods%28%29) for those you can override in a subclass (that would be the subset of _inheritable methods_ that's not `final`). And there actually **is** a TODO in the code to add an `isCallable`: 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)).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.