jdk/make/src/classes/build/tools/classfile/RemoveMethods.java
changeset 22864 f476f1ae4bd3
parent 22863 6a578e0f65ea
parent 22173 f130ca87de66
child 22865 3b8857d7b3cc
equal deleted inserted replaced
22863:6a578e0f65ea 22864:f476f1ae4bd3
     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.util.Set;
       
    34 import java.util.HashSet;
       
    35 
       
    36 import com.sun.tools.classfile.AccessFlags;
       
    37 import com.sun.tools.classfile.Attributes;
       
    38 import com.sun.tools.classfile.ClassFile;
       
    39 import com.sun.tools.classfile.ClassWriter;
       
    40 import com.sun.tools.classfile.ConstantPool;
       
    41 import com.sun.tools.classfile.Field;
       
    42 import com.sun.tools.classfile.Method;
       
    43 
       
    44 public class RemoveMethods {
       
    45 
       
    46     public static void main(String[] args) throws Exception {
       
    47         if (args.length < 2) {
       
    48             System.err.println("Usage: java RemoveMethods classfile output [method...]");
       
    49             System.exit(-1);
       
    50         }
       
    51 
       
    52         // class file to read
       
    53         Path input = Paths.get(args[0]);
       
    54 
       
    55         // class file to write, if directory then use the name of the input
       
    56         Path output = Paths.get(args[1]);
       
    57         if (Files.isDirectory(output))
       
    58             output = output.resolve(input.getFileName());
       
    59 
       
    60         // the methods to remove
       
    61         Set<String> methodsToRemove = new HashSet<>();
       
    62         int i = 2;
       
    63         while (i < args.length)
       
    64             methodsToRemove.add(args[i++]);
       
    65 
       
    66         // read class file
       
    67         ClassFile cf;
       
    68         try (InputStream in = Files.newInputStream(input)) {
       
    69              cf = ClassFile.read(in);
       
    70         }
       
    71 
       
    72         final int magic = cf.magic;
       
    73         final int major_version = cf.major_version;
       
    74         final int minor_version = cf.minor_version;
       
    75         final ConstantPool cp = cf.constant_pool;
       
    76         final AccessFlags access_flags = cf.access_flags;
       
    77         final int this_class = cf.this_class;
       
    78         final int super_class = cf.super_class;
       
    79         final int[] interfaces = cf.interfaces;
       
    80         final Field[] fields = cf.fields;
       
    81         final Attributes class_attrs = cf.attributes;
       
    82 
       
    83         // remove the requested methods, no signature check at this time
       
    84         Method[] methods = cf.methods;
       
    85         i = 0;
       
    86         while (i < methods.length) {
       
    87             Method m = methods[i];
       
    88             String name = m.getName(cp);
       
    89             if (methodsToRemove.contains(name)) {
       
    90                 int len = methods.length;
       
    91                 Method[] newMethods = new Method[len-1];
       
    92                 if (i > 0)
       
    93                     System.arraycopy(methods, 0, newMethods, 0, i);
       
    94                 int after = methods.length - i - 1;
       
    95                 if (after > 0)
       
    96                     System.arraycopy(methods, i+1, newMethods, i, after);
       
    97                 methods = newMethods;
       
    98                 String paramTypes = m.descriptor.getParameterTypes(cp);
       
    99                 System.out.format("Removed method %s%s from %s%n",
       
   100                     name, paramTypes, cf.getName());
       
   101                 continue;
       
   102             }
       
   103             i++;
       
   104         }
       
   105 
       
   106         // TBD, prune constant pool of entries that are no longer referenced
       
   107 
       
   108         // re-write class file
       
   109         cf = new ClassFile(magic, minor_version, major_version, cp, access_flags,
       
   110                 this_class, super_class, interfaces, fields, methods, class_attrs);
       
   111         try (OutputStream out = Files.newOutputStream(output)) {
       
   112              new ClassWriter().write(cf, out);
       
   113         }
       
   114     }
       
   115 }