From 7541b793dd1057c860403fb93bb82e9b2da15d3d Mon Sep 17 00:00:00 2001 From: "Evan W. Patton" Date: Thu, 10 Oct 2019 15:48:34 -0400 Subject: [PATCH] Raise error when using boxed primitive types in component APIs (#1846) * Raise error when using boxed primitive types in component APIs * Make ComponentProcessor error messages private --- .../scripts/ComponentProcessor.java | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/appinventor/components/src/com/google/appinventor/components/scripts/ComponentProcessor.java b/appinventor/components/src/com/google/appinventor/components/scripts/ComponentProcessor.java index 96848d426f1..e3dffe8071f 100644 --- a/appinventor/components/src/com/google/appinventor/components/scripts/ComponentProcessor.java +++ b/appinventor/components/src/com/google/appinventor/components/scripts/ComponentProcessor.java @@ -111,8 +111,10 @@ public abstract class ComponentProcessor extends AbstractProcessor { private static final String OUTPUT_PACKAGE = ""; - public static final String MISSING_SIMPLE_PROPERTY_ANNOTATION = + private static final String MISSING_SIMPLE_PROPERTY_ANNOTATION = "Designer property %s does not have a corresponding @SimpleProperty annotation."; + private static final String BOXED_TYPE_ERROR = + "Found use of boxed type %s. Please use the primitive type %s instead"; // Returned by getSupportedAnnotationTypes() private static final Set SUPPORTED_ANNOTATION_TYPES = ImmutableSet.of( @@ -148,6 +150,19 @@ public abstract class ComponentProcessor extends AbstractProcessor { private static final String TYPE_PLACEHOLDER = "%type%"; + private static final Map BOXED_TYPES = new HashMap<>(); + + static { + BOXED_TYPES.put("java.lang.Boolean", "boolean"); + BOXED_TYPES.put("java.lang.Byte", "byte"); + BOXED_TYPES.put("java.lang.Char", "char"); + BOXED_TYPES.put("java.lang.Short", "short"); + BOXED_TYPES.put("java.lang.Integer", "int"); + BOXED_TYPES.put("java.lang.Long", "long"); + BOXED_TYPES.put("java.lang.Float", "float"); + BOXED_TYPES.put("java.lang.Double", "double"); + } + // The next two fields are set in init(). /** * A handle allowing access to facilities provided by the annotation @@ -1603,6 +1618,10 @@ private void processConditionalAnnotations(ComponentInfo componentInfo, Element * legal return values */ protected final String javaTypeToYailType(String type) { + if (BOXED_TYPES.containsKey(type)) { + throw new IllegalArgumentException(String.format(BOXED_TYPE_ERROR, type, + BOXED_TYPES.get(type))); + } // boolean -> boolean if (type.equals("boolean")) { return type; @@ -1643,8 +1662,7 @@ protected final String javaTypeToYailType(String type) { return "component"; } - throw new RuntimeException("Cannot convert Java type '" + type + - "' to Yail type"); + throw new IllegalArgumentException("Cannot convert Java type '" + type + "' to Yail type"); } /**