lundi 20 avril 2020

How to dump or write modified java.lang.Class using reflection to file?

I'm trying to update the method annotation value in a java file at runtime. I'm able to update the value and finally, I got the changes. But don't know how to write the updated source code to file. I'm assuming all changes are present in forName. So please let me know any solution to write the modified java source code to file.

try {
     Class<?> forName = Class.forName("test.SortJsonContent");
     Method[] methods = forName.getDeclaredMethods();
     for (Method method : methods) {                
          method.setAccessible(true);
          if (method.isAnnotationPresent(AnnoTest.class)) {
                AnnoTest anno = method.getAnnotation(AnnoTest.class);
                changeAnnotationValue(anno, "test", "new val");                    
           }
      }

    } catch (Exception e) {
            System.err.println("error"+ e);
    }




Aucun commentaire:

Enregistrer un commentaire