Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

AnnotationImpl.java 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2006 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.bytecode.annotation;
  16. import javassist.ClassPool;
  17. import javassist.CtClass;
  18. import javassist.NotFoundException;
  19. import javassist.bytecode.AnnotationDefaultAttribute;
  20. import javassist.bytecode.ClassFile;
  21. import javassist.bytecode.MethodInfo;
  22. import java.lang.reflect.*;
  23. class AnnotationImpl implements InvocationHandler {
  24. private Annotation annotation;
  25. private ClassPool pool;
  26. private ClassLoader classLoader;
  27. /**
  28. * Constructs an annotation object.
  29. *
  30. * @param cl class loader for obtaining annotation types.
  31. * @param clazz the annotation type.
  32. * @param cp class pool for containing an annotation
  33. * type (or null).
  34. * @param anon the annotation.
  35. */
  36. public static Object make(ClassLoader cl, Class clazz, ClassPool cp,
  37. Annotation anon) {
  38. AnnotationImpl handler = new AnnotationImpl(anon, cp, cl);
  39. return Proxy.newProxyInstance(cl, new Class[] { clazz }, handler);
  40. }
  41. private AnnotationImpl(Annotation a, ClassPool cp, ClassLoader loader) {
  42. annotation = a;
  43. pool = cp;
  44. classLoader = loader;
  45. }
  46. public Object invoke(Object proxy, Method method, Object[] args)
  47. throws Throwable
  48. {
  49. String name = method.getName();
  50. MemberValue mv = annotation.getMemberValue(name);
  51. if (mv == null)
  52. return getDefault(name, method);
  53. else
  54. return mv.getValue(classLoader, pool, method);
  55. }
  56. private Object getDefault(String name, Method method)
  57. throws ClassNotFoundException, RuntimeException
  58. {
  59. String classname = annotation.getTypeName();
  60. if (pool != null)
  61. try {
  62. CtClass cc = pool.get(classname);
  63. ClassFile cf = cc.getClassFile2();
  64. MethodInfo minfo = cf.getMethod(name);
  65. if (minfo != null) {
  66. AnnotationDefaultAttribute ainfo
  67. = (AnnotationDefaultAttribute)
  68. minfo.getAttribute(AnnotationDefaultAttribute.tag);
  69. if (ainfo != null) {
  70. MemberValue mv = ainfo.getDefaultValue();
  71. return mv.getValue(classLoader, pool, method);
  72. }
  73. }
  74. }
  75. catch (NotFoundException e) {
  76. throw new RuntimeException("cannot find a class file: "
  77. + classname);
  78. }
  79. throw new RuntimeException("no default value: " + classname + "."
  80. + name + "()");
  81. }
  82. }