8157468: gc/testlibrary contains a lot of duplicated code
authorkzhaldyb
Tue, 30 Aug 2016 21:35:56 +0300
changeset 40916 77358f230cf0
parent 40915 7ca02bd6fbf6
child 40917 22ce5e78e152
8157468: gc/testlibrary contains a lot of duplicated code Reviewed-by: dfazunen, iignatyev
hotspot/test/gc/testlibrary/Helpers.java
--- a/hotspot/test/gc/testlibrary/Helpers.java	Fri Sep 02 11:18:04 2016 +0200
+++ b/hotspot/test/gc/testlibrary/Helpers.java	Tue Aug 30 21:35:56 2016 +0300
@@ -32,7 +32,6 @@
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
-import java.nio.file.Paths;
 
 public class Helpers {
 
@@ -124,20 +123,13 @@
      */
     public static String generate(String className, String superClass, String constructor, long fieldCount) {
 
-        StringBuilder builder = new StringBuilder();
-        builder.append(String.format("public class %s%s {\n", className, superClass == null ? ""
-                : " extends " + superClass));
-
-        if (constructor != null) {
-            builder.append(constructor);
-        }
-
-        for (int i = 0; i < fieldCount; ++i) {
-            builder.append(String.format("long f%d;\n", i));
-        }
-
-        builder.append("}\n");
-        return builder.toString();
+        return new StringBuilder()
+                .append(String.format("public class %s%s {\n", className, superClass == null ? ""
+                        : " extends " + superClass))
+                .append(constructor == null ? "" : constructor)
+                .append(fieldsGenerator(fieldCount))
+                .append("}\n")
+                .toString();
     }
 
     /**
@@ -212,50 +204,9 @@
                                                   Path workDir, String prefix)
             throws IOException, ClassNotFoundException {
 
-        if (instanceSize % SIZE_OF_LONG != 0L) {
-            throw new Error(String.format("Test bug: only sizes aligned by 8 bytes are supported and %d was specified",
-                    instanceSize));
-        }
-
-        long instanceSizeWithoutObjectHeader = instanceSize - WhiteBox.getWhiteBox().getObjectSize(new Object());
-
-        int generatedClassesCount;
-        int fieldsInLastClassCount;
-
-        int sizeOfLastFile = (int) (instanceSizeWithoutObjectHeader
-                % (MAXIMUM_AMOUNT_OF_FIELDS_IN_CLASS * SIZE_OF_LONG));
+        generateByTemplateAndCompile(className, null, "public class ${ClassName} extends ${BaseClass} {\n${Fields}}\n",
+                "", instanceSize, workDir, prefix);
 
-        if (sizeOfLastFile != 0) {
-            generatedClassesCount = (int) instanceSizeWithoutObjectHeader
-                    / (MAXIMUM_AMOUNT_OF_FIELDS_IN_CLASS * SIZE_OF_LONG) + 1;
-            fieldsInLastClassCount = sizeOfLastFile / SIZE_OF_LONG;
-        } else {
-            generatedClassesCount = (int) instanceSizeWithoutObjectHeader
-                    / (MAXIMUM_AMOUNT_OF_FIELDS_IN_CLASS * SIZE_OF_LONG);
-            fieldsInLastClassCount = MAXIMUM_AMOUNT_OF_FIELDS_IN_CLASS;
-        }
-
-        for (int i = 0; i < generatedClassesCount; i++) {
-            // for the last generated class we use specified class name
-            String clsName = (i == generatedClassesCount - 1) ? className : prefix + i;
-
-            // If we already have a file with the same name we do not create it again
-            if (Files.notExists(Paths.get(clsName + ".java"))) {
-                Helpers.compileClass(clsName, workDir,
-                        Helpers.generate(
-                                clsName,
-                                // for first generated class we don't have 'extends'
-                                (i == 0 ? null : prefix + (i - 1)),
-                                null,
-                                // for the last generated class we use different field count
-                                (i == generatedClassesCount - 1) ? fieldsInLastClassCount
-                                        : MAXIMUM_AMOUNT_OF_FIELDS_IN_CLASS));
-            } else {
-                System.out.println("Class " + clsName +
-                        ".java already exists, skipping class' generation and compilation");
-            }
-
-        }
         return classLoader.loadClass(className);
     }