diff options
author | Brian S. O'Neill <bronee@gmail.com> | 2008-04-19 20:59:33 +0000 |
---|---|---|
committer | Brian S. O'Neill <bronee@gmail.com> | 2008-04-19 20:59:33 +0000 |
commit | 487a17e439cf7ae75113ef796d8ee22d2a02f547 (patch) | |
tree | c669ed879a517ecb7e545c70bf8006dd9c537e7d /src/main/java/com | |
parent | d05cb470b1dbe5681039e2aafda331a964535348 (diff) |
Support layout changes when property @Name changes.
Diffstat (limited to 'src/main/java/com')
3 files changed, 62 insertions, 10 deletions
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<? extends Storable> 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)) {
diff --git a/src/main/java/com/amazon/carbonado/raw/GenericEncodingStrategy.java b/src/main/java/com/amazon/carbonado/raw/GenericEncodingStrategy.java index 7cf3468..edb54d8 100644 --- a/src/main/java/com/amazon/carbonado/raw/GenericEncodingStrategy.java +++ b/src/main/java/com/amazon/carbonado/raw/GenericEncodingStrategy.java @@ -48,6 +48,7 @@ import com.amazon.carbonado.info.ChainedProperty; import com.amazon.carbonado.info.Direction;
import com.amazon.carbonado.info.OrderedProperty;
import com.amazon.carbonado.info.StorableIndex;
+import com.amazon.carbonado.info.StorableInfo;
import com.amazon.carbonado.info.StorableIntrospector;
import com.amazon.carbonado.info.StorableProperty;
import com.amazon.carbonado.info.StorablePropertyAdapter;
@@ -2347,18 +2348,31 @@ public class GenericEncodingStrategy<S extends Storable> { doDrop: {
Class instanceVarClass = instanceVarType.toClass();
if (instanceVarClass != null) {
- Map<String, BeanProperty> props =
- BeanIntrospector.getAllProperties(instanceVarClass);
- BeanProperty prop = props.get(info.getPropertyName());
- if (prop != null) {
- if (prop.getType() == type.toClass()) {
+ Class propertyClass;
+ {
+ Class inferredType = StorableIntrospector.inferType(instanceVarClass);
+ if (inferredType != null) {
+ StorableInfo propInfo = StorableIntrospector.examine(inferredType);
+ Map<String, StorableProperty> props = propInfo.getAllProperties();
+ StorableProperty prop = props.get(info.getPropertyName());
+ propertyClass = prop == null ? null : prop.getType();
+ } else {
+ Map<String, BeanProperty> props =
+ BeanIntrospector.getAllProperties(instanceVarClass);
+ BeanProperty prop = props.get(info.getPropertyName());
+ propertyClass = prop == null ? null : prop.getType();
+ }
+ }
+
+ if (propertyClass != null) {
+ if (propertyClass == type.toClass()) {
break doDrop;
}
// Types differ, but if primitive types, perform conversion.
TypeDesc primType = type.toPrimitiveType();
if (primType != null) {
- TypeDesc propType = TypeDesc.forClass(prop.getType());
+ TypeDesc propType = TypeDesc.forClass(propertyClass);
TypeDesc primPropType = propType.toPrimitiveType();
if (primPropType != null) {
// Apply conversion and store property.
diff --git a/src/main/java/com/amazon/carbonado/raw/StorablePropertyInfo.java b/src/main/java/com/amazon/carbonado/raw/StorablePropertyInfo.java index e3a18b1..18db76b 100644 --- a/src/main/java/com/amazon/carbonado/raw/StorablePropertyInfo.java +++ b/src/main/java/com/amazon/carbonado/raw/StorablePropertyInfo.java @@ -129,4 +129,9 @@ public class StorablePropertyInfo implements GenericPropertyInfo { getWriteMethodName(), null, new TypeDesc[] {getPropertyType()});
}
}
+
+ @Override
+ public String toString() {
+ return mProp.toString();
+ }
}
|