From 6e894b3ebd0c2d6d2c2551ec013ae8db885d7ab4 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Sat, 27 Dec 2008 01:40:50 +0000 Subject: Support covariant properties. Cojen must support this feature too in order to work (revision 169). --- .../amazon/carbonado/gen/StorableGenerator.java | 46 ++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'src/main/java/com/amazon/carbonado/gen') diff --git a/src/main/java/com/amazon/carbonado/gen/StorableGenerator.java b/src/main/java/com/amazon/carbonado/gen/StorableGenerator.java index d194634..f84748f 100644 --- a/src/main/java/com/amazon/carbonado/gen/StorableGenerator.java +++ b/src/main/java/com/amazon/carbonado/gen/StorableGenerator.java @@ -968,6 +968,8 @@ public final class StorableGenerator { b.returnVoid(); } } + + addPropertyBridges(property); } } @@ -1760,6 +1762,50 @@ public final class StorableGenerator { } } + private void addPropertyBridges(StorableProperty property) { + Class[] covariantTypes = property.getCovariantTypes(); + if (covariantTypes == null || covariantTypes.length == 0) { + return; + } + + // Define copy bridges to allow covariant property types. + + for (Class type : covariantTypes) { + TypeDesc desc = TypeDesc.forClass(type); + + if (property.getReadMethod() != null && + property.getReadMethod().getReturnType() != type) + { + MethodInfo mi = addMethodIfNotFinal + (Modifiers.PUBLIC.toBridge(true), property.getReadMethodName(), desc, null); + + if (mi != null) { + CodeBuilder b = new CodeBuilder(mi); + b.loadThis(); + b.invoke(property.getReadMethod()); + b.returnValue(desc); + } + } + + if (property.getWriteMethod() != null && + property.getWriteMethod().getParameterTypes()[0] != type) + { + // Not actually defined as a bridge method since parameter type differs. + MethodInfo mi = addMethodIfNotFinal + (Modifiers.PUBLIC, property.getWriteMethodName(), null, new TypeDesc[] {desc}); + + if (mi != null) { + CodeBuilder b = new CodeBuilder(mi); + b.loadThis(); + b.loadLocal(b.getParameter(0)); + b.checkCast(TypeDesc.forClass(property.getType())); + b.invoke(property.getWriteMethod()); + b.returnVoid(); + } + } + } + } + /** * Generates a copy properties method with several options to control its * behavior. Although eight combinations can be defined, only four are -- cgit v1.2.3