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.

FactoryHelper.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2005 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.util.proxy;
  16. import java.io.BufferedOutputStream;
  17. import java.io.ByteArrayOutputStream;
  18. import java.io.DataOutputStream;
  19. import java.io.File;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import javassist.CannotCompileException;
  23. import javassist.bytecode.ClassFile;
  24. /**
  25. * A helper class for implementing <code>ProxyFactory</code>.
  26. * The users of <code>ProxyFactory</code> do not have to see this class.
  27. *
  28. * @see ProxyFactory
  29. */
  30. public class FactoryHelper {
  31. /**
  32. * Returns an index for accessing arrays in this class.
  33. *
  34. * @throws RuntimeException if a given type is not a primitive type.
  35. */
  36. public static final int typeIndex(Class type) {
  37. Class[] list = primitiveTypes;
  38. int n = list.length;
  39. for (int i = 0; i < n; i++)
  40. if (list[i] == type)
  41. return i;
  42. throw new RuntimeException("bad type:" + type.getName());
  43. }
  44. /**
  45. * <code>Class</code> objects representing primitive types.
  46. */
  47. public static final Class[] primitiveTypes = {
  48. Boolean.TYPE, Byte.TYPE, Character.TYPE, Short.TYPE, Integer.TYPE,
  49. Long.TYPE, Float.TYPE, Double.TYPE, Void.TYPE
  50. };
  51. /**
  52. * The fully-qualified names of wrapper classes for primitive types.
  53. */
  54. public static final String[] wrapperTypes = {
  55. "java.lang.Boolean", "java.lang.Byte", "java.lang.Character",
  56. "java.lang.Short", "java.lang.Integer", "java.lang.Long",
  57. "java.lang.Float", "java.lang.Double", "java.lang.Void"
  58. };
  59. /**
  60. * The descriptors of the constructors of wrapper classes.
  61. */
  62. public static final String[] wrapperDesc = {
  63. "(Z)V", "(B)V", "(C)V", "(S)V", "(I)V", "(J)V",
  64. "(F)V", "(D)V"
  65. };
  66. /**
  67. * The names of methods for obtaining a primitive value
  68. * from a wrapper object. For example, <code>intValue()</code>
  69. * is such a method for obtaining an integer value from a
  70. * <code>java.lang.Integer</code> object.
  71. */
  72. public static final String[] unwarpMethods = {
  73. "booleanValue", "byteValue", "charValue", "shortValue",
  74. "intValue", "longValue", "floatValue", "doubleValue"
  75. };
  76. /**
  77. * The descriptors of the unwrapping methods contained
  78. * in <code>unwrapMethods</code>.
  79. */
  80. public static final String[] unwrapDesc = {
  81. "()Z", "()B", "()C", "()S", "()I", "()J", "()F", "()D"
  82. };
  83. /**
  84. * The data size of primitive types. <code>long</code>
  85. * and <code>double</code> are 2; the others are 1.
  86. */
  87. public static final int[] dataSize = {
  88. 1, 1, 1, 1, 1, 2, 1, 2
  89. };
  90. /**
  91. * Loads a class file by a given class loader.
  92. */
  93. public static Class toClass(ClassFile cf, ClassLoader loader)
  94. throws CannotCompileException
  95. {
  96. try {
  97. byte[] b = toBytecode(cf);
  98. Class cl = Class.forName("java.lang.ClassLoader");
  99. java.lang.reflect.Method method = cl.getDeclaredMethod(
  100. "defineClass", new Class[] { String.class, byte[].class,
  101. Integer.TYPE, Integer.TYPE });
  102. method.setAccessible(true);
  103. Object[] args = new Object[] { cf.getName(), b, new Integer(0),
  104. new Integer(b.length) };
  105. Class clazz = (Class)method.invoke(loader, args);
  106. method.setAccessible(false);
  107. return clazz;
  108. }
  109. catch (RuntimeException e) {
  110. throw e;
  111. }
  112. catch (java.lang.reflect.InvocationTargetException e) {
  113. throw new CannotCompileException(e.getTargetException());
  114. }
  115. catch (Exception e) {
  116. throw new CannotCompileException(e);
  117. }
  118. }
  119. private static byte[] toBytecode(ClassFile cf) throws IOException {
  120. ByteArrayOutputStream barray = new ByteArrayOutputStream();
  121. DataOutputStream out = new DataOutputStream(barray);
  122. try {
  123. cf.write(out);
  124. }
  125. finally {
  126. out.close();
  127. }
  128. return barray.toByteArray();
  129. }
  130. /**
  131. * Writes a class file.
  132. */
  133. public static void writeFile(ClassFile cf, String directoryName)
  134. throws CannotCompileException {
  135. try {
  136. writeFile0(cf, directoryName);
  137. }
  138. catch (IOException e) {
  139. throw new CannotCompileException(e);
  140. }
  141. }
  142. private static void writeFile0(ClassFile cf, String directoryName)
  143. throws CannotCompileException, IOException {
  144. String classname = cf.getName();
  145. String filename = directoryName + File.separatorChar
  146. + classname.replace('.', File.separatorChar) + ".class";
  147. int pos = filename.lastIndexOf(File.separatorChar);
  148. if (pos > 0) {
  149. String dir = filename.substring(0, pos);
  150. if (!dir.equals("."))
  151. new File(dir).mkdirs();
  152. }
  153. DataOutputStream out = new DataOutputStream(new BufferedOutputStream(
  154. new FileOutputStream(filename)));
  155. try {
  156. cf.write(out);
  157. }
  158. catch (IOException e) {
  159. throw e;
  160. }
  161. finally {
  162. out.close();
  163. }
  164. }
  165. }