Java 我已经在运行时向方法添加了预定义的注释,我希望更改是永久的?
所以,假设我有一个类Person,我想在运行时将Myntra注释添加到getLastName()方法中(这是我使用Javassist库完成的) 但我希望更改是永久性的,以便下次运行时注释应该在那里 我该怎么做Java 我已经在运行时向方法添加了预定义的注释,我希望更改是永久的?,java,reflection,annotations,runtime,java-assist,Java,Reflection,Annotations,Runtime,Java Assist,所以,假设我有一个类Person,我想在运行时将Myntra注释添加到getLastName()方法中(这是我使用Javassist库完成的) 但我希望更改是永久性的,以便下次运行时注释应该在那里 我该怎么做 package com.flipkart.aditya.annotations; public class Person { private String firstName; private String lastName; private String ad
package com.flipkart.aditya.annotations;
public class Person {
private String firstName;
private String lastName;
private String adhaarID;
private int employeeID;
public Person(String firstName, String lastName, String adhaarID, int employeeID) {
this.firstName = firstName;
this.lastName = lastName;
this.adhaarID = adhaarID;
this.employeeID = employeeID;
}
@Myntra
@Jabong
public String getFullName()
{
return firstName+" "+lastName;
}
@Xyz
public String getFirstName()
{
return firstName;
}
public String getLastName() {
return lastName;
}
@Myntra
public String getAdhaarID() {
return adhaarID;
}
@Jabong
@Xyz
public int getEmployeeID() {
return employeeID;
}
}
这就是我如何使用javassist库实现的
package com.flipkart.aditya.annotations;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.bytecode.AnnotationsAttribute;
import javassist.bytecode.ClassFile;
import javassist.bytecode.ConstPool;
import javassist.bytecode.annotation.Annotation;
public class AddRunTimeAnnotation {
public static void addNewAnnotationToMethod(String className,String methodName) throws Exception{
ClassPool pool = ClassPool.getDefault();
CtClass cc = pool.getCtClass(className);
CtMethod ctMethod = cc.getDeclaredMethod(methodName);
ClassFile ccFile = cc.getClassFile();
ConstPool constpool = ccFile.getConstPool();
AnnotationsAttribute attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
Annotation annot = new Annotation("com.flipkart.aditya.annotations.Myntra", constpool);
attr.addAnnotation(annot);
ctMethod.getMethodInfo().addAttribute(attr);
Class<?> c =cc.toClass();
}
}
注意:Jabong和xyz也是与Myntra类似的注释,但与我的问题无关
这就是我的main()函数的样子-->我在这里向getLastName()方法添加一个新的Myntra注释,然后使用java反射打印所有带有Myntra注释的方法
public class NewApplicationToTestApproachOne {
public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
try {
AddRunTimeAnnotation.addNewAnnotationToMethod("com.flipkart.aditya.annotations.Person", "getLastName");
} catch (Exception e) {
e.printStackTrace();
}
Person person = new Person("Ravan", "lanka wale", "0000 0000 0000", 420);
Class<?> reflectClass = Person.class;
Method[] arrayOfMethods = reflectClass.getDeclaredMethods();
for (Method method : arrayOfMethods) {
if (method.isAnnotationPresent(Myntra.class)) {
System.out.println(method.invoke(person, null));
}
}
}
}
但是我希望添加到getLastName()方法的注释是永久的。
我不确定这是否可能?如果是-->请告诉我怎么做 将注释添加到方法定义中:
@Myntra
public String getLastName() {
return lastName;
}
您不需要在运行时动态添加注释的代码。只需声明它,它将在中编译。永久性的。嗯,这是我总能做到的。但我不想每次更新都对代码进行修改。因此,在编写代码时,您不希望再为注释添加一行,而是希望在运行时减慢应用程序的速度,以便将注释动态添加到某些方法(您应该在编译时就知道这一点)。对我来说,这就像说“我不会用洗碗机,因为我懒得把东西放进去。”。恐怕我一点也不明白动机。@AdityaVerma您已经有了在运行时执行此操作的代码。那么你的问题是什么?@AdityaVerma你应该决定。“在运行时”与“永久”相反。如果您愿意在编译类后插入处理步骤,则可以使用
writeFile(…)
而不是toClass()
来生成类文件以供后续使用。@AdityaVerma您已经有了代码。您只需替换Class c=cc.toClass()代码>带有cc.writeFile(“路径/到/目录”)代码>。或者,您可以调用toBytecode()
来获取byte[]
并使用您想要的任何I/O方法。请注意,覆盖当前使用的现有类文件仅在它们位于默认文件系统中(即不在jar或模块映像中)时有效。
Ravan lanka wale
0000 0000 0000
lanka wale
@Myntra
public String getLastName() {
return lastName;
}