Search in sources :

Example 1 with EnumMemberValue

use of javassist.bytecode.annotation.EnumMemberValue in project play-cookbook by spinscale.

the class XmlEnhancer method enhanceThisClass.

@Override
public void enhanceThisClass(ApplicationClass applicationClass) throws Exception {
    CtClass ctClass = makeClass(applicationClass);
    if (!ctClass.subtypeOf(classPool.get("play.db.jpa.JPABase"))) {
        return;
    }
    if (!hasAnnotation(ctClass, "javax.persistence.Entity")) {
        return;
    }
    ConstPool constpool = ctClass.getClassFile().getConstPool();
    AnnotationsAttribute attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
    if (!hasAnnotation(ctClass, "javax.xml.bind.annotation.XmlAccessorType")) {
        Annotation annot = new Annotation("javax.xml.bind.annotation.XmlAccessorType", constpool);
        EnumMemberValue enumValue = new EnumMemberValue(constpool);
        enumValue.setType("javax.xml.bind.annotation.XmlAccessType");
        enumValue.setValue("FIELD");
        annot.addMemberValue("value", enumValue);
        attr.addAnnotation(annot);
        ctClass.getClassFile().addAttribute(attr);
    }
    if (!hasAnnotation(ctClass, "javax.xml.bind.annotation.XmlRootElement")) {
        Annotation annot = new Annotation("javax.xml.bind.annotation.XmlRootElement", constpool);
        String entityName = ctClass.getName();
        String entity = entityName.substring(entityName.lastIndexOf('.') + 1).toLowerCase();
        annot.addMemberValue("name", new StringMemberValue(entity, constpool));
        attr.addAnnotation(annot);
        ctClass.getClassFile().addAttribute(attr);
    }
    applicationClass.enhancedByteCode = ctClass.toBytecode();
    ctClass.defrost();
}
Also used : EnumMemberValue(javassist.bytecode.annotation.EnumMemberValue) CtClass(javassist.CtClass) ConstPool(javassist.bytecode.ConstPool) StringMemberValue(javassist.bytecode.annotation.StringMemberValue) AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute) Annotation(javassist.bytecode.annotation.Annotation)

Aggregations

CtClass (javassist.CtClass)1 AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)1 ConstPool (javassist.bytecode.ConstPool)1 Annotation (javassist.bytecode.annotation.Annotation)1 EnumMemberValue (javassist.bytecode.annotation.EnumMemberValue)1 StringMemberValue (javassist.bytecode.annotation.StringMemberValue)1