Skip to content

Commit

Permalink
Raise error when using boxed primitive types in component APIs (#1846)
Browse files Browse the repository at this point in the history
* Raise error when using boxed primitive types in component APIs
* Make ComponentProcessor error messages private
  • Loading branch information
ewpatton authored and SusanRatiLane committed Oct 10, 2019
1 parent c2a2ac1 commit 7541b79
Showing 1 changed file with 21 additions and 3 deletions.
Expand Up @@ -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<String> SUPPORTED_ANNOTATION_TYPES = ImmutableSet.of(
Expand Down Expand Up @@ -148,6 +150,19 @@ public abstract class ComponentProcessor extends AbstractProcessor {

private static final String TYPE_PLACEHOLDER = "%type%";

private static final Map<String, String> 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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}

/**
Expand Down

0 comments on commit 7541b79

Please sign in to comment.