diff options
Diffstat (limited to 'src/main/java/com/amazon/carbonado/gen')
-rw-r--r-- | src/main/java/com/amazon/carbonado/gen/StorableGenerator.java | 46 |
1 files changed, 42 insertions, 4 deletions
diff --git a/src/main/java/com/amazon/carbonado/gen/StorableGenerator.java b/src/main/java/com/amazon/carbonado/gen/StorableGenerator.java index bb06708..3319d5c 100644 --- a/src/main/java/com/amazon/carbonado/gen/StorableGenerator.java +++ b/src/main/java/com/amazon/carbonado/gen/StorableGenerator.java @@ -21,7 +21,9 @@ package com.amazon.carbonado.gen; import java.lang.annotation.Annotation;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
+import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
@@ -449,8 +451,8 @@ public final class StorableGenerator<S extends Storable> { final int supportParam = 0;
MethodInfo mi = mClassFile.addConstructor(Modifiers.PROTECTED, params);
CodeBuilder b = new CodeBuilder(mi);
- b.loadThis();
- b.invokeSuperConstructor(null);
+
+ addInvokeSuperConstructor(b, supportParam);
//// this.support = support
b.loadThis();
@@ -466,8 +468,8 @@ public final class StorableGenerator<S extends Storable> { final int wrappedStorableParam = 1;
MethodInfo mi = mClassFile.addConstructor(Modifiers.PUBLIC, params);
CodeBuilder b = new CodeBuilder(mi);
- b.loadThis();
- b.invokeSuperConstructor(null);
+
+ addInvokeSuperConstructor(b, wrappedSupportParam);
//// this.wrappedSupport = wrappedSupport
b.loadThis();
@@ -1960,6 +1962,42 @@ public final class StorableGenerator<S extends Storable> { }
/**
+ * Generates code to invoke super class constructor with no arguments or a
+ * Repository.
+ */
+ private void addInvokeSuperConstructor(CodeBuilder b, final int supportParam) {
+ // Look for constructor that accepts a Repository.
+
+ boolean passRepo = false;
+ {
+ for (Constructor c : mStorableType.getDeclaredConstructors()) {
+ int modifiers = c.getModifiers();
+ if (!Modifier.isPublic(modifiers) && !Modifier.isProtected(modifiers)) {
+ continue;
+ }
+ if (c.getParameterTypes().length == 1) {
+ if (c.getParameterTypes()[0] == Repository.class) {
+ passRepo = true;
+ break;
+ }
+ }
+ }
+ }
+
+ b.loadThis();
+
+ if (passRepo) {
+ b.loadLocal(b.getParameter(supportParam));
+ b.invokeInterface(StorableSupport.class.getName(), "getRootRepository",
+ TypeDesc.forClass(Repository.class), null);
+ b.invokeSuperConstructor(new TypeDesc[] {TypeDesc.forClass(Repository.class)});
+ } else {
+ // Assume no-arg constructor.
+ b.invokeSuperConstructor(null);
+ }
+ }
+
+ /**
* If GEN_WRAPPED, generates a method implementation which delgates to the
* WrappedSupport. Also clears join property state if called method
* returns normally.
|