SerializedLambda.java 9.2 KB
Newer Older
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
/*
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
 * 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;
43
    private final Class<?> capturingClass;
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
    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
     */
76
    public SerializedLambda(Class<?> capturingClass,
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
                            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();
    }

    /** Get the name of the class that captured this lambda */
    public String getCapturingClass() {
102
        return capturingClass.getName().replace('.', '/');
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
    }

    /** Get 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 */
    public String getFunctionalInterfaceMethodName() {
        return functionalInterfaceMethodName;
    }

    /** Get the signature of the primary method for the functional interface to which this lambda has been converted */
    public String getFunctionalInterfaceMethodSignature() {
        return functionalInterfaceMethodSignature;
    }

    /** Get the method handle kind (see {@link MethodHandleInfo}) of the primary method for the functional interface
     * to which this lambda has been converted */
    public int getFunctionalInterfaceMethodKind() {
        return functionalInterfaceMethodKind;
    }

    /** Get the name of the class containing the implementation method */
    public String getImplClass() {
        return implClass;
    }

    /** Get the name of the implementation method */
    public String getImplMethodName() {
        return implMethodName;
    }

    /** Get the signature of the implementation method */
    public String getImplMethodSignature() {
        return implMethodSignature;
    }

    /** Get the method handle kind (see {@link MethodHandleInfo}) 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
     */
    public final String getInstantiatedMethodType() {
        return instantiatedMethodType;
    }

    /** Get 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 */
    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 {
169
                    Method m = capturingClass.getDeclaredMethod("$deserializeLambda$", SerializedLambda.class);
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
                    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);
    }
}