You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProxyObjectOutputStream.java 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. Alternatively, the contents of this file may be used under
  8. * the terms of the GNU Lesser General Public License Version 2.1 or later,
  9. * or the Apache License Version 2.0.
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. */
  16. package javassist.util.proxy;
  17. import java.io.IOException;
  18. import java.io.ObjectOutputStream;
  19. import java.io.ObjectStreamClass;
  20. import java.io.OutputStream;
  21. /**
  22. * An input stream class which knows how to serialize proxies created via {@link ProxyFactory}. It must
  23. * be used when serialising proxies created from a proxy factory configured with
  24. * {@link ProxyFactory#useWriteReplace} set to false. Subsequent deserialization of the serialized data
  25. * must employ a {@link ProxyObjectInputStream}
  26. *
  27. * @author Andrew Dinn
  28. */
  29. public class ProxyObjectOutputStream extends ObjectOutputStream
  30. {
  31. /**
  32. * create an output stream which can be used to serialize an object graph which includes proxies created
  33. * using class ProxyFactory
  34. * @param out
  35. * @throws IOException whenever ObjectOutputStream would also do so
  36. * @throws SecurityException whenever ObjectOutputStream would also do so
  37. * @throws NullPointerException if out is null
  38. */
  39. public ProxyObjectOutputStream(OutputStream out) throws IOException
  40. {
  41. super(out);
  42. }
  43. @Override
  44. protected void writeClassDescriptor(ObjectStreamClass desc) throws IOException {
  45. Class<?> cl = desc.forClass();
  46. if (ProxyFactory.isProxyClass(cl)) {
  47. writeBoolean(true);
  48. Class<?> superClass = cl.getSuperclass();
  49. Class<?>[] interfaces = cl.getInterfaces();
  50. byte[] signature = ProxyFactory.getFilterSignature(cl);
  51. String name = superClass.getName();
  52. writeObject(name);
  53. // we don't write the marker interface ProxyObject
  54. writeInt(interfaces.length - 1);
  55. for (int i = 0; i < interfaces.length; i++) {
  56. Class<?> interfaze = interfaces[i];
  57. if (interfaze != ProxyObject.class && interfaze != Proxy.class) {
  58. name = interfaces[i].getName();
  59. writeObject(name);
  60. }
  61. }
  62. writeInt(signature.length);
  63. write(signature);
  64. } else {
  65. writeBoolean(false);
  66. super.writeClassDescriptor(desc);
  67. }
  68. }
  69. }