-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTestInstancesOfClass.java
69 lines (54 loc) · 2.17 KB
/
TestInstancesOfClass.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import com.liubs.findinstances.jvmti.InstancesOfClass;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author Liubsyy
* @date 2023/10/20 10:17 PM
**/
class A{}
class B{}
public class TestInstancesOfClass {
private static <T> boolean isSame(List<T> list, Object[] insts){
if(null == list && null == insts){
return true;
}
if(null == list || null == insts || list.size() != insts.length) {
return false;
}
//先按hashcode排个序
list.sort(Comparator.comparingInt(Object::hashCode));
List<Object> list2 = Arrays.stream(insts).sorted(Comparator.comparingInt(Object::hashCode)).collect(Collectors.toList());
//每一个对象一定是全等
for(int i = 0,len=list.size();i<len ;i++) {
if(list.get(i) != list2.get(i)) {
return false;
}
}
return true;
}
public static void main(String[] args) {
int count = 10;
List<A> insts1 = new ArrayList<>();
for(int i = 0; i<count;i++) {
insts1.add(new A());
}
Object[] insts1_find = InstancesOfClass.getInstances(A.class);
System.out.println("A所有创建实例:" + insts1);
System.out.println("A所有查询出来的实例:" + Arrays.asList(insts1_find));
System.out.println("A的所有对象实例是否一致:"+ isSame(insts1,insts1_find));
List<B> insts2 = new ArrayList<>();
for(int i = 0; i<count;i++) {
insts2.add(new B());
}
Object[] insts2_find = InstancesOfClass.getInstances(B.class);
System.out.println("B所有创建实例:" + insts2);
System.out.println("B所有查询出来的实例:" + Arrays.asList(insts2_find));
System.out.println("B的所有对象实例是否一致:"+isSame(insts2,insts2_find));
//趋而复返,取A其中3个实例
List<A> an = InstancesOfClass.getInstanceList(A.class,3);
System.out.printf("A取其中%d个,是否所有A实例的子集:%s ", an.size(),insts1.containsAll(an) );
}
}