Path: blob/master/test/hotspot/jtreg/vmTestbase/vm/mlvm/share/ClassfileGenerator.java
41155 views
/*1* Copyright (c) 2011, 2018, 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package vm.mlvm.share;2425import java.io.File;26import java.io.FileOutputStream;27import java.io.IOException;2829import nsk.share.ArgumentParser;3031public abstract class ClassfileGenerator {3233public static class Klass {34private String pkgName;35private String className;36private byte[] bytes;37private String mainMethodName;38private String mainMethodSignature;3940public Klass(String packageName, String className, String mainMethodName, String mainMethodSignature, byte[] bytes) {41this.pkgName = packageName;42this.className = className;43this.mainMethodName = mainMethodName;44this.mainMethodSignature = mainMethodSignature;45this.bytes = bytes;46}4748public String getClassName() {49return (this.pkgName != null ? (this.pkgName + ".") : "") + this.className;50}5152public String getSimpleClassName() {53return this.className;54}5556public String getPackageName() {57return this.pkgName;58}5960public String getMainMethodName() {61return mainMethodName;62}6364public String getMainMethodSignature() {65return mainMethodSignature;66}6768public byte[] getBytes() {69return this.bytes;70}7172public void writeClass(String destDir) throws IOException {73boolean wroteOk = false;7475File outDir;76if (this.pkgName != null)77outDir = new File(destDir, this.pkgName.replace('.', '/'));78else79outDir = new File(".");8081outDir.mkdirs();8283File outFile = new File(outDir, this.className.concat(".class"));84FileOutputStream outStream = new FileOutputStream(outFile);85try {86outStream.write(this.bytes);87wroteOk = true;88} finally {89outStream.close();9091if (!wroteOk)92outFile.delete();93}94}95}9697protected String fullClassName, pkgName, shortClassName;9899public void setClassName(String pkgName, String shortClassName) {100this.pkgName = pkgName;101this.shortClassName = shortClassName;102fullClassName = (pkgName == null ? "" : (pkgName.replace('.', '/') + '/'))103+ shortClassName;104}105106public abstract Klass[] generateBytecodes();107108public static void main(String[] args) {109try {110Env.init(new ArgumentParser(args) {111@Override112protected boolean checkOption(String option, String value) {113if (option.equals("d"))114return true;115116return super.checkOption(option, value);117}118});119120Class<?> caller = Class.forName(Thread.currentThread()121.getStackTrace()[2].getClassName());122ClassfileGenerator gen = (ClassfileGenerator) caller.newInstance();123gen.setClassName(caller.getPackage().getName(), caller124.getSimpleName().replaceFirst("^GENERATE_", ""));125126String destDir = Env.getArgParser().getOptions().getProperty("d");127128Klass[] klasses = gen.generateBytecodes();129130for (Klass k : klasses)131k.writeClass(destDir);132133} catch (Exception e) {134Env.complain(e, "Generator caught an error");135System.exit(1);136}137}138}139140141