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

MoreElements.getLocalAndInheritedMethods or alternative should resolve type parameters if applicable #653

Open
victornoel opened this issue Jul 22, 2018 · 6 comments
Labels
Component: common P3 type=enhancement Make an existing feature better

Comments

@victornoel
Copy link

I have the following class hierarchy:

public interface ASuperInterface<A> {
  void test(A a);
}

public interface AnInterface extends ASuperInterface<String> {
  void test();
}

Calling MoreElements.getLocalAndInheritedMethods on the element representing AnInterface gives me a method void test(A a) instead of a method void test(String a).

If this is intended and there is no other recommended way to resolve type parameters like this, I think auto-common should include an helper to do so.

@eamonnmcmanus
Copy link
Member

The main problem is that the Eclipse compiler has never implemented Types.asMemberOf correctly. Some bugs have been fixed (382590 and 510118) but some remain (499026). That makes it hard to address otherwise reasonable requests like this, without throwing Eclipse users under the bus.

@victornoel
Copy link
Author

@eamonnmcmanus good point, thanks for the explanation.
By the way, don't you have the need in one of the Auto annotation processor or do you handle this problem in a different way than with Types.asMemberOf?

For the record, as a workaround, I tackled this problem during code generation. I am using javapoet and they have MethodSpec.overriding() taking the containing type and making some use of Types.asMemberOf to achieve the desired result. I suppose this mean MethodSpec behaviour is also touched by the Eclipse bugs, but it's better than nothing for my needs :)

@eamonnmcmanus
Copy link
Member

I'm still not really sure what the best solution is here. For the record, if you're not afraid of the Eclipse bug, you should be able to replace the type parameters yourself in this somewhat non-obvious way:

ExecutableElement originalMethod = ...;
Types types = processingEnv.getTypeUtils();
ExecutableElement replacedMethod =
    MoreElements.asExecutable(types.asMemberOf(someType, originalMethod));

Yes, we do run into this issue in AutoValue and we work around it as best we can.

@victornoel
Copy link
Author

@eamonnmcmanus yes, I did something like (but via javapoet).

Yes, we do run into this issue in AutoValue and we work around it as best we can.

Could you point me to where in the codebase this happen maybe?

@eamonnmcmanus
Copy link
Member

The main place is in EclipseHack.

@victornoel
Copy link
Author

@eamonnmcmanus thanks, and why not expose this behaviour in auto common? I'm sure it will be useful to many :)

@raghsriniv raghsriniv added the P3 label Jun 24, 2019
@nick-someone nick-someone added type=enhancement Make an existing feature better Component: common labels Aug 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: common P3 type=enhancement Make an existing feature better
Projects
None yet
Development

No branches or pull requests

4 participants