From 487a17e439cf7ae75113ef796d8ee22d2a02f547 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Sat, 19 Apr 2008 20:59:33 +0000 Subject: Support layout changes when property @Name changes. --- .../carbonado/info/StorableIntrospector.java | 41 +++++++++++++++++++--- 1 file changed, 37 insertions(+), 4 deletions(-) (limited to 'src/main/java/com/amazon/carbonado/info') diff --git a/src/main/java/com/amazon/carbonado/info/StorableIntrospector.java b/src/main/java/com/amazon/carbonado/info/StorableIntrospector.java index b21c8f3..ebcffb5 100644 --- a/src/main/java/com/amazon/carbonado/info/StorableIntrospector.java +++ b/src/main/java/com/amazon/carbonado/info/StorableIntrospector.java @@ -90,10 +90,10 @@ public class StorableIntrospector { /** * Examines the given class and returns a StorableInfo describing it. A * MalformedTypeException is thrown for a variety of reasons if the given - * class is not a well-defined Storable type. + * class is an invalid Storable type. * * @param type Storable type to examine - * @throws MalformedTypeException if Storable type is not well-formed + * @throws MalformedTypeException if Storable type is invalid * @throws IllegalArgumentException if type is null */ @SuppressWarnings("unchecked") @@ -309,6 +309,40 @@ public class StorableIntrospector { } } + /** + * Examines a class and determines what Storable type it implements. If it + * cannot be unambiguously inferred, null is returned. A non-null return + * value does not imply that the Storable type is valid, however. It must + * be {@link #examine examined} to check validity. + * + * @since 1.2 + */ + public static Class inferType(Class clazz) { + if (clazz == null || !Storable.class.isAssignableFrom(clazz)) { + return null; + } + + if (clazz.isAnnotationPresent(PrimaryKey.class)) { + return clazz; + } + + Class candidate = inferType(clazz.getSuperclass()); + + for (Class iface : clazz.getInterfaces()) { + Class inferred = inferType(iface); + if (inferred != null) { + if (candidate == null) { + candidate = inferred; + } else { + // Inference is ambiguous. + return null; + } + } + } + + return candidate; + } + private static class NameAndDirection { final String name; final Direction direction; @@ -487,8 +521,7 @@ public class StorableIntrospector { throw new MalformedTypeException(type, "Storable interface must be extended"); } } else { - throw new MalformedTypeException - (type, "Does not implement Storable interface"); + throw new MalformedTypeException(type, "Does not implement Storable interface"); } int modifiers = type.getModifiers(); if (Modifier.isFinal(modifiers)) { -- cgit v1.2.3