GetRemoteClass.java revision 9330:8b1f1c2a400f
1254721Semaste/*
2254721Semaste * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
3254721Semaste * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4254721Semaste *
5254721Semaste * This code is free software; you can redistribute it and/or modify it
6254721Semaste * under the terms of the GNU General Public License version 2 only, as
7254721Semaste * published by the Free Software Foundation.
8254721Semaste *
9254721Semaste * This code is distributed in the hope that it will be useful, but WITHOUT
10254721Semaste * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11254721Semaste * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12254721Semaste * version 2 for more details (a copy is included in the LICENSE file that
13254721Semaste * accompanied this code).
14254721Semaste *
15254721Semaste * You should have received a copy of the GNU General Public License version
16254721Semaste * 2 along with this work; if not, write to the Free Software Foundation,
17254721Semaste * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18254721Semaste *
19254721Semaste * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20254721Semaste * or visit www.oracle.com if you need additional information or have any
21254721Semaste * questions.
22254721Semaste */
23254721Semaste
24254721Semaste/* @test
25254721Semaste * @bug 4152295
26254721Semaste * @summary trivial optimization in RemoteProxy.extendsRemote
27254721Semaste *
28254721Semaste * @author Laird Dornin
29254721Semaste *
30254721Semaste * @library ../../testlibrary
31254721Semaste * @build TestLibrary
32254721Semaste * @run main/othervm GetRemoteClass
33254721Semaste */
34254721Semaste
35254721Semasteimport java.io.Serializable;
36254721Semasteimport java.rmi.Remote;
37254721Semasteimport sun.rmi.server.Util;
38254721Semasteimport java.lang.reflect.*;
39254721Semaste
40254721Semaste
41254721Semaste/**
42254721Semaste * Simple test to make sure that Util.getRemoteClass throws
43254721Semaste * exception in proper circumstances.  Pass two different classes, one
44254721Semaste * that extends Remote and one that only extends Serializable.  Should
45254721Semaste * throw exception for Serializable.
46254721Semaste */
47254721Semastepublic class GetRemoteClass {
48254721Semaste    public interface ExtendRemote extends Remote {
49254721Semaste    }
50254721Semaste
51254721Semaste    public static class TestRemote implements ExtendRemote {
52254721Semaste    }
53254721Semaste
54254721Semaste    public static class OnlySerializable implements Serializable {
55254721Semaste    }
56254721Semaste
57254721Semaste    public static void main(String[] argv) {
58254721Semaste
59254721Semaste        System.err.println("\nregression test for 4152295\n");
60254721Semaste
61254721Semaste        Method utilGetRemoteClass = null;
62254721Semaste
63254721Semaste        try {
64254721Semaste            /**
65254721Semaste             * Use reflection to set access overrides so that the test
66254721Semaste             * can call protected method, getRemoteClass
67254721Semaste             */
68254721Semaste            Class[] args = new Class[1];
69254721Semaste            args[0] = Class.class;
70254721Semaste            utilGetRemoteClass =
71254721Semaste                Util.class.getDeclaredMethod("getRemoteClass", args);
72254721Semaste            utilGetRemoteClass.setAccessible(true);
73254721Semaste
74254721Semaste            // getRemoteClass can be invoked without exception
75254721Semaste            utilGetRemoteClass.invoke
76254721Semaste                 (null , new Object [] {TestRemote.class});
77254721Semaste            System.err.println("remote class flagged as remote");
78254721Semaste
79254721Semaste            ClassNotFoundException cnfe = null;
80254721Semaste            try {
81254721Semaste                // getRemoteClass can be invoked without exception
82254721Semaste                utilGetRemoteClass.invoke
83254721Semaste                    (null , new Object [] {OnlySerializable.class});
84254721Semaste            } catch (InvocationTargetException e) {
85254721Semaste                System.err.println("got ClassNotFoundException; remote " +
86254721Semaste                                   "class flagged as nonremote");
87254721Semaste                cnfe = (ClassNotFoundException) e.getTargetException();
88254721Semaste            }
89254721Semaste            if (cnfe == null) {
90254721Semaste                TestLibrary.bomb("Serializable class flagged as remote?");
91254721Semaste            }
92254721Semaste
93254721Semaste            System.err.println("Test passed.");
94254721Semaste        } catch (Exception e) {
95254721Semaste            TestLibrary.bomb("Unexpected exception", e);
96254721Semaste        }
97254721Semaste    }
98254721Semaste}
99254721Semaste