Example usage for java.lang.instrument IllegalClassFormatException IllegalClassFormatException

List of usage examples for java.lang.instrument IllegalClassFormatException IllegalClassFormatException

Introduction

In this page you can find the example usage for java.lang.instrument IllegalClassFormatException IllegalClassFormatException.

Prototype

public IllegalClassFormatException(String s) 

Source Link

Document

Constructs an IllegalClassFormatException with the specified detail message.

Usage

From source file:com.ejisto.core.classloading.ClassTransformerImpl.java

private byte[] transform(String className, byte[] classFileBuffer, List<MockedField> mockedFields)
        throws IllegalClassFormatException {
    try {//from   w  w w.jav  a  2  s .  c o m
        trace("retrieving " + className + " from pool");
        ClassPool classPool = getClassPool();
        CtClass clazz;
        if (classFileBuffer == null) {
            clazz = classPool.get(getCanonicalClassName(className));
        } else {
            trace("class file bytes already loaded. Reusing it...");
            clazz = classPool.makeClass(new ByteArrayInputStream(classFileBuffer));
        }
        trace("instrumenting " + className);
        clazz.instrument(new ObjectEditor(new EjistoMethodFilter(contextPath, mockedFields)));
        trace("removing final modifier (if present)");
        removeFinalModifier(clazz);
        trace("adding default constructor, if none present ");
        addDefaultConstructor(clazz);
        trace("done. Returning bytecode");
        clazz.rebuildClassFile();
        byte[] result = clazz.toBytecode();
        clazz.detach();
        return result;
    } catch (Exception e) {
        LOGGER.error("error during transformation of class " + className, e);
        throw new IllegalClassFormatException(e.getMessage());
    }
}

From source file:org.broadleafcommerce.common.extensibility.jpa.convert.EntityMarkerClassTransformer.java

@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;
    }/*w  ww.  j a  v a  2s.com*/

    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;
}

From source file:org.broadleafcommerce.common.extensibility.jpa.convert.inheritance.SingleTableInheritanceClassTransformer.java

@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;
    }/*w  ww . ja  va  2  s.c  om*/

    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;
    }
}