-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNil.java
More file actions
224 lines (196 loc) · 6.86 KB
/
Nil.java
File metadata and controls
224 lines (196 loc) · 6.86 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
* #%L
* SciJava library for generic type reasoning.
* %%
* Copyright (C) 2016 - 2025 SciJava developers.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package org.scijava.types;
import com.google.common.reflect.TypeToken;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.Set;
import org.scijava.common3.Classes;
import org.scijava.common3.GenericTyped;
/**
* A "typed null" which knows its generic type, and can generate proxy objects
* implementing that type's interfaces, with customizable behavior per interface
* method via callbacks.
* <p>
* The term "nil" was chosen, despite lots of possibility for confusion with
* related paradigms such as the Scala language, because it is very short while
* loosely meaning the same thing as {@code null}, {@code None}, etc.
* </p>
*
* @author Curtis Rueden
*/
public abstract class Nil<T> implements GenericTyped, Proxyable<T> {
/** Generic type of the object. */
private final TypeToken<?> typeToken;
/** An object which holds any method callbacks. */
private final Object callbacks;
/**
* Creates a new {@code Nil} whose generic type (returned by
* {@link #type()}) is the one specified by the generic parameters used at
* construction.
* <p>
* For example:
* </p>
*
* <pre>
* Nil<List<Map<K, V>>> nil = new Nil<List<Map<K, V>>>() {};
* </pre>
* <p>
* Subsequent calls to {@code nil.type()} will return the proper
* generically typed result—in the above example, a
* {@link ParameterizedType} whose raw type is {@code List}, and whose single
* type parameter is a {@link ParameterizedType} with raw type of {@code Map}
* and {@link TypeVariable} type parameters of {@code K} and {@code V}
* respectively, including their bounds inferred by the compiler in the
* context where the expression was written.
* </p>
*/
public Nil() {
typeToken = new TypeToken<T>(getClass()) {};
callbacks = this;
}
public Nil(final Object callbacks) {
typeToken = new TypeToken<T>(getClass()) {};
this.callbacks = callbacks(callbacks);
}
/**
* Creates a {@code Nil} wrapping the given raw {@link Class}.
* <p>
* This method is intentionally private; use {@link Nil#of(Class)} to create
* such a {@code Nil} from calling code.
* </p>
*/
private Nil(final Class<T> rawType) {
typeToken = TypeToken.of(rawType);
callbacks = this;
}
/**
* Creates a {@code Nil} wrapping the given generic {@link Type}.
* <p>
* This method is intentionally private; use {@link Nil#of(Type)} to create
* such a {@code Nil} from calling code.
* </p>
*/
private Nil(final Type type) {
typeToken = TypeToken.of(type);
callbacks = this;
}
/**
* Creates a {@code Nil} wrapping the given generic {@link Type}, and
* possessing the specified method callbacks.
* <p>
* This method is intentionally private; use {@link Nil#of(Type, Object)} to
* create such a {@code Nil} from calling code.
* </p>
*/
private Nil(final Type type, final Object callbacks) {
typeToken = TypeToken.of(type);
this.callbacks = callbacks(callbacks);
}
// -- Static utility methods --
public static <T> Nil<T> of(final Class<T> rawType) {
return new Nil<>(rawType) {};
}
/**
* Creates a {@code Nil} of the given generic {@link Type}, with no extra
* method callbacks.
*/
public static Nil<?> of(final Type type) {
return new Nil<>(type) {};
}
/**
* Creates a {@code Nil} of the given {@link Type}, with extra method
* callbacks contained in the specified object.
*/
public static Nil<?> of(final Type type, final Object callbacks) {
return new Nil<>(type, callbacks) {};
}
// -- Object methods --
@Override
public String toString() {
return typeToken.toString();
}
// -- GenericTyped methods --
@Override
public Type type() {
return typeToken.getType();
}
// -- GenericProxy methods --
/**
* Create a proxy which implements all the same interfaces as this object's
* generic type.
* <p>
* Optionally,
* CTR FIXME - write up how method delegation works.
* </p>
*/
@Override
public T proxy() {
final var loader = Classes.classLoader();
// extract the generic type's interfaces
final Set<?> ifaceSet = typeToken.getTypes().interfaces().rawTypes();
final var appendGT = !ifaceSet.contains(GenericTyped.class);
final var ifaceCount = ifaceSet.size() + (appendGT ? 1 : 0);
final var interfaces = new Class<?>[ifaceCount];
ifaceSet.toArray(interfaces);
if (appendGT) interfaces[ifaceCount - 1] = GenericTyped.class;
// NB: Technically, this cast is not safe, because T might not be
// an interface, and thus might not be one of the proxy's types.
@SuppressWarnings("unchecked")
final var proxy = (T) Proxy.newProxyInstance(loader, interfaces,
new InvocationHandler() {
@Override
public Object invoke(final Object proxy, final Method method,
final Object[] args) throws Throwable
{
try {
// Look for a Nil subclass method of the same signature.
final var m = callbacks.getClass().getMethod(method.getName(), //
method.getParameterTypes());
return m.invoke(callbacks, args);
}
catch (final NoSuchMethodException exc) {
// NB: Default behavior is to do nothing and return null.
return Classes.nullValue(method.getReturnType());
}
}
}
);
return proxy;
}
// -- Helper methods --
/** Unwraps the given callbacks object. */
private Object callbacks(final Object o) {
return o instanceof Nil ? ((Nil<?>) o).callbacks : o;
}
}