jdk/src/share/classes/sun/rmi/server/Util.java
author smarks
Tue, 06 Aug 2013 14:24:05 -0700
changeset 19211 32a04c562026
parent 16923 50bfa0defec2
child 21655 55f32ae4f920
permissions -rw-r--r--
8022440: suppress deprecation warnings in sun.rmi Reviewed-by: mduigou
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
19211
32a04c562026 8022440: suppress deprecation warnings in sun.rmi
smarks
parents: 16923
diff changeset
     2
 * Copyright (c) 1996, 2013, 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: 715
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: 715
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: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package sun.rmi.server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.DataOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.reflect.InvocationHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.reflect.InvocationTargetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.reflect.Proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.rmi.Remote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.rmi.RemoteException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.rmi.StubNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.rmi.registry.Registry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.rmi.server.LogStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.rmi.server.ObjID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.rmi.server.RMIClientSocketFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.rmi.server.RemoteObjectInvocationHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.rmi.server.RemoteRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.rmi.server.RemoteStub;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.rmi.server.Skeleton;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.rmi.server.SkeletonNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.security.MessageDigest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.security.DigestOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.security.NoSuchAlgorithmException;
16923
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 14342
diff changeset
    51
import java.security.PrivilegedAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.util.WeakHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import sun.rmi.registry.RegistryImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import sun.rmi.runtime.Log;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import sun.rmi.transport.LiveRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import sun.rmi.transport.tcp.TCPEndpoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import sun.security.action.GetBooleanAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import sun.security.action.GetPropertyAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * A utility class with static methods for creating stubs/proxies and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * skeletons for remote objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 */
19211
32a04c562026 8022440: suppress deprecation warnings in sun.rmi
smarks
parents: 16923
diff changeset
    67
@SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
public final class Util {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /** "server" package log level */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    static final int logLevel = LogStream.parseLevel(
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    72
        AccessController.doPrivileged(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            new GetPropertyAction("sun.rmi.server.logLevel")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /** server reference log */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public static final Log serverRefLog =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        Log.getLog("sun.rmi.server.ref", "transport", Util.logLevel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /** cached value of property java.rmi.server.ignoreStubClasses */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private static final boolean ignoreStubClasses =
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    81
        AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    82
            new GetBooleanAction("java.rmi.server.ignoreStubClasses")).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /** cache of  impl classes that have no corresponding stub class */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    86
    private static final Map<Class<?>, Void> withoutStubs =
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    87
        Collections.synchronizedMap(new WeakHashMap<Class<?>, Void>(11));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /** parameter types for stub constructor */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private static final Class[] stubConsParamTypes = { RemoteRef.class };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private Util() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Returns a proxy for the specified implClass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * If both of the following criteria is satisfied, a dynamic proxy for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * the specified implClass is returned (otherwise a RemoteStub instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * for the specified implClass is returned):
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *    a) either the property java.rmi.server.ignoreStubClasses is true or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *       a pregenerated stub class does not exist for the impl class, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *    b) forceStubUse is false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * If the above criteria are satisfied, this method constructs a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * dynamic proxy instance (that implements the remote interfaces of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * implClass) constructed with a RemoteObjectInvocationHandler instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * constructed with the clientRef.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Otherwise, this method loads the pregenerated stub class (which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * extends RemoteStub and implements the remote interfaces of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * implClass) and constructs an instance of the pregenerated stub
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * class with the clientRef.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param implClass the class to obtain remote interfaces from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param clientRef the remote ref to use in the invocation handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param forceStubUse if true, forces creation of a RemoteStub
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @throws IllegalArgumentException if implClass implements illegal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * remote interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @throws StubNotFoundException if problem locating/creating stub or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * creating the dynamic proxy instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     **/
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   124
    public static Remote createProxy(Class<?> implClass,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                                     RemoteRef clientRef,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                     boolean forceStubUse)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        throws StubNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    {
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   129
        Class<?> remoteClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            remoteClass = getRemoteClass(implClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        } catch (ClassNotFoundException ex ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            throw new StubNotFoundException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                "object does not implement a remote interface: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                implClass.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (forceStubUse ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            !(ignoreStubClasses || !stubClassExists(remoteClass)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            return createStub(remoteClass, clientRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
16923
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 14342
diff changeset
   145
        final ClassLoader loader = implClass.getClassLoader();
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 14342
diff changeset
   146
        final Class[] interfaces = getRemoteInterfaces(implClass);
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 14342
diff changeset
   147
        final InvocationHandler handler =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            new RemoteObjectInvocationHandler(clientRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        /* REMIND: private remote interfaces? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        try {
16923
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 14342
diff changeset
   153
            return AccessController.doPrivileged(new PrivilegedAction<Remote>() {
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 14342
diff changeset
   154
                public Remote run() {
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 14342
diff changeset
   155
                    return (Remote) Proxy.newProxyInstance(loader,
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 14342
diff changeset
   156
                                                           interfaces,
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 14342
diff changeset
   157
                                                           handler);
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 14342
diff changeset
   158
                }});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            throw new StubNotFoundException("unable to create proxy", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
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
     * Returns true if a stub class for the given impl class can be loaded,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * otherwise returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @param remoteClass the class to obtain remote interfaces from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   170
    private static boolean stubClassExists(Class<?> remoteClass) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (!withoutStubs.containsKey(remoteClass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                Class.forName(remoteClass.getName() + "_Stub",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                              false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                              remoteClass.getClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            } catch (ClassNotFoundException cnfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                withoutStubs.put(remoteClass, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Returns the class/superclass that implements the remote interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @throws ClassNotFoundException if no class is found to have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * remote interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   190
    private static Class<?> getRemoteClass(Class<?> cl)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        throws ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        while (cl != null) {
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   194
            Class<?>[] interfaces = cl.getInterfaces();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            for (int i = interfaces.length -1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                if (Remote.class.isAssignableFrom(interfaces[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    return cl;          // this class implements remote object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            cl = cl.getSuperclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        throw new ClassNotFoundException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                "class does not implement java.rmi.Remote");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Returns an array containing the remote interfaces implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * by the given class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @param   remoteClass the class to obtain remote interfaces from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @throws  IllegalArgumentException if remoteClass implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *          any illegal remote interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @throws  NullPointerException if remoteClass is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   214
    private static Class<?>[] getRemoteInterfaces(Class<?> remoteClass) {
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   215
        ArrayList<Class<?>> list = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        getRemoteInterfaces(list, remoteClass);
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   217
        return list.toArray(new Class<?>[list.size()]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Fills the given array list with the remote interfaces implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * by the given class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @throws  IllegalArgumentException if the specified class implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *          any illegal remote interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @throws  NullPointerException if the specified class or list is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   228
    private static void getRemoteInterfaces(ArrayList<Class<?>> list, Class<?> cl) {
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   229
        Class<?> superclass = cl.getSuperclass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (superclass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            getRemoteInterfaces(list, superclass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   234
        Class<?>[] interfaces = cl.getInterfaces();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        for (int i = 0; i < interfaces.length; i++) {
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   236
            Class<?> intf = interfaces[i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
             * If it is a remote interface (if it extends from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
             * java.rmi.Remote) and is not already in the list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
             * then add the interface to the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            if (Remote.class.isAssignableFrom(intf)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                if (!(list.contains(intf))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    Method[] methods = intf.getMethods();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    for (int j = 0; j < methods.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                        checkMethod(methods[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    list.add(intf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            }
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Verifies that the supplied method has at least one declared exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * type that is RemoteException or one of its superclasses.  If not,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * then this method throws IllegalArgumentException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @throws IllegalArgumentException if m is an illegal remote method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    private static void checkMethod(Method m) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   262
        Class<?>[] ex = m.getExceptionTypes();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        for (int i = 0; i < ex.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            if (ex[i].isAssignableFrom(RemoteException.class))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            "illegal remote method encountered: " + m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * Creates a RemoteStub instance for the specified class, constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * with the specified RemoteRef.  The supplied class must be the most
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * derived class in the remote object's superclass chain that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * implements a remote interface.  The stub class name is the name of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * the specified remoteClass with the suffix "_Stub".  The loading of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * the stub class is initiated from class loader of the specified class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * (which may be the bootstrap class loader).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     **/
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   280
    private static RemoteStub createStub(Class<?> remoteClass, RemoteRef ref)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        throws StubNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        String stubname = remoteClass.getName() + "_Stub";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        /* Make sure to use the local stub loader for the stub classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
         * When loaded by the local loader the load path can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
         * propagated to remote clients, by the MarshalOutputStream/InStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
         * pickle methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   291
            Class<?> stubcl =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                Class.forName(stubname, false, remoteClass.getClassLoader());
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   293
            Constructor<?> cons = stubcl.getConstructor(stubConsParamTypes);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            return (RemoteStub) cons.newInstance(new Object[] { ref });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            throw new StubNotFoundException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                "Stub class not found: " + stubname, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        } catch (NoSuchMethodException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            throw new StubNotFoundException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                "Stub class missing constructor: " + stubname, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        } catch (InstantiationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            throw new StubNotFoundException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                "Can't create instance of stub class: " + stubname, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            throw new StubNotFoundException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                "Stub class constructor not public: " + stubname, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        } catch (InvocationTargetException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            throw new StubNotFoundException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                "Exception creating instance of stub class: " + stubname, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        } catch (ClassCastException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            throw new StubNotFoundException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                "Stub class not instance of RemoteStub: " + stubname, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * Locate and return the Skeleton for the specified remote object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    static Skeleton createSkeleton(Remote object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        throws SkeletonNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    {
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   323
        Class<?> cl;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            cl = getRemoteClass(object.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        } catch (ClassNotFoundException ex ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            throw new SkeletonNotFoundException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                "object does not implement a remote interface: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                object.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        // now try to load the skeleton based ont he name of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        String skelname = cl.getName() + "_Skel";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        try {
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   335
            Class<?> skelcl = Class.forName(skelname, false, cl.getClassLoader());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            return (Skeleton)skelcl.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        } catch (ClassNotFoundException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            throw new SkeletonNotFoundException("Skeleton class not found: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                                                skelname, ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        } catch (InstantiationException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            throw new SkeletonNotFoundException("Can't create skeleton: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                                                skelname, ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        } catch (IllegalAccessException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            throw new SkeletonNotFoundException("No public constructor: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                                                skelname, ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        } catch (ClassCastException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            throw new SkeletonNotFoundException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                "Skeleton not of correct class: " + skelname, ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * Compute the "method hash" of a remote method.  The method hash
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * is a long containing the first 64 bits of the SHA digest from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * the UTF encoded string of the method name and descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    public static long computeMethodHash(Method m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        long hash = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        ByteArrayOutputStream sink = new ByteArrayOutputStream(127);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            MessageDigest md = MessageDigest.getInstance("SHA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            DataOutputStream out = new DataOutputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                new DigestOutputStream(sink, md));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            String s = getMethodNameAndDescriptor(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            if (serverRefLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                serverRefLog.log(Log.VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    "string used for method hash: \"" + s + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            out.writeUTF(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            // use only the first 64 bits of the digest for the hash
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            byte hasharray[] = md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            for (int i = 0; i < Math.min(8, hasharray.length); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                hash += ((long) (hasharray[i] & 0xFF)) << (i * 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        } catch (IOException ignore) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            /* can't happen, but be deterministic anyway. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            hash = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        } catch (NoSuchAlgorithmException complain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            throw new SecurityException(complain.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        return hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * Return a string consisting of the given method's name followed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * its "method descriptor", as appropriate for use in the computation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * of the "method hash".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * See section 4.3.3 of The Java Virtual Machine Specification for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * the definition of a "method descriptor".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    private static String getMethodNameAndDescriptor(Method m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        StringBuffer desc = new StringBuffer(m.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        desc.append('(');
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   399
        Class<?>[] paramTypes = m.getParameterTypes();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        for (int i = 0; i < paramTypes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            desc.append(getTypeDescriptor(paramTypes[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        desc.append(')');
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   404
        Class<?> returnType = m.getReturnType();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        if (returnType == void.class) { // optimization: handle void here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            desc.append('V');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            desc.append(getTypeDescriptor(returnType));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        return desc.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * Get the descriptor of a particular type, as appropriate for either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * a parameter or return type in a method descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     */
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   417
    private static String getTypeDescriptor(Class<?> type) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        if (type.isPrimitive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            if (type == int.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                return "I";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            } else if (type == boolean.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                return "Z";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            } else if (type == byte.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                return "B";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            } else if (type == char.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                return "C";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            } else if (type == short.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                return "S";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            } else if (type == long.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                return "J";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            } else if (type == float.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                return "F";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            } else if (type == double.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                return "D";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            } else if (type == void.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                return "V";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                throw new Error("unrecognized primitive type: " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        } else if (type.isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
             * According to JLS 20.3.2, the getName() method on Class does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
             * return the VM type descriptor format for array classes (only);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
             * using that should be quicker than the otherwise obvious code:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
             *     return "[" + getTypeDescriptor(type.getComponentType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            return type.getName().replace('.', '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            return "L" + type.getName().replace('.', '/') + ";";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * Returns the binary name of the given type without package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * qualification.  Nested types are treated no differently from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * top-level types, so for a nested type, the returned name will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * still be qualified with the simple name of its enclosing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * top-level type (and perhaps other enclosing types), the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * separator will be '$', etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     **/
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   462
    public static String getUnqualifiedName(Class<?> c) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        String binaryName = c.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        return binaryName.substring(binaryName.lastIndexOf('.') + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
}