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.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Helpers {
@ -124,20 +123,13 @@ public class Helpers {
*/
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 @@ public class Helpers {
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));
}
generateByTemplateAndCompile(className, null, "public class ${ClassName} extends ${BaseClass} {\n${Fields}}\n",
"", instanceSize, workDir, prefix);
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);
}