1/* 2 * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* @test 25 * @bug 4099660 4102938 26 * @summary Remote classes not extending RemoteObject should be able to 27 * implement hashCode() and equals() methods so that instances 28 * can be successfully compared to RemoteObject instances 29 * (specifically: stubs) that contain the instance's RemoteRef. 30 * @author Peter Jones 31 * 32 * @build NotExtending_Stub NotExtending_Skel 33 * @run main/othervm NotExtending 34 */ 35 36 37import java.rmi.*; 38import java.rmi.server.*; 39 40public class NotExtending implements Remote { 41 42 /** remote stub for this server instance */ 43 private Remote stub; 44 /** value of stub's hash code */ 45 private int hashValue; 46 /** true if the hashValue field has been initialized */ 47 private boolean hashValueInitialized = false; 48 49 // no declared constructor - rely on implicit no-arg contructor 50 51 public Remote export() throws RemoteException { 52 stub = UnicastRemoteObject.exportObject(this); 53 setHashValue(stub.hashCode()); 54 return stub; 55 } 56 57 public void unexport() throws RemoteException { 58 UnicastRemoteObject.unexportObject(this, true); 59 } 60 61 private void setHashValue(int value) { 62 hashValue = value; 63 hashValueInitialized = true; 64 } 65 66 public int hashCode() { 67 /* 68 * Test fails if the hashCode() method is called (during export) 69 * before the correct hash value has been initialized. 70 */ 71 if (!hashValueInitialized) { 72 throw new AssertionError( 73 "hashCode() invoked before hashValue initialized"); 74 } 75 return hashValue; 76 } 77 78 public boolean equals(Object obj) { 79 return stub.equals(obj); 80 } 81 82 public static void main(String[] args) throws Exception { 83 NotExtending server = null; 84 85 try { 86 /* 87 * Verify that hashCode() is not invoked before it is 88 * initialized. Tests bugid 4102938. 89 */ 90 server = new NotExtending(); 91 Remote stub = server.export(); 92 System.err.println("Server exported without invoking hashCode()."); 93 94 /* 95 * Verify that passing stub to server's equals() method 96 * returns true. 97 */ 98 if (server.equals(stub)) { 99 System.err.println("server.equals(stub) returns true"); 100 } else { 101 throw new AssertionError("server.equals(stub) returns false"); 102 } 103 104 /* 105 * Verify that passing server to stub's equals() method 106 * returns true. Tests bugid 4099660. 107 */ 108 if (stub.equals(server)) { 109 System.err.println("stub.equals(server) returns true"); 110 } else { 111 throw new AssertionError("stub.equals(server) returns false"); 112 } 113 } finally { 114 if (server != null) { 115 server.unexport(); 116 } 117 } 118 } 119} 120