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

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