8157468: gc/testlibrary contains a lot of duplicated code

Reviewed-by: dfazunen, iignatyev
This commit is contained in:
Kirill Zhaldybin 2016-08-30 21:35:56 +03:00
parent e3e4334ed2
commit 8a4ddec874

View File

@ -32,7 +32,6 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths;
public class Helpers { public class Helpers {
@ -124,20 +123,13 @@ public class Helpers {
*/ */
public static String generate(String className, String superClass, String constructor, long fieldCount) { public static String generate(String className, String superClass, String constructor, long fieldCount) {
StringBuilder builder = new StringBuilder(); return new StringBuilder()
builder.append(String.format("public class %s%s {\n", className, superClass == null ? "" .append(String.format("public class %s%s {\n", className, superClass == null ? ""
: " extends " + superClass)); : " extends " + superClass))
.append(constructor == null ? "" : constructor)
if (constructor != null) { .append(fieldsGenerator(fieldCount))
builder.append(constructor); .append("}\n")
} .toString();
for (int i = 0; i < fieldCount; ++i) {
builder.append(String.format("long f%d;\n", i));
}
builder.append("}\n");
return builder.toString();
} }
/** /**
@ -212,50 +204,9 @@ public class Helpers {
Path workDir, String prefix) Path workDir, String prefix)
throws IOException, ClassNotFoundException { throws IOException, ClassNotFoundException {
if (instanceSize % SIZE_OF_LONG != 0L) { generateByTemplateAndCompile(className, null, "public class ${ClassName} extends ${BaseClass} {\n${Fields}}\n",
throw new Error(String.format("Test bug: only sizes aligned by 8 bytes are supported and %d was specified", "", instanceSize, workDir, prefix);
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));
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); return classLoader.loadClass(className);
} }