aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/skylarkinterface
diff options
context:
space:
mode:
authorGravatar cparsons <cparsons@google.com>2018-03-22 08:42:22 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-03-22 08:43:31 -0700
commit52c3d908e2d0beffff7336052cf3fc9849d8092b (patch)
tree9fdfe115d2d25d6e3e257119df1b41cfd702b8de /src/main/java/com/google/devtools/build/lib/skylarkinterface
parent8c434685584388a6e5e1b70685e9d9acbb948831 (diff)
Add annotation-processor verification that only one of Param.type or Param.allowedTypes is used.
RELNOTES: None. PiperOrigin-RevId: 190070309
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/skylarkinterface')
-rw-r--r--src/main/java/com/google/devtools/build/lib/skylarkinterface/processor/SkylarkCallableProcessor.java26
1 files changed, 25 insertions, 1 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/skylarkinterface/processor/SkylarkCallableProcessor.java b/src/main/java/com/google/devtools/build/lib/skylarkinterface/processor/SkylarkCallableProcessor.java
index 841bdd1d5a..ef1dc953b7 100644
--- a/src/main/java/com/google/devtools/build/lib/skylarkinterface/processor/SkylarkCallableProcessor.java
+++ b/src/main/java/com/google/devtools/build/lib/skylarkinterface/processor/SkylarkCallableProcessor.java
@@ -30,6 +30,7 @@ import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
+import javax.lang.model.type.MirroredTypeException;
import javax.tools.Diagnostic;
/**
@@ -48,6 +49,10 @@ import javax.tools.Diagnostic;
* The number of method parameters much match the number of annotation-declared parameters
* plus the number of interpreter-supplied parameters.
* </li>
+ * <li>
+ * Each parameter, if explicitly typed, may only use either 'type' or 'allowedTypes',
+ * not both.
+ * </li>
* </ul>
*
* <p>These properties can be relied upon at runtime without additional checks.
@@ -55,7 +60,6 @@ import javax.tools.Diagnostic;
@SupportedAnnotationTypes({"com.google.devtools.build.lib.skylarkinterface.SkylarkCallable"})
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public final class SkylarkCallableProcessor extends AbstractProcessor {
-
private Messager messager;
private static final String LOCATION = "com.google.devtools.build.lib.events.Location";
@@ -104,6 +108,26 @@ public final class SkylarkCallableProcessor extends AbstractProcessor {
+ "empty)",
parameter.name()));
}
+ if ((parameter.allowedTypes().length > 0)
+ && (!"java.lang.Object".equals(paramTypeFieldCanonicalName(parameter)))) {
+ throw new SkylarkCallableProcessorException(
+ methodElement,
+ String.format("Parameter '%s' has both 'type' and 'allowedTypes' specified. Only"
+ + " one may be specified.",
+ parameter.name()));
+ }
+ }
+ }
+
+ private String paramTypeFieldCanonicalName(Param param) {
+ try {
+ return param.type().toString();
+ } catch (MirroredTypeException exception) {
+ // This is a hack to obtain the actual canonical name of param.type(). Doing this ths
+ // "correct" way results in far less readable code.
+ // Since this processor is only for compile-time checks, this isn't efficiency we need
+ // to worry about.
+ return exception.getTypeMirror().toString();
}
}