java中的动态注释更改

时间:2013-05-27 10:21:18

标签: java reflection playframework annotations hotswap

我必须在运行时更改java中模型类的注释。 该场景在我的网页上,当我更改下拉值并点击“应用”按钮时,我有一个下拉列表我必须根据下拉值在我的模型类上添加注释。

为此,我使用javaassit在类上添加注释。我认为注释已正确应用但我无法重新加载该类。我试图使用HotswapAgent来做到这一点。但看起来不像它的工作。

我正在使用Play框架

任何帮助都将受到高度赞赏。

ClassPool pool= Enhancer.newClassPool();
CtClass cc = pool.get("Test");
ClassFile cf = cc.getClassFile(); 
ConstPool cp = cf.getConstPool();
// Code for creating and adding annotation comes here.....
ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
DataOutputStream os = new DataOutputStream(bos);
cf.write(os);
os.close(); 
ClassDefinition cd = new ClassDefinition(Test.class, bos.toByteArray()); 
HotswapAgent hh = new HotswapAgent();
hh.reload(cd);

1 个答案:

答案 0 :(得分:0)

请尝试此操作,如果使用相同的类加载器,则可以加载它

https://www.baeldung.com/java-reflection-change-annotation-params