0

Java's reflection API gives us almost all information about our source code. However, no matter how much I tried, couldn't find a solution for my specific problem.

I have a class like this:

public class SomeClass
{

    private String someField;

    private int someOtherField;

    private List<@NotNull String> someCollectionField;

}

I iterate all fields of this class via reflection, and try to get @NotNull annotation used in List<String>, but it seems it's impossible using reflection.

Is there any way I can do this?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
YgzNsl
  • 44
  • 2
  • You mean something like this? https://stackoverflow.com/a/4454783/10802597 – MrFisherman Jan 08 '21 at 13:47
  • Nope. This does not solve. Field.isAnnotationPresent() method only works on annotations that are directly present on the field. In my case, @NotNull annotation is present on generic parameter. – YgzNsl Jan 08 '21 at 13:58
  • 2
    There is your answer https://stackoverflow.com/questions/38486360/are-annotations-on-a-type-parameter-accessible-in-runtime – MrFisherman Jan 08 '21 at 14:13
  • Actually, yes. This is the answer. Thanks a lot! – YgzNsl Jan 08 '21 at 14:43
  • 1
    Does this answer your question? [Are annotations on a type parameter accessible in runtime?](https://stackoverflow.com/questions/38486360/are-annotations-on-a-type-parameter-accessible-in-runtime) – MrFisherman Jan 08 '21 at 16:11
  • The linked question is mine. I've since forked and extended an excellent reflection library, Gentyref, to work with annotated types, and released the fork under the name GeantyRef. Check it out as it enables very advanced type manipulations. – kaqqao Jan 11 '21 at 17:45

0 Answers0