0

I am writing an annotation processor and i need to scan all classes with certain annotation to get all fields and create json object with same structure of the class.

For example:

@ClassToJson
public class Person {
    private String name;
    private String surname;

    /*getter and setter*/
}

My output is:

{
    "name": "string",
    "surname": "string"
} 

Now i am wondering how can i handle classes like this one:

public class PhoneNumber {
    private String countryCode;
    private String phoneNumber;

    /*getter and setter*/
}

@ClassToJson
public class Person {
    private String name;
    private String surname;
    private PhoneNumber phoneNumber;

    /*getter and setter*/
}

i would like get output like this:

{
    "name": "string",
    "surname": "string",
    "phoneNumber": {
        "countryCode": "string",
        "phoneNumber": "string"
    }
}

Luke
  • 516
  • 2
  • 10
  • 1
    How do you currently know that a class is _not_ "custom"? – Kevin Anderson Sep 21 '19 at 11:40
  • @KevinAnderson Classes that need to be scanned are annotated with my custom annotation – Luke Sep 21 '19 at 11:47
  • You need to scan recursively by getting the type of each field and scanning those classes as well. – Dave Newton Sep 21 '19 at 11:51
  • Yes, that was my idea but objects like String, Double, Integer, etc etc... are type too but them should not be scanned. I should filter all fields in order to delete object like "String", "Double", "Integer"... Etc etc. That's not great solution i think. – Luke Sep 21 '19 at 12:04
  • Is it permitted to have custom annotations on classes like PhoneNumber. That way you could know what are the custom classes ? – Ramachandran.A.G Sep 21 '19 at 12:11
  • Is permitted if PhoneNumber is mine. If do i take PhoneNumber from external library? i cannot put my custom annotation on it but it should be scanned. My custom annotation tell me which class should be scanned, the point is: If the class to be scanned has one fields that is an object it will be scanned too. – Luke Sep 21 '19 at 12:22
  • You can just filter out field types by package/module and skip java ones, or create just a list of exclusion – GotoFinal Sep 23 '19 at 08:58
  • @GotoFinal thanks for the answer, at least i did like you said. I just skipped all object that contains "java.lang" and "java.util". – Luke Sep 23 '19 at 11:00
  • Skipping types like `String`, `Double`, `Integer`, etc. is not great solution, it’s the *only* solution. You have an assumption that certain types are intrinsically handled when appearing in the JSON output in their `toString()` form. That’s a specific finite set of types. You should handle them exactly like that. – Holger Sep 23 '19 at 16:30
  • I didn't skipped it at all, i just handled them in a different way. When i found types like String, Double, Integer and so on i instantiate them and i call .toString() on that obj. When i found types that are not from java i build JSON with Gson. I am using recursive algorithm to do that. Now i having trouble with lists, sets and maps. I think i should handle this types in different way. The only problem is that my recursive method has more than 200 lines of code and more than 15 recursive calls. Is difficult to add new functionality. I'm trying to find better solution – Luke Sep 23 '19 at 21:22
  • Check this link where it uses annotation processor to generate the DTO classes from Entity classes basis on custom annotations. https://www.thetechnojournals.com/2019/12/annotation-processor-to-generate-dto.html – Ashok Prajapati Dec 05 '19 at 16:49

0 Answers0