Fix generators (needed to flush PrintWriters, even though it's backed by an in-memory buffer)

This commit is contained in:
Michael Pfaff 2022-09-09 00:42:36 -04:00
parent 00d87d7003
commit 314078eb11
Signed by: michael
GPG Key ID: CF402C4A012AA9D4
4 changed files with 40 additions and 30 deletions

View File

@ -310,17 +310,20 @@ public class GeneratorVisitor extends ElementKindVisitor6<Void, Void> {
native_writer.append("(env, clazz, num_functions, functions);\n");
native_writer.append("}");
}
native_writer.flush();
saveGeneratedCSource(env.getMessager(), this.gen_native_path, d, spawn((Callable<byte[]>) () -> native_writer1.toByteArray()), startTime);
}
private void doJavaGen(TypeElement e, Collection<ExecutableElement> methods, Collection<VariableElement> fields) {
long startTime = System.currentTimeMillis();
ByteArrayOutputStream java_writer = new ByteArrayOutputStream();
PrintWriter pw = new PrintWriter(java_writer);
try {
generateJavaSource(e, new PrintWriter(java_writer), methods, fields);
generateJavaSource(e, pw, methods, fields);
} catch (IOException ex) {
throw new RuntimeException("Failed to generate the Java sources for " + e, ex);
}
pw.flush();
saveGeneratedJavaSource(env.getMessager(), this.gen_java_path, e, spawn((Callable<byte[]>) () -> java_writer.toByteArray()), startTime);
}

View File

@ -138,6 +138,7 @@ public class CLGeneratorProcessor extends AbstractProcessor {
}
writer.println("}");
writer.flush();
saveGeneratedJavaSource(processingEnv.getMessager(), genJavaPath, "org.lwjgl.opencl." + CLCAPS_CLASS_NAME, spawn((Callable<byte[]>) () -> writer1.toByteArray()), startTime);
}
@ -165,6 +166,7 @@ public class CLGeneratorProcessor extends AbstractProcessor {
CLPDCapabilitiesGenerator.generateToString(writer, templates, capsType);
writer.println("}");
writer.flush();
saveGeneratedJavaSource(processingEnv.getMessager(), genJavaPath, "org.lwjgl.opencl." + capsName, spawn((Callable<byte[]>) () -> writer1.toByteArray()), startTime);
}
}

View File

@ -172,6 +172,7 @@ public class GLGeneratorProcessor extends AbstractProcessor {
writer.println("\t}");
writer.println("}");
writer.flush();
saveGeneratedJavaSource(env.getMessager(), genJavaPath, "org.lwjgl.opengl." + Utils.CONTEXT_CAPS_CLASS_NAME, spawn((Callable<byte[]>) () -> writer1.toByteArray()), startTime);
}
}

View File

@ -97,7 +97,7 @@ public class GLReferencesGeneratorProcessor extends AbstractProcessor {
if ( cached_reference_annotation != null && cached_reference_annotation.name().length() == 0 ) {
Class nio_type = Utils.getNIOBufferType(param.asType());
String reference_name = Utils.getReferenceName(interface_decl, method, param);
writer.println("\t\tthis." + reference_name + " = null;");
writer.append("\t\tthis.").append(reference_name).append(" = null;\n");
}
}
}
@ -108,8 +108,7 @@ public class GLReferencesGeneratorProcessor extends AbstractProcessor {
if ( cached_reference_annotation != null && cached_reference_annotation.name().length() == 0 ) {
Class nio_type = Utils.getNIOBufferType(param.asType());
String reference_name = Utils.getReferenceName(interface_decl, method, param);
writer.print("\t\t\tthis." + reference_name + " = ");
writer.println(REFERENCES_PARAMETER_NAME + "." + reference_name + ";");
writer.append("\t\t\tthis.").append(reference_name).append(" = ").append(REFERENCES_PARAMETER_NAME + ".").append(reference_name).append(";\n");
}
}
}
@ -143,8 +142,8 @@ public class GLReferencesGeneratorProcessor extends AbstractProcessor {
throw new RuntimeException(param + " in method " + method + " in " + interface_decl + " is annotated with "
+ cached_reference_annotation.annotationType().getSimpleName() + " but the parameter is not a NIO buffer");
}
writer.print("\t" + nio_type.getName() + " " + Utils.getReferenceName(interface_decl, method, param));
writer.println(";");
writer.append("\t" + nio_type.getName() + " " + Utils.getReferenceName(interface_decl, method, param));
writer.append(";\n");
}
}
}
@ -162,44 +161,49 @@ public class GLReferencesGeneratorProcessor extends AbstractProcessor {
private void generateReferencesSource(ProcessingEnvironment env, Path genJavaPath, Set<TypeElement> templates) throws IOException {
long startTime = System.currentTimeMillis();
ByteArrayOutputStream writer1 = new ByteArrayOutputStream();
// interesting note: autoFlush does absolutely nothing
PrintWriter writer = new PrintWriter(writer1);
writer.println("/* MACHINE GENERATED FILE, DO NOT EDIT */");
writer.println();
writer.println("package org.lwjgl.opengl;");
writer.println();
writer.println("class " + REFERENCES_CLASS_NAME + " extends BaseReferences {");
writer.println("\t" + REFERENCES_CLASS_NAME + "(ContextCapabilities caps) {");
writer.println("\t\tsuper(caps);");
writer.println("\t}");
templates.stream().parallel().filter(tmpl -> tmpl.getKind().isInterface()).map(interface_decl -> {
writer.append("/* MACHINE GENERATED FILE, DO NOT EDIT */\n\n");
writer.append("package org.lwjgl.opengl;\n\n");
writer.append("class " + REFERENCES_CLASS_NAME + " extends BaseReferences {\n");
writer.append("\t" + REFERENCES_CLASS_NAME + "(ContextCapabilities caps) {\n");
writer.append("\t\tsuper(caps);\n");
writer.append("\t}\n\n");
writer.flush();
templates.parallelStream().filter(tmpl -> tmpl.getKind().isInterface()).map(interface_decl -> {
ByteArrayOutputStream writer2 = new ByteArrayOutputStream();
PrintWriter writer3 = new PrintWriter(writer2);
generateReferencesFromMethods(env, writer3, interface_decl);
writer3.flush();
return writer2.toByteArray();
}).sequential().forEach(writer1::writeBytes);
writer.println();
writer.println("\tvoid copy(" + REFERENCES_CLASS_NAME + " " + REFERENCES_PARAMETER_NAME + ", int mask) {");
writer.println("\t\tsuper.copy(" + REFERENCES_PARAMETER_NAME + ", mask);");
writer.println("\t\tif ( (mask & GL11.GL_CLIENT_VERTEX_ARRAY_BIT) != 0 ) {");
templates.stream().parallel().filter(tmpl -> tmpl.getKind().isInterface()).map(interface_decl -> {
writer.append("\tvoid copy(" + REFERENCES_CLASS_NAME + " " + REFERENCES_PARAMETER_NAME + ", int mask) {\n");
writer.append("\t\tsuper.copy(" + REFERENCES_PARAMETER_NAME + ", mask);\n");
writer.append("\t\tif ( (mask & GL11.GL_CLIENT_VERTEX_ARRAY_BIT) != 0 ) {\n");
writer.flush();
templates.parallelStream().filter(tmpl -> tmpl.getKind().isInterface()).map(interface_decl -> {
ByteArrayOutputStream writer2 = new ByteArrayOutputStream();
PrintWriter writer3 = new PrintWriter(writer2);
generateCopiesFromMethods(processingEnv, writer, interface_decl);
generateCopiesFromMethods(processingEnv, writer3, interface_decl);
writer3.flush();
return writer2.toByteArray();
}).sequential().forEach(writer1::writeBytes);
writer.println("\t\t}");
writer.println("\t}");
writer.println("\tvoid clear() {");
writer.println("\t\tsuper.clear();");
templates.stream().parallel().filter(tmpl -> tmpl.getKind().isInterface()).map(interface_decl -> {
writer.append("\t\t}\n");
writer.append("\t}\n");
writer.append("\tvoid clear() {\n");
writer.append("\t\tsuper.clear();\n");
writer.flush();
templates.parallelStream().filter(tmpl -> tmpl.getKind().isInterface()).map(interface_decl -> {
ByteArrayOutputStream writer2 = new ByteArrayOutputStream();
PrintWriter writer3 = new PrintWriter(writer2);
generateClearsFromMethods(processingEnv, writer, interface_decl);
generateClearsFromMethods(processingEnv, writer3, interface_decl);
writer3.flush();
return writer2.toByteArray();
}).sequential().forEach(writer1::writeBytes);
writer.println("\t}");
writer.println("}");
writer.append("\t}\n");
writer.append("}\n");
writer.flush();
saveGeneratedJavaSource(env.getMessager(), genJavaPath, "org.lwjgl.opengl." + REFERENCES_CLASS_NAME, spawn((Callable<byte[]>) () -> writer1.toByteArray()), startTime);
}