Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/make/src/classes/build/tools/classfile/RemoveMethods.java
32287 views
/*1* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package build.tools.classfile;2627import java.nio.file.Files;28import java.nio.file.Path;29import java.nio.file.Paths;30import java.io.InputStream;31import java.io.OutputStream;32import java.util.Set;33import java.util.HashSet;3435import com.sun.tools.classfile.AccessFlags;36import com.sun.tools.classfile.Attributes;37import com.sun.tools.classfile.ClassFile;38import com.sun.tools.classfile.ClassWriter;39import com.sun.tools.classfile.ConstantPool;40import com.sun.tools.classfile.Field;41import com.sun.tools.classfile.Method;4243public class RemoveMethods {4445public static void main(String[] args) throws Exception {46if (args.length < 2) {47System.err.println("Usage: java RemoveMethods classfile output [method...]");48System.exit(-1);49}5051// class file to read52Path input = Paths.get(args[0]);5354// class file to write, if directory then use the name of the input55Path output = Paths.get(args[1]);56if (Files.isDirectory(output))57output = output.resolve(input.getFileName());5859// the methods to remove60Set<String> methodsToRemove = new HashSet<>();61int i = 2;62while (i < args.length)63methodsToRemove.add(args[i++]);6465// read class file66ClassFile cf;67try (InputStream in = Files.newInputStream(input)) {68cf = ClassFile.read(in);69}7071final int magic = cf.magic;72final int major_version = cf.major_version;73final int minor_version = cf.minor_version;74final ConstantPool cp = cf.constant_pool;75final AccessFlags access_flags = cf.access_flags;76final int this_class = cf.this_class;77final int super_class = cf.super_class;78final int[] interfaces = cf.interfaces;79final Field[] fields = cf.fields;80final Attributes class_attrs = cf.attributes;8182// remove the requested methods, no signature check at this time83Method[] methods = cf.methods;84i = 0;85while (i < methods.length) {86Method m = methods[i];87String name = m.getName(cp);88if (methodsToRemove.contains(name)) {89int len = methods.length;90Method[] newMethods = new Method[len-1];91if (i > 0)92System.arraycopy(methods, 0, newMethods, 0, i);93int after = methods.length - i - 1;94if (after > 0)95System.arraycopy(methods, i+1, newMethods, i, after);96methods = newMethods;97String paramTypes = m.descriptor.getParameterTypes(cp);98System.out.format("Removed method %s%s from %s%n",99name, paramTypes, cf.getName());100continue;101}102i++;103}104105// TBD, prune constant pool of entries that are no longer referenced106107// re-write class file108cf = new ClassFile(magic, minor_version, major_version, cp, access_flags,109this_class, super_class, interfaces, fields, methods, class_attrs);110try (OutputStream out = Files.newOutputStream(output)) {111new ClassWriter().write(cf, out);112}113}114}115116117