dimanche 19 avril 2020

How to add a Method and Field in a Lombok Like fashion to a Java Class?

I have the following case:

I have class with a field and a Annotation:


public class TestClass{

   @Property
   private Object testValue;
}

My goal is to create a Method for each annotated field that would create a "boilerplate code in a lombok like fashion" for the Property for each Value that is annotated that should look like this


  public Object getTestValueProperty{
   return testValue;  
  }

I can access the fields over reflection and anotations, And read the Type etc. But there is no Way to "Create" a Method Structure and adding it to the class on runtime.

I looked into the following: http://notatube.blogspot.com/2010/12/project-lombok-creating-custom.html

But I failed to get access the JavacAnnotationHandler to create the new Method. Since this article is 10 Years Old, I would assume this is not up to date anymore and that there are other ways to "inject" a method into a class.

If you could give me any resources or Ideas how to research this topic I would appriciate it.





Aucun commentaire:

Enregistrer un commentaire