1
2
3
4
5 package mockit.internal.reflection;
6
7 import edu.umd.cs.findbugs.annotations.NonNull;
8 import edu.umd.cs.findbugs.annotations.Nullable;
9
10 import java.lang.reflect.ParameterizedType;
11 import java.lang.reflect.Proxy;
12 import java.lang.reflect.Type;
13 import java.lang.reflect.TypeVariable;
14 import java.util.ArrayList;
15 import java.util.List;
16
17
18
19
20
21
22
23
24
25 public interface EmptyProxy {
26 final class Impl {
27 private Impl() {
28 }
29
30 @NonNull
31 public static <E> E newEmptyProxy(@Nullable ClassLoader loader, @NonNull Type... interfacesToBeProxied) {
32 List<Class<?>> interfaces = new ArrayList<>();
33
34 for (Type type : interfacesToBeProxied) {
35 addInterface(interfaces, type);
36 }
37
38 if (loader == null) {
39
40 loader = interfaces.get(0).getClassLoader();
41 }
42
43 if (loader == EmptyProxy.class.getClassLoader()) {
44 interfaces.add(EmptyProxy.class);
45 }
46
47 Class<?>[] interfacesArray = interfaces.toArray(new Class<?>[interfaces.size()]);
48
49
50 return (E) Proxy.newProxyInstance(loader, interfacesArray, MockInvocationHandler.INSTANCE);
51 }
52
53 private static void addInterface(@NonNull List<Class<?>> interfaces, @NonNull Type type) {
54 if (type instanceof Class<?>) {
55 interfaces.add((Class<?>) type);
56 } else if (type instanceof ParameterizedType) {
57 ParameterizedType paramType = (ParameterizedType) type;
58 interfaces.add((Class<?>) paramType.getRawType());
59 } else if (type instanceof TypeVariable) {
60 TypeVariable<?> typeVar = (TypeVariable<?>) type;
61 addBoundInterfaces(interfaces, typeVar.getBounds());
62 }
63 }
64
65 private static void addBoundInterfaces(@NonNull List<Class<?>> interfaces, @NonNull Type[] bounds) {
66 for (Type bound : bounds) {
67 addInterface(interfaces, bound);
68 }
69 }
70 }
71 }