jeudi 31 août 2017

Bundle existing classes together inside a parent class Java

I have a usecase in Java, where I have a custom Annotation. Certain classes are annotated with this annotation. Now I need to find all these classes in my package, then either at compile time or when the application comes up, create a new class with these fields along with some other fields. Expect this class as part of the request body at an end point which gets populated to its subclasses whenever a request comes.

For Instance:-

   @CustomAnnotation
    class User{
        String name;
        Integer id;
    }

    @CustomAnnotation 
    class Product{
        String id;
        String description;
        String name;
    }

These are the two classes which user creates and annotates it with @CustomAnnotation.

Now I want to write a piece of code which looks for all the classes marked with @CustomAnnotation and create another class:-

class Custom{
     String assignedTo;
     User users[];
     Product products[];
}

map this to an endpoint say localhost:8080/assign Now every time a request comes to localhost:8080/assign class Custom gets populated and returns arrays of Users and Products respectively back to the main control for further logic.

I believe with a language like Nodejs it would have been easy because of it's dynamic nature. What would be the right approach towards solving this problem in Java, using reflections would be the right approach or should I have a look at Groovy? can I export groovy code as a jar file which could be imported into any project and create the endpoint inside that application? Is it possible to write code which creates classes at compile time or runtime in Java?

I think spring framework does something similar.

Thanks





Aucun commentaire:

Enregistrer un commentaire