src/java.rmi/share/classes/java/rmi/server/RemoteObjectInvocationHandler.java
author rriggs
Wed, 02 Oct 2019 13:57:03 -0400
changeset 58446 5c83830390ba
parent 47216 71c04702a3d5
permissions -rw-r--r--
8231663: Incorrect GPL header in some RMI/SQL package-info.java files Reviewed-by: bpb, iris, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
31714
ffeeccb4c043 8076409: Reinforce RMI framework
smarks
parents: 25859
diff changeset
     2
 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4512
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4512
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4512
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4512
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4512
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.rmi.server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.InvalidObjectException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.reflect.InvocationHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.Proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.rmi.Remote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.rmi.UnexpectedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.rmi.activation.Activatable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.WeakHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.rmi.server.Util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.rmi.server.WeakClassHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * An implementation of the <code>InvocationHandler</code> interface for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * use with Java Remote Method Invocation (Java RMI).  This invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * handler can be used in conjunction with a dynamic proxy instance as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * replacement for a pregenerated stub class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p>Applications are not expected to use this class directly.  A remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * object exported to use a dynamic proxy with {@link UnicastRemoteObject}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * or {@link Activatable} has an instance of this class as that proxy's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * invocation handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author  Ann Wollrath
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public class RemoteObjectInvocationHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    extends RemoteObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    implements InvocationHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private static final long serialVersionUID = 2L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * A weak hash map, mapping classes to weak hash maps that map
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * method objects to method hashes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private static final MethodToHash_Maps methodToHash_Maps =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        new MethodToHash_Maps();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Creates a new <code>RemoteObjectInvocationHandler</code> constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * with the specified <code>RemoteRef</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @param ref the remote ref
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @throws NullPointerException if <code>ref</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public RemoteObjectInvocationHandler(RemoteRef ref) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        super(ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (ref == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Processes a method invocation made on the encapsulating
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * proxy instance, <code>proxy</code>, and returns the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * <p><code>RemoteObjectInvocationHandler</code> implements this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * <p>If <code>method</code> is one of the following methods, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * is processed as described below:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * <li>{@link Object#hashCode Object.hashCode}: Returns the hash
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * code value for the proxy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * <li>{@link Object#equals Object.equals}: Returns <code>true</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * if the argument (<code>args[0]</code>) is an instance of a dynamic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * proxy class and this invocation handler is equal to the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * handler of that argument, and returns <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * <li>{@link Object#toString Object.toString}: Returns a string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * representation of the proxy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
31714
ffeeccb4c043 8076409: Reinforce RMI framework
smarks
parents: 25859
diff changeset
   105
     * <p>If <code>method</code> overrides {@link Object#finalize Object.finalize},
ffeeccb4c043 8076409: Reinforce RMI framework
smarks
parents: 25859
diff changeset
   106
     * it is ignored.
ffeeccb4c043 8076409: Reinforce RMI framework
smarks
parents: 25859
diff changeset
   107
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * <p>Otherwise, a remote call is made as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * <li>If <code>proxy</code> is not an instance of the interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * {@link Remote}, then an {@link IllegalArgumentException} is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * <li>Otherwise, the {@link RemoteRef#invoke invoke} method is invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * on this invocation handler's <code>RemoteRef</code>, passing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * <code>proxy</code>, <code>method</code>, <code>args</code>, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * method hash (defined in section 8.3 of the "Java Remote Method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Invocation (RMI) Specification") for <code>method</code>, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * result is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * <li>If an exception is thrown by <code>RemoteRef.invoke</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * that exception is a checked exception that is not assignable to any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * exception in the <code>throws</code> clause of the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * implemented by the <code>proxy</code>'s class, then that exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * is wrapped in an {@link UnexpectedException} and the wrapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * exception is thrown.  Otherwise, the exception thrown by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * <code>invoke</code> is thrown by this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * <p>The semantics of this method are unspecified if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * arguments could not have been produced by an instance of some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * valid dynamic proxy class containing this invocation handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param proxy the proxy instance that the method was invoked on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param method the <code>Method</code> instance corresponding to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * interface method invoked on the proxy instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @param args an array of objects containing the values of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * arguments passed in the method invocation on the proxy instance, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * <code>null</code> if the method takes no arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @return the value to return from the method invocation on the proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @throws  Throwable the exception to throw from the method invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * on the proxy instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public Object invoke(Object proxy, Method method, Object[] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        throws Throwable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    {
33296
6fb299910fb8 8076339: Better handling of remote object invocation
smarks
parents: 31714
diff changeset
   148
        if (! Proxy.isProxyClass(proxy.getClass())) {
6fb299910fb8 8076339: Better handling of remote object invocation
smarks
parents: 31714
diff changeset
   149
            throw new IllegalArgumentException("not a proxy");
6fb299910fb8 8076339: Better handling of remote object invocation
smarks
parents: 31714
diff changeset
   150
        }
6fb299910fb8 8076339: Better handling of remote object invocation
smarks
parents: 31714
diff changeset
   151
6fb299910fb8 8076339: Better handling of remote object invocation
smarks
parents: 31714
diff changeset
   152
        if (Proxy.getInvocationHandler(proxy) != this) {
6fb299910fb8 8076339: Better handling of remote object invocation
smarks
parents: 31714
diff changeset
   153
            throw new IllegalArgumentException("handler mismatch");
6fb299910fb8 8076339: Better handling of remote object invocation
smarks
parents: 31714
diff changeset
   154
        }
6fb299910fb8 8076339: Better handling of remote object invocation
smarks
parents: 31714
diff changeset
   155
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (method.getDeclaringClass() == Object.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            return invokeObjectMethod(proxy, method, args);
31714
ffeeccb4c043 8076409: Reinforce RMI framework
smarks
parents: 25859
diff changeset
   158
        } else if ("finalize".equals(method.getName()) && method.getParameterCount() == 0) {
ffeeccb4c043 8076409: Reinforce RMI framework
smarks
parents: 25859
diff changeset
   159
            return null; // ignore
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            return invokeRemoteMethod(proxy, method, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Handles java.lang.Object methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    private Object invokeObjectMethod(Object proxy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                                      Method method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                                      Object[] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        String name = method.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (name.equals("hashCode")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            return hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        } else if (name.equals("equals")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            Object obj = args[0];
33296
6fb299910fb8 8076339: Better handling of remote object invocation
smarks
parents: 31714
diff changeset
   179
            InvocationHandler hdlr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                proxy == obj ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                (obj != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                 Proxy.isProxyClass(obj.getClass()) &&
33296
6fb299910fb8 8076339: Better handling of remote object invocation
smarks
parents: 31714
diff changeset
   184
                 (hdlr = Proxy.getInvocationHandler(obj)) instanceof RemoteObjectInvocationHandler &&
6fb299910fb8 8076339: Better handling of remote object invocation
smarks
parents: 31714
diff changeset
   185
                 this.equals(hdlr));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        } else if (name.equals("toString")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            return proxyToString(proxy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                "unexpected Object method: " + method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Handles remote methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    private Object invokeRemoteMethod(Object proxy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                                      Method method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                                      Object[] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            if (!(proxy instanceof Remote)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    "proxy not Remote instance");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            return ref.invoke((Remote) proxy, method, args,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                              getMethodHash(method));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            if (!(e instanceof RuntimeException)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                Class<?> cl = proxy.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    method = cl.getMethod(method.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                          method.getParameterTypes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                } catch (NoSuchMethodException nsme) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                    throw (IllegalArgumentException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                        new IllegalArgumentException().initCause(nsme);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                Class<?> thrownType = e.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                for (Class<?> declaredType : method.getExceptionTypes()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    if (declaredType.isAssignableFrom(thrownType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                        throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                e = new UnexpectedException("unexpected exception", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Returns a string representation for a proxy that uses this invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    private String proxyToString(Object proxy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        Class<?>[] interfaces = proxy.getClass().getInterfaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if (interfaces.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            return "Proxy[" + this + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        String iface = interfaces[0].getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (iface.equals("java.rmi.Remote") && interfaces.length > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            iface = interfaces[1].getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        int dot = iface.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (dot >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            iface = iface.substring(dot + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        return "Proxy[" + iface + "," + this + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @throws InvalidObjectException unconditionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    private void readObjectNoData() throws InvalidObjectException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        throw new InvalidObjectException("no data in stream; class: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                                         this.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Returns the method hash for the specified method.  Subsequent calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * to "getMethodHash" passing the same method argument should be faster
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * since this method caches internally the result of the method to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * method hash mapping.  The method hash is calculated using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * "computeMethodHash" method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @param method the remote method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @return the method hash for the specified method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    private static long getMethodHash(Method method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        return methodToHash_Maps.get(method.getDeclaringClass()).get(method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * A weak hash map, mapping classes to weak hash maps that map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * method objects to method hashes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    private static class MethodToHash_Maps
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        extends WeakClassHashMap<Map<Method,Long>>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        MethodToHash_Maps() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        protected Map<Method,Long> computeValue(Class<?> remoteClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            return new WeakHashMap<Method,Long>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                public synchronized Long get(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    Long hash = super.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    if (hash == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                        Method method = (Method) key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                        hash = Util.computeMethodHash(method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                        put(method, hash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    return hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
}