1 package alt.jiapi.tool; 2 3 import java.util.*; 4 import java.io.*; 5 6 import alt.jiapi.file.*; 7 8 13 public class ClassTool { 14 public static void main(String [] args) throws Exception { 15 if (args.length < 3) { 16 System.out.println("ClassTool <file.class> remove-attribute <name>"); 17 System.exit(0); 18 } 19 20 ClassTool ct = new ClassTool(); 21 ClassFile cf = ClassFile.parse(args[0]); 22 23 if ("remove-attribute".equals(args[1])) { 24 if (args.length == 3) { 25 ct.removeAttribute(cf, args[2]); 26 } 27 else { 28 ct.removeAttribute(cf, args[2], args[3]); 29 } 30 } 31 32 byte[] bytes = cf.toBytes(); 33 FileOutputStream fos = new FileOutputStream(args[0]); 34 fos.write(bytes); 35 } 36 37 38 public void removeAttribute(ClassFile cf, String name) { 39 List attrs = cf.getAttributes(); 40 boolean found = false; 41 42 for (int i = 0; i < attrs.size(); i++) { 43 Attribute a = (Attribute)attrs.get(i); 44 if (name.equals(a.getName())) { 45 attrs.remove(i); 46 found = true; 47 break; 48 } 49 } 50 51 if (!found) { 52 System.out.println("ERROR: Could not find class attribute " +name); 53 } 54 } 55 56 public void removeAttribute(ClassFile cf, String method, String name) { 57 Method m = findMethod(method, cf.getMethods()); 58 if (m == null) { 59 System.out.println("ERROR: Could not find method " + method); 60 return; 61 } 62 63 List attrs = m.getAttributes(); 64 boolean found = false; 65 66 for (int i = 0; i < attrs.size(); i++) { 67 Attribute a = (Attribute)attrs.get(i); 68 69 if (name.equals(a.getName())) { 70 attrs.remove(i); 71 found = true; 72 break; 73 } 74 } 75 76 if (!found) { 77 System.out.println("ERROR: Could not find method attribute "+name); 78 } 79 } 80 81 82 private Method findMethod(String name, List il) { 83 Iterator i = il.iterator(); 84 while(i.hasNext()) { 85 Method m = (Method)i.next(); 86 if (name.equals(m.getName())) { 87 return m; 88 } 89 } 90 91 return null; 92 } 93 } 94 | Popular Tags |