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