From e4c6e860c162f2d23db16e9e54c7fa2c51bfe8f7 Mon Sep 17 00:00:00 2001 From: Redkale <8730487+redkale@users.noreply.github.com> Date: Sat, 16 May 2020 09:12:06 +0800 Subject: [PATCH] =?UTF-8?q?DataCallAttribute=E7=A7=BB=E8=87=B3RpcCallAttri?= =?UTF-8?q?bute?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/org/redkale/service/RpcCallAttribute.java | 117 ++++++++++++++++++ .../source/DataCallArrayAttribute.java | 61 --------- src/org/redkale/source/DataCallAttribute.java | 72 ----------- src/org/redkale/source/DataSqlSource.java | 4 +- 4 files changed, 119 insertions(+), 135 deletions(-) create mode 100644 src/org/redkale/service/RpcCallAttribute.java delete mode 100644 src/org/redkale/source/DataCallArrayAttribute.java delete mode 100644 src/org/redkale/source/DataCallAttribute.java diff --git a/src/org/redkale/service/RpcCallAttribute.java b/src/org/redkale/service/RpcCallAttribute.java new file mode 100644 index 000000000..13cfc98f3 --- /dev/null +++ b/src/org/redkale/service/RpcCallAttribute.java @@ -0,0 +1,117 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.redkale.service; + +import java.io.Serializable; +import java.lang.reflect.*; +import java.util.concurrent.ConcurrentHashMap; +import org.redkale.util.Attribute; + +/** + * + *

+ * 详情见: https://redkale.org + * + * @author zhangjx + */ +public class RpcCallAttribute implements Attribute { + + public static final RpcCallAttribute instance = new RpcCallAttribute(); + + private static final ConcurrentHashMap attributes = new ConcurrentHashMap<>(); + + static Attribute load(final Class clazz) { + Attribute rs = attributes.get(clazz); + if (rs != null) return rs; + synchronized (attributes) { + rs = attributes.get(clazz); + if (rs == null) { + Class cltmp = clazz; + do { + for (Field field : cltmp.getDeclaredFields()) { + try { + rs = Attribute.create(cltmp, field); + attributes.put(clazz, rs); + return rs; + } catch (RuntimeException e) { + } + } + } while ((cltmp = cltmp.getSuperclass()) != Object.class); + } + return rs; + } + } + + @Override + public Class type() { + return Serializable.class; + } + + @Override + public Class declaringClass() { + return Object.class; + } + + @Override + public String field() { + return ""; + } + + @Override + public Serializable get(final Object obj) { + if (obj == null) return null; + return load(obj.getClass()).get(obj); + } + + @Override + public void set(final Object obj, final Serializable key) { + if (obj == null) return; + load(obj.getClass()).set(obj, key); + } + + @SuppressWarnings("unchecked") + public static class RpcCallArrayAttribute implements Attribute { + + public static final RpcCallArrayAttribute instance = new RpcCallArrayAttribute(); + + @Override + public Class type() { + return (Class) Object.class; + } + + @Override + public Class declaringClass() { + return (Class) (Class) Object[].class; + } + + @Override + public String field() { + return ""; + } + + @Override + public F get(final T[] objs) { + if (objs == null || objs.length == 0) return null; + final Attribute attr = RpcCallAttribute.load(objs[0].getClass()); + final Object keys = Array.newInstance(attr.type(), objs.length); + for (int i = 0; i < objs.length; i++) { + Array.set(keys, i, attr.get(objs[i])); + } + return (F) keys; + } + + @Override + public void set(final T[] objs, final F keys) { + if (objs == null || objs.length == 0) return; + final Attribute attr = RpcCallAttribute.load(objs[0].getClass()); + for (int i = 0; i < objs.length; i++) { + attr.set(objs[i], (Serializable) Array.get(keys, i)); + } + } + + } + +} diff --git a/src/org/redkale/source/DataCallArrayAttribute.java b/src/org/redkale/source/DataCallArrayAttribute.java deleted file mode 100644 index 8d8ccb244..000000000 --- a/src/org/redkale/source/DataCallArrayAttribute.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package org.redkale.source; - -import java.io.*; -import java.lang.reflect.*; -import org.redkale.util.*; - -/** - * - *

- * 详情见: https://redkale.org - * - * @author zhangjx - * @param Entity类的类型 - * @param 字段的类型 - */ -@SuppressWarnings("unchecked") -public final class DataCallArrayAttribute implements Attribute { - - public static final DataCallArrayAttribute instance = new DataCallArrayAttribute(); - - @Override - public Class type() { - return (Class) Object.class; - } - - @Override - public Class declaringClass() { - return (Class) (Class) Object[].class; - } - - @Override - public String field() { - return ""; - } - - @Override - public F get(final T[] objs) { - if (objs == null || objs.length == 0) return null; - final Attribute attr = DataCallAttribute.load(objs[0].getClass()); - final Object keys = Array.newInstance(attr.type(), objs.length); - for (int i = 0; i < objs.length; i++) { - Array.set(keys, i, attr.get(objs[i])); - } - return (F) keys; - } - - @Override - public void set(final T[] objs, final F keys) { - if (objs == null || objs.length == 0) return; - final Attribute attr = DataCallAttribute.load(objs[0].getClass()); - for (int i = 0; i < objs.length; i++) { - attr.set(objs[i], (Serializable) Array.get(keys, i)); - } - } - -} diff --git a/src/org/redkale/source/DataCallAttribute.java b/src/org/redkale/source/DataCallAttribute.java deleted file mode 100644 index 371fb85a2..000000000 --- a/src/org/redkale/source/DataCallAttribute.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package org.redkale.source; - -import java.io.*; -import java.lang.reflect.*; -import java.util.concurrent.*; -import org.redkale.util.*; - -/** - * - *

详情见: https://redkale.org - * @author zhangjx - */ -public class DataCallAttribute implements Attribute { - - public static final DataCallAttribute instance = new DataCallAttribute(); - - private static final ConcurrentHashMap attributes = new ConcurrentHashMap<>(); - - static Attribute load(final Class clazz) { - Attribute rs = attributes.get(clazz); - if (rs != null) return rs; - synchronized (attributes) { - rs = attributes.get(clazz); - if (rs == null) { - Class cltmp = clazz; - do { - for (Field field : cltmp.getDeclaredFields()) { - try { - rs = Attribute.create(cltmp, field); - attributes.put(clazz, rs); - return rs; - } catch (RuntimeException e) { - } - } - } while ((cltmp = cltmp.getSuperclass()) != Object.class); - } - return rs; - } - } - - @Override - public Class type() { - return Serializable.class; - } - - @Override - public Class declaringClass() { - return Object.class; - } - - @Override - public String field() { - return ""; - } - - @Override - public Serializable get(final Object obj) { - if (obj == null) return null; - return load(obj.getClass()).get(obj); - } - - @Override - public void set(final Object obj, final Serializable key) { - if (obj == null) return; - load(obj.getClass()).set(obj, key); - } -} diff --git a/src/org/redkale/source/DataSqlSource.java b/src/org/redkale/source/DataSqlSource.java index 455980e38..a029b9d0d 100644 --- a/src/org/redkale/source/DataSqlSource.java +++ b/src/org/redkale/source/DataSqlSource.java @@ -313,7 +313,7 @@ public abstract class DataSqlSource extends AbstractService implement * @return 影响的记录条数 */ @Override - public int insert(@RpcCall(DataCallArrayAttribute.class) T... entitys) { + public int insert(T... entitys) { if (entitys.length == 0) return 0; checkEntity("insert", false, entitys); final EntityInfo info = loadEntityInfo((Class) entitys[0].getClass()); @@ -340,7 +340,7 @@ public abstract class DataSqlSource extends AbstractService implement } @Override - public CompletableFuture insertAsync(@RpcCall(DataCallArrayAttribute.class) T... entitys) { + public CompletableFuture insertAsync(T... entitys) { if (entitys.length == 0) return CompletableFuture.completedFuture(0); CompletableFuture future = checkEntity("insert", true, entitys); if (future != null) return future;