test/hotspot/jtreg/vmTestbase/nsk/share/gc/Generator.java
changeset 50168 2f59dc95847d
equal deleted inserted replaced
50167:cc705c956798 50168:2f59dc95847d
       
     1 /*
       
     2  * Copyright (c) 2004, 2018, 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.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 package nsk.share.gc;
       
    25 
       
    26 import java.io.*;
       
    27 
       
    28 /**
       
    29  * <tt>Generator</tt> creates classes that have huge number of fields
       
    30  * (about 65536). All files are generated into a directory that is passed
       
    31  * as a first argument to method <code>main</code> The classes are intended to
       
    32  * be used used by <code>gc/gctests/LargeObjects/large00*</code> tests.
       
    33  * <p>
       
    34  * The class generates:
       
    35  * <ul>
       
    36  * <li> 8 "simple" parent classes that have 65500 fields of 7 primitive
       
    37  *      types and type Object and have just one modifier;
       
    38  * <li> 8 "simple" child classes that extend correspondent parent class and
       
    39  *      have 46 fileds of the same type (total number of fields in the class
       
    40  *      is over the limitation 65535);
       
    41  * <li> 8 "simple" child classes that extend correspondent parent class and
       
    42  *      have 35 fileds of the same type (total number of fields in the class
       
    43  *      is under the limitation 65535);
       
    44  * <li> 5 "combined" parent classes that have 32616 fields of each primitive
       
    45  *      types, type Object, one and two dimensional arrays of each type with
       
    46  *      one modifier;
       
    47  * <li> 5 "combined" child classes that extend correspondent parent class and
       
    48  *      have 33030 fileds (total number of fields in the class is over the
       
    49  *      limitation 65535);
       
    50  * <li> 5 "combined" child classes that extend correspondent parent class and
       
    51  *      have 33018 fileds (total number of fields in the class is under the
       
    52  *      limitation 65535).
       
    53  * </ul>
       
    54  */
       
    55 public class Generator {
       
    56 
       
    57     // All tested field types
       
    58     private final static String[] TYPES = {"byte", "int", "long", "short",
       
    59                                            "boolean", "double", "float",
       
    60                                            "Object"};
       
    61 
       
    62     // All tested field modifiers
       
    63     private final static String[] SIMPLE_MODIFIERS = {"static", "private",
       
    64                                                       "public", "protected",
       
    65                                                       "transient", "volatile",
       
    66                                                       "static", "public"};
       
    67 
       
    68     // All tested field modifiers
       
    69     private final static String[] COMBINED_MODIFIERS = {"static", "public",
       
    70                                                        "protected", "transient",
       
    71                                                        "volatile"};
       
    72 
       
    73     // Some constants that are used during generaion of each file
       
    74     private final static String EXT = ".java";
       
    75     private final static String PACKAGE = "package nsk.share.gc.newclass;\n";
       
    76     private final static String CLASS_BEGIN = "public class ";
       
    77 
       
    78     private final static String PARENT_SUFFIX = "parent";
       
    79     private final static String PARENT_FIELD_PREFIX = "p";
       
    80     private final static int SIMPLE_PARENT_FIELDS_NUM = 65500;
       
    81     private final static int COMBINED_PARENT_FIELDS_NUM = 32616;
       
    82 
       
    83     private final static String FIELD_NAME = "f";
       
    84 
       
    85     private final static String LCHILD_SUFFIX = "lchild";
       
    86     private final static int SIMPLE_LCHILD_FIELDS_NUM = 46;
       
    87     private final static int COMBINED_LCHILD_FIELDS_NUM = 33030;
       
    88 
       
    89     private final static String SCHILD_SUFFIX = "schild";
       
    90     private final static int SIMPLE_SCHILD_FIELDS_NUM = 35;
       
    91     private final static int COMBINED_SCHILD_FIELDS_NUM = 33018;
       
    92 
       
    93     /**
       
    94      * Default constructor.
       
    95      *
       
    96      */
       
    97     public Generator() {}
       
    98 
       
    99     /**
       
   100      * Generates classes.
       
   101      *
       
   102      * @param argv array of arguments passed to the class.
       
   103      *
       
   104      * @throws <tt>FileNotFoundException</tt> if directory that.is passed as
       
   105      * the first argument does not exist.
       
   106      *
       
   107      */
       
   108     public static void main(String[] argv) throws FileNotFoundException {
       
   109 
       
   110         // Generate classes that have fields of one type and with one
       
   111         // modifier
       
   112         for (int i = 0; i < SIMPLE_MODIFIERS.length; i++)
       
   113             generateSimple(System.out, argv[0], TYPES[i], SIMPLE_MODIFIERS[i]);
       
   114 
       
   115         // Generate classes that have fields of different types with one
       
   116         // modifier
       
   117         for (int i = 0; i < COMBINED_MODIFIERS.length; i++)
       
   118             generateCombined(System.out, argv[0], COMBINED_MODIFIERS[i]);
       
   119     }
       
   120 
       
   121     // Generate classes that have fields of one type and with one modifier
       
   122     private static void generateSimple(PrintStream out, String dir, String type,
       
   123                                  String modifier) throws FileNotFoundException {
       
   124 
       
   125         // Generate parent class
       
   126         String parentName = modifier + "_" + type + "_" + PARENT_SUFFIX;
       
   127         try (PrintWriter pw = getPrintWriter(dir, parentName + EXT)) {
       
   128 
       
   129             pw.println(PACKAGE);
       
   130             pw.println(CLASS_BEGIN + parentName + " {");
       
   131 
       
   132             // Even if "private" modifier is tested, parent class must have "public"
       
   133             // fields, so that child class could have access to them
       
   134             String m = modifier;
       
   135             if (modifier.equals("private"))
       
   136                 m = "public";
       
   137             for (int i = 0; i < SIMPLE_PARENT_FIELDS_NUM; i++)
       
   138                 pw.println(m + " " + type + " "
       
   139                          + PARENT_FIELD_PREFIX + FIELD_NAME + (i + 1) + ";");
       
   140             pw.println("public Object objP = null;");
       
   141             pw.println("}");
       
   142         }
       
   143 
       
   144         // Generate two children that extend the parent
       
   145         generateSimpleChild(modifier + "_" + type + "_" + LCHILD_SUFFIX,
       
   146                             type, modifier, dir, parentName,
       
   147                             SIMPLE_LCHILD_FIELDS_NUM);
       
   148         generateSimpleChild(modifier + "_" + type + "_" + SCHILD_SUFFIX,
       
   149                             type, modifier, dir, parentName,
       
   150                             SIMPLE_SCHILD_FIELDS_NUM);
       
   151     }
       
   152 
       
   153     // Generate a child that extends specified parent class
       
   154     private static void generateSimpleChild(String className, String type,
       
   155                                             String modifier, String dir,
       
   156                                             String parentName, int num)
       
   157                                                   throws FileNotFoundException {
       
   158         try (PrintWriter pw = getPrintWriter(dir, className + EXT)) {
       
   159 
       
   160             pw.println(PACKAGE);
       
   161             pw.println(CLASS_BEGIN + className + " extends " + parentName + " {");
       
   162             for (int i = 0; i < num; i++)
       
   163                 pw.println(modifier + " " + type + " " + FIELD_NAME + (i + 1)
       
   164                          + ";");
       
   165             pw.println("public Object objC = null;");
       
   166             pw.println("}");
       
   167         }
       
   168     }
       
   169 
       
   170     // Generate classes that have fields of different types with one modifier
       
   171     private static void generateCombined(PrintStream out, String dir,
       
   172                                  String modifier) throws FileNotFoundException {
       
   173 
       
   174         // Generate parent class
       
   175         String parentName = modifier + "_combination_" + PARENT_SUFFIX;
       
   176         try (PrintWriter pw = getPrintWriter(dir, parentName + EXT)){
       
   177 
       
   178             pw.println(PACKAGE);
       
   179             pw.println(CLASS_BEGIN + parentName + " {");
       
   180             String pattern = PARENT_FIELD_PREFIX + FIELD_NAME;
       
   181             for (int i = 0; i < COMBINED_PARENT_FIELDS_NUM; ) {
       
   182                 for (int j = 0; j < TYPES.length; j++) {
       
   183                     pw.println(modifier + " " + TYPES[j] + " "
       
   184                              + pattern + (i + 1) + ", "
       
   185                              + pattern + (i + 2) + "[], "
       
   186                              + pattern + (i + 3) + "[][];");
       
   187                     i = i + 3;
       
   188                 }
       
   189             }
       
   190             pw.println("public Object objP = null;");
       
   191             pw.println("}");
       
   192         }
       
   193 
       
   194         // Generate two children that extend the parent
       
   195         generateCombinedChild(modifier + "_combination_" + LCHILD_SUFFIX,
       
   196                               modifier, dir, parentName,
       
   197                               COMBINED_LCHILD_FIELDS_NUM);
       
   198         generateCombinedChild(modifier + "_combination_" + SCHILD_SUFFIX,
       
   199                               modifier, dir, parentName,
       
   200                               COMBINED_SCHILD_FIELDS_NUM);
       
   201     }
       
   202 
       
   203     // Generate a child that extends specified parent class
       
   204     private static void generateCombinedChild(String className, String modifier,
       
   205                                               String dir, String parentName,
       
   206                                               int num)
       
   207                                                   throws FileNotFoundException {
       
   208         try (PrintWriter pw = getPrintWriter(dir, className + EXT)) {
       
   209 
       
   210             pw.println(PACKAGE);
       
   211             pw.println(CLASS_BEGIN + className + " extends " + parentName + " {");
       
   212             for (int i = 0; i < num; )
       
   213                 for (int j = 0; j < TYPES.length; j++) {
       
   214                     pw.println(modifier + " " + TYPES[j] + " "
       
   215                              + FIELD_NAME + (i + 1) + ", "
       
   216                              + FIELD_NAME + (i + 2) + "[], "
       
   217                              + FIELD_NAME + (i + 3) + "[][];");
       
   218                     i = i + 3;
       
   219                     if (i >= num)
       
   220                         break;
       
   221                 }
       
   222             pw.println("public Object objC = null;");
       
   223             pw.println("}");
       
   224         }
       
   225     }
       
   226 
       
   227     // Create a new PrintWriter
       
   228     private static PrintWriter getPrintWriter(String dir, String name)
       
   229                                                   throws FileNotFoundException {
       
   230         FileOutputStream stream = new FileOutputStream(new File(dir, name));
       
   231         return new PrintWriter(stream, true);
       
   232     }
       
   233 }