You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mnemonic.apache.org by ga...@apache.org on 2018/02/22 04:47:27 UTC

mnemonic git commit: MNEMONIC-455: Simply the Factory proxy construction

Repository: mnemonic
Updated Branches:
  refs/heads/master a2abf28b3 -> eb9e7f8bd


MNEMONIC-455: Simply the Factory proxy construction


Project: http://git-wip-us.apache.org/repos/asf/mnemonic/repo
Commit: http://git-wip-us.apache.org/repos/asf/mnemonic/commit/eb9e7f8b
Tree: http://git-wip-us.apache.org/repos/asf/mnemonic/tree/eb9e7f8b
Diff: http://git-wip-us.apache.org/repos/asf/mnemonic/diff/eb9e7f8b

Branch: refs/heads/master
Commit: eb9e7f8bd67a9a4ea0b0c027e5638f8fbbd366f4
Parents: a2abf28
Author: Yanhui Zhao <yz...@apache.org>
Authored: Tue Feb 20 21:21:14 2018 -0800
Committer: Yanhui Zhao <yz...@apache.org>
Committed: Tue Feb 20 21:21:14 2018 -0800

----------------------------------------------------------------------
 .../mnemonic/EntityFactoryProxyHelper.java      | 110 +++++++++++++++++++
 1 file changed, 110 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mnemonic/blob/eb9e7f8b/mnemonic-core/src/main/java/org/apache/mnemonic/EntityFactoryProxyHelper.java
----------------------------------------------------------------------
diff --git a/mnemonic-core/src/main/java/org/apache/mnemonic/EntityFactoryProxyHelper.java b/mnemonic-core/src/main/java/org/apache/mnemonic/EntityFactoryProxyHelper.java
new file mode 100644
index 0000000..04d777d
--- /dev/null
+++ b/mnemonic-core/src/main/java/org/apache/mnemonic/EntityFactoryProxyHelper.java
@@ -0,0 +1,110 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.mnemonic;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+public class EntityFactoryProxyHelper<D extends Durable>
+    implements EntityFactoryProxy {
+
+  protected Method fcreatemtd, frestoremtd;
+
+  public EntityFactoryProxyHelper(Class<D> clazz) throws ClassNotFoundException, NoSuchMethodException {
+    Class c = Class.forName(clazz.getName() + "Factory");
+    Method[] fmtds = c.getDeclaredMethods();
+    for (int i = 0; i < fmtds.length; ++i) {
+      if (fmtds[i].getName().equals("create") && fmtds[i].getParameterCount() == 4) {
+        fcreatemtd = fmtds[i];
+        break;
+      }
+    }
+    if (null == fcreatemtd) {
+      throw new NoSuchMethodException("Not found proper factory create(...) method");
+    }
+    for (int i = 0; i < fmtds.length; ++i) {
+      if (fmtds[i].getName().equals("restore") && fmtds[i].getParameterCount() == 5) {
+        frestoremtd = fmtds[i];
+        break;
+      }
+    }
+    if (null == frestoremtd) {
+      throw new NoSuchMethodException("Not found proper factory restore(...) method");
+    }
+  }
+
+  @Override
+  public <A extends RestorableAllocator<A>> D create(
+      A allocator, EntityFactoryProxy[] factoryproxys,
+      DurableType[] gfields, boolean autoreclaim) {
+    Object o = null;
+    try {
+      o = fcreatemtd.invoke(null, allocator, factoryproxys, gfields, autoreclaim);
+    } catch (IllegalAccessException e) {
+      e.printStackTrace();
+    } catch (InvocationTargetException e) {
+      e.printStackTrace();
+    }
+    return (D)o;
+  }
+
+  @Override
+  public <A extends RestorableAllocator<A>> D create(
+      ParameterHolder<A> ph) {
+    Object o = null;
+    try {
+      o = fcreatemtd.invoke(null, ph.getAllocator(),
+          ph.getEntityFactoryProxies(), ph.getGenericTypes(), ph.getAutoReclaim());
+    } catch (IllegalAccessException e) {
+      e.printStackTrace();
+    } catch (InvocationTargetException e) {
+      e.printStackTrace();
+    }
+    return (D)o;
+  }
+
+  @Override
+  public <A extends RestorableAllocator<A>> D restore(
+      A allocator, EntityFactoryProxy[] factoryproxys,
+      DurableType[] gfields, long phandler, boolean autoreclaim) {
+    Object o = null;
+    try {
+      o = frestoremtd.invoke(null, allocator, factoryproxys, gfields, phandler, autoreclaim);
+    } catch (IllegalAccessException e) {
+      e.printStackTrace();
+    } catch (InvocationTargetException e) {
+      e.printStackTrace();
+    }
+    return (D)o;
+  }
+
+  @Override
+  public <A extends RestorableAllocator<A>> D restore(
+      ParameterHolder<A> ph) {
+    Object o = null;
+    try {
+      o = frestoremtd.invoke(null, ph.getAllocator(),
+          ph.getEntityFactoryProxies(), ph.getGenericTypes(), ph.getHandler(), ph.getAutoReclaim());
+    } catch (IllegalAccessException e) {
+      e.printStackTrace();
+    } catch (InvocationTargetException e) {
+      e.printStackTrace();
+    }
+    return (D)o;
+  }
+}