Search in sources :

Example 11 with Annotation

use of javassist.bytecode.annotation.Annotation in project BroadleafCommerce by BroadleafCommerce.

the class DirectCopyClassTransformer method reviewDirectCopyTransformAnnotations.

/**
 * Retrieves {@link DirectCopyTransformTypes} that are placed as annotations on classes.
 * @param clazz
 * @param mySkipOverlaps
 * @param myRenameMethodOverlaps
 * @param matchedPatterns
 * @return
 */
protected XFormParams reviewDirectCopyTransformAnnotations(CtClass clazz, boolean mySkipOverlaps, boolean myRenameMethodOverlaps, List<DirectCopyIgnorePattern> matchedPatterns) {
    List<?> attributes = clazz.getClassFile().getAttributes();
    Iterator<?> itr = attributes.iterator();
    List<String> templates = new ArrayList<>();
    List<Boolean> skips = new ArrayList<>();
    List<Boolean> renames = new ArrayList<>();
    XFormParams response = new XFormParams();
    check: {
        while (itr.hasNext()) {
            Object object = itr.next();
            if (AnnotationsAttribute.class.isAssignableFrom(object.getClass())) {
                AnnotationsAttribute attr = (AnnotationsAttribute) object;
                Annotation[] items = attr.getAnnotations();
                for (Annotation annotation : items) {
                    String typeName = annotation.getTypeName();
                    if (typeName.equals(DirectCopyTransform.class.getName())) {
                        ArrayMemberValue arrayMember = (ArrayMemberValue) annotation.getMemberValue("value");
                        for (MemberValue arrayMemberValue : arrayMember.getValue()) {
                            AnnotationMemberValue member = (AnnotationMemberValue) arrayMemberValue;
                            Annotation memberAnnot = member.getValue();
                            ArrayMemberValue annot = (ArrayMemberValue) memberAnnot.getMemberValue("templateTokens");
                            List<String> addedTemplates = new ArrayList<>();
                            for (MemberValue memberValue : annot.getValue()) {
                                String val = ((StringMemberValue) memberValue).getValue();
                                addedTemplates.addAll(reviewTemplateTokens(matchedPatterns, val));
                            }
                            templates.addAll(addedTemplates);
                            BooleanMemberValue skipAnnot = (BooleanMemberValue) memberAnnot.getMemberValue("skipOverlaps");
                            if (skipAnnot != null) {
                                for (int j = 0; j < addedTemplates.size(); j++) {
                                    skips.add(skipAnnot.getValue());
                                }
                            } else {
                                for (int j = 0; j < addedTemplates.size(); j++) {
                                    skips.add(mySkipOverlaps);
                                }
                            }
                            BooleanMemberValue renameAnnot = (BooleanMemberValue) memberAnnot.getMemberValue("renameMethodOverlaps");
                            if (renameAnnot != null) {
                                for (int j = 0; j < addedTemplates.size(); j++) {
                                    renames.add(renameAnnot.getValue());
                                }
                            } else {
                                for (int j = 0; j < addedTemplates.size(); j++) {
                                    renames.add(myRenameMethodOverlaps);
                                }
                            }
                        }
                        response.setXformVals(templates.toArray(new String[templates.size()]));
                        response.setXformSkipOverlaps(skips.toArray(new Boolean[skips.size()]));
                        response.setXformRenameMethodOverlaps(renames.toArray(new Boolean[renames.size()]));
                        break check;
                    }
                }
            }
        }
    }
    return response;
}
Also used : BooleanMemberValue(javassist.bytecode.annotation.BooleanMemberValue) StringMemberValue(javassist.bytecode.annotation.StringMemberValue) AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute) ArrayList(java.util.ArrayList) Annotation(javassist.bytecode.annotation.Annotation) MemberValue(javassist.bytecode.annotation.MemberValue) AnnotationMemberValue(javassist.bytecode.annotation.AnnotationMemberValue) ArrayMemberValue(javassist.bytecode.annotation.ArrayMemberValue) BooleanMemberValue(javassist.bytecode.annotation.BooleanMemberValue) StringMemberValue(javassist.bytecode.annotation.StringMemberValue) AnnotationMemberValue(javassist.bytecode.annotation.AnnotationMemberValue) ArrayMemberValue(javassist.bytecode.annotation.ArrayMemberValue)

Example 12 with Annotation

use of javassist.bytecode.annotation.Annotation 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)

Example 13 with Annotation

use of javassist.bytecode.annotation.Annotation in project audit4j-core by audit4j.

the class AnnotationDB method populate.

protected void populate(Annotation[] annotations, String className) {
    if (annotations == null)
        return;
    Set<String> classAnnotations = classIndex.get(className);
    for (Annotation ann : annotations) {
        Set<String> classes = annotationIndex.get(ann.getTypeName());
        if (classes == null) {
            classes = new HashSet<String>();
            annotationIndex.put(ann.getTypeName(), classes);
        }
        classes.add(className);
        classAnnotations.add(ann.getTypeName());
    }
}
Also used : Annotation(javassist.bytecode.annotation.Annotation)

Example 14 with Annotation

use of javassist.bytecode.annotation.Annotation in project coprhd-controller by CoprHD.

the class DbSchemaChanger method addAnnotation.

/**
 * add an annotation to a method
 *
 * @param methodName the method to which the annotation to be added
 * @param annotationName the annotation name, it should be a full name
 * @param values the attributes of the annotation
 */
public DbSchemaChanger addAnnotation(String methodName, String annotationName, Map<String, Object> values) throws Exception {
    // looking for the method to apply the annotation on
    CtMethod methodDescriptor = cc.getDeclaredMethod(methodName);
    // create the annotation
    ClassFile ccFile = cc.getClassFile();
    ccFile.setVersionToJava5();
    ConstPool constpool = ccFile.getConstPool();
    MethodInfo minfo = methodDescriptor.getMethodInfo();
    AnnotationsAttribute attr = (AnnotationsAttribute) minfo.getAttribute(AnnotationsAttribute.visibleTag);
    if (attr == null) {
        attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
    }
    Annotation annot = new Annotation(annotationName, constpool);
    Set<Map.Entry<String, Object>> entries = values.entrySet();
    for (Map.Entry<String, Object> entry : entries) {
        String attrName = entry.getKey();
        Object attrValue = entry.getValue();
        if (attrValue instanceof String) {
            annot.addMemberValue(attrName, new StringMemberValue((String) attrValue, ccFile.getConstPool()));
        } else {
            throw new RuntimeException(String.format("Unsupported attribute type %s of %s", attrName, attrValue));
        }
    }
    attr.addAnnotation(annot);
    // add the annotation to the method descriptor
    minfo.addAttribute(attr);
    log.info("add {} to method {}", attr, methodDescriptor);
    return this;
}
Also used : ConstPool(javassist.bytecode.ConstPool) ClassFile(javassist.bytecode.ClassFile) StringMemberValue(javassist.bytecode.annotation.StringMemberValue) AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute) Annotation(javassist.bytecode.annotation.Annotation) MethodInfo(javassist.bytecode.MethodInfo) Map(java.util.Map) CtMethod(javassist.CtMethod)

Example 15 with Annotation

use of javassist.bytecode.annotation.Annotation in project hibernate-orm by hibernate.

the class FieldWriter method addAnnotations.

/* --- */
private static void addAnnotations(FieldInfo fieldInfo, Class<?>[] annotations) {
    AnnotationsAttribute annotationsAttribute = (AnnotationsAttribute) fieldInfo.getAttribute(AnnotationsAttribute.visibleTag);
    if (annotationsAttribute == null) {
        annotationsAttribute = new AnnotationsAttribute(fieldInfo.getConstPool(), AnnotationsAttribute.visibleTag);
        fieldInfo.addAttribute(annotationsAttribute);
    }
    for (Class<?> annotation : annotations) {
        annotationsAttribute.addAnnotation(new Annotation(annotation.getName(), fieldInfo.getConstPool()));
    }
}
Also used : AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute) Annotation(javassist.bytecode.annotation.Annotation)

Aggregations

Annotation (javassist.bytecode.annotation.Annotation)26 AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)19 StringMemberValue (javassist.bytecode.annotation.StringMemberValue)12 ConstPool (javassist.bytecode.ConstPool)11 ClassFile (javassist.bytecode.ClassFile)9 ArrayList (java.util.ArrayList)7 CtClass (javassist.CtClass)7 ClassPool (javassist.ClassPool)6 CtMethod (javassist.CtMethod)5 AnnotationMemberValue (javassist.bytecode.annotation.AnnotationMemberValue)5 ArrayMemberValue (javassist.bytecode.annotation.ArrayMemberValue)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)4 MethodInfo (javassist.bytecode.MethodInfo)4 EnumMemberValue (javassist.bytecode.annotation.EnumMemberValue)4 MemberValue (javassist.bytecode.annotation.MemberValue)4 DataInputStream (java.io.DataInputStream)3 HashSet (java.util.HashSet)3 CtField (javassist.CtField)3 ClassMemberValue (javassist.bytecode.annotation.ClassMemberValue)3