Search in sources :

Example 86 with CtClass

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

the class TestJavassistTransformerExecutor_transform_with_nested_classes method do_nothing_if_nestedClass_is_not_modified.

@Test
public void do_nothing_if_nestedClass_is_not_modified() throws Exception {
    // given
    final String className = oneTestClass();
    final CtClass nestedClass = mock("nestedClass", CtClass.class);
    expect(nestedClass.isModified()).andReturn(false);
    final Iterator<String> classNames = classNames(className);
    final CtClass candidateClass = configureCandidateClassFoTransformation(className, stampedClassWithNestedClasses(className, nestedClass));
    replay(nestedClass, candidateClass, classNames, this.classPool, this.classTransformer);
    // when
    executeTransform(classNames);
    // then
    verify(nestedClass, candidateClass, classNames, this.classPool, this.classTransformer);
}
Also used : CtClass(javassist.CtClass) Test(org.junit.Test)

Example 87 with CtClass

use of javassist.CtClass in project drill by axbaretto.

the class GuavaPatcher method patchCloseables.

private static void patchCloseables() throws Exception {
    ClassPool cp = ClassPool.getDefault();
    CtClass cc = cp.get("com.google.common.io.Closeables");
    // Add back the Closeables.closeQuietly() method for old consumers.
    CtMethod newmethod = CtNewMethod.make("public static void closeQuietly(java.io.Closeable closeable) { try{closeable.close();}catch(Exception e){} }", cc);
    cc.addMethod(newmethod);
    // Load the modified class instead of the original.
    cc.toClass();
    logger.info("Google's Closeables patched for old HBase Guava version.");
}
Also used : CtClass(javassist.CtClass) ClassPool(javassist.ClassPool) CtMethod(javassist.CtMethod)

Example 88 with CtClass

use of javassist.CtClass in project drill by apache.

the class GuavaPatcher method patchCloseables.

private static void patchCloseables() {
    try {
        ClassPool cp = getClassPool();
        CtClass cc = cp.get("com.google.common.io.Closeables");
        // Add back the Closeables.closeQuietly() method for old consumers.
        CtMethod newMethod = CtNewMethod.make("public static void closeQuietly(java.io.Closeable closeable) { try{closeable.close();}catch(Exception e){} }", cc);
        cc.addMethod(newMethod);
        // Load the modified class instead of the original.
        cc.toClass();
        logger.info("Google's Closeables patched for old HBase Guava version.");
    } catch (Exception e) {
        logUnableToPatchException(e);
    }
}
Also used : CtClass(javassist.CtClass) ClassPool(javassist.ClassPool) CtMethod(javassist.CtMethod)

Example 89 with CtClass

use of javassist.CtClass in project drill by apache.

the class ProtobufPatcher method patchGeneratedMessageLiteBuilder.

/**
 * MapR-DB client extends {@link com.google.protobuf.GeneratedMessageLite.Builder} and overrides some methods,
 * that were made final in version 3.6+ of protobuf.
 * This method removes the final modifiers.
 * Also, adding back a default constructor that was removed.
 */
private static void patchGeneratedMessageLiteBuilder() {
    try {
        ClassPool classPool = getClassPool();
        CtClass builder = classPool.get(protobufPackage + "GeneratedMessageLite$Builder");
        removeFinal(builder.getDeclaredMethod("isInitialized"));
        removeFinal(builder.getDeclaredMethod("clear"));
        builder.addConstructor(CtNewConstructor.defaultConstructor(builder));
        builder.toClass();
    } catch (Exception e) {
        logger.warn("Unable to patch Protobuf.", e);
    }
}
Also used : CtClass(javassist.CtClass) ClassPool(javassist.ClassPool) CannotCompileException(javassist.CannotCompileException)

Example 90 with CtClass

use of javassist.CtClass in project drill by apache.

the class ProtobufPatcher method patchGeneratedMessageLite.

/**
 * MapR-DB client extends {@link com.google.protobuf.GeneratedMessageLite} and overrides some methods,
 * that were made final in version 3.6+ of protobuf.
 * This method removes the final modifiers.
 */
private static void patchGeneratedMessageLite() {
    try {
        ClassPool classPool = getClassPool();
        CtClass generatedMessageLite = classPool.get(protobufPackage + "GeneratedMessageLite");
        removeFinal(generatedMessageLite.getDeclaredMethod("getParserForType"));
        removeFinal(generatedMessageLite.getDeclaredMethod("isInitialized"));
        // The method was removed, but it is used in com.mapr.fs.proto.Dbserver.
        // Adding it back.
        generatedMessageLite.addMethod(CtNewMethod.make("protected void makeExtensionsImmutable() { }", generatedMessageLite));
        // A constructor with this signature was removed. Adding it back.
        String className = protobufPackage + "GeneratedMessageLite.Builder";
        generatedMessageLite.addConstructor(CtNewConstructor.make("protected GeneratedMessageLite(" + className + " builder) { }", generatedMessageLite));
        // This single method was added instead of several abstract methods.
        // MapR-DB client doesn't use it, but it was added in overridden equals() method.
        // Adding default implementation.
        CtMethod dynamicMethod = generatedMessageLite.getDeclaredMethod("dynamicMethod", new CtClass[] { classPool.get(protobufPackage + "GeneratedMessageLite$MethodToInvoke"), classPool.get("java.lang.Object"), classPool.get("java.lang.Object") });
        className = protobufPackage + "GeneratedMessageLite.MethodToInvoke";
        String dynamicMethodBody = MessageFormat.format("if ($1.equals({0}.GET_DEFAULT_INSTANCE)) '{'" + "  return this;" + "'}' else if ($1.equals({0}.BUILD_MESSAGE_INFO)) '{' " + "  {1}StructuralMessageInfo.Builder builder = {1}StructuralMessageInfo.newBuilder();" + "  builder.withSyntax({1}ProtoSyntax.PROTO2);" + "  builder.withDefaultInstance(this);" + "  return builder.build();" + "'}' else '{'" + "  return null;" + "'}'", className, protobufPackage);
        addImplementation(dynamicMethod, dynamicMethodBody);
        generatedMessageLite.toClass();
    } catch (Exception e) {
        logger.warn("Unable to patch Protobuf.", e);
    }
}
Also used : CtClass(javassist.CtClass) ClassPool(javassist.ClassPool) CtMethod(javassist.CtMethod) CannotCompileException(javassist.CannotCompileException)

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