aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google
diff options
context:
space:
mode:
authorGravatar ccalvarin <ccalvarin@google.com>2017-08-23 00:50:12 +0200
committerGravatar Damien Martin-Guillerez <dmarting@google.com>2017-08-23 13:31:28 +0200
commit31599166a464a207168b9abbcc23678487253e57 (patch)
tree50fc1a2270cb4b64a6d8d8ef008d710d6b9f1b06 /src/main/java/com/google
parentdc506f66eb02e883f95bbb295288b3663bc909ff (diff)
Adding compile time checks to the options parser.
Keeping this simple for now, just checking that the options are declared in accepted places. Will convert static, annotation-content based checks to be done here instead of at runtime in a later change. TESTED: Added an option in a non-OptionBase, and verified that it failed to compile. PiperOrigin-RevId: 166126408
Diffstat (limited to 'src/main/java/com/google')
-rw-r--r--src/main/java/com/google/devtools/common/options/BUILD17
-rw-r--r--src/main/java/com/google/devtools/common/options/OptionProcessor.java104
2 files changed, 121 insertions, 0 deletions
diff --git a/src/main/java/com/google/devtools/common/options/BUILD b/src/main/java/com/google/devtools/common/options/BUILD
index 949720187b..42cdff3eae 100644
--- a/src/main/java/com/google/devtools/common/options/BUILD
+++ b/src/main/java/com/google/devtools/common/options/BUILD
@@ -20,6 +20,23 @@ java_library(
java_library(
name = "options",
+ exported_plugins = [":options_preprocessor"],
+ exports = [":options_internal"],
+)
+
+java_plugin(
+ name = "options_preprocessor",
+ srcs = [
+ "OptionProcessor.java",
+ ],
+ processor_class = "com.google.devtools.common.options.OptionProcessor",
+ deps = [
+ ":options_internal",
+ ],
+)
+
+java_library(
+ name = "options_internal",
srcs = glob(
["*.java"],
exclude = [
diff --git a/src/main/java/com/google/devtools/common/options/OptionProcessor.java b/src/main/java/com/google/devtools/common/options/OptionProcessor.java
new file mode 100644
index 0000000000..c2ef64409b
--- /dev/null
+++ b/src/main/java/com/google/devtools/common/options/OptionProcessor.java
@@ -0,0 +1,104 @@
+// Copyright 2017 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+package com.google.devtools.common.options;
+
+import java.util.Set;
+import javax.annotation.processing.AbstractProcessor;
+import javax.annotation.processing.Messager;
+import javax.annotation.processing.ProcessingEnvironment;
+import javax.annotation.processing.RoundEnvironment;
+import javax.annotation.processing.SupportedAnnotationTypes;
+import javax.annotation.processing.SupportedSourceVersion;
+import javax.lang.model.SourceVersion;
+import javax.lang.model.element.Element;
+import javax.lang.model.element.ElementKind;
+import javax.lang.model.element.TypeElement;
+import javax.lang.model.type.TypeMirror;
+import javax.lang.model.util.Elements;
+import javax.lang.model.util.Types;
+import javax.tools.Diagnostic;
+
+/**
+ * Annotation processor for {@link Option}.
+ *
+ * <p>The {@link OptionsParser} only accepts publicly declared options in {@link
+ * OptionsBase}-inheriting classes, and there is no support for {@link Option} annotated fields
+ * declared elsewhere or privately. Prevent such uses from compiling.
+ */
+@SupportedAnnotationTypes({"com.google.devtools.common.options.Option"})
+@SupportedSourceVersion(SourceVersion.RELEASE_8)
+public final class OptionProcessor extends AbstractProcessor {
+
+ private Types typeUtils;
+ private Elements elementUtils;
+ private Messager messager;
+
+ @Override
+ public synchronized void init(ProcessingEnvironment processingEnv) {
+ super.init(processingEnv);
+ typeUtils = processingEnv.getTypeUtils();
+ elementUtils = processingEnv.getElementUtils();
+ messager = processingEnv.getMessager();
+ }
+
+ private static class OptionProcessorException extends Exception {
+ private Element elementInError;
+
+ OptionProcessorException(Element element, String message) {
+ super(message);
+ elementInError = element;
+ }
+ }
+
+ /** Check that the Option variables only occur in OptionBase-inheriting classes. */
+ private void checkInOptionBase(Element annotatedElement) throws OptionProcessorException {
+ if (annotatedElement.getEnclosingElement().getKind() != ElementKind.CLASS) {
+ throw new OptionProcessorException(annotatedElement, "The field should belong to a class.");
+ }
+ TypeMirror thisOptionClass = annotatedElement.getEnclosingElement().asType();
+ TypeMirror optionsBase =
+ elementUtils.getTypeElement("com.google.devtools.common.options.OptionsBase").asType();
+ if (!typeUtils.isAssignable(thisOptionClass, optionsBase)) {
+ throw new OptionProcessorException(
+ annotatedElement,
+ "@Option annotated fields can only be in classes that inherit from OptionsBase.");
+ }
+ }
+
+ @Override
+ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
+ try {
+ for (Element annotatedElement : roundEnv.getElementsAnnotatedWith(Option.class)) {
+ // Only fields are annotated with Option, this should already be checked by the
+ // @Target(ElementType.FIELD) annotation.
+
+ checkInOptionBase(annotatedElement);
+ }
+ } catch (OptionProcessorException e) {
+ error(e.elementInError, e.getMessage());
+ }
+ // Claim all Option annotated fields.
+ return true;
+ }
+
+ /**
+ * Prints an error message & fails the compilation.
+ *
+ * @param e The element which has caused the error. Can be null
+ * @param msg The error message
+ */
+ public void error(Element e, String msg) {
+ messager.printMessage(Diagnostic.Kind.ERROR, msg, e);
+ }
+}