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

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