1 /* 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package build.tools.classfile; 27 28 import java.nio.file.Files; 29 import java.nio.file.Path; 30 import java.nio.file.Paths; 31 import java.io.InputStream; 32 import java.io.OutputStream; 33 import java.io.IOException; 34 import java.util.Set; 35 import java.util.HashSet; 36 37 import com.sun.tools.classfile.AccessFlags; 38 import com.sun.tools.classfile.Attributes; 39 import com.sun.tools.classfile.ClassFile; 40 import com.sun.tools.classfile.ClassReader; 41 import com.sun.tools.classfile.ClassWriter; 42 import com.sun.tools.classfile.ConstantPool; 43 import com.sun.tools.classfile.Field; 44 import com.sun.tools.classfile.Method; 45 46 public class RemoveMethods { 47 48 public static void main(String[] args) throws Exception { 49 if (args.length < 2) { 50 System.err.println("Usage: java RemoveMethods classfile output [method...]"); 51 System.exit(-1); 52 } 53 54 // class file to read 55 Path input = Paths.get(args[0]); 56 57 // class file to write, if directory then use the name of the input 58 Path output = Paths.get(args[1]); 59 if (Files.isDirectory(output)) 60 output = output.resolve(input.getFileName()); 61 62 // the methods to remove 63 Set<String> methodsToRemove = new HashSet<>(); 64 int i = 2; 65 while (i < args.length) 66 methodsToRemove.add(args[i++]); 67 68 // read class file 69 ClassFile cf; 70 try (InputStream in = Files.newInputStream(input)) { 71 cf = ClassFile.read(in); 72 } 73 74 final int magic = cf.magic; 75 final int major_version = cf.major_version; 76 final int minor_version = cf.minor_version; 77 final ConstantPool cp = cf.constant_pool; 78 final AccessFlags access_flags = cf.access_flags; 79 final int this_class = cf.this_class; 80 final int super_class = cf.super_class; 81 final int[] interfaces = cf.interfaces; 82 final Field[] fields = cf.fields; 83 final Attributes class_attrs = cf.attributes; 84 85 // remove the requested methods, no signature check at this time 86 Method[] methods = cf.methods; 87 i = 0; 88 while (i < methods.length) { 89 Method m = methods[i]; 90 String name = m.getName(cp); 91 if (methodsToRemove.contains(name)) { 92 int len = methods.length; 93 Method[] newMethods = new Method[len-1]; 94 if (i > 0) 95 System.arraycopy(methods, 0, newMethods, 0, i); 96 int after = methods.length - i - 1; 97 if (after > 0) 98 System.arraycopy(methods, i+1, newMethods, i, after); 99 methods = newMethods; 100 String paramTypes = m.descriptor.getParameterTypes(cp); 101 System.out.format("Removed method %s%s from %s%n", 102 name, paramTypes, cf.getName()); 103 continue; 104 } 105 i++; 106 } 107 108 // TBD, prune constant pool of entries that are no longer referenced 109 110 // re-write class file 111 cf = new ClassFile(magic, minor_version, major_version, cp, access_flags, 112 this_class, super_class, interfaces, fields, methods, class_attrs); 113 try (OutputStream out = Files.newOutputStream(output)) { 114 new ClassWriter().write(cf, out); 115 } 116 } 117 }