use of javassist.bytecode.DuplicateMemberException in project fakereplace by fakereplace.
the class Transformer method addStaticConstructorForInstrumentation.
private static void addStaticConstructorForInstrumentation(ClassFile file) {
try {
MethodInfo m = new MethodInfo(file.getConstPool(), "<clinit>", "()V");
m.setAccessFlags(AccessFlag.PUBLIC | AccessFlag.STATIC);
Bytecode b = new Bytecode(file.getConstPool());
b.add(Opcode.RETURN);
m.setCodeAttribute(b.toCodeAttribute());
file.addMethod(m);
} catch (DuplicateMemberException e) {
// e.printStackTrace();
}
}
use of javassist.bytecode.DuplicateMemberException in project fakereplace by fakereplace.
the class Transformer method addConstructorForInstrumentation.
private void addConstructorForInstrumentation(ClassFile file) {
MethodInfo ret = new MethodInfo(file.getConstPool(), "<init>", Constants.ADDED_CONSTRUCTOR_DESCRIPTOR);
Bytecode code = new Bytecode(file.getConstPool());
// if the class does not have a constructor return
if (!ManipulationUtils.addBogusConstructorCall(file, code)) {
return;
}
CodeAttribute ca = code.toCodeAttribute();
ca.setMaxLocals(4);
ret.setCodeAttribute(ca);
ret.setAccessFlags(AccessFlag.PUBLIC | AccessFlag.SYNTHETIC);
try {
ca.computeMaxStack();
file.addMethod(ret);
} catch (DuplicateMemberException e) {
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of javassist.bytecode.DuplicateMemberException in project fakereplace by fakereplace.
the class Transformer method addAbstractMethodForInstrumentation.
/**
* Adds a method to a class that re can redefine when the class is reloaded
*/
private void addAbstractMethodForInstrumentation(ClassFile file) {
try {
MethodInfo m = new MethodInfo(file.getConstPool(), Constants.ADDED_METHOD_NAME, Constants.ADDED_METHOD_DESCRIPTOR);
m.setAccessFlags(AccessFlag.PUBLIC | AccessFlag.ABSTRACT | AccessFlag.SYNTHETIC);
file.addMethod(m);
} catch (DuplicateMemberException e) {
// e.printStackTrace();
}
}
use of javassist.bytecode.DuplicateMemberException in project fakereplace by fakereplace.
the class Transformer method addMethodForInstrumentation.
/**
* Adds a method to a class that re can redefine when the class is reloaded
*/
private void addMethodForInstrumentation(ClassFile file) {
try {
MethodInfo m = new MethodInfo(file.getConstPool(), Constants.ADDED_METHOD_NAME, Constants.ADDED_METHOD_DESCRIPTOR);
m.setAccessFlags(AccessFlag.PUBLIC | AccessFlag.SYNTHETIC);
Bytecode b = new Bytecode(file.getConstPool(), 5, 3);
if (BuiltinClassData.skipInstrumentation(file.getSuperclass())) {
b.addNew(NoSuchMethodError.class.getName());
b.add(Opcode.DUP);
b.addInvokespecial(NoSuchMethodError.class.getName(), "<init>", "()V");
b.add(Opcode.ATHROW);
} else {
// delegate to the parent class
b.add(Bytecode.ALOAD_0);
b.add(Bytecode.ILOAD_1);
b.add(Bytecode.ALOAD_2);
b.addInvokespecial(file.getSuperclass(), Constants.ADDED_METHOD_NAME, Constants.ADDED_METHOD_DESCRIPTOR);
b.add(Bytecode.ARETURN);
}
CodeAttribute ca = b.toCodeAttribute();
m.setCodeAttribute(ca);
file.addMethod(m);
} catch (DuplicateMemberException e) {
// e.printStackTrace();
}
try {
MethodInfo m = new MethodInfo(file.getConstPool(), Constants.ADDED_STATIC_METHOD_NAME, Constants.ADDED_METHOD_DESCRIPTOR);
m.setAccessFlags(AccessFlag.PUBLIC | AccessFlag.STATIC | AccessFlag.SYNTHETIC);
Bytecode b = new Bytecode(file.getConstPool(), 5, 3);
b.addNew(NoSuchMethodError.class.getName());
b.add(Opcode.DUP);
b.addInvokespecial(NoSuchMethodError.class.getName(), "<init>", "()V");
b.add(Opcode.ATHROW);
CodeAttribute ca = b.toCodeAttribute();
m.setCodeAttribute(ca);
file.addMethod(m);
} catch (DuplicateMemberException e) {
// e.printStackTrace();
}
}
use of javassist.bytecode.DuplicateMemberException in project fakereplace by fakereplace.
the class MethodReplacementTransformer method createRemovedMethod.
private static MethodInfo createRemovedMethod(ClassFile file, MethodData md, Class<?> oldClass, Set<MethodData> methodsToRemove) {
if (md.getMethodName().equals("<clinit>")) {
// if the static constructor is removed it gets added later on
return null;
// in the process
}
// load up the existing method object
MethodInfo m = new MethodInfo(file.getConstPool(), md.getMethodName(), md.getDescriptor());
m.setAccessFlags(md.getAccessFlags());
// put the old annotations on the class
if (md.getMethodName().equals("<init>")) {
Constructor<?> meth;
try {
meth = md.getConstructor(oldClass);
} catch (Exception e) {
throw new RuntimeException("Error accessing existing constructor via reflection in not found", e);
}
m.addAttribute(AnnotationReplacer.duplicateAnnotationsAttribute(file.getConstPool(), meth));
} else {
Method meth;
try {
meth = md.getMethod(oldClass);
} catch (Exception e) {
throw new RuntimeException("Error accessing existing method via reflection in not found", e);
}
m.addAttribute(AnnotationReplacer.duplicateAnnotationsAttribute(file.getConstPool(), meth));
}
Bytecode b = new Bytecode(file.getConstPool(), 5, 3);
b.addNew("java.lang.NoSuchMethodError");
b.add(Opcode.DUP);
b.addInvokespecial("java.lang.NoSuchMethodError", "<init>", "()V");
b.add(Bytecode.ATHROW);
CodeAttribute ca = b.toCodeAttribute();
m.setCodeAttribute(ca);
try {
ca.computeMaxStack();
file.addMethod(m);
} catch (DuplicateMemberException e) {
logger.error("Duplicate error", e);
} catch (BadBytecode e) {
logger.error("Bad bytecode", e);
}
methodsToRemove.add(md);
return m;
}
Aggregations