0

Is there a way to extract into some structured format (JSON, XML, whatever) partial information from a number of constructor calls expressions?

Say we have code like:

public enum Model {
   TOYOTA, 
   FORD
}

public enum Dealership {
   TOYOTA_GRAND_PRIX(Model.TOYOTA, Set.of("LAND-CRUISER", "AVENSIS")),
   FORD_DELUXE(Model.FORD, Set.of("FOCUS"));

   private final Model model;
   private final Set<String> brands;
}

and I would like to get something like

[
 {
  "model": "TOYOTA", 
  "brands": ["LAND-CRUISER", "AVENSIS"]
 }, 
 {
  "model": "FORD", 
  "brands": ["FOCUS"]
 }
]

is there a way to achieve (something like) this, with SSR?

62mkv
  • 1,444
  • 1
  • 16
  • 28
  • 1
    I don't think it is possible to achieve this using SSR. But it looks like it would not be very hard to just write some Java code to iterate over the enum constants and print out the desired JSON or XML. Or is this not possible for some reason? – Bas Leijdekkers Sep 01 '22 at 12:24
  • Hi @BasLeijdekkers thanks for the reply! Well, yeah, I ended up writing a unit test to check for invariants I was interested in :) and indeed, I used to write tests to output structured information about the code (also, enums related, most probably). But it kinda feels IntelliJ should be able to do that! I can not explain this feeling :) but it certainly knows it's ins and outs in the code and there could be some extension point into that meta-information that could take that AST, apply some processing to it, and output the results. So I thought SSR could be a good candidate.. – 62mkv Sep 02 '22 at 10:30
  • First, you need to create a Unit Test class, and define a instance of that `enum` class and use `gson` or `Jackson` to serialize it as a `JSON` string. – wonsuc Oct 10 '22 at 03:28
  • 1
    thanks @wonsuc. this is a solution one arrives at, eventually :))) – 62mkv Oct 10 '22 at 14:19

0 Answers0