DataCallAttribute移至RpcCallAttribute

This commit is contained in:
Redkale
2020-05-16 09:12:06 +08:00
parent 02cf587fcf
commit e4c6e860c1
4 changed files with 119 additions and 135 deletions

View File

@@ -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;
/**
*
* <p>
* 详情见: https://redkale.org
*
* @author zhangjx
*/
public class RpcCallAttribute implements Attribute<Object, Serializable> {
public static final RpcCallAttribute instance = new RpcCallAttribute();
private static final ConcurrentHashMap<Class, Attribute> attributes = new ConcurrentHashMap<>();
static <T> Attribute<T, Serializable> 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<Serializable> type() {
return Serializable.class;
}
@Override
public Class<Object> 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<T, F> implements Attribute<T[], F> {
public static final RpcCallArrayAttribute instance = new RpcCallArrayAttribute();
@Override
public Class<? extends F> type() {
return (Class<F>) Object.class;
}
@Override
public Class<T[]> declaringClass() {
return (Class<T[]>) (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<T, Serializable> 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<T, Serializable> attr = RpcCallAttribute.load(objs[0].getClass());
for (int i = 0; i < objs.length; i++) {
attr.set(objs[i], (Serializable) Array.get(keys, i));
}
}
}
}

View File

@@ -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.*;
/**
*
* <p>
* 详情见: https://redkale.org
*
* @author zhangjx
* @param <T> Entity类的类型
* @param <F> 字段的类型
*/
@SuppressWarnings("unchecked")
public final class DataCallArrayAttribute<T, F> implements Attribute<T[], F> {
public static final DataCallArrayAttribute instance = new DataCallArrayAttribute();
@Override
public Class<? extends F> type() {
return (Class<F>) Object.class;
}
@Override
public Class<T[]> declaringClass() {
return (Class<T[]>) (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<T, Serializable> 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<T, Serializable> attr = DataCallAttribute.load(objs[0].getClass());
for (int i = 0; i < objs.length; i++) {
attr.set(objs[i], (Serializable) Array.get(keys, i));
}
}
}

View File

@@ -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.*;
/**
*
* <p> 详情见: https://redkale.org
* @author zhangjx
*/
public class DataCallAttribute implements Attribute<Object, Serializable> {
public static final DataCallAttribute instance = new DataCallAttribute();
private static final ConcurrentHashMap<Class, Attribute> attributes = new ConcurrentHashMap<>();
static <T> Attribute<T, Serializable> 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<Serializable> type() {
return Serializable.class;
}
@Override
public Class<Object> 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);
}
}

View File

@@ -313,7 +313,7 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
* @return 影响的记录条数
*/
@Override
public <T> int insert(@RpcCall(DataCallArrayAttribute.class) T... entitys) {
public <T> int insert(T... entitys) {
if (entitys.length == 0) return 0;
checkEntity("insert", false, entitys);
final EntityInfo<T> info = loadEntityInfo((Class<T>) entitys[0].getClass());
@@ -340,7 +340,7 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
}
@Override
public <T> CompletableFuture<Integer> insertAsync(@RpcCall(DataCallArrayAttribute.class) T... entitys) {
public <T> CompletableFuture<Integer> insertAsync(T... entitys) {
if (entitys.length == 0) return CompletableFuture.completedFuture(0);
CompletableFuture future = checkEntity("insert", true, entitys);
if (future != null) return future;