From 6d8eabce931e9d95f3b84214e2b07df7af062406 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Fri, 13 Apr 2012 16:00:46 +0000 Subject: [PATCH] Improve the semantics of @Transform handling. @Transform is first sought on the field itself, which overrides any other handling. Next it is sought on the class that represents the field (i.e. if the field is type Foo we look for an @Transform annotation on class Foo). We avoid doing this somewhat expensive search if the field has a type that we know cannot have an @Transform annotation. If we've still found no @Transform annotation (which is unfortunately the overwhelmingly common case), we then create a stock marshaller. --- .../depot/impl/FieldMarshaller.java | 53 +++++++++++-------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/samskivert/depot/impl/FieldMarshaller.java b/src/main/java/com/samskivert/depot/impl/FieldMarshaller.java index fd44b1c..13561d7 100644 --- a/src/main/java/com/samskivert/depot/impl/FieldMarshaller.java +++ b/src/main/java/com/samskivert/depot/impl/FieldMarshaller.java @@ -68,32 +68,41 @@ public abstract class FieldMarshaller // first, look for a @Transform annotation on the field (cheap) Transform xform = field.getAnnotation(Transform.class); if (xform == null) { - // next look for a marshaller for the basic type (cheapish) - FieldMarshaller marshaller = createMarshaller(field.getType()); - if (marshaller != null) { - marshaller.create(field); - return marshaller; + // next look for an @Transform annotation on the field type; we only do this if it's a + // non-primitive/non-string type because this search is somewhat expensive + Class ftype = field.getType(); + if (!ftype.isPrimitive() && !ftype.equals(String.class)) { + xform = findTransformAnnotation(field.getType()); } - - // finally look for an @Transform annotation on the field type (expensive) - xform = findTransformAnnotation(field.getType()); - checkArgument(xform != null, "Cannot marshall " + field + "."); } - try { - // weirdly if we inline the xformer creation into the createTransformingMarshaller - // call, we get a type error; must be some subtlety of wildcard capture... - Transformer xformer = xform.value().newInstance(); - return createTransformingMarshaller(xformer, field, xform); - } catch (InstantiationException e) { - throw new IllegalArgumentException( - Logger.format("Unable to create Transformer", "xclass", xform.value(), - "field", field), e); - } catch (IllegalAccessException e) { - throw new IllegalArgumentException( - Logger.format("Unable to create Transformer", "xclass", xform.value(), - "field", field), e); + // if we found a transform annotation, create a transforming marshaller + if (xform != null) { + try { + // weirdly if we inline the xformer creation into the createTransformingMarshaller + // call, we get a type error; must be some subtlety of wildcard capture... + Transformer xformer = xform.value().newInstance(); + return createTransformingMarshaller(xformer, field, xform); + } catch (InstantiationException e) { + throw new IllegalArgumentException( + Logger.format("Unable to create Transformer", "xclass", xform.value(), + "field", field), e); + } catch (IllegalAccessException e) { + throw new IllegalArgumentException( + Logger.format("Unable to create Transformer", "xclass", xform.value(), + "field", field), e); + } } + + // last look for a marshaller for the basic type (cheapish) + FieldMarshaller marshaller = createMarshaller(field.getType()); + if (marshaller != null) { + marshaller.create(field); + return marshaller; + } + + // at this point we must throw up our hands + throw new IllegalArgumentException("Cannot marshall " + field + "."); } protected static FieldMarshaller createTransformingMarshaller (