169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal/*
269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Javassist, a Java-bytecode translator toolkit.
369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Copyright (C) 1999-2010 Shigeru Chiba. All Rights Reserved.
469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * The contents of this file are subject to the Mozilla Public License Version
669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 1.1 (the "License"); you may not use this file except in compliance with
769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * the License.  Alternatively, the contents of this file may be used under
869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * the terms of the GNU Lesser General Public License Version 2.1 or later.
969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
1069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Software distributed under the License is distributed on an "AS IS" basis,
1169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
1269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * for the specific language governing rights and limitations under the
1369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * License.
1469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */
1569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
1669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpackage javassist.util.proxy;
1769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
1869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport java.io.IOException;
1969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport java.io.ObjectOutputStream;
2069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport java.io.ObjectStreamClass;
2169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport java.io.OutputStream;
2269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
2369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal/**
2469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * An input stream class which knows how to serialize proxies created via {@link ProxyFactory}. It must
2569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * be used when serialising proxies created from a proxy factory configured with
2669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * {@link ProxyFactory#useWriteReplace} set to false. Subsequent deserialization of the serialized data
2769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * must employ a {@link ProxyObjectInputStream}
2869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
2969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @author Andrew Dinn
3069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */
3169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpublic class ProxyObjectOutputStream extends ObjectOutputStream
3269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal{
3369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
3469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * create an output stream which can be used to serialize an object graph which includes proxies created
3569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * using class ProxyFactory
3669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param out
3769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @throws IOException whenever ObjectOutputStream would also do so
3869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @throws SecurityException whenever ObjectOutputStream would also do so
3969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @throws NullPointerException if out is null
4069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
4169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public ProxyObjectOutputStream(OutputStream out) throws IOException
4269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    {
4369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        super(out);
4469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
4569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
4669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    protected void writeClassDescriptor(ObjectStreamClass desc) throws IOException {
4769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        Class cl = desc.forClass();
4869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (ProxyFactory.isProxyClass(cl)) {
4969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            writeBoolean(true);
5069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            Class superClass = cl.getSuperclass();
5169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            Class[] interfaces = cl.getInterfaces();
5269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            byte[] signature = ProxyFactory.getFilterSignature(cl);
5369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            String name = superClass.getName();
5469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            writeObject(name);
5569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            // we don't write the marker interface ProxyObject
5669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            writeInt(interfaces.length - 1);
5769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            for (int i = 0; i < interfaces.length; i++) {
5869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                Class interfaze = interfaces[i];
5969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                if (interfaze != ProxyObject.class) {
6069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    name = interfaces[i].getName();
6169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    writeObject(name);
6269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                }
6369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            }
6469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            writeInt(signature.length);
6569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            write(signature);
6669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        } else {
6769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            writeBoolean(false);
6869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            super.writeClassDescriptor(desc);
6969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
7069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
7169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal}
72