private void generateCLPDCapabilitiesSource(final Class<? extends Annotation> capsType, final String capsName, final Class<? extends PointerWrapper> objectType, final String objectName) throws IOException { final PrintWriter writer = env.getFiler().createTextFile(Filer.Location.SOURCE_TREE, "org.lwjgl.opencl", new File(capsName + ".java"), null); printHeader(writer); writer.println("import java.util.*;"); writer.println(); CLPDCapabilitiesGenerator.generateClassPrologue(writer, capsName); final Collection<TypeDeclaration> templates = DeclarationFilter.getFilter(InterfaceDeclaration.class).filter(env.getSpecifiedTypeDeclarations()); for ( final TypeDeclaration t : templates ) { if ( t.getAnnotation(capsType) != null ) CLPDCapabilitiesGenerator.generateExtensions(writer, (InterfaceDeclaration)t); } writer.println(); CLPDCapabilitiesGenerator.generateConstructor(writer, templates, capsType, capsName, objectType, objectName); CLPDCapabilitiesGenerator.generateGetters(writer); CLPDCapabilitiesGenerator.generateToString(writer, templates, capsType); writer.println("}"); writer.close(); }
static void generateConstructor(final PrintWriter writer, final Collection<TypeDeclaration> templates, final Class<? extends Annotation> capsType, final String capsName, final Class<? extends PointerWrapper> objectType, final String objectName) { writer.println("\tpublic " + capsName + "(final " + objectType.getSimpleName() + ' ' + objectName + ") {"); writer.println("\t\tfinal String extensionList = " + objectName + ".getInfoString(CL10.CL_" + objectName.toUpperCase() + "_EXTENSIONS);\n" + "\t\tfinal String version = " + objectName + ".getInfoString(CL10.CL_" + objectName.toUpperCase() + "_VERSION);\n" + "\t\tif ( !version.startsWith(\"OpenCL \") )\n" + "\t\t\tthrow new RuntimeException(\"Invalid OpenCL version string: \" + version);\n\n" + "\t\ttry {\n" + "\t\t\tfinal StringTokenizer tokenizer = new StringTokenizer(version.substring(7), \". \");\n" + "\n" + "\t\t\tmajorVersion = Integer.parseInt(tokenizer.nextToken());\n" + "\t\t\tminorVersion = Integer.parseInt(tokenizer.nextToken());\n"); for ( int major = 1; major <= CL_VERSIONS.length; major++ ) { for ( final int minor : CL_VERSIONS[major - 1] ) writer.println("\t\t\tOpenCL" + Integer.toString(major) + Integer.toString(minor) + " = " + major + " < majorVersion || (" + major + " == majorVersion && " + minor + " <= minorVersion);"); } writer.println("\t\t} catch (RuntimeException e) {\n" + "\t\t\tthrow new RuntimeException(\"The major and/or minor OpenCL version \\\"\" + version + \"\\\" is malformed: \" + e.getMessage());\n" + "\t\t}\n"); writer.println("\t\tfinal Set<String> extensions = APIUtil.getExtensions(extensionList);"); for ( final TypeDeclaration t : templates ) { if ( t.getAnnotation(capsType) == null ) continue; final String extName = CLGeneratorProcessorFactory.getExtensionName(t.getSimpleName()); writer.print("\t\t" + extName + " = extensions.contains(\"" + extName.toLowerCase() + "\")"); if ( !t.getMethods().isEmpty() ) writer.print(" && CLCapabilities." + extName); writer.println(";"); } writer.println("\t}\n"); }