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

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