javassist icon indicating copy to clipboard operation
javassist copied to clipboard

Annotation processing while creating a new class using javassist

Open abhinavtk97 opened this issue 3 years ago • 2 comments

I have been trying to use javassist to create some classes dynamically and I want to add some annotations to them, which is supposed to create separate helper classes when compiling the annotated class. I was able to create the new class with annotations with javassist and I was able to save it as a file, but the issue is that the annotation is not processed and the helper classes are not created. I tried decompiling the class generated by javassist and recompiling the same java file using intellij and it seems to generate the helper classes properly.

It would be great if someone can confirm if javassist does support annotation processing while doing compilation and if there is a workaround for doing this for a class created using javassist.

abhinavtk97 avatar Mar 31 '21 12:03 abhinavtk97

Suggest give some examples or unit tests. And your environment, javassist version.

NingZhang-e avatar Apr 01 '21 06:04 NingZhang-e

CtField field = new CtField(classPool.get(typeClazz.getName()), productField.getKey(),
                    productSpuClass);
            field.setModifiers(AccessFlag.PUBLIC);
            ConstPool constPool = field.getFieldInfo().getConstPool();
            
            List<Annotation> list = createAnnotations(field, productSpuClass, productField);
            AnnotationsAttribute fieldAttr =
                    new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
            fieldAttr.setAnnotations(list.toArray(new Annotation[list.size()]));
            field.getFieldInfo().addAttribute(fieldAttr);

Annotation can be successfully created on the window platform, but Annotation cannot be created successfully on linux docker

zksite avatar Apr 23 '21 10:04 zksite