Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix the issue #103 by create two new FastCloners #109

Merged
merged 10 commits into from
Aug 4, 2021
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
<dependency>
<groupId>org.objenesis</groupId>
<artifactId>objenesis</artifactId>
<version>3.0.1</version>
<version>3.2</version>
</dependency>
</dependencies>
<profiles>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/rits/cloning/Cloner.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.lang.reflect.Field;

import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.math.BigInteger;
Expand Down Expand Up @@ -113,6 +114,10 @@ protected void registerFastCloners() {
registerInaccessibleClassToBeFastCloned("java.util.ArrayList$SubList", subListCloner);
registerInaccessibleClassToBeFastCloned("java.util.SubList", subListCloner);
registerInaccessibleClassToBeFastCloned("java.util.RandomAccessSubList", subListCloner);
FastClonerListOf12 listOf12 = new FastClonerListOf12();
registerInaccessibleClassToBeFastCloned("java.util.ImmutableCollections$List12",listOf12);
FastClonerSetOf12 setOf12 = new FastClonerSetOf12();
registerInaccessibleClassToBeFastCloned("java.util.ImmutableCollections$Set12",setOf12);
}

protected void registerInaccessibleClassToBeFastCloned(String className, IFastCloner fastCloner) {
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/rits/cloning/FastClonerListOf12.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.rits.cloning;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class FastClonerListOf12 implements IFastCloner {
@SuppressWarnings({"unchecked", "rawtypes"})
public Object clone(final Object t, final IDeepCloner cloner, final Map<Object, Object> clones) {
List al = (List) t;
if (al.size() == 1) {
return List.of(cloner.deepClone(al.get(0), clones));
} else if (al.size() == 2) {
Object o1 = cloner.deepClone(al.get(0), clones);
Object o2 = cloner.deepClone(al.get(1), clones);
return List.of(o1, o2);
} else {
return new ArrayList<>();
}
}

}
20 changes: 20 additions & 0 deletions src/main/java/com/rits/cloning/FastClonerSetOf12.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.rits.cloning;

import java.util.*;

public class FastClonerSetOf12 implements IFastCloner {
@SuppressWarnings({"unchecked", "rawtypes"})
public Object clone(final Object t, final IDeepCloner cloner, final Map<Object, Object> clones) {
Set set = (Set) t;
Object[] a = set.toArray();
if (set.size() == 1) {
return Set.of(cloner.deepClone(a[0], clones));
} else if (set.size() == 2) {
Object o1 = cloner.deepClone(a[0], clones);
Object o2 = cloner.deepClone(a[1], clones);
return Set.of(o1, o2);
} else {
return new HashSet<>();
}
}
}
40 changes: 39 additions & 1 deletion src/test/java/com/rits/tests/cloning/TestCloner.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@

import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
Expand Down Expand Up @@ -39,6 +42,24 @@ public class TestCloner extends TestCase {
static private class MyAX {
}

public void testCloneListOf12() {
List list1 = List.of(1);
Assert.assertEquals(list1, cloner.deepClone(list1));
Assert.assertEquals(1, cloner.deepClone(list1).size());
List list2 = List.of(1, 2);
Assert.assertEquals(list2, cloner.deepClone(list2));
Assert.assertEquals(2, cloner.deepClone(list2).size());
}

public void testCloneSetOf12() {
Set set1 = Set.of(1);
Assert.assertEquals(set1, cloner.deepClone(set1));
Assert.assertEquals(1, cloner.deepClone(set1).size());
Set set2 = Set.of(1, 2);
Assert.assertEquals(set2, cloner.deepClone(set2));
Assert.assertEquals(2, cloner.deepClone(set2).size());
}

public void testCalendarTimezone() {
TimeZone timeZone = TimeZone.getTimeZone("America/Los_Angeles");
Calendar c = Calendar.getInstance(timeZone);
Expand Down Expand Up @@ -875,7 +896,7 @@ public void testLinkedHashSetEquals() {
/**
* Test if insertion order of LinkedhashSet is the same
*/
public void testLinkedHashSetInserationOrder() {
public void testLinkedHashSetIterationOrder() {
LinkedHashSet<Integer> originalSet = new LinkedHashSet<>();
for (int i = 1000; i >= 1; i--) {
originalSet.add(i);
Expand Down Expand Up @@ -915,5 +936,22 @@ class StaticTransient extends ArrayList<String> {

cloner.deepClone(new StaticTransient());
}

public void ignoreTestLambda() {
// this fails with "Caused by: java.lang.ClassNotFoundException: com.rits.tests.cloning.TestCloner$$Lambda$54.0x0000000800c24210"
Function<ZonedDateTime, Integer> f = ZonedDateTime::getNano;
cloner.deepClone(f);
}

private static class ClassWithEnum {
TimeUnit timeUnit;
}

public void testClassWithEnum() {
ClassWithEnum a = new ClassWithEnum();
a.timeUnit = TimeUnit.SECONDS;
ClassWithEnum b = cloner.deepClone(a);
assertSame(TimeUnit.SECONDS, b.timeUnit);
}
}