Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

MethodSpec.overriding shouldn't copy annotations #482

Closed
gk5885 opened this issue Jul 27, 2016 · 3 comments
Closed

MethodSpec.overriding shouldn't copy annotations #482

gk5885 opened this issue Jul 27, 2016 · 3 comments

Comments

@gk5885
Copy link
Collaborator

gk5885 commented Jul 27, 2016

For a few reasons:

  • It's not necessarily semantically correct. @Nullable, for example, might apply to the method being overridden, but not necessarily to the implementation of the override.
  • It doesn't necessarily compile. A private annotation on the method being overridden nested in the same type that declares the method is not accessible from a different type that might override the method.
@sormuras
Copy link
Contributor

Two valid points, Gregory.

What do you guys prefer:

  • replace annotation copy code by just adding an @Override tag
  • guard current implementation by boolean switches like:
Builder overriding(ExecutableElement method, boolean copyMethodAnnotations, boolean copyParameterAnnotations) {

@gk5885
Copy link
Collaborator Author

gk5885 commented Jul 29, 2016

My vote is for the first one. It's the simple, safe, predictable behavior.

The second one, is unlikely to be all that useful. You have to know something about the annotations on the method you're overriding in order to be able to predict if copying them is safe/correct. If you've come that far you might as well just put them on the overriding method yourself. There might be a version that took a Predicate or something that might be useful, but that seems like overkill to me.

The third assumes that a blacklist (rather than a whitelist) is what you're going to want. I think more often than not, it might be the other way around, but I think a Predicate or something similar might work better for that too.

gk5885 added a commit to gk5885/javapoet that referenced this issue Aug 5, 2016
sormuras pushed a commit to sormuras/javapoet that referenced this issue Sep 2, 2016
Egorand pushed a commit that referenced this issue Apr 20, 2020
* Don't copy parameter annotations when creating a ParameterSpec.

This further preserves the behaviour discussed in #482.
Unifying it for both MethodSpec.overriding and ParameterSpec.get.

* Add compilation test for when overriding a method with private annotations.

* Address PR comments:

* Remove unused import
* Rename newly added test
@HenryLoenwind
Copy link

HenryLoenwind commented Oct 7, 2020

Is it only me who finds it really annoying that copying a parameter WITH annotations now is a major effort that involves research into how to do that? VariableElements are not the easiest thing to work with...

Aside from that, I find the behaviour of ParamSpec.get() very unexpected. I would have expected it to give me an object that contains the same information as its parameter, not some stripped down version. It's more ParamSpec.getSomethingThatLooksABitLike() now...

PS: I'm a bit annoyed because I had to find out why my code wasn't working from reading comments in yours. Not even in a javadoc, but hidden away inside the method.

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

No branches or pull requests

3 participants