rt/emul/compact/src/main/java/java/lang/invoke/SerializedLambda.java
changeset 1692 2f800fdc371e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compact/src/main/java/java/lang/invoke/SerializedLambda.java	Sun Sep 14 19:27:44 2014 +0200
     1.3 @@ -0,0 +1,257 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +package java.lang.invoke;
    1.29 +
    1.30 +import java.io.Serializable;
    1.31 +import java.lang.reflect.Method;
    1.32 +import java.security.AccessController;
    1.33 +import java.security.PrivilegedActionException;
    1.34 +import java.security.PrivilegedExceptionAction;
    1.35 +import java.util.Objects;
    1.36 +
    1.37 +/**
    1.38 + * Serialized form of a lambda expression.  The properties of this class
    1.39 + * represent the information that is present at the lambda factory site, including
    1.40 + * static metafactory arguments such as the identity of the primary functional
    1.41 + * interface method and the identity of the implementation method, as well as
    1.42 + * dynamic metafactory arguments such as values captured from the lexical scope
    1.43 + * at the time of lambda capture.
    1.44 + *
    1.45 + * <p>Implementors of serializable lambdas, such as compilers or language
    1.46 + * runtime libraries, are expected to ensure that instances deserialize properly.
    1.47 + * One means to do so is to ensure that the {@code writeReplace} method returns
    1.48 + * an instance of {@code SerializedLambda}, rather than allowing default
    1.49 + * serialization to proceed.
    1.50 + *
    1.51 + * <p>{@code SerializedLambda} has a {@code readResolve} method that looks for
    1.52 + * a (possibly private) static method called
    1.53 + * {@code $deserializeLambda$(SerializedLambda)} in the capturing class, invokes
    1.54 + * that with itself as the first argument, and returns the result.  Lambda classes
    1.55 + * implementing {@code $deserializeLambda$} are responsible for validating
    1.56 + * that the properties of the {@code SerializedLambda} are consistent with a
    1.57 + * lambda actually captured by that class.
    1.58 + *
    1.59 + * @see LambdaMetafactory
    1.60 + */
    1.61 +public final class SerializedLambda implements Serializable {
    1.62 +    private static final long serialVersionUID = 8025925345765570181L;
    1.63 +    private final Class<?> capturingClass;
    1.64 +    private final String functionalInterfaceClass;
    1.65 +    private final String functionalInterfaceMethodName;
    1.66 +    private final String functionalInterfaceMethodSignature;
    1.67 +    private final String implClass;
    1.68 +    private final String implMethodName;
    1.69 +    private final String implMethodSignature;
    1.70 +    private final int implMethodKind;
    1.71 +    private final String instantiatedMethodType;
    1.72 +    private final Object[] capturedArgs;
    1.73 +
    1.74 +    /**
    1.75 +     * Create a {@code SerializedLambda} from the low-level information present
    1.76 +     * at the lambda factory site.
    1.77 +     *
    1.78 +     * @param capturingClass The class in which the lambda expression appears
    1.79 +     * @param functionalInterfaceClass Name, in slash-delimited form, of static
    1.80 +     *                                 type of the returned lambda object
    1.81 +     * @param functionalInterfaceMethodName Name of the functional interface
    1.82 +     *                                      method for the present at the
    1.83 +     *                                      lambda factory site
    1.84 +     * @param functionalInterfaceMethodSignature Signature of the functional
    1.85 +     *                                           interface method present at
    1.86 +     *                                           the lambda factory site
    1.87 +     * @param implMethodKind Method handle kind for the implementation method
    1.88 +     * @param implClass Name, in slash-delimited form, for the class holding
    1.89 +     *                  the implementation method
    1.90 +     * @param implMethodName Name of the implementation method
    1.91 +     * @param implMethodSignature Signature of the implementation method
    1.92 +     * @param instantiatedMethodType The signature of the primary functional
    1.93 +     *                               interface method after type variables
    1.94 +     *                               are substituted with their instantiation
    1.95 +     *                               from the capture site
    1.96 +     * @param capturedArgs The dynamic arguments to the lambda factory site,
    1.97 +     *                     which represent variables captured by
    1.98 +     *                     the lambda
    1.99 +     */
   1.100 +    public SerializedLambda(Class<?> capturingClass,
   1.101 +                            String functionalInterfaceClass,
   1.102 +                            String functionalInterfaceMethodName,
   1.103 +                            String functionalInterfaceMethodSignature,
   1.104 +                            int implMethodKind,
   1.105 +                            String implClass,
   1.106 +                            String implMethodName,
   1.107 +                            String implMethodSignature,
   1.108 +                            String instantiatedMethodType,
   1.109 +                            Object[] capturedArgs) {
   1.110 +        this.capturingClass = capturingClass;
   1.111 +        this.functionalInterfaceClass = functionalInterfaceClass;
   1.112 +        this.functionalInterfaceMethodName = functionalInterfaceMethodName;
   1.113 +        this.functionalInterfaceMethodSignature = functionalInterfaceMethodSignature;
   1.114 +        this.implMethodKind = implMethodKind;
   1.115 +        this.implClass = implClass;
   1.116 +        this.implMethodName = implMethodName;
   1.117 +        this.implMethodSignature = implMethodSignature;
   1.118 +        this.instantiatedMethodType = instantiatedMethodType;
   1.119 +        this.capturedArgs = Objects.requireNonNull(capturedArgs).clone();
   1.120 +    }
   1.121 +
   1.122 +    /**
   1.123 +     * Get the name of the class that captured this lambda.
   1.124 +     * @return the name of the class that captured this lambda
   1.125 +     */
   1.126 +    public String getCapturingClass() {
   1.127 +        return capturingClass.getName().replace('.', '/');
   1.128 +    }
   1.129 +
   1.130 +    /**
   1.131 +     * Get the name of the invoked type to which this
   1.132 +     * lambda has been converted
   1.133 +     * @return the name of the functional interface class to which
   1.134 +     * this lambda has been converted
   1.135 +     */
   1.136 +    public String getFunctionalInterfaceClass() {
   1.137 +        return functionalInterfaceClass;
   1.138 +    }
   1.139 +
   1.140 +    /**
   1.141 +     * Get the name of the primary method for the functional interface
   1.142 +     * to which this lambda has been converted.
   1.143 +     * @return the name of the primary methods of the functional interface
   1.144 +     */
   1.145 +    public String getFunctionalInterfaceMethodName() {
   1.146 +        return functionalInterfaceMethodName;
   1.147 +    }
   1.148 +
   1.149 +    /**
   1.150 +     * Get the signature of the primary method for the functional
   1.151 +     * interface to which this lambda has been converted.
   1.152 +     * @return the signature of the primary method of the functional
   1.153 +     * interface
   1.154 +     */
   1.155 +    public String getFunctionalInterfaceMethodSignature() {
   1.156 +        return functionalInterfaceMethodSignature;
   1.157 +    }
   1.158 +
   1.159 +    /**
   1.160 +     * Get the name of the class containing the implementation
   1.161 +     * method.
   1.162 +     * @return the name of the class containing the implementation
   1.163 +     * method
   1.164 +     */
   1.165 +    public String getImplClass() {
   1.166 +        return implClass;
   1.167 +    }
   1.168 +
   1.169 +    /**
   1.170 +     * Get the name of the implementation method.
   1.171 +     * @return the name of the implementation method
   1.172 +     */
   1.173 +    public String getImplMethodName() {
   1.174 +        return implMethodName;
   1.175 +    }
   1.176 +
   1.177 +    /**
   1.178 +     * Get the signature of the implementation method.
   1.179 +     * @return the signature of the implementation method
   1.180 +     */
   1.181 +    public String getImplMethodSignature() {
   1.182 +        return implMethodSignature;
   1.183 +    }
   1.184 +
   1.185 +    /**
   1.186 +     * Get the method handle kind (see {@link MethodHandleInfo}) of
   1.187 +     * the implementation method.
   1.188 +     * @return the method handle kind of the implementation method
   1.189 +     */
   1.190 +    public int getImplMethodKind() {
   1.191 +        return implMethodKind;
   1.192 +    }
   1.193 +
   1.194 +    /**
   1.195 +     * Get the signature of the primary functional interface method
   1.196 +     * after type variables are substituted with their instantiation
   1.197 +     * from the capture site.
   1.198 +     * @return the signature of the primary functional interface method
   1.199 +     * after type variable processing
   1.200 +     */
   1.201 +    public final String getInstantiatedMethodType() {
   1.202 +        return instantiatedMethodType;
   1.203 +    }
   1.204 +
   1.205 +    /**
   1.206 +     * Get the count of dynamic arguments to the lambda capture site.
   1.207 +     * @return the count of dynamic arguments to the lambda capture site
   1.208 +     */
   1.209 +    public int getCapturedArgCount() {
   1.210 +        return capturedArgs.length;
   1.211 +    }
   1.212 +
   1.213 +    /**
   1.214 +     * Get a dynamic argument to the lambda capture site.
   1.215 +     * @param i the argument to capture
   1.216 +     * @return a dynamic argument to the lambda capture site
   1.217 +     */
   1.218 +    public Object getCapturedArg(int i) {
   1.219 +        return capturedArgs[i];
   1.220 +    }
   1.221 +
   1.222 +    private Object readResolve() throws ReflectiveOperationException {
   1.223 +        try {
   1.224 +            Method deserialize = AccessController.doPrivileged(new PrivilegedExceptionAction<Method>() {
   1.225 +                @Override
   1.226 +                public Method run() throws Exception {
   1.227 +                    Method m = capturingClass.getDeclaredMethod("$deserializeLambda$", SerializedLambda.class);
   1.228 +                    m.setAccessible(true);
   1.229 +                    return m;
   1.230 +                }
   1.231 +            });
   1.232 +
   1.233 +            return deserialize.invoke(null, this);
   1.234 +        }
   1.235 +        catch (PrivilegedActionException e) {
   1.236 +            Exception cause = e.getException();
   1.237 +            if (cause instanceof ReflectiveOperationException)
   1.238 +                throw (ReflectiveOperationException) cause;
   1.239 +            else if (cause instanceof RuntimeException)
   1.240 +                throw (RuntimeException) cause;
   1.241 +            else
   1.242 +                throw new RuntimeException("Exception in SerializedLambda.readResolve", e);
   1.243 +        }
   1.244 +    }
   1.245 +
   1.246 +    @Override
   1.247 +    public String toString() {
   1.248 +        return String.format("SerializedLambda[%s=%s, %s=%s.%s:%s, " +
   1.249 +                             "%s=%s %s.%s:%s, %s=%s, %s=%d]",
   1.250 +                             "capturingClass", capturingClass,
   1.251 +                             "functionalInterfaceMethod", functionalInterfaceClass,
   1.252 +                               functionalInterfaceMethodName,
   1.253 +                               functionalInterfaceMethodSignature,
   1.254 +                             "implementation",
   1.255 +                               null,
   1.256 +                               implClass, implMethodName, implMethodSignature,
   1.257 +                             "instantiatedMethodType", instantiatedMethodType,
   1.258 +                             "numCaptured", capturedArgs.length);
   1.259 +    }
   1.260 +}