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.

ProxyObjectInputStream.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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.InputStream;
  19. import java.io.ObjectInputStream;
  20. import java.io.ObjectStreamClass;
  21. /**
  22. * An input stream class which knows how to deserialize proxies created via {@link ProxyFactory} and
  23. * serializedo via a {@link ProxyObjectOutputStream}. It must be used when deserialising proxies created
  24. * from a proxy factory configured with {@link ProxyFactory#useWriteReplace} set to false.
  25. *
  26. * @author Andrew Dinn
  27. */
  28. public class ProxyObjectInputStream extends ObjectInputStream
  29. {
  30. /**
  31. * create an input stream which can be used to deserialize an object graph which includes proxies created
  32. * using class ProxyFactory. the classloader used to resolve proxy superclass and interface names
  33. * read from the input stream will default to the current thread's context class loader or the system
  34. * classloader if the context class loader is null.
  35. * @param in
  36. * @throws java.io.StreamCorruptedException whenever ObjectInputStream would also do so
  37. * @throws IOException whenever ObjectInputStream would also do so
  38. * @throws SecurityException whenever ObjectInputStream would also do so
  39. * @throws NullPointerException if in is null
  40. */
  41. public ProxyObjectInputStream(InputStream in) throws IOException
  42. {
  43. super(in);
  44. loader = Thread.currentThread().getContextClassLoader();
  45. if (loader == null) {
  46. loader = ClassLoader.getSystemClassLoader();
  47. }
  48. }
  49. /**
  50. * Reset the loader to be
  51. * @param loader
  52. */
  53. public void setClassLoader(ClassLoader loader)
  54. {
  55. if (loader != null) {
  56. this.loader = loader;
  57. } else {
  58. loader = ClassLoader.getSystemClassLoader();
  59. }
  60. }
  61. protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException {
  62. boolean isProxy = readBoolean();
  63. if (isProxy) {
  64. String name = (String)readObject();
  65. Class superClass = loader.loadClass(name);
  66. int length = readInt();
  67. Class[] interfaces = new Class[length];
  68. for (int i = 0; i < length; i++) {
  69. name = (String)readObject();
  70. interfaces[i] = loader.loadClass(name);
  71. }
  72. length = readInt();
  73. byte[] signature = new byte[length];
  74. read(signature);
  75. ProxyFactory factory = new ProxyFactory();
  76. // we must always use the cache and never use writeReplace when using
  77. // ProxyObjectOutputStream and ProxyObjectInputStream
  78. factory.setUseCache(true);
  79. factory.setUseWriteReplace(false);
  80. factory.setSuperclass(superClass);
  81. factory.setInterfaces(interfaces);
  82. Class proxyClass = factory.createClass(signature);
  83. return ObjectStreamClass.lookup(proxyClass);
  84. } else {
  85. return super.readClassDescriptor();
  86. }
  87. }
  88. /**
  89. * the loader to use to resolve classes for proxy superclass and interface names read
  90. * from the stream. defaults to the context class loader of the thread which creates
  91. * the input stream or the system class loader if the context class loader is null.
  92. */
  93. private ClassLoader loader;
  94. }