Skip to content

Commit f0751cb

Browse files
author
qiujiayu
committed
deepClone中增加type参数,便于在能获得type的情况下使用
1 parent 84c74af commit f0751cb

File tree

13 files changed

+47
-30
lines changed

13 files changed

+47
-30
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<modelVersion>4.0.0</modelVersion>
33
<groupId>com.github.qiujiayu</groupId>
44
<artifactId>autoload-cache</artifactId>
5-
<version>4.12</version>
5+
<version>4.13-SNAPSHOT</version>
66
<packaging>jar</packaging>
77
<name>AutoLoadCache</name>
88
<description>User Spring AOP and annotation to do with cache.</description>
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.jarvis.cache.clone;
22

33
import java.lang.reflect.Method;
4+
import java.lang.reflect.Type;
45

56
import com.rits.cloning.Cloner;
67

@@ -9,7 +10,7 @@ public class Cloning implements ICloner {
910
private final Cloner cloner=new Cloner();
1011

1112
@Override
12-
public Object deepClone(Object obj) throws Exception {
13+
public Object deepClone(Object obj, final Type type) throws Exception {
1314
if(null == obj) {
1415
return null;
1516
}
@@ -18,7 +19,7 @@ public Object deepClone(Object obj) throws Exception {
1819

1920
@Override
2021
public Object[] deepCloneMethodArgs(Method method, Object[] args) throws Exception {
21-
return (Object[])deepClone(args);
22+
return (Object[])deepClone(args, null);
2223
}
2324

2425
}

src/main/java/com/jarvis/cache/clone/ICloner.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.jarvis.cache.clone;
22

33
import java.lang.reflect.Method;
4+
import java.lang.reflect.Type;
45

56
/**
67
* 深度复制
@@ -11,10 +12,11 @@ public interface ICloner {
1112
/**
1213
* 深度复制Object
1314
* @param obj Object
15+
* @param type obj的类型,方便以json来处理时,提升性能,如果获取不到type,则可以为null
1416
* @return Object
1517
* @throws Exception
1618
*/
17-
Object deepClone(Object obj) throws Exception;
19+
Object deepClone(Object obj, final Type type) throws Exception;
1820

1921
/**
2022
* 深度复制 Method 中的参数

src/main/java/com/jarvis/cache/map/CachePointCut.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public void setCache(final CacheKeyTO cacheKeyTO, final CacheWrapper<Object> res
8686
CacheWrapper<Object> value=null;
8787
if(copyValue) {
8888
try {
89-
value=(CacheWrapper<Object>)this.getCloner().deepClone(value);
89+
value=(CacheWrapper<Object>)this.getCloner().deepClone(result, null);// 这里type为null,因为有可以是设置@ExCache缓存
9090
} catch(Exception e) {
9191
e.printStackTrace();
9292
}
@@ -153,7 +153,11 @@ public CacheWrapper<Object> get(final CacheKeyTO cacheKeyTO, final Method method
153153
}
154154
if(copyValue) {
155155
try {
156-
return (CacheWrapper<Object>)this.getCloner().deepClone(value);
156+
CacheWrapper<Object> res=new CacheWrapper<Object>();
157+
res.setExpire(value.getExpire());
158+
res.setLastLoadTime(value.getLastLoadTime());
159+
res.setCacheObject(this.getCloner().deepClone(value.getCacheObject(), method.getReturnType()));
160+
return res;
157161
} catch(Exception e) {
158162
e.printStackTrace();
159163
}

src/main/java/com/jarvis/cache/serializer/CompressorSerializer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ public Object deserialize(final byte[] bytes, final Type returnType) throws Exce
7676
}
7777

7878
@Override
79-
public Object deepClone(Object obj) throws Exception {
80-
return serializer.deepClone(obj);
79+
public Object deepClone(Object obj, final Type type) throws Exception {
80+
return serializer.deepClone(obj, type);
8181
}
8282

8383
@Override

src/main/java/com/jarvis/cache/serializer/FastjsonSerializer.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,14 @@ public Object deserialize(final byte[] bytes, final Type returnType) throws Exce
5151

5252
@SuppressWarnings({"rawtypes", "unchecked"})
5353
@Override
54-
public Object deepClone(Object obj) throws Exception {
54+
public Object deepClone(Object obj, final Type type) throws Exception {
5555
if(null == obj) {
5656
return null;
5757
}
58+
if(null != type) {
59+
String json=JSON.toJSONString(obj, features);
60+
return JSON.parseObject(json, type);
61+
}
5862
Class<?> clazz=obj.getClass();
5963
if(clazz.isArray()) {
6064
Object[] arr=(Object[])obj;
@@ -63,7 +67,7 @@ public Object deepClone(Object obj) throws Exception {
6367
((Object)clazz == (Object)Object[].class) ? (Object[])new Object[arr.length] : (Object[])Array.newInstance(
6468
clazz.getComponentType(), arr.length);
6569
for(int i=0; i < arr.length; i++) {
66-
res[i]=deepClone(arr[i]);
70+
res[i]=deepClone(arr[i], null);
6771
}
6872
return res;
6973
} else if(obj instanceof Collection) {
@@ -72,7 +76,7 @@ public Object deepClone(Object obj) throws Exception {
7276

7377
Iterator<?> it=tempCol.iterator();
7478
while(it.hasNext()) {
75-
Object val=deepClone(it.next());
79+
Object val=deepClone(it.next(), null);
7680
res.add(val);
7781
}
7882
return res;
@@ -84,10 +88,16 @@ public Object deepClone(Object obj) throws Exception {
8488
Map.Entry entry=(Entry)it.next();
8589
Object key=entry.getKey();
8690
Object val=entry.getValue();
87-
res.put(deepClone(key), deepClone(val));
91+
res.put(deepClone(key, null), deepClone(val, null));
8892
}
8993
return res;
90-
94+
} else if(obj instanceof CacheWrapper) {
95+
CacheWrapper<Object> wrapper=(CacheWrapper<Object>)obj;
96+
CacheWrapper<Object> res=new CacheWrapper<Object>();
97+
res.setExpire(wrapper.getExpire());
98+
res.setLastLoadTime(wrapper.getLastLoadTime());
99+
res.setCacheObject(deepClone(wrapper.getCacheObject(), null));
100+
return res;
91101
} else {
92102
String json=JSON.toJSONString(obj, features);
93103
return JSON.parseObject(json, clazz);
@@ -116,7 +126,7 @@ public Object[] deepCloneMethodArgs(Method method, Object[] args) throws Excepti
116126
String json=JSON.toJSONString(obj, features);
117127
res[i]=JSON.parseObject(json, genericParameterType);
118128
} else {
119-
res[i]=deepClone(obj);
129+
res[i]=deepClone(obj, null);
120130
}
121131
}
122132
return res;

src/main/java/com/jarvis/cache/serializer/HessianSerializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public Object deserialize(final byte[] bytes, final Type returnType) throws Exce
5959
}
6060

6161
@Override
62-
public Object deepClone(Object obj) throws Exception {
62+
public Object deepClone(Object obj, final Type type) throws Exception {
6363
return deserialize(serialize(obj), null);
6464
}
6565

src/main/java/com/jarvis/cache/serializer/JdkSerializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public byte[] serialize(Object obj) throws Exception {
3333
}
3434

3535
@Override
36-
public Object deepClone(Object obj) throws Exception {
36+
public Object deepClone(Object obj, final Type type) throws Exception {
3737
return deserialize(serialize(obj), null);
3838
}
3939

src/main/java/com/jarvis/cache/serializer/StringSerializer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public byte[] serialize(String string) throws Exception {
3737
}
3838

3939
@Override
40-
public Object deepClone(Object obj) throws Exception {
40+
public Object deepClone(Object obj, final Type type) throws Exception {
4141
if(null == obj) {
4242
return obj;
4343
}
@@ -47,6 +47,6 @@ public Object deepClone(Object obj) throws Exception {
4747

4848
@Override
4949
public Object[] deepCloneMethodArgs(Method method, Object[] args) throws Exception {
50-
return (Object[])deepClone(args);
50+
return (Object[])deepClone(args, null);
5151
}
5252
}

src/test/java/com/test/JdkSerializerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static void main(String[] args) throws Exception {
1515
CacheWrapper wrapper=new CacheWrapper();
1616
wrapper.setCacheObject(Simple.getSimple());
1717

18-
new JdkSerializer().deepClone(wrapper);
18+
new JdkSerializer().deepClone(wrapper, null);
1919
byte[] data=null;
2020
for(int i=0; i < 1000; i++) {
2121
data=write(wrapper);

0 commit comments

Comments
 (0)