test/jdk/java/io/Serializable/proxy/replace/Test.java
changeset 58565 baa5969ecf34
parent 47216 71c04702a3d5
equal deleted inserted replaced
58564:218a1a642c6f 58565:baa5969ecf34
     1 /*
     1 /*
     2  * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    28 
    28 
    29 import java.io.*;
    29 import java.io.*;
    30 import java.lang.reflect.*;
    30 import java.lang.reflect.*;
    31 
    31 
    32 public class Test implements InvocationHandler, Serializable {
    32 public class Test implements InvocationHandler, Serializable {
       
    33     private static final long serialVersionUID = 1L;
    33 
    34 
    34     static ClassLoader loader = Test.class.getClassLoader();
    35     static ClassLoader loader = Test.class.getClassLoader();
    35 
    36 
    36     public Object invoke(Object proxy, Method method, Object[] args)
    37     public Object invoke(Object proxy, Method method, Object[] args)
    37         throws Throwable
    38         throws Throwable
    38     {
    39     {
    39         String methName = method.getName();
    40         String methName = method.getName();
    40         if (methName.equals("writeReplace")) {
    41         if (methName.equals("writeReplace")) {
    41             return Proxy.newProxyInstance(
    42             return Proxy.newProxyInstance(
    42                 loader, new Class[] { ReadResolve.class }, this);
    43                 loader, new Class<?>[] { ReadResolve.class }, this);
    43         } else if (methName.equals("readResolve")) {
    44         } else if (methName.equals("readResolve")) {
    44             return Proxy.newProxyInstance(
    45             return Proxy.newProxyInstance(
    45                 loader, new Class[] { Resolved.class }, this);
    46                 loader, new Class<?>[] { Resolved.class }, this);
    46         } else if (method.getDeclaringClass() == Object.class) {
    47         } else if (method.getDeclaringClass() == Object.class) {
    47             return method.invoke(this, args);
    48             return method.invoke(this, args);
    48         } else {
    49         } else {
    49             throw new Error();
    50             throw new Error();
    50         }
    51         }
    52 
    53 
    53     public static void main(String[] args) throws Exception {
    54     public static void main(String[] args) throws Exception {
    54         ByteArrayOutputStream bout = new ByteArrayOutputStream();
    55         ByteArrayOutputStream bout = new ByteArrayOutputStream();
    55         ObjectOutputStream oout = new ObjectOutputStream(bout);
    56         ObjectOutputStream oout = new ObjectOutputStream(bout);
    56         oout.writeObject(Proxy.newProxyInstance(
    57         oout.writeObject(Proxy.newProxyInstance(
    57             loader, new Class[] { WriteReplace.class }, new Test()));
    58             loader, new Class<?>[] { WriteReplace.class }, new Test()));
    58         oout.close();
    59         oout.close();
    59         ObjectInputStream oin = new ObjectInputStream(
    60         ObjectInputStream oin = new ObjectInputStream(
    60             new ByteArrayInputStream(bout.toByteArray()));
    61             new ByteArrayInputStream(bout.toByteArray()));
    61         if (!(oin.readObject() instanceof Resolved)) {
    62         if (!(oin.readObject() instanceof Resolved)) {
    62             throw new Error();
    63             throw new Error();