lwjgl/src/java/org/lwjgl/util/generator/GeneratorProcessor.java

101 lines
4.1 KiB
Java

/*
* Copyright (c) 2002-2008 LWJGL Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'LWJGL' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.lwjgl.util.generator;
import java.io.File;
import java.io.FileFilter;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Future;
import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.ElementFilter;
import javax.tools.Diagnostic;
import static org.lwjgl.util.generator.Utils.await;
import static org.lwjgl.util.generator.Utils.getRequiredOption;
import static org.lwjgl.util.generator.Utils.getOptionalOption;
/**
* Generator tool for creating the java classes and native code from an
* annotated template java interface.
*
* @author elias_naur <elias_naur@users.sourceforge.net>
* @version $Revision$ $Id$
*/
@SupportedAnnotationTypes({ "*" })
@SupportedSourceVersion(SourceVersion.RELEASE_10)
@SupportedOptions({ "genJavaPath", "genNativePath", "typemap", "generatechecks", "nogeneratechecks", "contextspecific" })
public final class GeneratorProcessor extends AbstractProcessor {
private static boolean first_round = true;
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if ( roundEnv.processingOver() || !first_round ) {
System.exit(0);
return true;
}
Map<String, String> options = processingEnv.getOptions();
String typemap_classname = getRequiredOption(options, "typemap", "the name of a TypeMap class.");
Path gen_java_path = getOptionalOption(options, "genJavaPath", "a path.", Path::of);
Path gen_native_path = getOptionalOption(options, "genNativePath", "a path.", Path::of);
boolean generate_error_checks = options.containsKey("generatechecks") && !options.containsKey("nogeneratechecks");
boolean context_specific = options.containsKey("contextspecific");
boolean validate = !options.containsKey("novalidate");
try {
TypeMap type_map = (TypeMap)(Class.forName(typemap_classname).newInstance());
GeneratorVisitor visitor = new GeneratorVisitor(processingEnv, gen_java_path, gen_native_path, type_map, generate_error_checks, context_specific, validate);
for (TypeElement file : ElementFilter.typesIn(roundEnv.getRootElements())) {
try {
file.accept(visitor, null);
} catch (Exception e) {
throw new RuntimeException("\n-- Failed to process template: " + file.asType().toString() + " --", e);
}
}
Future<Void> future;
while ((future = visitor.futures.poll()) != null) {
await(future);
}
first_round = false;
return true;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}