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 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2006 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. import java.net.URL;
  18. import java.net.MalformedURLException;
  19. /**
  20. * A <code>ByteArrayClassPath</code> contains bytes that is served as
  21. * a class file to a <code>ClassPool</code>. It is useful to convert
  22. * a byte array to a <code>CtClass</code> object.
  23. *
  24. * <p>For example, if you want to convert a byte array <code>b</code>
  25. * into a <code>CtClass</code> object representing the class with a name
  26. * <code>classname</code>, then do as following:
  27. *
  28. * <ul><pre>
  29. * ClassPool cp = ClassPool.getDefault();
  30. * cp.insertClassPath(new ByteArrayClassPath(classname, b));
  31. * CtClass cc = cp.get(classname);
  32. * </pre></ul>
  33. *
  34. * <p>The <code>ClassPool</code> object <code>cp</code> uses the created
  35. * <code>ByteArrayClassPath</code> object as the source of the class file.
  36. *
  37. * <p>A <code>ByteArrayClassPath</code> must be instantiated for every
  38. * class. It contains only a single class file.
  39. *
  40. * @see javassist.ClassPath
  41. * @see ClassPool#insertClassPath(ClassPath)
  42. * @see ClassPool#appendClassPath(ClassPath)
  43. * @see ClassPool#makeClass(InputStream)
  44. */
  45. public class ByteArrayClassPath implements ClassPath {
  46. protected String classname;
  47. protected byte[] classfile;
  48. /*
  49. * Creates a <code>ByteArrayClassPath</code> containing the given
  50. * bytes.
  51. *
  52. * @param name a fully qualified class name
  53. * @param classfile the contents of a class file.
  54. */
  55. public ByteArrayClassPath(String name, byte[] classfile) {
  56. this.classname = name;
  57. this.classfile = classfile;
  58. }
  59. /**
  60. * Closes this class path.
  61. */
  62. public void close() {}
  63. public String toString() {
  64. return "byte[]:" + classname;
  65. }
  66. /**
  67. * Opens the class file.
  68. */
  69. public InputStream openClassfile(String classname) {
  70. if(this.classname.equals(classname))
  71. return new ByteArrayInputStream(classfile);
  72. else
  73. return null;
  74. }
  75. /**
  76. * Obtains the URL.
  77. */
  78. public URL find(String classname) {
  79. if(this.classname.equals(classname)) {
  80. String cname = classname.replace('.', '/') + ".class";
  81. try {
  82. // return new File(cname).toURL();
  83. return new URL("file:/ByteArrayClassPath/" + cname);
  84. }
  85. catch (MalformedURLException e) {}
  86. }
  87. return null;
  88. }
  89. }