-1

Lets suppose i have a class:

public class Foo {

    int a;
    int b;

}

And i'm creating an object of anonymous class. The question is that is it technically possibile to set annotation on fields a and b like below but in run time?

Foo anonymous = new Foo() {
    @Annotation
    int a;
    @Annotation
    int b;

}
lilkunien
  • 71
  • 2
  • 3
  • 7
  • Your current code declares *separate* fields which *hide* ones inherited from superclass. Regarding "is that is it technically possibile to set annotation .. at runtime" maybe [Adding Java Annotations at Runtime](https://stackoverflow.com/q/1635108) will help. – Pshemo Jun 25 '19 at 21:57
  • What are you trying to achieve by setting annotations at runtime? If you are trying to use a framework feature that uses annotations, there are often ways to use the same feature programmatically. – kapex Jun 25 '19 at 22:20
  • @kapex yes, but the problem is that i dont know which class i will get on input in order to return object with annotated fields of superclass – lilkunien Jun 26 '19 at 07:06
  • @kapex i want to decide what fields i want to serialize and which not, to achive it programmatically means to set transient key word on a field in runtime what seems to me less achievable than annotating fields in runtime – lilkunien Jun 26 '19 at 07:17

1 Answers1

0

For those who still search an answer for the question you will find it here: Adding Java Annotations at Runtime

lilkunien
  • 71
  • 2
  • 3
  • 7