title: Jackson deserialisation/TypeReference for dynamically loaded pojo class
body: <p>I have a requirement to get <code>JSON</code> input Pojo instance and I am using <code>Jackson 2</code> library and below <code>readValue</code> method could deserialise using typeReferencing :</p>
  
  <pre><code>POJO_ClassName p = mapper.readValue(new TypeReference&lt; POJO_ClassName &gt;() {});
  </code></pre>
  
  <p>But the problem is that as <code>POJO</code> is created and loaded at runtime dynamically, how do I get <code>JSON</code> to <code>POJO</code> instance/object as I do not have fully qualified class (POJO_ClassName) name for above statement?</p>
  
  <p>Note: I use <code>jsonSchema2pojo</code> library to generate <code>POJO</code> classes at runtime.</p>
  
  <p>Here is code snippet, I am using to generate <code>POJO</code> for <code>JSON</code>  at runtime 
  and trying</p>
  
  <pre><code>  String classPath="com.EnrichmentService.Thread72"; 
       String classLocation = System.getProperty("user.dir")
                           + "/src/main/java"; JCodeModel codeModel = new JCodeModel();
  
       final RuleFactory ruleFactory = new RuleFactory(config,
                           new Jackson2Annotator(config), new SchemaStore());
  
       final SchemaMapper mapperSchema = new SchemaMapper(ruleFactory,
                           new SchemaGenerator());
  
       mapperSchema.generate(codeModel, "EsRootDoc",classPath, json);
  
       codeModel.build(new File(classLocation));  // generates pojo classes
  
       // Till above jsonSchema2Pojo pojo generation all Good !!
        // EsRootDoc instance is needed for further drools drl validations.
  
       com.EnrichmentService.Thread72.EsRootDoc p = mapper.readValue(new TypeReference&lt;com.EnrichmentService.Thread72.EsRootDoc&gt;() {}); 
  // see alternative way as well in my 24Aug17 edit at the end of this question
  </code></pre>
  
  <p>But as <code>com.EnrichmentService.Thread72.EsRootDoc</code> has yet not been generated compiler would error to class not Found.</p>
  
  <p>Main Points:</p>
  
  <p>1) Same Pojo classes generated at run time iteratively but with different properties as JSON input changes each time.</p>
  
  <p>2) Even tried 
  Object pojo =mapper.readValue(json,Class.forName("com.EnrichmentService.Thread72.EsRootDoc")); as class.forName does not replace an existing class!</p>
  
  <p><strong>Edit 24 Aug17</strong> - Here is my custom class loader : </p>
  
  <p><strong>Note: Indexer is class which load dynamic EsRootDoc/POJO class at run time.</strong> </p>
  
  <pre><code> static class TestClassLoader extends ClassLoader {
              @Override
              public Class&lt;?&gt; loadClass(String name) throws ClassNotFoundException {
                  if (name.equals("com.EnrichmentService.Thread72.EsRootDoc")) {
                      try {
                          InputStream is = Indexer.class.getClassLoader().getResourceAsStream("com/EnrichmentService/Thread72/EsRootDoc.class");
                          byte[] buf = new byte[is.available()];
                          int len = is.read(buf);
  
                          Class&lt;?&gt; c=defineClass(name, buf, 0, len);
                          resolveClass(c);
                          return c;
  
  
                      } catch (IOException e) {
                          throw new ClassNotFoundException("", e);
                      }
                  }
                  return getParent().loadClass(name);
              }
          }
  </code></pre>
  
  <p>I have tried using above TestClassLoader custom class loader as an alternative way is like this :</p>
  
  <pre><code>Class cls = new      TestClassLoader().loadClass("com.EnrichmentService.Thread72.EsRootDoc");
      Object obj = cls.newInstance();
      cls.getMethod("getCrawlerSource").invoke(obj);
      p=mapper.readValue(json, cls);  // but here i am getting the same deserialization exception as earlier.
  </code></pre>
  
  <p>Referred an old answer@ <a href="https://stackoverflow.com/questions/14316996/how-to-replace-classes-in-a-running-application-in-java#answer-14317506">How to replace classes in a running application in java ?</a></p>
  
  <p><strong>Edit2: 24Aug17</strong>
   Exception being faced stackTrace is here: <a href="https://pastebin.com/ckCu2uWx" rel="noreferrer">https://pastebin.com/ckCu2uWx</a></p>