001: package hero.util;
002:
003: import java.io.File;
004: import java.io.FileInputStream;
005: import java.io.FileNotFoundException;
006: import java.io.FileOutputStream;
007: import java.io.IOException;
008: import java.io.InputStream;
009:
010: import org.apache.tools.ant.BuildException;
011: import org.apache.tools.ant.Task;
012: import org.objectweb.asm.ClassAdapter;
013: import org.objectweb.asm.ClassReader;
014: import org.objectweb.asm.ClassVisitor;
015: import org.objectweb.asm.ClassWriter;
016: import org.objectweb.asm.FieldVisitor;
017: import org.objectweb.asm.MethodVisitor;
018: import org.objectweb.asm.Opcodes;
019: import org.objectweb.asm.commons.EmptyVisitor;
020:
021: public class RemoveMethod extends Task {
022:
023: private File classdir = null;
024: private String method = null;
025:
026: public void setClassdir(File classdir) {
027: this .classdir = classdir;
028: }
029:
030: public void setMethod(String method) {
031: this .method = method;
032: }
033:
034: public void execute() throws BuildException {
035: System.out.println("Removing method starting with '" + method
036: + "' from the classes in folder : " + classdir);
037: File[] fileList = classdir.listFiles();
038: InputStream is;
039: ClassReader cr;
040: ClassWriter cw;
041: FileOutputStream fos;
042: for (int i = 0; i < fileList.length; i++) {
043: try {
044: is = new FileInputStream(fileList[i]);
045: cr = new ClassReader(is);
046: cw = new ClassWriter(true);
047: RemoveMethodAdapter cv = new RemoveMethodAdapter(cw,
048: method);
049: cr.accept(cv, false);
050: byte[] byteCodeClass = cw.toByteArray();
051: fos = null;
052: String fName = fileList[i].toString();
053: fos = new FileOutputStream(fName);
054: fos.write(byteCodeClass);
055: } catch (FileNotFoundException e) {
056: e.printStackTrace();
057: } catch (IOException e) {
058: e.printStackTrace();
059: }
060: }
061: System.out.println("Done!");
062: }
063: }
064:
065: class RemoveMethodAdapter extends ClassAdapter implements Opcodes {
066: private String method = null;
067:
068: public RemoveMethodAdapter(ClassVisitor cv, String method) {
069: super (cv);
070: this .method = method;
071: }
072:
073: /*
074: * Method that manipulates class methods
075: *
076: * @see org.objectweb.asm.ClassVisitor#visitMethod(int, java.lang.String, java.lang.String, java.lang.String, java.lang.String[])
077: */
078: public MethodVisitor visitMethod(final int access,
079: final String name, final String desc,
080: final String signature, final String[] exceptions) {
081: if (name.startsWith(method)) {
082: return new EmptyVisitor();
083: } else {
084: return cv.visitMethod(access, name, desc, signature,
085: exceptions);
086: }
087: }
088:
089: /*
090: * Method that manipulates class atributes
091: *
092: * @see org.objectweb.asm.ClassVisitor#visitField(int, java.lang.String, java.lang.String, java.lang.String, java.lang.Object)
093: */
094: public FieldVisitor visitField(final int access, final String name,
095: final String desc, final String signature,
096: final Object value) {
097: if ("myString".equals(name)) {
098: return cv.visitField(ACC_PUBLIC, "newMyString", desc,
099: signature, value);
100: } else {
101: return cv.visitField(access, name, desc, signature, value);
102: }
103: }
104: }
|