-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerializedLambda.java
More file actions
258 lines (241 loc) · 9.78 KB
/
Copy pathSerializedLambda.java
File metadata and controls
258 lines (241 loc) · 9.78 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package java.lang.invoke;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Objects;
/**
* Serialized form of a lambda expression. The properties of this class
* represent the information that is present at the lambda factory site, including
* static metafactory arguments such as the identity of the primary functional
* interface method and the identity of the implementation method, as well as
* dynamic metafactory arguments such as values captured from the lexical scope
* at the time of lambda capture.
*
* <p>Implementors of serializable lambdas, such as compilers or language
* runtime libraries, are expected to ensure that instances deserialize properly.
* One means to do so is to ensure that the {@code writeReplace} method returns
* an instance of {@code SerializedLambda}, rather than allowing default
* serialization to proceed.
*
* <p>{@code SerializedLambda} has a {@code readResolve} method that looks for
* a (possibly private) static method called
* {@code $deserializeLambda$(SerializedLambda)} in the capturing class, invokes
* that with itself as the first argument, and returns the result. Lambda classes
* implementing {@code $deserializeLambda$} are responsible for validating
* that the properties of the {@code SerializedLambda} are consistent with a
* lambda actually captured by that class.
*
* @see LambdaMetafactory
*/
public final class SerializedLambda implements Serializable {
private static final long serialVersionUID = 8025925345765570181L;
private final Class<?> capturingClass;
private final String functionalInterfaceClass;
private final String functionalInterfaceMethodName;
private final String functionalInterfaceMethodSignature;
private final String implClass;
private final String implMethodName;
private final String implMethodSignature;
private final int implMethodKind;
private final String instantiatedMethodType;
private final Object[] capturedArgs;
/**
* Create a {@code SerializedLambda} from the low-level information present
* at the lambda factory site.
*
* @param capturingClass The class in which the lambda expression appears
* @param functionalInterfaceClass Name, in slash-delimited form, of static
* type of the returned lambda object
* @param functionalInterfaceMethodName Name of the functional interface
* method for the present at the
* lambda factory site
* @param functionalInterfaceMethodSignature Signature of the functional
* interface method present at
* the lambda factory site
* @param implMethodKind Method handle kind for the implementation method
* @param implClass Name, in slash-delimited form, for the class holding
* the implementation method
* @param implMethodName Name of the implementation method
* @param implMethodSignature Signature of the implementation method
* @param instantiatedMethodType The signature of the primary functional
* interface method after type variables
* are substituted with their instantiation
* from the capture site
* @param capturedArgs The dynamic arguments to the lambda factory site,
* which represent variables captured by
* the lambda
*/
public SerializedLambda(Class<?> capturingClass,
String functionalInterfaceClass,
String functionalInterfaceMethodName,
String functionalInterfaceMethodSignature,
int implMethodKind,
String implClass,
String implMethodName,
String implMethodSignature,
String instantiatedMethodType,
Object[] capturedArgs) {
this.capturingClass = capturingClass;
this.functionalInterfaceClass = functionalInterfaceClass;
this.functionalInterfaceMethodName = functionalInterfaceMethodName;
this.functionalInterfaceMethodSignature = functionalInterfaceMethodSignature;
this.implMethodKind = implMethodKind;
this.implClass = implClass;
this.implMethodName = implMethodName;
this.implMethodSignature = implMethodSignature;
this.instantiatedMethodType = instantiatedMethodType;
this.capturedArgs = Objects.requireNonNull(capturedArgs).clone();
}
/**
* Get the name of the class that captured this lambda.
* @return the name of the class that captured this lambda
*/
public String getCapturingClass() {
return capturingClass.getName().replace('.', '/');
}
/**
* Get the name of the invoked type to which this
* lambda has been converted
* @return the name of the functional interface class to which
* this lambda has been converted
*/
public String getFunctionalInterfaceClass() {
return functionalInterfaceClass;
}
/**
* Get the name of the primary method for the functional interface
* to which this lambda has been converted.
* @return the name of the primary methods of the functional interface
*/
public String getFunctionalInterfaceMethodName() {
return functionalInterfaceMethodName;
}
/**
* Get the signature of the primary method for the functional
* interface to which this lambda has been converted.
* @return the signature of the primary method of the functional
* interface
*/
public String getFunctionalInterfaceMethodSignature() {
return functionalInterfaceMethodSignature;
}
/**
* Get the name of the class containing the implementation
* method.
* @return the name of the class containing the implementation
* method
*/
public String getImplClass() {
return implClass;
}
/**
* Get the name of the implementation method.
* @return the name of the implementation method
*/
public String getImplMethodName() {
return implMethodName;
}
/**
* Get the signature of the implementation method.
* @return the signature of the implementation method
*/
public String getImplMethodSignature() {
return implMethodSignature;
}
/**
* Get the method handle kind (see {@link MethodHandleInfo}) of
* the implementation method.
* @return the method handle kind of the implementation method
*/
public int getImplMethodKind() {
return implMethodKind;
}
/**
* Get the signature of the primary functional interface method
* after type variables are substituted with their instantiation
* from the capture site.
* @return the signature of the primary functional interface method
* after type variable processing
*/
public final String getInstantiatedMethodType() {
return instantiatedMethodType;
}
/**
* Get the count of dynamic arguments to the lambda capture site.
* @return the count of dynamic arguments to the lambda capture site
*/
public int getCapturedArgCount() {
return capturedArgs.length;
}
/**
* Get a dynamic argument to the lambda capture site.
* @param i the argument to capture
* @return a dynamic argument to the lambda capture site
*/
public Object getCapturedArg(int i) {
return capturedArgs[i];
}
private Object readResolve() throws ReflectiveOperationException {
try {
Method deserialize = AccessController.doPrivileged(new PrivilegedExceptionAction<Method>() {
@Override
public Method run() throws Exception {
Method m = capturingClass.getDeclaredMethod("$deserializeLambda$", SerializedLambda.class);
m.setAccessible(true);
return m;
}
});
return deserialize.invoke(null, this);
}
catch (PrivilegedActionException e) {
Exception cause = e.getException();
if (cause instanceof ReflectiveOperationException)
throw (ReflectiveOperationException) cause;
else if (cause instanceof RuntimeException)
throw (RuntimeException) cause;
else
throw new RuntimeException("Exception in SerializedLambda.readResolve", e);
}
}
@Override
public String toString() {
String implKind=MethodHandleInfo.referenceKindToString(implMethodKind);
return String.format("SerializedLambda[%s=%s, %s=%s.%s:%s, " +
"%s=%s %s.%s:%s, %s=%s, %s=%d]",
"capturingClass", capturingClass,
"functionalInterfaceMethod", functionalInterfaceClass,
functionalInterfaceMethodName,
functionalInterfaceMethodSignature,
"implementation",
implKind,
implClass, implMethodName, implMethodSignature,
"instantiatedMethodType", instantiatedMethodType,
"numCaptured", capturedArgs.length);
}
}