Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/sysdict/share/ChainGen.java
41161 views
/*1* Copyright (c) 2002, 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 nsk.sysdict.share;2425import java.io.*;2627/**28* This tools generates a chain of classes.29* For more details, use:30* <pre>31* java ChainGen -help32* </pre>33*/34public class ChainGen {35private static final int FATS_HEIGHT = 5;36private static final int FATS_WEIGHT = 10000;37private static final int LEANS_HEIGHT = 50;38private static final int LEANS_WEIGHT = 1;3940private static void explain(Object x) {41System.err.println("# " + x);42}4344public static void main(String args[]) {45if (args.length < 1 || args[0].toLowerCase().startsWith("-h")) {46explain("Generates a chain classes extending each other.");47explain("");48explain("Use:");49explain(" java ChainGen $NAME $HEIGHT $WEIGHT");50explain("Or:");51explain(" java ChainGen \"fats\"");52explain("Or:");53explain(" java ChainGen \"leans\"");54explain("");55explain("This creates:");56explain(" ${NAME}Info.java class displaying HEIGHT and WEIGHT.");57explain(" ${NAME}XXXXXX.java defining classes chain.");58explain("");59explain("Here:");60explain(" HEIGHT and WEIGHT must be positive integers.");61explain(" Defaults for \"fats\": HEIGHT is " + FATS_HEIGHT62+ ", WEIGHT is " + FATS_WEIGHT +".");63explain(" Defaults for \"leans\": HEIGHT is " + LEANS_HEIGHT64+ ", WEIGHT is " + LEANS_WEIGHT +".");65System.exit(1);66};67String name;68int height, weight;69if (args[0].toLowerCase().equals("fats")) {70name = "Fats";71height = FATS_HEIGHT;72weight = FATS_WEIGHT;73} else if (args[0].toLowerCase().equals("leans")) {74name = "Leans";75height = LEANS_HEIGHT;76weight = LEANS_WEIGHT;77} else {78name = args[0];79height = Integer.parseInt(args[1]);80weight = Integer.parseInt(args[2]);81};82try {83doit(name, height, weight);84} catch (IOException exception) {85exception.printStackTrace(System.err);86System.exit(1);87}88}8990private static void doit(String name, int height, int weight)91throws FileNotFoundException92{93// Constants definition:94PrintStream info = new PrintStream(95new FileOutputStream(new File(name + "Info.java"))96);97info.println("// This file is generated by: ChainGen.java");98info.println("package nsk.sysdict.share;");99info.println("public class " + name + "Info {");100info.println(" public static final int HEIGHT = " + height + ";");101info.println(" private static final int WEIGHT = " + weight + ";");102info.println(" public static final String rootName = \"" + name + "\";");103info.println(" public static final String[] nodeNames = new String[] {");104// Generate a series of fat classes:105for (int index=0; index<height; index++) {106String suffix = digits(index,6);107String className = name + suffix;108info.println(" \"" + suffix + "\""109+ (index+1<height? "," : ""));110PrintStream chain = new PrintStream(111new FileOutputStream(new File(className + ".java"))112);113chain.println("// This file is generated by: ChainGen.java");114chain.println("package nsk.sysdict.share;");115chain.println("class " + className116+ (index>0? " extends " + name + digits(index-1,6): "")117+ " {");118for (int w=0; w<weight; w++) {119String fieldName = "fill_" + className + "_" + digits(w,6);120chain.println(" static long " + fieldName + ";");121}122chain.println("}");123chain.close();124};125//126info.println(" };");127info.println("}");128info.close();129}130131/**132* Convert <tt>x</tt> to <tt>n</tt>-digits string.133*/134private static String digits(int x, int n) {135String s = "" + x;136while (s.length() < n)137s = "0" + s;138return s;139}140}141142143