2013-02-16 12:36:54 -08:00
|
|
|
/*
|
2013-06-27 19:02:02 -07:00
|
|
|
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
|
2013-02-16 12:36:54 -08:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation. Oracle designates this
|
|
|
|
* particular file as subject to the "Classpath" exception as provided
|
|
|
|
* by Oracle in the LICENSE file that accompanied this code.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
|
|
|
*/
|
|
|
|
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 the identity of the primary functional interface method, the identity of the
|
|
|
|
* implementation method, and any variables captured from the local environment at the time of lambda capture.
|
|
|
|
*
|
|
|
|
* @see LambdaMetafactory
|
|
|
|
*/
|
|
|
|
public final class SerializedLambda implements Serializable {
|
|
|
|
private static final long serialVersionUID = 8025925345765570181L;
|
2013-02-26 10:38:58 -08:00
|
|
|
private final Class<?> capturingClass;
|
2013-02-16 12:36:54 -08:00
|
|
|
private final String functionalInterfaceClass;
|
|
|
|
private final String functionalInterfaceMethodName;
|
|
|
|
private final String functionalInterfaceMethodSignature;
|
|
|
|
private final int functionalInterfaceMethodKind;
|
|
|
|
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 functionalInterfaceMethodKind Method handle kind (see {@link MethodHandleInfo}) for the
|
|
|
|
* functional interface method handle present at the lambda factory site
|
|
|
|
* @param functionalInterfaceClass Name, in slash-delimited form, for the functional interface class present at the
|
|
|
|
* lambda factory site
|
|
|
|
* @param functionalInterfaceMethodName Name of the primary method for the functional interface present at the
|
|
|
|
* lambda factory site
|
|
|
|
* @param functionalInterfaceMethodSignature Signature of the primary method for the functional interface 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
|
|
|
|
*/
|
2013-02-26 10:38:58 -08:00
|
|
|
public SerializedLambda(Class<?> capturingClass,
|
2013-02-16 12:36:54 -08:00
|
|
|
int functionalInterfaceMethodKind,
|
|
|
|
String functionalInterfaceClass,
|
|
|
|
String functionalInterfaceMethodName,
|
|
|
|
String functionalInterfaceMethodSignature,
|
|
|
|
int implMethodKind,
|
|
|
|
String implClass,
|
|
|
|
String implMethodName,
|
|
|
|
String implMethodSignature,
|
|
|
|
String instantiatedMethodType,
|
|
|
|
Object[] capturedArgs) {
|
|
|
|
this.capturingClass = capturingClass;
|
|
|
|
this.functionalInterfaceMethodKind = functionalInterfaceMethodKind;
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2013-06-27 19:02:02 -07:00
|
|
|
/**
|
|
|
|
* Get the name of the class that captured this lambda.
|
|
|
|
* @return the name of the class that captured this lambda
|
|
|
|
*/
|
2013-02-16 12:36:54 -08:00
|
|
|
public String getCapturingClass() {
|
2013-02-26 10:38:58 -08:00
|
|
|
return capturingClass.getName().replace('.', '/');
|
2013-02-16 12:36:54 -08:00
|
|
|
}
|
|
|
|
|
2013-06-27 19:02:02 -07:00
|
|
|
/**
|
|
|
|
* Get the name of the functional interface class to which this
|
|
|
|
* lambda has been converted
|
|
|
|
* @return the name of the functional interface this lambda has
|
|
|
|
* been converted to
|
|
|
|
*/
|
2013-02-16 12:36:54 -08:00
|
|
|
public String getFunctionalInterfaceClass() {
|
|
|
|
return functionalInterfaceClass;
|
|
|
|
}
|
|
|
|
|
2013-06-27 19:02:02 -07:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2013-02-16 12:36:54 -08:00
|
|
|
public String getFunctionalInterfaceMethodName() {
|
|
|
|
return functionalInterfaceMethodName;
|
|
|
|
}
|
|
|
|
|
2013-06-27 19:02:02 -07:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2013-02-16 12:36:54 -08:00
|
|
|
public String getFunctionalInterfaceMethodSignature() {
|
|
|
|
return functionalInterfaceMethodSignature;
|
|
|
|
}
|
|
|
|
|
2013-06-27 19:02:02 -07:00
|
|
|
/**
|
|
|
|
* Get the method handle kind (see {@link MethodHandleInfo}) of
|
|
|
|
* the primary method for the functional interface to which this
|
|
|
|
* lambda has been converted
|
|
|
|
* @return the method handle kind of the primary method of
|
|
|
|
* functional interface
|
|
|
|
*/
|
2013-02-16 12:36:54 -08:00
|
|
|
public int getFunctionalInterfaceMethodKind() {
|
|
|
|
return functionalInterfaceMethodKind;
|
|
|
|
}
|
|
|
|
|
2013-06-27 19:02:02 -07:00
|
|
|
/**
|
|
|
|
* Get the name of the class containing the implementation
|
|
|
|
* method.
|
|
|
|
* @return the name of the class containing the implementation
|
|
|
|
* method
|
|
|
|
*/
|
2013-02-16 12:36:54 -08:00
|
|
|
public String getImplClass() {
|
|
|
|
return implClass;
|
|
|
|
}
|
|
|
|
|
2013-06-27 19:02:02 -07:00
|
|
|
/**
|
|
|
|
* Get the name of the implementation method.
|
|
|
|
* @return the name of the implementation method
|
|
|
|
*/
|
2013-02-16 12:36:54 -08:00
|
|
|
public String getImplMethodName() {
|
|
|
|
return implMethodName;
|
|
|
|
}
|
|
|
|
|
2013-06-27 19:02:02 -07:00
|
|
|
/**
|
|
|
|
* Get the signature of the implementation method.
|
|
|
|
* @return the signature of the implementation method
|
|
|
|
*/
|
2013-02-16 12:36:54 -08:00
|
|
|
public String getImplMethodSignature() {
|
|
|
|
return implMethodSignature;
|
|
|
|
}
|
|
|
|
|
2013-06-27 19:02:02 -07:00
|
|
|
/**
|
|
|
|
* Get the method handle kind (see {@link MethodHandleInfo}) of
|
|
|
|
* the implementation method.
|
|
|
|
* @return the method handle kind of the implementation method
|
|
|
|
*/
|
2013-02-16 12:36:54 -08:00
|
|
|
public int getImplMethodKind() {
|
|
|
|
return implMethodKind;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-27 19:02:02 -07:00
|
|
|
* 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
|
2013-02-16 12:36:54 -08:00
|
|
|
*/
|
|
|
|
public final String getInstantiatedMethodType() {
|
|
|
|
return instantiatedMethodType;
|
|
|
|
}
|
|
|
|
|
2013-06-27 19:02:02 -07:00
|
|
|
/**
|
|
|
|
* Get the count of dynamic arguments to the lambda capture site.
|
|
|
|
* @return the count of dynamic arguments to the lambda capture site
|
|
|
|
*/
|
2013-02-16 12:36:54 -08:00
|
|
|
public int getCapturedArgCount() {
|
|
|
|
return capturedArgs.length;
|
|
|
|
}
|
|
|
|
|
2013-06-27 19:02:02 -07:00
|
|
|
/**
|
|
|
|
* Get a dynamic argument to the lambda capture site.
|
|
|
|
* @param i the argument to capture
|
|
|
|
* @return a dynamic argument to the lambda capture site
|
|
|
|
*/
|
2013-02-16 12:36:54 -08:00
|
|
|
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 {
|
2013-02-26 10:38:58 -08:00
|
|
|
Method m = capturingClass.getDeclaredMethod("$deserializeLambda$", SerializedLambda.class);
|
2013-02-16 12:36:54 -08:00
|
|
|
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() {
|
|
|
|
return String.format("SerializedLambda[capturingClass=%s, functionalInterfaceMethod=%s %s.%s:%s, " +
|
|
|
|
"implementation=%s %s.%s:%s, instantiatedMethodType=%s, numCaptured=%d]",
|
|
|
|
capturingClass, MethodHandleInfo.getReferenceKindString(functionalInterfaceMethodKind),
|
|
|
|
functionalInterfaceClass, functionalInterfaceMethodName, functionalInterfaceMethodSignature,
|
|
|
|
MethodHandleInfo.getReferenceKindString(implMethodKind), implClass, implMethodName,
|
|
|
|
implMethodSignature, instantiatedMethodType, capturedArgs.length);
|
|
|
|
}
|
|
|
|
}
|