0

I am working on a project where I need to generate JSON schemas for all of the objects that are returned from my API.

I am given a String such as:

"com.example.CustomListing<MyClass>"

My goal is to create a schema for CustomListing and insert a $ref to MyClass in all the places it is used in CustomListing.

Jackson offers a way to create schemas from generics here but I need to have Class object for that generic type.

I can create a generic Class object using a method here but it requires me to know the type ahead of time, which I do not.

How can I get a Class<CustomListing<MyClass>> when I am only given the String "com.example.CustomListing<MyClass>"?


EDIT: I am creating these schemas in a JavaDoc doclet, so I am working at compile time, hence the limitations.

Community
  • 1
  • 1
Vidia
  • 471
  • 5
  • 17
  • 1
    There is no such thing as `Class>`. – Sotirios Delimanolis Aug 08 '14 at 15:29
  • Generics are a compile time concept. There is (almost) no point to know the generic type argument a runtime and it will not help here. – Sotirios Delimanolis Aug 08 '14 at 15:30
  • I updated my question. I am working in a javadoc doclet. I am creating a documentation engine that needs to document the JSON schema for those generic types, so I can't just ignore them. – Vidia Aug 08 '14 at 15:34
  • Sure but you want to use the `Class` object with Jackson at runtime. You probably want a `TypeReference`, but I'm not exactly sure what you are working with. – Sotirios Delimanolis Aug 08 '14 at 15:37
  • @Vidia Java Doclet has the API to access the type parameters. Have you tried at it? – Alexey Gavrilov Aug 09 '14 at 06:37
  • That is how I am getting the string. And I have seen the TypeReference but I need to set the Generics of that dynamically, which is what I am asking about. – Vidia Aug 11 '14 at 01:12

1 Answers1

0

I think I have found a direction to go for the solution. I am using ObjectMapper from Jackson which allows me to configure modules to process any given type. I believe I can register some to deal with a generic (such as <T>) and it would know that in this case <T> would be referencing MyClass instead and insert the $ref.

Vidia
  • 471
  • 5
  • 17