Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/gc/Generator.java
41161 views
/*1* Copyright (c) 2004, 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.share.gc;2425import java.io.*;2627/**28* <tt>Generator</tt> creates classes that have huge number of fields29* (about 65536). All files are generated into a directory that is passed30* as a first argument to method <code>main</code> The classes are intended to31* be used used by <code>gc/gctests/LargeObjects/large00*</code> tests.32* <p>33* The class generates:34* <ul>35* <li> 8 "simple" parent classes that have 65500 fields of 7 primitive36* types and type Object and have just one modifier;37* <li> 8 "simple" child classes that extend correspondent parent class and38* have 46 fileds of the same type (total number of fields in the class39* is over the limitation 65535);40* <li> 8 "simple" child classes that extend correspondent parent class and41* have 35 fileds of the same type (total number of fields in the class42* is under the limitation 65535);43* <li> 5 "combined" parent classes that have 32616 fields of each primitive44* types, type Object, one and two dimensional arrays of each type with45* one modifier;46* <li> 5 "combined" child classes that extend correspondent parent class and47* have 33030 fileds (total number of fields in the class is over the48* limitation 65535);49* <li> 5 "combined" child classes that extend correspondent parent class and50* have 33018 fileds (total number of fields in the class is under the51* limitation 65535).52* </ul>53*/54public class Generator {5556// All tested field types57private final static String[] TYPES = {"byte", "int", "long", "short",58"boolean", "double", "float",59"Object"};6061// All tested field modifiers62private final static String[] SIMPLE_MODIFIERS = {"static", "private",63"public", "protected",64"transient", "volatile",65"static", "public"};6667// All tested field modifiers68private final static String[] COMBINED_MODIFIERS = {"static", "public",69"protected", "transient",70"volatile"};7172// Some constants that are used during generaion of each file73private final static String EXT = ".java";74private final static String PACKAGE = "package nsk.share.gc.newclass;\n";75private final static String CLASS_BEGIN = "public class ";7677private final static String PARENT_SUFFIX = "parent";78private final static String PARENT_FIELD_PREFIX = "p";79private final static int SIMPLE_PARENT_FIELDS_NUM = 65500;80private final static int COMBINED_PARENT_FIELDS_NUM = 32616;8182private final static String FIELD_NAME = "f";8384private final static String LCHILD_SUFFIX = "lchild";85private final static int SIMPLE_LCHILD_FIELDS_NUM = 46;86private final static int COMBINED_LCHILD_FIELDS_NUM = 33030;8788private final static String SCHILD_SUFFIX = "schild";89private final static int SIMPLE_SCHILD_FIELDS_NUM = 35;90private final static int COMBINED_SCHILD_FIELDS_NUM = 33018;9192/**93* Default constructor.94*95*/96public Generator() {}9798/**99* Generates classes.100*101* @param argv array of arguments passed to the class.102*103* @throws <tt>FileNotFoundException</tt> if directory that.is passed as104* the first argument does not exist.105*106*/107public static void main(String[] argv) throws FileNotFoundException {108109// Generate classes that have fields of one type and with one110// modifier111for (int i = 0; i < SIMPLE_MODIFIERS.length; i++)112generateSimple(System.out, argv[0], TYPES[i], SIMPLE_MODIFIERS[i]);113114// Generate classes that have fields of different types with one115// modifier116for (int i = 0; i < COMBINED_MODIFIERS.length; i++)117generateCombined(System.out, argv[0], COMBINED_MODIFIERS[i]);118}119120// Generate classes that have fields of one type and with one modifier121private static void generateSimple(PrintStream out, String dir, String type,122String modifier) throws FileNotFoundException {123124// Generate parent class125String parentName = modifier + "_" + type + "_" + PARENT_SUFFIX;126try (PrintWriter pw = getPrintWriter(dir, parentName + EXT)) {127128pw.println(PACKAGE);129pw.println(CLASS_BEGIN + parentName + " {");130131// Even if "private" modifier is tested, parent class must have "public"132// fields, so that child class could have access to them133String m = modifier;134if (modifier.equals("private"))135m = "public";136for (int i = 0; i < SIMPLE_PARENT_FIELDS_NUM; i++)137pw.println(m + " " + type + " "138+ PARENT_FIELD_PREFIX + FIELD_NAME + (i + 1) + ";");139pw.println("public Object objP = null;");140pw.println("}");141}142143// Generate two children that extend the parent144generateSimpleChild(modifier + "_" + type + "_" + LCHILD_SUFFIX,145type, modifier, dir, parentName,146SIMPLE_LCHILD_FIELDS_NUM);147generateSimpleChild(modifier + "_" + type + "_" + SCHILD_SUFFIX,148type, modifier, dir, parentName,149SIMPLE_SCHILD_FIELDS_NUM);150}151152// Generate a child that extends specified parent class153private static void generateSimpleChild(String className, String type,154String modifier, String dir,155String parentName, int num)156throws FileNotFoundException {157try (PrintWriter pw = getPrintWriter(dir, className + EXT)) {158159pw.println(PACKAGE);160pw.println(CLASS_BEGIN + className + " extends " + parentName + " {");161for (int i = 0; i < num; i++)162pw.println(modifier + " " + type + " " + FIELD_NAME + (i + 1)163+ ";");164pw.println("public Object objC = null;");165pw.println("}");166}167}168169// Generate classes that have fields of different types with one modifier170private static void generateCombined(PrintStream out, String dir,171String modifier) throws FileNotFoundException {172173// Generate parent class174String parentName = modifier + "_combination_" + PARENT_SUFFIX;175try (PrintWriter pw = getPrintWriter(dir, parentName + EXT)){176177pw.println(PACKAGE);178pw.println(CLASS_BEGIN + parentName + " {");179String pattern = PARENT_FIELD_PREFIX + FIELD_NAME;180for (int i = 0; i < COMBINED_PARENT_FIELDS_NUM; ) {181for (int j = 0; j < TYPES.length; j++) {182pw.println(modifier + " " + TYPES[j] + " "183+ pattern + (i + 1) + ", "184+ pattern + (i + 2) + "[], "185+ pattern + (i + 3) + "[][];");186i = i + 3;187}188}189pw.println("public Object objP = null;");190pw.println("}");191}192193// Generate two children that extend the parent194generateCombinedChild(modifier + "_combination_" + LCHILD_SUFFIX,195modifier, dir, parentName,196COMBINED_LCHILD_FIELDS_NUM);197generateCombinedChild(modifier + "_combination_" + SCHILD_SUFFIX,198modifier, dir, parentName,199COMBINED_SCHILD_FIELDS_NUM);200}201202// Generate a child that extends specified parent class203private static void generateCombinedChild(String className, String modifier,204String dir, String parentName,205int num)206throws FileNotFoundException {207try (PrintWriter pw = getPrintWriter(dir, className + EXT)) {208209pw.println(PACKAGE);210pw.println(CLASS_BEGIN + className + " extends " + parentName + " {");211for (int i = 0; i < num; )212for (int j = 0; j < TYPES.length; j++) {213pw.println(modifier + " " + TYPES[j] + " "214+ FIELD_NAME + (i + 1) + ", "215+ FIELD_NAME + (i + 2) + "[], "216+ FIELD_NAME + (i + 3) + "[][];");217i = i + 3;218if (i >= num)219break;220}221pw.println("public Object objC = null;");222pw.println("}");223}224}225226// Create a new PrintWriter227private static PrintWriter getPrintWriter(String dir, String name)228throws FileNotFoundException {229FileOutputStream stream = new FileOutputStream(new File(dir, name));230return new PrintWriter(stream, true);231}232}233234235