Search in sources :

Example 1 with AnnotationsAttribute

use of javassist.bytecode.AnnotationsAttribute in project powermock by powermock.

the class TestClassTransformer method removeTestMethodAnnotationFrom.

private void removeTestMethodAnnotationFrom(CtMethod m) throws ClassNotFoundException {
    final AnnotationsAttribute attr = (AnnotationsAttribute) m.getMethodInfo().getAttribute(AnnotationsAttribute.visibleTag);
    javassist.bytecode.annotation.Annotation[] newAnnotations = new javassist.bytecode.annotation.Annotation[attr.numAnnotations() - 1];
    int i = -1;
    for (javassist.bytecode.annotation.Annotation a : attr.getAnnotations()) {
        if (a.getTypeName().equals(testMethodAnnotationType.getName())) {
            continue;
        }
        newAnnotations[++i] = a;
    }
    attr.setAnnotations(newAnnotations);
}
Also used : AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute) Annotation(java.lang.annotation.Annotation)

Example 2 with AnnotationsAttribute

use of javassist.bytecode.AnnotationsAttribute 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 3 with AnnotationsAttribute

use of javassist.bytecode.AnnotationsAttribute in project gwt-test-utils by gwt-test-utils.

the class JavassistUtils method getInvisibleAnnotationStringValue.

/**
     * Retrieve the String value of an annotation which is not available at runtime.
     *
     * @param clazz      The annotated class
     * @param annotation The annotation which is not visible at runtime
     * @param name       The name of the String property of the annotation to retrieve
     * @return The String value of the annotation or null if the annotation or its property is not
     * present
     */
public static String getInvisibleAnnotationStringValue(Class<?> clazz, Class<? extends Annotation> annotation, String name) {
    CtClass ctClass = GwtClassPool.getCtClass(clazz);
    ctClass.defrost();
    AnnotationsAttribute attr = (AnnotationsAttribute) ctClass.getClassFile().getAttribute(AnnotationsAttribute.visibleTag);
    if (attr == null) {
        attr = (AnnotationsAttribute) ctClass.getClassFile().getAttribute(AnnotationsAttribute.invisibleTag);
    }
    if (attr == null) {
        return null;
    }
    javassist.bytecode.annotation.Annotation an = attr.getAnnotation(annotation.getName());
    ctClass.freeze();
    return an != null ? ((StringMemberValue) an.getMemberValue(name)).getValue() : null;
}
Also used : CtClass(javassist.CtClass) AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute)

Example 4 with AnnotationsAttribute

use of javassist.bytecode.AnnotationsAttribute in project gwt-test-utils by gwt-test-utils.

the class JavassistUtils method getInvisibleAnnotationStringValue.

/**
     * Retrieve the String value of an annotation which is not available at runtime.
     *
     * @param method     The annotated method
     * @param annotation The annotation which is not visible at runtime
     * @param name       The name of the String property of the annotation to retrieve
     * @return The String value of the annotation or null if the annotation or its property is not
     * present
     */
public static String getInvisibleAnnotationStringValue(Method method, Class<? extends Annotation> annotation, String name) {
    CtClass ctClass = GwtClassPool.getCtClass(method.getDeclaringClass());
    ctClass.defrost();
    AnnotationsAttribute attr = (AnnotationsAttribute) ctClass.getClassFile().getMethod(method.getName()).getAttribute(AnnotationsAttribute.visibleTag);
    if (attr == null) {
        attr = (AnnotationsAttribute) ctClass.getClassFile().getMethod(method.getName()).getAttribute(AnnotationsAttribute.invisibleTag);
    }
    if (attr == null) {
        return null;
    }
    javassist.bytecode.annotation.Annotation an = attr.getAnnotation(annotation.getName());
    ctClass.freeze();
    return an != null ? ((StringMemberValue) an.getMemberValue(name)).getValue() : null;
}
Also used : CtClass(javassist.CtClass) AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute)

Example 5 with AnnotationsAttribute

use of javassist.bytecode.AnnotationsAttribute in project hibernate-orm by hibernate.

the class ClassFileArchiveEntryHandler method toClassDescriptor.

private ClassDescriptor toClassDescriptor(ClassFile classFile, ArchiveEntry entry) {
    ClassDescriptor.Categorization categorization = ClassDescriptor.Categorization.OTHER;
    ;
    final AnnotationsAttribute visibleAnnotations = (AnnotationsAttribute) classFile.getAttribute(AnnotationsAttribute.visibleTag);
    if (visibleAnnotations != null) {
        if (visibleAnnotations.getAnnotation(Entity.class.getName()) != null || visibleAnnotations.getAnnotation(MappedSuperclass.class.getName()) != null || visibleAnnotations.getAnnotation(Embeddable.class.getName()) != null) {
            categorization = ClassDescriptor.Categorization.MODEL;
        } else if (visibleAnnotations.getAnnotation(Converter.class.getName()) != null) {
            categorization = ClassDescriptor.Categorization.CONVERTER;
        }
    }
    return new ClassDescriptorImpl(classFile.getName(), categorization, entry.getStreamAccess());
}
Also used : AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute) ClassDescriptorImpl(org.hibernate.boot.archive.scan.internal.ClassDescriptorImpl) Converter(javax.persistence.Converter) Embeddable(javax.persistence.Embeddable)

Aggregations

AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)7 CtClass (javassist.CtClass)4 Annotation (java.lang.annotation.Annotation)2 Annotation (javassist.bytecode.annotation.Annotation)2 EnumMemberValue (javassist.bytecode.annotation.EnumMemberValue)2 StringMemberValue (javassist.bytecode.annotation.StringMemberValue)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 ClassPool (javassist.ClassPool)1 CtField (javassist.CtField)1 ClassFile (javassist.bytecode.ClassFile)1 ConstPool (javassist.bytecode.ConstPool)1 ArrayMemberValue (javassist.bytecode.annotation.ArrayMemberValue)1 BooleanMemberValue (javassist.bytecode.annotation.BooleanMemberValue)1 ByteMemberValue (javassist.bytecode.annotation.ByteMemberValue)1 CharMemberValue (javassist.bytecode.annotation.CharMemberValue)1 ClassMemberValue (javassist.bytecode.annotation.ClassMemberValue)1 DoubleMemberValue (javassist.bytecode.annotation.DoubleMemberValue)1 FloatMemberValue (javassist.bytecode.annotation.FloatMemberValue)1 IntegerMemberValue (javassist.bytecode.annotation.IntegerMemberValue)1