AnnotationInvocationHandler.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /* sun.reflect.annotation.AnnotationInvocationHandler
  2. Copyright (C) 2006
  3. Free Software Foundation, Inc.
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. package sun.reflect.annotation;
  33. import java.io.Serializable;
  34. import java.lang.annotation.Annotation;
  35. import java.lang.annotation.AnnotationTypeMismatchException;
  36. import java.lang.annotation.IncompleteAnnotationException;
  37. import java.lang.reflect.InvocationHandler;
  38. import java.lang.reflect.InvocationTargetException;
  39. import java.lang.reflect.Method;
  40. import java.lang.reflect.Proxy;
  41. import java.util.Arrays;
  42. import java.util.Iterator;
  43. import java.util.Map;
  44. /**
  45. * This class exists for serialization compatibility with the JDK.
  46. * VMs can choose to implement annotations by constructing proxies
  47. * with this invocation handler, but that is not required.
  48. * If a different strategy for proxy objects is chosen, they can
  49. * have a writeReplace method to substitute a Proxy based on this
  50. * invocation handler is used for serialization.
  51. */
  52. public final class AnnotationInvocationHandler
  53. implements InvocationHandler, Serializable
  54. {
  55. private static final long serialVersionUID = 6182022883658399397L;
  56. private final Class type;
  57. private final Map memberValues;
  58. /**
  59. * Construct a new invocation handler for an annotation proxy.
  60. * Note that the VM is responsible for filling the memberValues map
  61. * with the default values of all the annotation members.
  62. */
  63. public AnnotationInvocationHandler(Class type, Map memberValues)
  64. {
  65. this.type = type;
  66. this.memberValues = memberValues;
  67. }
  68. public static Annotation create(Class type, Map memberValues)
  69. {
  70. for (Method m : type.getDeclaredMethods())
  71. {
  72. String name = m.getName();
  73. if (! memberValues.containsKey(name))
  74. {
  75. // FIXME: what to do about exceptions here?
  76. memberValues.put(name, m.getDefaultValue());
  77. }
  78. }
  79. AnnotationInvocationHandler handler
  80. = new AnnotationInvocationHandler(type, memberValues);
  81. return (Annotation) Proxy.newProxyInstance(type.getClassLoader(),
  82. new Class[] { type },
  83. handler);
  84. }
  85. /**
  86. * Compare an instance of AnnotationInvocationHandler with another object.
  87. * Note that the other object does not have to be an
  88. * AnnotationInvocationHandler, any implementation of the annotation
  89. * interface is allowed to be compared for equality.
  90. * Note that this makes the equals method asymmetric, but this behavior
  91. * is specified by Annotation.equals and identical to the JDK.
  92. *
  93. * This method is public for use by other parts of the VM. Some VMs
  94. * (can) use different representations of annotations that reuse this
  95. * method.
  96. */
  97. public static boolean equals(Class type, Map memberValues, Object other)
  98. {
  99. if (type.isInstance(other))
  100. {
  101. try
  102. {
  103. Method[] methods = type.getDeclaredMethods();
  104. if (methods.length == memberValues.size())
  105. {
  106. for (int i = 0; i < methods.length; i++)
  107. {
  108. String key = methods[i].getName();
  109. Object val = methods[i].invoke(other, new Object[0]);
  110. if (! deepEquals(memberValues.get(key), val))
  111. {
  112. return false;
  113. }
  114. }
  115. return true;
  116. }
  117. }
  118. catch (IllegalAccessException _)
  119. {
  120. // Ignore exception, like the JDK
  121. }
  122. catch (InvocationTargetException _)
  123. {
  124. // Ignore exception, like the JDK
  125. }
  126. }
  127. return false;
  128. }
  129. private static boolean deepEquals(Object o1, Object o2)
  130. {
  131. if (o1 == o2)
  132. return true;
  133. if (o1 == null || o2 == null)
  134. return false;
  135. if (o1 instanceof boolean[] && o2 instanceof boolean[])
  136. return Arrays.equals((boolean[]) o1, (boolean[]) o2);
  137. if (o1 instanceof byte[] && o2 instanceof byte[])
  138. return Arrays.equals((byte[]) o1, (byte[]) o2);
  139. if (o1 instanceof char[] && o2 instanceof char[])
  140. return Arrays.equals((char[]) o1, (char[]) o2);
  141. if (o1 instanceof short[] && o2 instanceof short[])
  142. return Arrays.equals((short[]) o1, (short[]) o2);
  143. if (o1 instanceof int[] && o2 instanceof int[])
  144. return Arrays.equals((int[]) o1, (int[]) o2);
  145. if (o1 instanceof float[] && o2 instanceof float[])
  146. return Arrays.equals((float[]) o1, (float[]) o2);
  147. if (o1 instanceof long[] && o2 instanceof long[])
  148. return Arrays.equals((long[]) o1, (long[]) o2);
  149. if (o1 instanceof double[] && o2 instanceof double[])
  150. return Arrays.equals((double[]) o1, (double[]) o2);
  151. if (o1 instanceof Object[] && o2 instanceof Object[])
  152. return Arrays.equals((Object[]) o1, (Object[]) o2);
  153. return o1.equals(o2);
  154. }
  155. private static int deepHashCode(Object obj)
  156. {
  157. if (obj instanceof boolean[])
  158. return Arrays.hashCode((boolean[]) obj);
  159. if (obj instanceof byte[])
  160. return Arrays.hashCode((byte[]) obj);
  161. if (obj instanceof char[])
  162. return Arrays.hashCode((char[]) obj);
  163. if (obj instanceof short[])
  164. return Arrays.hashCode((short[]) obj);
  165. if (obj instanceof int[])
  166. return Arrays.hashCode((int[]) obj);
  167. if (obj instanceof float[])
  168. return Arrays.hashCode((float[]) obj);
  169. if (obj instanceof long[])
  170. return Arrays.hashCode((long[]) obj);
  171. if (obj instanceof double[])
  172. return Arrays.hashCode((double[]) obj);
  173. if (obj instanceof Object[])
  174. return Arrays.hashCode((Object[]) obj);
  175. return obj.hashCode();
  176. }
  177. /**
  178. * Compute the hashCode for an annotation. Note that the algorithm is
  179. * specified by Annotation.hashCode.
  180. *
  181. * This method is public for use by other parts of the VM. Some VMs
  182. * (can) use different representations of annotations that reuse this
  183. * method.
  184. */
  185. public static int hashCode(Class type, Map memberValues)
  186. {
  187. int h = 0;
  188. Iterator iter = memberValues.keySet().iterator();
  189. while (iter.hasNext())
  190. {
  191. Object key = iter.next();
  192. Object val = memberValues.get(key);
  193. h += deepHashCode(val) ^ 127 * key.hashCode();
  194. }
  195. return h;
  196. }
  197. private static String deepToString(Object obj)
  198. {
  199. if (obj instanceof boolean[])
  200. return Arrays.toString((boolean[]) obj);
  201. if (obj instanceof byte[])
  202. return Arrays.toString((byte[]) obj);
  203. if (obj instanceof char[])
  204. return Arrays.toString((char[]) obj);
  205. if (obj instanceof short[])
  206. return Arrays.toString((short[]) obj);
  207. if (obj instanceof int[])
  208. return Arrays.toString((int[]) obj);
  209. if (obj instanceof float[])
  210. return Arrays.toString((float[]) obj);
  211. if (obj instanceof long[])
  212. return Arrays.toString((long[]) obj);
  213. if (obj instanceof double[])
  214. return Arrays.toString((double[]) obj);
  215. if (obj instanceof Object[])
  216. return Arrays.toString((Object[]) obj);
  217. return obj.toString();
  218. }
  219. /**
  220. * This method is public for use by other parts of the VM. Some VMs
  221. * (can) use different representations of annotations that reuse this
  222. * method.
  223. */
  224. public static String toString(Class type, Map memberValues)
  225. {
  226. StringBuffer sb = new StringBuffer();
  227. sb.append('@').append(type.getName()).append('(');
  228. String sep = "";
  229. Iterator iter = memberValues.keySet().iterator();
  230. while (iter.hasNext())
  231. {
  232. Object key = iter.next();
  233. Object val = memberValues.get(key);
  234. sb.append(sep).append(key).append('=').append(deepToString(val));
  235. sep = ", ";
  236. }
  237. sb.append(')');
  238. return sb.toString();
  239. }
  240. private static Class getBoxedReturnType(Method method)
  241. {
  242. Class returnType = method.getReturnType();
  243. if (returnType == boolean.class)
  244. return Boolean.class;
  245. if (returnType == byte.class)
  246. return Byte.class;
  247. if (returnType == char.class)
  248. return Character.class;
  249. if (returnType == short.class)
  250. return Short.class;
  251. if (returnType == int.class)
  252. return Integer.class;
  253. if (returnType == float.class)
  254. return Float.class;
  255. if (returnType == long.class)
  256. return Long.class;
  257. if (returnType == double.class)
  258. return Double.class;
  259. return returnType;
  260. }
  261. private Object arrayClone(Object obj)
  262. {
  263. if (obj instanceof boolean[])
  264. return ((boolean[]) obj).clone();
  265. if (obj instanceof byte[])
  266. return ((byte[]) obj).clone();
  267. if (obj instanceof char[])
  268. return ((char[]) obj).clone();
  269. if (obj instanceof short[])
  270. return ((short[]) obj).clone();
  271. if (obj instanceof int[])
  272. return ((int[]) obj).clone();
  273. if (obj instanceof float[])
  274. return ((float[]) obj).clone();
  275. if (obj instanceof long[])
  276. return ((long[]) obj).clone();
  277. if (obj instanceof double[])
  278. return ((double[]) obj).clone();
  279. if (obj instanceof Object[])
  280. return ((Object[]) obj).clone();
  281. return obj;
  282. }
  283. public Object invoke(Object proxy, Method method, Object[] args)
  284. throws Throwable
  285. {
  286. String methodName = method.getName().intern();
  287. if (args == null || args.length == 0)
  288. {
  289. if (methodName == "toString")
  290. {
  291. return toString(type, memberValues);
  292. }
  293. else if (methodName == "hashCode")
  294. {
  295. return Integer.valueOf(hashCode(type, memberValues));
  296. }
  297. else if (methodName == "annotationType")
  298. {
  299. return type;
  300. }
  301. else
  302. {
  303. Object val = memberValues.get(methodName);
  304. if (val == null)
  305. {
  306. throw new IncompleteAnnotationException(type, methodName);
  307. }
  308. if (! getBoxedReturnType(method).isInstance(val))
  309. {
  310. throw new AnnotationTypeMismatchException(method,
  311. val.getClass().getName());
  312. }
  313. if (val.getClass().isArray())
  314. {
  315. val = arrayClone(val);
  316. }
  317. return val;
  318. }
  319. }
  320. else if (args.length == 1)
  321. {
  322. if (methodName == "equals")
  323. {
  324. return Boolean.valueOf(equals(type, memberValues, args[0]));
  325. }
  326. }
  327. throw new InternalError("Invalid annotation proxy");
  328. }
  329. }