Aspectj BCEL添加方法参数注释不起作用

时间:2019-04-02 06:10:34

标签: java cglib bcel

使用以下Java代码创建一个动态类,并加载它,

ClassGen classGen = new ClassGen("test", "java.lang.Object", "test", java.lang.reflect.Modifier.PUBLIC, null);

MethodGen methodGen = new MethodGen(java.lang.reflect.Modifier.PUBLIC, BasicType.getType(void.class), new Type[] {new ObjectType("java.lang.String")}, new String[] {"param"}, "testMethod", "test", new InstructionList(), classGen.getConstantPool());

NameValuePair nameValuePair = new NameValuePair("name", new SimpleElementValue(ElementValue.STRING, classGen.getConstantPool(), "param"), classGen.getConstantPool());

AnnotationGen annotation = new AnnotationGen(new ObjectType(RequestParam.class.getName()), Arrays.asList(nameValuePair), true, classGen.getConstantPool());

methodGen.addParameterAnnotation(0, annotation);

classGen.addMethod(methodGen.getMethod());
org.aspectj.apache.bcel.classfile.JavaClass javaClass = classGen.getJavaClass();
Class<?> defineClass = new com.google.core.utils.ByteClassLoader().defineClass("test", javaClass.getBytes());

defineClass.getMethods()[0].getParameters()[0].getDeclaredAnnotations();

代码的最后一行返回一个空数组,我该如何解决?

0 个答案:

没有答案