Search in sources :

Example 71 with CtClass

use of javassist.CtClass 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)

Example 72 with CtClass

use of javassist.CtClass in project ideaagent by mrshawnho.

the class IdeaTransformer method transform.

@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
    if (className == null || (!className.startsWith("com/a/") && !className.startsWith("com/seventh7/"))) {
        return null;
    }
    try {
        if ("com/a/a/a/p".equals(className)) {
            count.incrementAndGet();
        } else if ("com/seventh7/widget/iedis/T".equals(className)) {
            iedis = true;
        } else if ("com/a/b/h/a/b".equals(className)) {
            mybatis = true;
        }
        CtClass ctClass = pool.makeClass(new ByteArrayInputStream(classfileBuffer), false);
        ClassFile classFile = ctClass.getClassFile();
        ConstPool constPool = classFile.getConstPool();
        List methods = classFile.getMethods();
        for (Object method : methods) {
            MethodInfo methodInfo = (MethodInfo) method;
            CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
            if (codeAttribute == null)
                continue;
            CodeIterator iter = codeAttribute.iterator();
            if (methodInfo.getName().equals("<init>")) {
                enableAgent(constPool, methodInfo, iter);
            }
            modifyUrl(constPool, methodInfo, iter);
            modifyCert(constPool, methodInfo, iter);
            modifyResult(constPool, methodInfo, iter);
            if ("com/a/a/a/p".equals(className)) {
                if (count.get() == 1) {
                    if (iedis && !mybatis) {
                        modifyIedis(constPool, methodInfo, iter);
                    } else if (!iedis && mybatis) {
                        modifyMybatis(constPool, methodInfo, iter);
                    } else if (iedis && mybatis) {
                        modifyIedis(constPool, methodInfo, iter);
                    }
                } else if (count.get() == 2) {
                    modifyMybatis(constPool, methodInfo, iter);
                }
            }
        }
        return ctClass.toBytecode();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : CtClass(javassist.CtClass) ByteArrayInputStream(java.io.ByteArrayInputStream) List(java.util.List) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException)

Example 73 with CtClass

use of javassist.CtClass in project javassist-maven-plugin by icon-Systemhaus-GmbH.

the class TestJavassistTransformerExecuter method hasStamp_returns_false_if_NotFoundException_is_thrown.

@Test
public void hasStamp_returns_false_if_NotFoundException_is_thrown() throws NotFoundException {
    // given
    final CtClass ctClass = mock("ctClass", CtClass.class);
    final NotFoundException internalException = new NotFoundException("expected exception");
    expect(ctClass.getDeclaredField(startsWith(STAMP_FIELD_NAME))).andThrow(internalException);
    replay(ctClass);
    // when
    assertThat(sut.hasStamp(ctClass), is(false));
    // then
    verify(ctClass);
}
Also used : CtClass(javassist.CtClass) NotFoundException(javassist.NotFoundException) Test(org.junit.Test)

Example 74 with CtClass

use of javassist.CtClass in project javassist-maven-plugin by icon-Systemhaus-GmbH.

the class TestJavassistTransformerExecuter method applyStamp_on_Interface.

@Test
public void applyStamp_on_Interface() throws CannotCompileException, NotFoundException {
    // given
    final String className = "test.TestClass";
    final CtClass candidateClass = mock("candidateClass", CtClass.class);
    final Capture<CtField> fieldCaptures = newInstance();
    // createStampField
    expect(candidateClass.isInterface()).andReturn(true);
    expect(candidateClass.getClassPool()).andReturn(classPool).anyTimes();
    expect(candidateClass.getClassFile2()).andReturn(new ClassFile(false, className, null));
    expect(candidateClass.isFrozen()).andReturn(false);
    expect(candidateClass.getName()).andReturn(className).anyTimes();
    candidateClass.addField(capture(fieldCaptures), anyObject(CtField.Initializer.class));
    replay(candidateClass, classPool);
    // when
    sut.applyStamp(candidateClass);
    // then
    verify(candidateClass, classPool);
    assertThat("generated stamp field starts with the constant prefix.", fieldCaptures.getValue().getName(), org.hamcrest.core.StringStartsWith.startsWith(JavassistTransformerExecutor.STAMP_FIELD_NAME));
    assertThat("generated stamp field must have the right modifiers.", fieldCaptures.getValue().getModifiers(), is(STATIC | FINAL | PUBLIC));
    assertThat("generated stamp field is a boolean.", fieldCaptures.getValue().getType(), is(booleanType));
}
Also used : CtClass(javassist.CtClass) ClassFile(javassist.bytecode.ClassFile) CtField(javassist.CtField) Test(org.junit.Test)

Example 75 with CtClass

use of javassist.CtClass in project javassist-maven-plugin by icon-Systemhaus-GmbH.

the class TestJavassistTransformerExecutor_TransformerClasses_handling method applyTransformation_if_shouldTransform_returns_true_and_class_has_no_stamp.

@Test
public void applyTransformation_if_shouldTransform_returns_true_and_class_has_no_stamp() throws Exception {
    // given
    final String className = oneTestClass();
    final Capture<CtClass> classCapture_shouldTransform = newInstance();
    final Capture<CtClass> classCapture_applyTransformations = newInstance();
    final IClassTransformer mockTransformer = createMock("mockTransformer", IClassTransformer.class);
    expect(mockTransformer.shouldTransform(capture(classCapture_shouldTransform))).andReturn(true);
    mockTransformer.applyTransformations(capture(classCapture_applyTransformations));
    expectLastCall().times(1);
    replay(mockTransformer);
    sut.setTransformerClasses(mockTransformer);
    // when
    sut.execute();
    // then
    verify(mockTransformer);
    assertEquals(className, classCapture_shouldTransform.getValue().getName());
    assertEquals(className, classCapture_applyTransformations.getValue().getName());
}
Also used : CtClass(javassist.CtClass) IClassTransformer(javassist.build.IClassTransformer) Test(org.junit.Test)

Aggregations

CtClass (javassist.CtClass)271 CtMethod (javassist.CtMethod)96 ClassPool (javassist.ClassPool)93 NotFoundException (javassist.NotFoundException)85 Test (org.junit.Test)63 CannotCompileException (javassist.CannotCompileException)62 CtField (javassist.CtField)53 IOException (java.io.IOException)35 CtConstructor (javassist.CtConstructor)26 Method (java.lang.reflect.Method)17 LoaderClassPath (javassist.LoaderClassPath)16 ClassFile (javassist.bytecode.ClassFile)14 ArrayList (java.util.ArrayList)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)11 ConstPool (javassist.bytecode.ConstPool)11 File (java.io.File)8 FileNotFoundException (java.io.FileNotFoundException)8 Collectors (java.util.stream.Collectors)8 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)7