Search in sources :

Example 16 with ClassFile

use of javassist.bytecode.ClassFile in project BroadleafCommerce by BroadleafCommerce.

the class AlterTableNameClassTransformer method transform.

@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
    // Lambdas and anonymous methods in Java 8 do not have a class name defined and so no transformation should be done
    if (className == null || StringUtils.isBlank(getTargetedClass()) || StringUtils.isBlank(getTableName())) {
        return null;
    }
    byte[] classBytes = null;
    String convertedClassName = className.replace('/', '.');
    if (convertedClassName.equalsIgnoreCase(getTargetedClass())) {
        try {
            String targetValue = getTableName();
            if (LOG.isDebugEnabled()) {
                LOG.debug("Altering " + convertedClassName + " table name");
            }
            ClassFile classFile = new ClassFile(new DataInputStream(new ByteArrayInputStream(classfileBuffer)));
            ConstPool constantPool = classFile.getConstPool();
            alterTableAnnotation(classFile, targetValue, constantPool);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            DataOutputStream os = new DataOutputStream(bos);
            classFile.write(os);
            os.close();
            classBytes = bos.toByteArray();
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new IllegalClassFormatException("Unable to convert " + convertedClassName + " to a SingleTable inheritance strategy: " + ex.getMessage());
        }
    }
    return classBytes;
}
Also used : ConstPool(javassist.bytecode.ConstPool) ClassFile(javassist.bytecode.ClassFile) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException) NotFoundException(javassist.NotFoundException)

Example 17 with ClassFile

use of javassist.bytecode.ClassFile in project BroadleafCommerce by BroadleafCommerce.

the class EntityMarkerClassTransformer method transform.

@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
    // Lambdas and anonymous methods in Java 8 do not have a class name defined and so no transformation should be done
    if (className == null) {
        return null;
    }
    String convertedClassName = className.replace('/', '.');
    if (isIgnored(convertedClassName)) {
        return null;
    }
    try {
        ClassFile classFile = new ClassFile(new DataInputStream(new ByteArrayInputStream(classfileBuffer)));
        List<?> attributes = classFile.getAttributes();
        Iterator<?> itr = attributes.iterator();
        while (itr.hasNext()) {
            Object object = itr.next();
            if (AnnotationsAttribute.class.isAssignableFrom(object.getClass())) {
                boolean containsTypeLevelAnnotation = containsTypeLevelPersistenceAnnotation(((AnnotationsAttribute) object).getAnnotations());
                if (containsTypeLevelAnnotation) {
                    LOG.debug("Marking " + convertedClassName + " as transformed");
                    transformedEntityClassNames.add(convertedClassName);
                } else {
                    LOG.debug("Marking " + convertedClassName + " as picked up by the transformer but not detected as an entity");
                    transformedNonEntityClassNames.add(convertedClassName);
                }
            }
        }
    } catch (Exception e) {
        LOG.error(e);
        throw new IllegalClassFormatException("Unable to mark " + convertedClassName + " as transformed.");
    }
    // We don't need to transform anything, so we'll return null
    return null;
}
Also used : ClassFile(javassist.bytecode.ClassFile) ByteArrayInputStream(java.io.ByteArrayInputStream) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException) DataInputStream(java.io.DataInputStream) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException)

Example 18 with ClassFile

use of javassist.bytecode.ClassFile in project BroadleafCommerce by BroadleafCommerce.

the class MaterializedClobTypeClassTransformer method transform.

@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
    if (className == null) {
        return null;
    }
    String convertedClassName = className.replace('/', '.');
    if (isIgnored(convertedClassName)) {
        return null;
    }
    try {
        boolean transformed = false;
        ClassFile classFile = new ClassFile(new DataInputStream(new ByteArrayInputStream(classfileBuffer)));
        boolean containsTypeLevelAnnotation = false;
        {
            List<?> attributes = classFile.getAttributes();
            Iterator<?> itr = attributes.iterator();
            while (itr.hasNext()) {
                Object object = itr.next();
                if (AnnotationsAttribute.class.isAssignableFrom(object.getClass())) {
                    containsTypeLevelAnnotation = containsTypeLevelPersistenceAnnotation(((AnnotationsAttribute) object).getAnnotations());
                }
            }
        }
        if (containsTypeLevelAnnotation) {
            List<FieldInfo> fieldInfos = classFile.getFields();
            ConstPool constantPool = classFile.getConstPool();
            for (FieldInfo myField : fieldInfos) {
                List<?> attributes = myField.getAttributes();
                Iterator<?> itr = attributes.iterator();
                AnnotationsAttribute annotationsAttribute = new AnnotationsAttribute(constantPool, AnnotationsAttribute.visibleTag);
                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(Type.class.getName())) {
                                StringMemberValue annot = (StringMemberValue) annotation.getMemberValue("type");
                                if (annot != null && annot.getValue().equals(StringClobType.class.getName())) {
                                    Annotation clobType = new Annotation(Type.class.getName(), constantPool);
                                    StringMemberValue type = new StringMemberValue(constantPool);
                                    type.setValue(MaterializedClobType.class.getName());
                                    clobType.addMemberValue("type", type);
                                    annotationsAttribute.addAnnotation(clobType);
                                    transformed = true;
                                } else {
                                    annotationsAttribute.addAnnotation(annotation);
                                }
                            } else {
                                annotationsAttribute.addAnnotation(annotation);
                            }
                        }
                        if (transformed) {
                            itr.remove();
                        }
                    }
                }
                if (transformed) {
                    myField.addAttribute(annotationsAttribute);
                }
            }
        }
        if (transformed) {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            DataOutputStream os = new DataOutputStream(bos);
            classFile.write(os);
            os.close();
            return bos.toByteArray();
        } else {
            return null;
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new IllegalClassFormatException("Unable to convert " + convertedClassName + " to sandbox: " + ex.getMessage());
    }
}
Also used : ConstPool(javassist.bytecode.ConstPool) ClassFile(javassist.bytecode.ClassFile) StringMemberValue(javassist.bytecode.annotation.StringMemberValue) DataOutputStream(java.io.DataOutputStream) AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) Annotation(javassist.bytecode.annotation.Annotation) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException) MaterializedClobType(org.hibernate.type.MaterializedClobType) Type(org.hibernate.annotations.Type) StringClobType(org.hibernate.type.StringClobType) ByteArrayInputStream(java.io.ByteArrayInputStream) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException) Iterator(java.util.Iterator) MaterializedClobType(org.hibernate.type.MaterializedClobType) ArrayList(java.util.ArrayList) List(java.util.List) FieldInfo(javassist.bytecode.FieldInfo)

Example 19 with ClassFile

use of javassist.bytecode.ClassFile in project BroadleafCommerce by BroadleafCommerce.

the class SingleTableInheritanceClassTransformer method transform.

@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
    // Lambdas and anonymous methods in Java 8 do not have a class name defined and so no transformation should be done
    if (className == null) {
        return null;
    }
    if (infos.isEmpty()) {
        return null;
    }
    String convertedClassName = className.replace('/', '.');
    SingleTableInheritanceInfo key = new SingleTableInheritanceInfo();
    key.setClassName(convertedClassName);
    int pos = infos.indexOf(key);
    if (pos >= 0) {
        try {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Converting " + convertedClassName + " to a SingleTable inheritance strategy.");
            }
            SingleTableInheritanceInfo myInfo = infos.get(pos);
            ClassFile classFile = new ClassFile(new DataInputStream(new ByteArrayInputStream(classfileBuffer)));
            ConstPool constantPool = classFile.getConstPool();
            AnnotationsAttribute annotationsAttribute = new AnnotationsAttribute(constantPool, AnnotationsAttribute.visibleTag);
            List<?> attributes = classFile.getAttributes();
            Iterator<?> itr = attributes.iterator();
            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(Inheritance.class.getName())) {
                            annotationsAttribute.addAnnotation(annotation);
                        }
                    }
                    itr.remove();
                }
            }
            Annotation inheritance = new Annotation(Inheritance.class.getName(), constantPool);
            ClassPool pool = ClassPool.getDefault();
            pool.importPackage("javax.persistence");
            pool.importPackage("java.lang");
            EnumMemberValue strategy = (EnumMemberValue) Annotation.createMemberValue(constantPool, pool.makeClass("InheritanceType"));
            strategy.setType(InheritanceType.class.getName());
            strategy.setValue(InheritanceType.SINGLE_TABLE.name());
            inheritance.addMemberValue("strategy", strategy);
            annotationsAttribute.addAnnotation(inheritance);
            if (myInfo.getDiscriminatorName() != null) {
                Annotation discriminator = new Annotation(DiscriminatorColumn.class.getName(), constantPool);
                StringMemberValue name = new StringMemberValue(constantPool);
                name.setValue(myInfo.getDiscriminatorName());
                discriminator.addMemberValue("name", name);
                EnumMemberValue discriminatorType = (EnumMemberValue) Annotation.createMemberValue(constantPool, pool.makeClass("DiscriminatorType"));
                discriminatorType.setType(DiscriminatorType.class.getName());
                discriminatorType.setValue(myInfo.getDiscriminatorType().name());
                discriminator.addMemberValue("discriminatorType", discriminatorType);
                IntegerMemberValue length = new IntegerMemberValue(constantPool);
                length.setValue(myInfo.getDiscriminatorLength());
                discriminator.addMemberValue("length", length);
                annotationsAttribute.addAnnotation(discriminator);
            }
            classFile.addAttribute(annotationsAttribute);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            DataOutputStream os = new DataOutputStream(bos);
            classFile.write(os);
            os.close();
            return bos.toByteArray();
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new IllegalClassFormatException("Unable to convert " + convertedClassName + " to a SingleTable inheritance strategy: " + ex.getMessage());
        }
    } else {
        return null;
    }
}
Also used : EnumMemberValue(javassist.bytecode.annotation.EnumMemberValue) ConstPool(javassist.bytecode.ConstPool) StringMemberValue(javassist.bytecode.annotation.StringMemberValue) DataOutputStream(java.io.DataOutputStream) Inheritance(javax.persistence.Inheritance) ClassPool(javassist.ClassPool) InheritanceType(javax.persistence.InheritanceType) IntegerMemberValue(javassist.bytecode.annotation.IntegerMemberValue) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException) ClassFile(javassist.bytecode.ClassFile) AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) Annotation(javassist.bytecode.annotation.Annotation) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException) DiscriminatorType(javax.persistence.DiscriminatorType) ByteArrayInputStream(java.io.ByteArrayInputStream) DiscriminatorColumn(javax.persistence.DiscriminatorColumn)

Example 20 with ClassFile

use of javassist.bytecode.ClassFile in project BroadleafCommerce by BroadleafCommerce.

the class RemoveAnnotationClassTransformer method transform.

@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
    // Lambdas and anonymous methods in Java 8 do not have a class name defined and so no transformation should be done
    if (className == null) {
        return null;
    }
    // Be careful with Apache library usage in this class (e.g. ArrayUtils). Usage will likely cause a ClassCircularityError
    // under JRebel. Favor not including outside libraries and unnecessary classes.
    CtClass clazz = null;
    try {
        String convertedClassName = className.replace('/', '.');
        if (!classNames.isEmpty() && classNames.contains(convertedClassName) && (conditionalPropertyName == null || isPropertyEnabled(conditionalPropertyName))) {
            ClassPool classPool = ClassPool.getDefault();
            clazz = classPool.makeClass(new ByteArrayInputStream(classfileBuffer), false);
            clazz.defrost();
            ClassFile classFile = clazz.getClassFile();
            ConstPool constantPool = classFile.getConstPool();
            {
                List<?> attributes = classFile.getAttributes();
                AnnotationsAttribute annotationsAttribute = stripAnnotation(constantPool, attributes);
                classFile.addAttribute(annotationsAttribute);
            }
            {
                List<FieldInfo> fieldInfos = classFile.getFields();
                for (FieldInfo myField : fieldInfos) {
                    List<?> attributes = myField.getAttributes();
                    AnnotationsAttribute annotationsAttribute = stripAnnotation(constantPool, attributes);
                    myField.addAttribute(annotationsAttribute);
                }
            }
            return clazz.toBytecode();
        }
    } catch (ClassCircularityError error) {
        error.printStackTrace();
        throw error;
    } catch (Exception e) {
        throw new RuntimeException("Unable to transform class", e);
    } finally {
        if (clazz != null) {
            try {
                clazz.detach();
            } catch (Exception e) {
            // do nothing
            }
        }
    }
    return null;
}
Also used : CtClass(javassist.CtClass) ConstPool(javassist.bytecode.ConstPool) ClassFile(javassist.bytecode.ClassFile) ByteArrayInputStream(java.io.ByteArrayInputStream) AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute) ClassPool(javassist.ClassPool) ArrayList(java.util.ArrayList) List(java.util.List) FieldInfo(javassist.bytecode.FieldInfo) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException) BeansException(org.springframework.beans.BeansException)

Aggregations

ClassFile (javassist.bytecode.ClassFile)51 DataInputStream (java.io.DataInputStream)15 ClassPool (javassist.ClassPool)15 DataOutputStream (java.io.DataOutputStream)14 CtClass (javassist.CtClass)14 AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)14 ConstPool (javassist.bytecode.ConstPool)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 ByteArrayInputStream (java.io.ByteArrayInputStream)11 IOException (java.io.IOException)11 MethodInfo (javassist.bytecode.MethodInfo)11 CtField (javassist.CtField)10 NotFoundException (javassist.NotFoundException)9 Annotation (javassist.bytecode.annotation.Annotation)9 Test (org.junit.Test)9 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)8 HashSet (java.util.HashSet)7 CtMethod (javassist.CtMethod)7 FieldInfo (javassist.bytecode.FieldInfo)7 Bytecode (javassist.bytecode.Bytecode)5