If you know the code to remove you can do it with Javassist easily:
In the next example this will remove all lines that contains the method "printStackTrace" of any "Exception" class, all the magic occurs with the instrument
and replace
methods
...
ClassPool cp = ClassPool.getDefault();
try{
CtClass ct = cp.getCtClass("com.cm.main.ConcretClass");
CtMethod m = ct.getDeclaredMethod("testException");
m.instrument(new ExprEditor() {
public void edit(MethodCall m) throws CannotCompileException {
String regexPattern = ".*Exception";
if (m.getClassName().matches(regexPattern) && m.getMethodName().matches("printStackTrace")) {
m.replace(";");
}
}
;
});
} catch (CannotCompileException e) {
e.printStackTrace();
} catch (NotFoundException e) {
e.printStackTrace();
} catch (BadBytecode badBytecode) {
badBytecode.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
...
The ConcretClass
:
public class ConcretClass{
public String getName() {
return this.name + "-Extra";
}
public void testException(){
try {
FileOutputStream file = new FileOutputStream("C:\\Temp\\downloads");
file.close();
} catch (FileNotFoundException e2) {
e2.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}