1 /* 2 * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package java.lang.invoke; 26 27 import java.io.Serializable; 28 import java.io.InvalidObjectException; 29 import java.io.ObjectStreamException; 30 import java.lang.reflect.Method; 31 import java.security.AccessController; 32 import java.security.PrivilegedActionException; 33 import java.security.PrivilegedExceptionAction; 34 import java.util.Objects; 35 36 /** 37 * Serialized form of a lambda expression. The properties of this class 38 * represent the information that is present at the lambda factory site, including 39 * static metafactory arguments such as the identity of the primary functional 40 * interface method and the identity of the implementation method, as well as 41 * dynamic metafactory arguments such as values captured from the lexical scope 42 * at the time of lambda capture. 43 * 44 * <p>Implementors of serializable lambdas, such as compilers or language 45 * runtime libraries, are expected to ensure that instances deserialize properly. 46 * One means to do so is to ensure that the {@code writeReplace} method returns 47 * an instance of {@code SerializedLambda}, rather than allowing default 48 * serialization to proceed. 49 * 50 * <p>{@code SerializedLambda} has a {@code readResolve} method that looks for 51 * a (possibly private) static method called 52 * {@code $deserializeLambda$(SerializedLambda)} in the capturing class, invokes 53 * that with itself as the first argument, and returns the result. Lambda classes 54 * implementing {@code $deserializeLambda$} are responsible for validating 55 * that the properties of the {@code SerializedLambda} are consistent with a 56 * lambda actually captured by that class. 57 * 58 * <p>The identity of a function object produced by deserializing the serialized 59 * form is unpredictable, and therefore identity-sensitive operations (such as 60 * reference equality, object locking, and {@code System.identityHashCode()} may 61 * produce different results in different implementations, or even upon 62 * different deserializations in the same implementation. 63 * 64 * @see LambdaMetafactory 65 * @since 1.8 66 */ 67 public final class SerializedLambda implements Serializable { 68 @java.io.Serial 69 private static final long serialVersionUID = 8025925345765570181L; 70 private final Class<?> capturingClass; 71 private final String functionalInterfaceClass; 72 private final String functionalInterfaceMethodName; 73 private final String functionalInterfaceMethodSignature; 74 private final String implClass; 75 private final String implMethodName; 76 private final String implMethodSignature; 77 private final int implMethodKind; 78 private final String instantiatedMethodType; 79 private final Object[] capturedArgs; 80 81 /** 82 * Create a {@code SerializedLambda} from the low-level information present 83 * at the lambda factory site. 84 * 85 * @param capturingClass The class in which the lambda expression appears 86 * @param functionalInterfaceClass Name, in slash-delimited form, of static 87 * type of the returned lambda object 88 * @param functionalInterfaceMethodName Name of the functional interface 89 * method for the present at the 90 * lambda factory site 91 * @param functionalInterfaceMethodSignature Signature of the functional 92 * interface method present at 93 * the lambda factory site 94 * @param implMethodKind Method handle kind for the implementation method 95 * @param implClass Name, in slash-delimited form, for the class holding 96 * the implementation method 97 * @param implMethodName Name of the implementation method 98 * @param implMethodSignature Signature of the implementation method 99 * @param instantiatedMethodType The signature of the primary functional 100 * interface method after type variables 101 * are substituted with their instantiation 102 * from the capture site 103 * @param capturedArgs The dynamic arguments to the lambda factory site, 104 * which represent variables captured by 105 * the lambda 106 */ 107 public SerializedLambda(Class<?> capturingClass, 108 String functionalInterfaceClass, 109 String functionalInterfaceMethodName, 110 String functionalInterfaceMethodSignature, 111 int implMethodKind, 112 String implClass, 113 String implMethodName, 114 String implMethodSignature, 115 String instantiatedMethodType, 116 Object[] capturedArgs) { 117 this.capturingClass = capturingClass; 118 this.functionalInterfaceClass = functionalInterfaceClass; 119 this.functionalInterfaceMethodName = functionalInterfaceMethodName; 120 this.functionalInterfaceMethodSignature = functionalInterfaceMethodSignature; 121 this.implMethodKind = implMethodKind; 122 this.implClass = implClass; 123 this.implMethodName = implMethodName; 124 this.implMethodSignature = implMethodSignature; 125 this.instantiatedMethodType = instantiatedMethodType; 126 this.capturedArgs = Objects.requireNonNull(capturedArgs).clone(); 127 } 128 129 /** 130 * Get the name of the class that captured this lambda. 131 * @return the name of the class that captured this lambda 132 */ 133 public String getCapturingClass() { 134 return capturingClass.getName().replace('.', '/'); 135 } 136 137 /** 138 * Get the name of the invoked type to which this 139 * lambda has been converted 140 * @return the name of the functional interface class to which 141 * this lambda has been converted 142 */ 143 public String getFunctionalInterfaceClass() { 144 return functionalInterfaceClass; 145 } 146 147 /** 148 * Get the name of the primary method for the functional interface 149 * to which this lambda has been converted. 150 * @return the name of the primary methods of the functional interface 151 */ 152 public String getFunctionalInterfaceMethodName() { 153 return functionalInterfaceMethodName; 154 } 155 156 /** 157 * Get the signature of the primary method for the functional 158 * interface to which this lambda has been converted. 159 * @return the signature of the primary method of the functional 160 * interface 161 */ 162 public String getFunctionalInterfaceMethodSignature() { 163 return functionalInterfaceMethodSignature; 164 } 165 166 /** 167 * Get the name of the class containing the implementation 168 * method. 169 * @return the name of the class containing the implementation 170 * method 171 */ 172 public String getImplClass() { 173 return implClass; 174 } 175 176 /** 177 * Get the name of the implementation method. 178 * @return the name of the implementation method 179 */ 180 public String getImplMethodName() { 181 return implMethodName; 182 } 183 184 /** 185 * Get the signature of the implementation method. 186 * @return the signature of the implementation method 187 */ 188 public String getImplMethodSignature() { 189 return implMethodSignature; 190 } 191 192 /** 193 * Get the method handle kind (see {@link MethodHandleInfo}) of 194 * the implementation method. 195 * @return the method handle kind of the implementation method 196 */ 197 public int getImplMethodKind() { 198 return implMethodKind; 199 } 200 201 /** 202 * Get the signature of the primary functional interface method 203 * after type variables are substituted with their instantiation 204 * from the capture site. 205 * @return the signature of the primary functional interface method 206 * after type variable processing 207 */ 208 public final String getInstantiatedMethodType() { 209 return instantiatedMethodType; 210 } 211 212 /** 213 * Get the count of dynamic arguments to the lambda capture site. 214 * @return the count of dynamic arguments to the lambda capture site 215 */ 216 public int getCapturedArgCount() { 217 return capturedArgs.length; 218 } 219 220 /** 221 * Get a dynamic argument to the lambda capture site. 222 * @param i the argument to capture 223 * @return a dynamic argument to the lambda capture site 224 */ 225 public Object getCapturedArg(int i) { 226 return capturedArgs[i]; 227 } 228 229 @java.io.Serial 230 private Object readResolve() throws ObjectStreamException { 231 try { 232 Method deserialize = AccessController.doPrivileged(new PrivilegedExceptionAction<>() { 233 @Override 234 public Method run() throws Exception { 235 Method m = capturingClass.getDeclaredMethod("$deserializeLambda$", SerializedLambda.class); 236 m.setAccessible(true); 237 return m; 238 } 239 }); 240 241 return deserialize.invoke(null, this); 242 } catch (ReflectiveOperationException roe) { 243 ObjectStreamException ose = new InvalidObjectException("ReflectiveOperationException during deserialization"); 244 ose.initCause(roe); 245 throw ose; 246 } catch (PrivilegedActionException e) { 247 Exception cause = e.getException(); 248 if (cause instanceof RuntimeException) 249 throw (RuntimeException) cause; 250 else 251 throw new RuntimeException("Exception in SerializedLambda.readResolve", e); 252 } 253 } 254 255 @Override 256 public String toString() { 257 String implKind=MethodHandleInfo.referenceKindToString(implMethodKind); 258 return String.format("SerializedLambda[%s=%s, %s=%s.%s:%s, " + 259 "%s=%s %s.%s:%s, %s=%s, %s=%d]", 260 "capturingClass", capturingClass, 261 "functionalInterfaceMethod", functionalInterfaceClass, 262 functionalInterfaceMethodName, 263 functionalInterfaceMethodSignature, 264 "implementation", 265 implKind, 266 implClass, implMethodName, implMethodSignature, 267 "instantiatedMethodType", instantiatedMethodType, 268 "numCaptured", capturedArgs.length); 269 } 270 }