1 /* 2 * Copyright (c) 2000, 2010, 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 * @summary Verifies basic correct functioning of proxy serialization. 26 */ 27 28 import java.io.*; 29 import java.lang.reflect.*; 30 import java.util.Random; 31 32 // proxy interfaces 33 interface Foo { int foo(); } 34 interface Bar { float bar(); } 35 36 // dummy invocation handler 37 class Handler implements InvocationHandler, Serializable { 38 39 static Method fooMethod, barMethod; 40 static { 41 try { 42 fooMethod = Foo.class.getDeclaredMethod("foo", new Class[0]); 43 barMethod = Bar.class.getDeclaredMethod("bar", new Class[0]); 44 } catch (NoSuchMethodException ex) { 45 throw new Error(); 46 } 47 } 48 49 int foo; 50 float bar; 51 52 Handler(int foo, float bar) { 53 this.foo = foo; 54 this.bar = bar; 55 } 56 57 public Object invoke(Object proxy, Method method, Object[] args) 58 throws Throwable 59 { 60 if (method.equals(fooMethod)) { 61 return new Integer(foo); 62 } else if (method.equals(barMethod)) { 63 return new Float(bar); 64 } else { 65 throw new UnsupportedOperationException(); 66 } 67 } 68 } 69 70 // ObjectInputStream incapable of resolving proxy classes 71 class ProxyBlindInputStream extends ObjectInputStream { 72 73 ProxyBlindInputStream(InputStream in) throws IOException { super(in); } 74 75 protected Class resolveProxyClass(String[] interfaces) 76 throws IOException, ClassNotFoundException 77 { 78 throw new ClassNotFoundException(); 79 } 80 } 81 82 public class Basic { 83 public static void main(String[] args) throws Exception { 84 ClassLoader loader = Basic.class.getClassLoader(); 85 Class[] interfaces = new Class[] { Foo.class, Bar.class }; 86 Random rand = new Random(); 87 int foo = rand.nextInt(); 88 float bar = rand.nextFloat(); 89 ObjectOutputStream oout; 90 ObjectInputStream oin; 91 ByteArrayOutputStream bout; 92 Object proxy; 93 94 95 // test simple proxy write + read 96 bout = new ByteArrayOutputStream(); 97 oout = new ObjectOutputStream(bout); 98 oout.writeObject(Proxy.newProxyInstance( 99 loader, interfaces, new Handler(foo, bar))); 100 oout.close(); 101 102 oin = new ObjectInputStream( 103 new ByteArrayInputStream(bout.toByteArray())); 104 proxy = oin.readObject(); 105 if ((((Foo) proxy).foo() != foo) || (((Bar) proxy).bar() != bar)) { 106 throw new Error(); 107 } 108 109 110 // test missing proxy class ClassNotFoundException 111 oin = new ProxyBlindInputStream( 112 new ByteArrayInputStream(bout.toByteArray())); 113 try { 114 oin.readObject(); 115 throw new Error(); 116 } catch (ClassNotFoundException ex) { 117 } 118 } 119 }