From 52c3d908e2d0beffff7336052cf3fc9849d8092b Mon Sep 17 00:00:00 2001 From: cparsons Date: Thu, 22 Mar 2018 08:42:22 -0700 Subject: Add annotation-processor verification that only one of Param.type or Param.allowedTypes is used. RELNOTES: None. PiperOrigin-RevId: 190070309 --- .../processor/SkylarkCallableProcessor.java | 26 +++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'src/main/java/com/google/devtools/build/lib/skylarkinterface') 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. * + *
  • + * Each parameter, if explicitly typed, may only use either 'type' or 'allowedTypes', + * not both. + *
  • * * *

    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(); } } -- cgit v1.2.3