Java Code Examples for org.graalvm.compiler.options.processor.OptionProcessor

Following code examples demonstrate how to use org.graalvm.compiler.options.processor.OptionProcessorfrom java. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.graalvm.compiler.options.processor.OptionProcessorand various code implementation of this class.

                if (isRuntime) {
                    out.println("import com.oracle.truffle.api.Option;");
                    out.println("import org.graalvm.options.OptionCategory;");
                } 


    public boolean doProcess(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        if (roundEnv.processingOver()) {
            return true;
        }

        TypeElement sharedTruffleOptionsTypeElement = getTypeElement(SHARED_TRUFFLE_OPTIONS_CLASS_NAME);
        TypeMirror sharedTruffleOptionsTypeMirror = sharedTruffleOptionsTypeElement.asType();

        for (Element element : roundEnv.getElementsAnnotatedWith(sharedTruffleOptionsTypeElement)) {
            Element pkgElement = element.getEnclosingElement();
            if (pkgElement.getKind() != ElementKind.PACKAGE) {
                processingEnv.getMessager().printMessage(Kind.ERROR, "Truffle options holder must be a top level class", element);
                break;
            }
            TypeElement topDeclaringType = (TypeElement) element;

            AnnotationMirror annotation = getAnnotation(topDeclaringType, sharedTruffleOptionsTypeMirror);
            Boolean isRuntime = getAnnotationValue(annotation, "runtime", Boolean.class);
            String className = getAnnotationValue(annotation, "name", String.class);
            String pkg = ((PackageElement) pkgElement).getQualifiedName().toString();
            String optionKeyClassName = isRuntime ? "org.graalvm.options.OptionKey" : "org.graalvm.compiler.options.OptionKey";

            OptionsInfo info = new OptionsInfo(pkg, className);
            info.originatingElements.add(topDeclaringType);
            Filer filer = processingEnv.getFiler();
            try (PrintWriter out = OptionProcessor.createSourceFile(pkg, className, filer, topDeclaringType)) {

                out.println("package " + pkg + ";");
                out.println("");
                out.println("import " + optionKeyClassName + ";");
                if (isRuntime) {
                    out.println("import com.oracle.truffle.api.Option;");
                    out.println("import org.graalvm.options.OptionCategory;");
                }
                out.println("");
                out.println("");
                out.println("public abstract class " + className + " {");

                for (Option option : Option.options) {
                    String defaultValue = option.defaultValue;
                    if (isRuntime) {
                        if (option.javadocExtra != null) {
                            out.printf("    \n");
                        }
                        String help;
                        if (option.help == null) {
                            help = "";
                        } else if (option.help.length > 1) {
                            help = option.help[0] + "%n" + Arrays.asList(option.help)
                                            .subList(1, option.help.length)
                                            .collect(Collectors.joining());
                        } else {
                            help = option.help[0];
                        }
                        if ("null".equals(defaultValue)) {
                            defaultValue = "null, org.graalvm.options.OptionType.defaultType(" + option.type + ".class)";
                        }
                        out.printf("    @Option(help = \"%s\", category = OptionCategory.%s)\n", help, option.category);
                    } else {
                        String optionType;
                        if (option.category.equals("INTERNAL")) {
                            optionType = "Debug";
                        } else {
                            optionType = option.category.charAt(0) + option.category.substring(1).toLowerCase();
                        }
                        out.printf("    \n");

                        String help = option.help[0];
                        List<String> extraHelp = option.help.length > 1 ? Arrays.asList(option.help).subList(1, option.help.length) : new ArrayList<>();
                        info.options.add(new OptionInfo(option.name, optionType, help, extraHelp, option.type, pkg + '.' + className, option.name));
                    }
                    out.printf("    public static final OptionKey<%s> %s = new OptionKey<>(%s);\n", option.type, option.name, defaultValue);
                    out.println();
                }
                out.println("}");

                if (!info.options.isEmpty()) {
                    OptionProcessor.createOptionsDescriptorsFile(processingEnv, info);
                }
            }
        }
        return true;
    } 

Advertisement
Javadoc
Processes static fields annotated with Option. An OptionDescriptors implementation is generated for

each top level class containing at least one such field. The name of the generated class for top level class com.foo.Bar is com.foo.Bar_OptionDescriptors.

Read More
Advertisement