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.

ByteArrayClassPath.java 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2003 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. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. */
  15. package javassist;
  16. import java.io.*;
  17. /**
  18. * A <code>ByteArrayClassPath</code> contains bytes that is served as
  19. * a class file to a <code>ClassPool</code>. It is useful to convert
  20. * a byte array to a <code>CtClass</code> object.
  21. *
  22. * <p>For example, if you want to convert a byte array <code>b</code>
  23. * into a <code>CtClass</code> object representing the class with a name
  24. * <code>classname</code>, then do as following:
  25. *
  26. * <ul><pre>
  27. * ClassPool cp = ClassPool.getDefault();
  28. * cp.insertClassPath(new ByteArrayClassPath(classname, b));
  29. * CtClass cc = cp.get(classname);
  30. * </pre></ul>
  31. *
  32. * <p>The <code>ClassPool</code> object <code>cp</code> uses the created
  33. * <code>ByteArrayClassPath</code> object as the source of the class file.
  34. *
  35. * <p>A <code>ByteArrayClassPath</code> must be instantiated for every
  36. * class. It contains only a single class file.
  37. *
  38. * @see javassist.ClassPath
  39. * @see ClassPool#insertClassPath(ClassPath)
  40. * @see ClassPool#appendClassPath(ClassPath)
  41. */
  42. public class ByteArrayClassPath implements ClassPath {
  43. protected String classname;
  44. protected byte[] classfile;
  45. /*
  46. * Creates a <code>ByteArrayClassPath</code> containing the given
  47. * bytes.
  48. *
  49. * @param name a fully qualified class name
  50. * @param classfile the contents of a class file.
  51. */
  52. public ByteArrayClassPath(String name, byte[] classfile) {
  53. this.classname = name;
  54. this.classfile = classfile;
  55. }
  56. /**
  57. * Closes this class path.
  58. */
  59. public void close() {}
  60. public String toString() {
  61. return "byte[]:" + classname;
  62. }
  63. /**
  64. * Opens a class file.
  65. */
  66. public InputStream openClassfile(String classname) {
  67. if(this.classname.equals(classname))
  68. return new ByteArrayInputStream(classfile);
  69. else
  70. return null;
  71. }
  72. }