Commit 1dc8c0da authored by yanzg's avatar yanzg

身份证识别

parent 608d2b96
...@@ -281,7 +281,7 @@ public class ObjectHelper { ...@@ -281,7 +281,7 @@ public class ObjectHelper {
* @param fromName 来源名称 * @param fromName 来源名称
* @return 名称结果 * @return 名称结果
*/ */
private static String getSimpleName(String fromName) { public static String getMethodSimpleName(String fromName) {
String toName = getSimpleFieldName(fromName); String toName = getSimpleFieldName(fromName);
if (toName.startsWith(METHOD_IS)) { if (toName.startsWith(METHOD_IS)) {
toName = toName.substring(METHOD_IS.length()); toName = toName.substring(METHOD_IS.length());
...@@ -361,7 +361,7 @@ public class ObjectHelper { ...@@ -361,7 +361,7 @@ public class ObjectHelper {
} }
String methodNameSource = method.getName(); String methodNameSource = method.getName();
String methodNameSimple = methodNameSource.toLowerCase(); String methodNameSimple = methodNameSource.toLowerCase();
String toName = getSimpleName(methodNameSource); String toName = getMethodSimpleName(methodNameSource);
if ("getClass".equals(methodNameSource)) { if ("getClass".equals(methodNameSource)) {
continue; continue;
} }
......
...@@ -2,7 +2,6 @@ package helper; ...@@ -2,7 +2,6 @@ package helper;
import com.alibaba.fastjson.TypeReference; import com.alibaba.fastjson.TypeReference;
import com.yanzuoguang.util.helper.JsonHelper; import com.yanzuoguang.util.helper.JsonHelper;
import com.yanzuoguang.util.vo.MapRow;
import helper.vo.MapSub; import helper.vo.MapSub;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
...@@ -48,14 +47,26 @@ public class TestJsonHelper { ...@@ -48,14 +47,26 @@ public class TestJsonHelper {
@Test @Test
public void testMapRow() { public void testMapRow() {
String key = "age"; String key = "age";
String name = "颜佐光";
MapSub from = new MapSub(); MapSub from = new MapSub();
from.setName("颜佐光"); MapSub from1 = new MapSub();
from.put("age",15); from.setName(name);
from.setName(name);
from1.setName(name);
from1.setName(name);
from.setYear(15);
from1.setYear(15);
from.put("age", 15);
String json = JsonHelper.serialize(from); String json = JsonHelper.serialize(from);
MapSub to = JsonHelper.deserialize(json, new TypeReference<MapSub>() { MapSub to = JsonHelper.deserialize(json, new TypeReference<MapSub>() {
}); });
Assert.assertEquals(from.getName(), to.getName()); Assert.assertEquals(from.getName(), to.getName());
Assert.assertEquals(from.getYear(), to.getYear());
Assert.assertEquals(from.get(key), to.get(key)); Assert.assertEquals(from.get(key), to.get(key));
Assert.assertEquals(from.getName(), name);
Assert.assertEquals(from, to); Assert.assertEquals(from, to);
} }
} }
......
package helper.vo;
import com.alibaba.fastjson.TypeReference;
import com.yanzuoguang.util.base.ObjectHelper;
import com.yanzuoguang.util.vo.MapRow;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class MapRowCache extends MapRow {
private final static Map<String, TypeReference<?>> mapMethodReference = new ConcurrentHashMap<>();
private final static Map<Class<?>, DataInfo> mapClassDataInfo = new ConcurrentHashMap<>();
public MapRowCache() {
}
@Override
public Object get(Object key) {
return super.get(key);
}
@Override
public Object put(String key, Object value) {
return super.put(key, value);
}
protected <T> T getField(Get<T> getter) {
DataInfo dataInfo = getDataInfo(getter.getClass(), 3);
System.out.println(dataInfo.getName() + "()" + " called by " + dataInfo.getCls().getName());
return null;
}
protected <T> void putField(Set<T> setter, T value) {
DataInfo dataInfo = getDataInfo(setter.getClass(), 3);
System.out.println(dataInfo.getName() + "()" + " called by " + dataInfo.getCls().getName());
}
private DataInfo getDataInfo(Class<?> cls, int pos) {
if (!mapClassDataInfo.containsKey(cls)) {
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
String methodName = stackTraceElements[pos].getMethodName();
String fieldName = getMethodFieldName(methodName);
String fieldClsName = getMethodFieldClsName(this.getClass(), fieldName);
if (!mapMethodReference.containsKey(fieldClsName)) {
throw new IllegalArgumentException("未注册类型:" + cls.getName() + " 方法关联:" + methodName);
}
DataInfo dataInfo = new DataInfo(methodName, cls, mapMethodReference.get(cls));
mapClassDataInfo.put(cls, dataInfo);
return dataInfo;
}
return mapClassDataInfo.get(cls);
}
private static String getMethodFieldClsName(Class<?> cls, String fieldName) {
return cls.getName() + "." + fieldName;
}
protected static void register(Class<?> cls, String fieldName, TypeReference<?> typeReference) {
String methodFieldClsName = getMethodFieldClsName(cls, fieldName);
mapMethodReference.put(methodFieldClsName, typeReference);
}
protected interface Get<T> {
T get();
}
protected interface Set<T> {
void set(T t);
}
/**
* 字段关联的数据信息
*/
protected static class DataInfo {
private final String name;
private final Class<?> cls;
private final TypeReference<?> typeReference;
public DataInfo(String name, Class<?> cls, TypeReference<?> typeReference) {
this.name = name;
this.cls = cls;
this.typeReference = typeReference;
}
public String getName() {
return name;
}
public Class<?> getCls() {
return cls;
}
public TypeReference<?> getTypeReference() {
return typeReference;
}
}
/**
* 获取方法字段名
*
* @param fullName 方法全名
* @return 方法字段名
*/
protected String getMethodFieldName(String fullName) {
return ObjectHelper.getMethodSimpleName(fullName);
}
}
package helper.vo; package helper.vo;
import com.yanzuoguang.util.vo.MapRow; import com.alibaba.fastjson.TypeReference;
public class MapSub extends MapRow { public class MapSub extends MapRowCache {
private String name; static {
TypeReference<String> typeStringReference = new TypeReference<String>() {
};
TypeReference<Integer> typeIntegerReference = new TypeReference<Integer>() {
};
register(MapSub.class, "name", typeStringReference);
register(MapSub.class, "year", typeIntegerReference);
}
public String getName() { public String getName() {
return name; return this.getField(this::getName);
} }
public void setName(String name) { public void setName(String name) {
this.name = name; this.putField(this::setName, name);
}
public int getYear() {
return this.getField(this::getYear);
}
public void setYear(int age) {
this.putField(this::setYear, age);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment