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);
}
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.");
}
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);
}
}
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);
}
}
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);
}
}
Aggregations