annotate graal/com.oracle.jvmci.options.processor/src/com/oracle/jvmci/options/processor/OptionProcessor.java @ 21554:b1530a6cce8c

renamed com.oracle.graal.[debug|options|hotspotvmconfig]* modules to com.oracle.jvmci.[debug|options|hotspotvmconfig]* modules (JBS:GRAAL-53)
author Doug Simon <doug.simon@oracle.com>
date Tue, 26 May 2015 23:21:15 +0200
parents graal/com.oracle.graal.options.processor/src/com/oracle/graal/options/processor/OptionProcessor.java@71e05c3a1e12
children d563baeca9df
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9849
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
1 /*
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
4 *
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
7 * published by the Free Software Foundation.
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
8 *
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
13 * accompanied this code).
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
14 *
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
18 *
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
21 * questions.
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
22 */
21554
b1530a6cce8c renamed com.oracle.graal.[debug|options|hotspotvmconfig]* modules to com.oracle.jvmci.[debug|options|hotspotvmconfig]* modules (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21096
diff changeset
23 package com.oracle.jvmci.options.processor;
9849
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
24
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
25 import java.io.*;
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
26 import java.util.*;
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
27
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
28 import javax.annotation.processing.*;
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
29 import javax.lang.model.*;
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
30 import javax.lang.model.element.*;
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
31 import javax.lang.model.type.*;
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
32 import javax.lang.model.util.*;
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
33 import javax.tools.Diagnostic.Kind;
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
34 import javax.tools.*;
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
35
21554
b1530a6cce8c renamed com.oracle.graal.[debug|options|hotspotvmconfig]* modules to com.oracle.jvmci.[debug|options|hotspotvmconfig]* modules (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21096
diff changeset
36 import com.oracle.jvmci.options.*;
b1530a6cce8c renamed com.oracle.graal.[debug|options|hotspotvmconfig]* modules to com.oracle.jvmci.[debug|options|hotspotvmconfig]* modules (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21096
diff changeset
37
21096
71e05c3a1e12 Move OptionProcessor to its own project
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 16732
diff changeset
38
9849
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
39 /**
9919
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
40 * Processes static fields annotated with {@link Option}. An {@link Options} service is generated
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
41 * for each top level class containing at least one such field. These service objects can be
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
42 * retrieved as follows:
15043
33eee38d70a3 Fix raw type references in OptionProcessor.
Josef Eisl <josef.eisl@jku.at>
parents: 14763
diff changeset
43 *
9849
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
44 * <pre>
16732
c958f16fd52f use correct class loader when looking up services
Doug Simon <doug.simon@oracle.com>
parents: 16010
diff changeset
45 * ServiceLoader&lt;Options&gt; sl = ServiceLoader.load(Options.class);
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15043
diff changeset
46 * for (Options opts : sl) {
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15043
diff changeset
47 * for (OptionDescriptor desc : sl) {
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15043
diff changeset
48 * // use desc
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15043
diff changeset
49 * }
9849
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
50 * }
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
51 * </pre>
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
52 */
21554
b1530a6cce8c renamed com.oracle.graal.[debug|options|hotspotvmconfig]* modules to com.oracle.jvmci.[debug|options|hotspotvmconfig]* modules (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21096
diff changeset
53 @SupportedAnnotationTypes({"com.oracle.jvmci.options.Option"})
9849
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
54 public class OptionProcessor extends AbstractProcessor {
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
55
14763
a6c1c3eb20c4 transition to JDK8
Doug Simon <doug.simon@oracle.com>
parents: 14596
diff changeset
56 @Override
a6c1c3eb20c4 transition to JDK8
Doug Simon <doug.simon@oracle.com>
parents: 14596
diff changeset
57 public SourceVersion getSupportedSourceVersion() {
a6c1c3eb20c4 transition to JDK8
Doug Simon <doug.simon@oracle.com>
parents: 14596
diff changeset
58 return SourceVersion.latest();
a6c1c3eb20c4 transition to JDK8
Doug Simon <doug.simon@oracle.com>
parents: 14596
diff changeset
59 }
a6c1c3eb20c4 transition to JDK8
Doug Simon <doug.simon@oracle.com>
parents: 14596
diff changeset
60
9849
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
61 private final Set<Element> processed = new HashSet<>();
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
62
9919
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
63 private void processElement(Element element, OptionsInfo info) {
9849
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
64
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
65 if (!element.getModifiers().contains(Modifier.STATIC)) {
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
66 processingEnv.getMessager().printMessage(Kind.ERROR, "Option field must be static", element);
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
67 return;
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
68 }
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
69
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
70 Option annotation = element.getAnnotation(Option.class);
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
71 assert annotation != null;
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
72 assert element instanceof VariableElement;
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
73 assert element.getKind() == ElementKind.FIELD;
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
74 VariableElement field = (VariableElement) element;
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
75 String fieldName = field.getSimpleName().toString();
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
76
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
77 Elements elements = processingEnv.getElementUtils();
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
78 Types types = processingEnv.getTypeUtils();
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
79
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
80 TypeMirror fieldType = field.asType();
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
81 if (fieldType.getKind() != TypeKind.DECLARED) {
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
82 processingEnv.getMessager().printMessage(Kind.ERROR, "Option field must be of type " + OptionValue.class.getName(), element);
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
83 return;
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
84 }
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
85 DeclaredType declaredFieldType = (DeclaredType) fieldType;
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
86
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
87 TypeMirror optionValueType = elements.getTypeElement(OptionValue.class.getName()).asType();
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
88 if (!types.isSubtype(fieldType, types.erasure(optionValueType))) {
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
89 String msg = String.format("Option field type %s is not a subclass of %s", fieldType, optionValueType);
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
90 processingEnv.getMessager().printMessage(Kind.ERROR, msg, element);
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
91 return;
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
92 }
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
93
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
94 if (!field.getModifiers().contains(Modifier.STATIC)) {
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
95 processingEnv.getMessager().printMessage(Kind.ERROR, "Option field must be static", element);
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
96 return;
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
97 }
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
98
11378
7e297d001c30 option annotation processor ensures that a non-empty help message starts with a capital letter
Doug Simon <doug.simon@oracle.com>
parents: 11363
diff changeset
99 String help = annotation.help();
7e297d001c30 option annotation processor ensures that a non-empty help message starts with a capital letter
Doug Simon <doug.simon@oracle.com>
parents: 11363
diff changeset
100 if (help.length() != 0) {
7e297d001c30 option annotation processor ensures that a non-empty help message starts with a capital letter
Doug Simon <doug.simon@oracle.com>
parents: 11363
diff changeset
101 char firstChar = help.charAt(0);
7e297d001c30 option annotation processor ensures that a non-empty help message starts with a capital letter
Doug Simon <doug.simon@oracle.com>
parents: 11363
diff changeset
102 if (!Character.isUpperCase(firstChar)) {
7e297d001c30 option annotation processor ensures that a non-empty help message starts with a capital letter
Doug Simon <doug.simon@oracle.com>
parents: 11363
diff changeset
103 processingEnv.getMessager().printMessage(Kind.ERROR, "Option help text must start with upper case letter", element);
7e297d001c30 option annotation processor ensures that a non-empty help message starts with a capital letter
Doug Simon <doug.simon@oracle.com>
parents: 11363
diff changeset
104 return;
7e297d001c30 option annotation processor ensures that a non-empty help message starts with a capital letter
Doug Simon <doug.simon@oracle.com>
parents: 11363
diff changeset
105 }
7e297d001c30 option annotation processor ensures that a non-empty help message starts with a capital letter
Doug Simon <doug.simon@oracle.com>
parents: 11363
diff changeset
106 }
7e297d001c30 option annotation processor ensures that a non-empty help message starts with a capital letter
Doug Simon <doug.simon@oracle.com>
parents: 11363
diff changeset
107
9849
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
108 String optionName = annotation.name();
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
109 if (optionName.equals("")) {
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
110 optionName = fieldName;
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
111 }
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
112
16007
23ae0cbcb2ae OptionProcessor needs to find the type parameter of an OptionValue field declaration
Doug Simon <doug.simon@oracle.com>
parents: 15370
diff changeset
113 DeclaredType declaredOptionValueType = declaredFieldType;
23ae0cbcb2ae OptionProcessor needs to find the type parameter of an OptionValue field declaration
Doug Simon <doug.simon@oracle.com>
parents: 15370
diff changeset
114 while (!types.isSameType(types.erasure(declaredOptionValueType), types.erasure(optionValueType))) {
23ae0cbcb2ae OptionProcessor needs to find the type parameter of an OptionValue field declaration
Doug Simon <doug.simon@oracle.com>
parents: 15370
diff changeset
115 List<? extends TypeMirror> directSupertypes = types.directSupertypes(declaredFieldType);
23ae0cbcb2ae OptionProcessor needs to find the type parameter of an OptionValue field declaration
Doug Simon <doug.simon@oracle.com>
parents: 15370
diff changeset
116 assert !directSupertypes.isEmpty();
23ae0cbcb2ae OptionProcessor needs to find the type parameter of an OptionValue field declaration
Doug Simon <doug.simon@oracle.com>
parents: 15370
diff changeset
117 declaredOptionValueType = (DeclaredType) directSupertypes.get(0);
23ae0cbcb2ae OptionProcessor needs to find the type parameter of an OptionValue field declaration
Doug Simon <doug.simon@oracle.com>
parents: 15370
diff changeset
118 }
23ae0cbcb2ae OptionProcessor needs to find the type parameter of an OptionValue field declaration
Doug Simon <doug.simon@oracle.com>
parents: 15370
diff changeset
119
23ae0cbcb2ae OptionProcessor needs to find the type parameter of an OptionValue field declaration
Doug Simon <doug.simon@oracle.com>
parents: 15370
diff changeset
120 assert !declaredOptionValueType.getTypeArguments().isEmpty();
23ae0cbcb2ae OptionProcessor needs to find the type parameter of an OptionValue field declaration
Doug Simon <doug.simon@oracle.com>
parents: 15370
diff changeset
121 String optionType = declaredOptionValueType.getTypeArguments().get(0).toString();
9849
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
122 if (optionType.startsWith("java.lang.")) {
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
123 optionType = optionType.substring("java.lang.".length());
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
124 }
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
125
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
126 Element enclosing = element.getEnclosingElement();
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
127 String declaringClass = "";
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
128 String separator = "";
9919
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
129 Set<Element> originatingElementsList = info.originatingElements;
9865
9006bc30a951 add all enclosing elements of an annotated field as originating elements
Doug Simon <doug.simon@oracle.com>
parents: 9849
diff changeset
130 originatingElementsList.add(field);
9849
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
131 while (enclosing != null) {
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
132 if (enclosing.getKind() == ElementKind.CLASS || enclosing.getKind() == ElementKind.INTERFACE) {
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
133 if (enclosing.getModifiers().contains(Modifier.PRIVATE)) {
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
134 String msg = String.format("Option field cannot be declared in a private %s %s", enclosing.getKind().name().toLowerCase(), enclosing);
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
135 processingEnv.getMessager().printMessage(Kind.ERROR, msg, element);
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
136 return;
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
137 }
9919
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
138 originatingElementsList.add(enclosing);
9849
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
139 declaringClass = enclosing.getSimpleName() + separator + declaringClass;
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
140 separator = ".";
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
141 } else {
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
142 assert enclosing.getKind() == ElementKind.PACKAGE;
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
143 }
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
144 enclosing = enclosing.getEnclosingElement();
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
145 }
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
146
11378
7e297d001c30 option annotation processor ensures that a non-empty help message starts with a capital letter
Doug Simon <doug.simon@oracle.com>
parents: 11363
diff changeset
147 info.options.add(new OptionInfo(optionName, help, optionType, declaringClass, field));
9919
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
148 }
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
149
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
150 private void createFiles(OptionsInfo info) {
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
151 String pkg = ((PackageElement) info.topDeclaringType.getEnclosingElement()).getQualifiedName().toString();
9986
4f542ceb5fed added VerifyHotSpotOptionsPhase to ensure that global state is not initialized from options prior to command line parsing
Doug Simon <doug.simon@oracle.com>
parents: 9937
diff changeset
152 Name topDeclaringClass = info.topDeclaringType.getSimpleName();
9919
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
153
9986
4f542ceb5fed added VerifyHotSpotOptionsPhase to ensure that global state is not initialized from options prior to command line parsing
Doug Simon <doug.simon@oracle.com>
parents: 9937
diff changeset
154 String optionsClassName = topDeclaringClass + "_" + Options.class.getSimpleName();
9919
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
155 Element[] originatingElements = info.originatingElements.toArray(new Element[info.originatingElements.size()]);
9849
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
156
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
157 Filer filer = processingEnv.getFiler();
9919
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
158 try (PrintWriter out = createSourceFile(pkg, optionsClassName, filer, originatingElements)) {
9849
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
159
9901
4fcd38b13eb1 disabled emitting and checking of copyright header in files generated by OptionProcessor
Doug Simon <doug.simon@oracle.com>
parents: 9865
diff changeset
160 out.println("// CheckStyle: stop header check");
9919
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
161 out.println("// GENERATED CONTENT - DO NOT EDIT");
9986
4f542ceb5fed added VerifyHotSpotOptionsPhase to ensure that global state is not initialized from options prior to command line parsing
Doug Simon <doug.simon@oracle.com>
parents: 9937
diff changeset
162 out.println("// Source: " + topDeclaringClass + ".java");
9849
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
163 out.println("package " + pkg + ";");
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
164 out.println("");
9919
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
165 out.println("import java.util.*;");
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
166 out.println("import " + Options.class.getPackage().getName() + ".*;");
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
167 out.println("");
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
168 out.println("public class " + optionsClassName + " implements " + Options.class.getSimpleName() + " {");
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
169 out.println(" @Override");
9986
4f542ceb5fed added VerifyHotSpotOptionsPhase to ensure that global state is not initialized from options prior to command line parsing
Doug Simon <doug.simon@oracle.com>
parents: 9937
diff changeset
170 String desc = OptionDescriptor.class.getSimpleName();
4f542ceb5fed added VerifyHotSpotOptionsPhase to ensure that global state is not initialized from options prior to command line parsing
Doug Simon <doug.simon@oracle.com>
parents: 9937
diff changeset
171 out.println(" public Iterator<" + desc + "> iterator() {");
11363
b332bfd012a4 added suppression filter so that long help messages for Options can be supported without triggering Checkstyle errors in generated sources
Doug Simon <doug.simon@oracle.com>
parents: 9986
diff changeset
172 out.println(" // CheckStyle: stop line length check");
9986
4f542ceb5fed added VerifyHotSpotOptionsPhase to ensure that global state is not initialized from options prior to command line parsing
Doug Simon <doug.simon@oracle.com>
parents: 9937
diff changeset
173 out.println(" List<" + desc + "> options = Arrays.asList(");
9919
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
174
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
175 boolean needPrivateFieldAccessor = false;
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
176 int i = 0;
13944
911e540a2116 Sort options alphabetically so that diffs do not show false positives
Christian Wimmer <christian.wimmer@oracle.com>
parents: 11378
diff changeset
177 Collections.sort(info.options);
9919
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
178 for (OptionInfo option : info.options) {
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
179 String optionValue;
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
180 if (option.field.getModifiers().contains(Modifier.PRIVATE)) {
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
181 needPrivateFieldAccessor = true;
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
182 optionValue = "field(" + option.declaringClass + ".class, \"" + option.field.getSimpleName() + "\")";
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
183 } else {
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
184 optionValue = option.declaringClass + "." + option.field.getSimpleName();
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
185 }
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
186 String name = option.name;
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
187 String type = option.type;
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
188 String help = option.help;
9986
4f542ceb5fed added VerifyHotSpotOptionsPhase to ensure that global state is not initialized from options prior to command line parsing
Doug Simon <doug.simon@oracle.com>
parents: 9937
diff changeset
189 String declaringClass = option.declaringClass;
4f542ceb5fed added VerifyHotSpotOptionsPhase to ensure that global state is not initialized from options prior to command line parsing
Doug Simon <doug.simon@oracle.com>
parents: 9937
diff changeset
190 Name fieldName = option.field.getSimpleName();
9919
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
191 String comma = i == info.options.size() - 1 ? "" : ",";
14596
2e35f6eb8684 Fixed option processor to always produce unix line feeds which led to checkstyle warnings on windows.
Christian Humer <christian.humer@gmail.com>
parents: 14157
diff changeset
192 out.printf(" new %s(\"%s\", %s.class, \"%s\", %s.class, \"%s\", %s)%s\n", desc, name, type, help, declaringClass, fieldName, optionValue, comma);
9919
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
193 i++;
9849
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
194 }
9919
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
195 out.println(" );");
11363
b332bfd012a4 added suppression filter so that long help messages for Options can be supported without triggering Checkstyle errors in generated sources
Doug Simon <doug.simon@oracle.com>
parents: 9986
diff changeset
196 out.println(" // CheckStyle: resume line length check");
9919
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
197 out.println(" return options.iterator();");
9849
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
198 out.println(" }");
9919
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
199 if (needPrivateFieldAccessor) {
15043
33eee38d70a3 Fix raw type references in OptionProcessor.
Josef Eisl <josef.eisl@jku.at>
parents: 14763
diff changeset
200 out.println(" private static " + OptionValue.class.getSimpleName() + "<?> field(Class<?> declaringClass, String fieldName) {");
9849
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
201 out.println(" try {");
9919
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
202 out.println(" java.lang.reflect.Field field = declaringClass.getDeclaredField(fieldName);");
9849
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
203 out.println(" field.setAccessible(true);");
15043
33eee38d70a3 Fix raw type references in OptionProcessor.
Josef Eisl <josef.eisl@jku.at>
parents: 14763
diff changeset
204 out.println(" return (" + OptionValue.class.getSimpleName() + "<?>) field.get(null);");
9849
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
205 out.println(" } catch (Exception e) {");
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
206 out.println(" throw (InternalError) new InternalError().initCause(e);");
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
207 out.println(" }");
9919
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
208 out.println(" }");
9849
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
209 }
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
210 out.println("}");
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
211 }
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
212
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
213 try {
9919
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
214 createProviderFile(pkg, optionsClassName, originatingElements);
9849
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
215 } catch (IOException e) {
9919
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
216 processingEnv.getMessager().printMessage(Kind.ERROR, e.getMessage(), info.topDeclaringType);
9849
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
217 }
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
218 }
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
219
9865
9006bc30a951 add all enclosing elements of an annotated field as originating elements
Doug Simon <doug.simon@oracle.com>
parents: 9849
diff changeset
220 private void createProviderFile(String pkg, String providerClassName, Element... originatingElements) throws IOException {
9849
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
221 String filename = "META-INF/providers/" + pkg + "." + providerClassName;
9865
9006bc30a951 add all enclosing elements of an annotated field as originating elements
Doug Simon <doug.simon@oracle.com>
parents: 9849
diff changeset
222 FileObject file = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", filename, originatingElements);
9849
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
223 PrintWriter writer = new PrintWriter(new OutputStreamWriter(file.openOutputStream(), "UTF-8"));
9919
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
224 writer.println(Options.class.getName());
9849
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
225 writer.close();
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
226 }
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
227
9865
9006bc30a951 add all enclosing elements of an annotated field as originating elements
Doug Simon <doug.simon@oracle.com>
parents: 9849
diff changeset
228 protected PrintWriter createSourceFile(String pkg, String relativeName, Filer filer, Element... originatingElements) {
9849
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
229 try {
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
230 // Ensure Unix line endings to comply with Graal code style guide checked by Checkstyle
9865
9006bc30a951 add all enclosing elements of an annotated field as originating elements
Doug Simon <doug.simon@oracle.com>
parents: 9849
diff changeset
231 JavaFileObject sourceFile = filer.createSourceFile(pkg + "." + relativeName, originatingElements);
9849
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
232 return new PrintWriter(sourceFile.openWriter()) {
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
233
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
234 @Override
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
235 public void println() {
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
236 print("\n");
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
237 }
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
238 };
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
239 } catch (IOException e) {
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
240 throw new RuntimeException(e);
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
241 }
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
242 }
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
243
13944
911e540a2116 Sort options alphabetically so that diffs do not show false positives
Christian Wimmer <christian.wimmer@oracle.com>
parents: 11378
diff changeset
244 static class OptionInfo implements Comparable<OptionInfo> {
9919
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
245
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
246 final String name;
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
247 final String help;
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
248 final String type;
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
249 final String declaringClass;
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
250 final VariableElement field;
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
251
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
252 public OptionInfo(String name, String help, String type, String declaringClass, VariableElement field) {
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
253 this.name = name;
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
254 this.help = help;
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
255 this.type = type;
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
256 this.declaringClass = declaringClass;
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
257 this.field = field;
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
258 }
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
259
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
260 @Override
13944
911e540a2116 Sort options alphabetically so that diffs do not show false positives
Christian Wimmer <christian.wimmer@oracle.com>
parents: 11378
diff changeset
261 public int compareTo(OptionInfo other) {
911e540a2116 Sort options alphabetically so that diffs do not show false positives
Christian Wimmer <christian.wimmer@oracle.com>
parents: 11378
diff changeset
262 return name.compareTo(other.name);
911e540a2116 Sort options alphabetically so that diffs do not show false positives
Christian Wimmer <christian.wimmer@oracle.com>
parents: 11378
diff changeset
263 }
911e540a2116 Sort options alphabetically so that diffs do not show false positives
Christian Wimmer <christian.wimmer@oracle.com>
parents: 11378
diff changeset
264
911e540a2116 Sort options alphabetically so that diffs do not show false positives
Christian Wimmer <christian.wimmer@oracle.com>
parents: 11378
diff changeset
265 @Override
9919
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
266 public String toString() {
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
267 return declaringClass + "." + field;
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
268 }
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
269 }
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
270
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
271 static class OptionsInfo {
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
272
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
273 final Element topDeclaringType;
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
274 final List<OptionInfo> options = new ArrayList<>();
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
275 final Set<Element> originatingElements = new HashSet<>();
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
276
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
277 public OptionsInfo(Element topDeclaringType) {
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
278 this.topDeclaringType = topDeclaringType;
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
279 }
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
280 }
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
281
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
282 private static Element topDeclaringType(Element element) {
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
283 Element enclosing = element.getEnclosingElement();
14118
380984cdb29d fixed incorrect test for null
Doug Simon <doug.simon@oracle.com>
parents: 13944
diff changeset
284 if (enclosing == null || enclosing.getKind() == ElementKind.PACKAGE) {
9919
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
285 assert element.getKind() == ElementKind.CLASS || element.getKind() == ElementKind.INTERFACE;
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
286 return element;
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
287 }
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
288 return topDeclaringType(enclosing);
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
289 }
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
290
9849
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
291 @Override
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
292 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
293 if (roundEnv.processingOver()) {
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
294 return true;
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
295 }
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
296
9919
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
297 Map<Element, OptionsInfo> map = new HashMap<>();
9849
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
298 for (Element element : roundEnv.getElementsAnnotatedWith(Option.class)) {
9919
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
299 if (!processed.contains(element)) {
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
300 processed.add(element);
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
301 Element topDeclaringType = topDeclaringType(element);
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
302 OptionsInfo options = map.get(topDeclaringType);
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
303 if (options == null) {
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
304 options = new OptionsInfo(topDeclaringType);
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
305 map.put(topDeclaringType, options);
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
306 }
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
307 processElement(element, options);
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
308 }
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
309 }
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
310
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
311 boolean ok = true;
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
312 Map<String, OptionInfo> uniqueness = new HashMap<>();
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
313 for (OptionsInfo info : map.values()) {
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
314 for (OptionInfo option : info.options) {
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
315 OptionInfo conflict = uniqueness.put(option.name, option);
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
316 if (conflict != null) {
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
317 processingEnv.getMessager().printMessage(Kind.ERROR, "Duplicate option names for " + option + " and " + conflict, option.field);
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
318 ok = false;
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
319 }
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
320 }
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
321 }
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
322
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
323 if (ok) {
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
324 for (OptionsInfo info : map.values()) {
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
325 createFiles(info);
af909f4b80a9 options are grouped per top level class/interface when accessed via the service mechanism
Doug Simon <doug.simon@oracle.com>
parents: 9901
diff changeset
326 }
9849
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
327 }
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
328
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
329 return true;
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
330 }
e876c2a6954f extensible option system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
331 }