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.

Annotation.java 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 2004 Bill Burke. 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.bytecode.ConstPool;
  17. import javassist.bytecode.Descriptor;
  18. import javassist.CtClass;
  19. import javassist.CtMethod;
  20. import java.io.IOException;
  21. import java.util.HashMap;
  22. import java.util.Set;
  23. import java.util.Iterator;
  24. /**
  25. * The <code>annotation</code> structure.
  26. *
  27. * <p>An instance of this class is returned by
  28. * <code>getAnnotations()</code> in <code>AnnotationsAttribute</code>
  29. * or in <code>ParameterAnnotationsAttribute</code>.
  30. *
  31. * @see javassist.bytecode.AnnotationsAttribute#getAnnotations()
  32. * @see javassist.bytecode.ParameterAnnotationsAttribute#getAnnotations()
  33. *
  34. * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
  35. * @author Shigeru Chiba
  36. */
  37. public class Annotation {
  38. static class Pair {
  39. int name;
  40. MemberValue value;
  41. }
  42. ConstPool pool;
  43. int typeIndex;
  44. HashMap members; // this sould be LinkedHashMap
  45. // but it is not supported by JDK 1.3.
  46. /**
  47. * Constructs an annotation including no members. A member can be
  48. * later added to the created annotation by <code>addMemberValue()</code>.
  49. *
  50. * @param type the index into the constant pool table.
  51. * the entry at that index must be the
  52. * <code>CONSTANT_Utf8_Info</code> structure
  53. * repreenting the name of the annotation interface type.
  54. * @param cp the constant pool table.
  55. *
  56. * @see #addMemberValue(String, MemberValue)
  57. */
  58. public Annotation(int type, ConstPool cp) {
  59. pool = cp;
  60. typeIndex = type;
  61. members = null;
  62. }
  63. /**
  64. * Constructs an annotation including no members. A member can be
  65. * later added to the created annotation by <code>addMemberValue()</code>.
  66. *
  67. * @param typeName the name of the annotation interface type.
  68. * @param cp the constant pool table.
  69. *
  70. * @see #addMemberValue(String, MemberValue)
  71. */
  72. public Annotation(String typeName, ConstPool cp) {
  73. this(cp.addUtf8Info(Descriptor.of(typeName)), cp);
  74. }
  75. /**
  76. * Constructs an annotation that can be accessed through the interface
  77. * represented by <code>clazz</code>. The values of the members are
  78. * not specified.
  79. *
  80. * @param cp the constant pool table.
  81. * @param clazz the interface.
  82. */
  83. public Annotation(ConstPool cp, CtClass clazz)
  84. throws javassist.NotFoundException
  85. {
  86. // todo Enums are not supported right now.
  87. this(cp.addUtf8Info(Descriptor.of(clazz.getName())), cp);
  88. if (!clazz.isInterface())
  89. throw new RuntimeException(
  90. "Only interfaces are allowed for Annotation creation.");
  91. CtMethod methods[] = clazz.getDeclaredMethods();
  92. if (methods.length > 0)
  93. members = new HashMap();
  94. for (int i = 0; i < methods.length; i++) {
  95. CtClass returnType = methods[i].getReturnType();
  96. addMemberValue(methods[i].getName(),
  97. createMemberValue(cp, returnType));
  98. }
  99. }
  100. private MemberValue createMemberValue(ConstPool cp, CtClass returnType)
  101. throws javassist.NotFoundException
  102. {
  103. if (returnType == CtClass.booleanType)
  104. return new BooleanMemberValue(cp);
  105. else if (returnType == CtClass.byteType)
  106. return new ByteMemberValue(cp);
  107. else if (returnType == CtClass.charType)
  108. return new CharMemberValue(cp);
  109. else if (returnType == CtClass.shortType)
  110. return new ShortMemberValue(cp);
  111. else if (returnType == CtClass.intType)
  112. return new IntegerMemberValue(cp);
  113. else if (returnType == CtClass.longType)
  114. return new LongMemberValue(cp);
  115. else if (returnType == CtClass.floatType)
  116. return new FloatMemberValue(cp);
  117. else if (returnType == CtClass.doubleType)
  118. return new DoubleMemberValue(cp);
  119. else if (returnType.getName().equals("java.lang.Class"))
  120. return new ClassMemberValue(cp);
  121. else if (returnType.getName().equals("java.lang.String"))
  122. return new StringMemberValue(cp);
  123. else if (returnType.isArray()) {
  124. CtClass arrayType = returnType.getComponentType();
  125. MemberValue type = createMemberValue(cp, arrayType);
  126. return new ArrayMemberValue(type, cp);
  127. }
  128. else if (returnType.isInterface()) {
  129. Annotation info = new Annotation(cp, returnType);
  130. return new AnnotationMemberValue(info, cp);
  131. }
  132. else {
  133. // treat as enum. I know this is not typed,
  134. // but JBoss has an Annotation Compiler for JDK 1.4
  135. // and I want it to work with that. - Bill Burke
  136. EnumMemberValue emv = new EnumMemberValue(cp);
  137. emv.setType(returnType.getName());
  138. return emv;
  139. }
  140. }
  141. /**
  142. * Adds a new member.
  143. *
  144. * @param nameIndex the index into the constant pool table.
  145. * The entry at that index must be
  146. * a <code>CONSTANT_Utf8_info</code> structure.
  147. * structure representing the member name.
  148. * @param value the member value.
  149. */
  150. public void addMemberValue(int nameIndex, MemberValue value) {
  151. Pair p = new Pair();
  152. p.name = nameIndex;
  153. p.value = value;
  154. addMemberValue(p);
  155. }
  156. /**
  157. * Adds a new member.
  158. *
  159. * @param name the member name.
  160. * @param value the member value.
  161. */
  162. public void addMemberValue(String name, MemberValue value) {
  163. Pair p = new Pair();
  164. p.name = pool.addUtf8Info(name);
  165. p.value = value;
  166. if (members == null)
  167. members = new HashMap();
  168. members.put(name, p);
  169. }
  170. private void addMemberValue(Pair pair) {
  171. String name = pool.getUtf8Info(pair.name);
  172. if (members == null)
  173. members = new HashMap();
  174. members.put(name, pair);
  175. }
  176. /**
  177. * Returns a string representation of this object.
  178. */
  179. public String toString() {
  180. StringBuffer buf = new StringBuffer("@");
  181. buf.append(getTypeName());
  182. if (members != null) {
  183. buf.append("(");
  184. Iterator mit = members.keySet().iterator();
  185. while (mit.hasNext()) {
  186. String name = (String)mit.next();
  187. buf.append(name).append("=").append(getMemberValue(name));
  188. if (mit.hasNext())
  189. buf.append(", ");
  190. }
  191. buf.append(")");
  192. }
  193. return buf.toString();
  194. }
  195. /**
  196. * Obtains the name of the annotation type.
  197. */
  198. public String getTypeName() {
  199. return Descriptor.toClassName(pool.getUtf8Info(typeIndex));
  200. }
  201. /**
  202. * Obtains all the member names.
  203. *
  204. * @return null if no members are defined.
  205. */
  206. public Set getMemberNames() {
  207. if (members == null)
  208. return null;
  209. else
  210. return members.keySet();
  211. }
  212. /**
  213. * Obtains the member value with the given name.
  214. *
  215. * @return null if the member cannot be found.
  216. */
  217. public MemberValue getMemberValue(String name) {
  218. if (members == null)
  219. return null;
  220. else {
  221. Pair p = (Pair)members.get(name);
  222. if (p == null)
  223. return null;
  224. else
  225. return p.value;
  226. }
  227. }
  228. /**
  229. * Writes this annotation.
  230. *
  231. * @param writer the output.
  232. */
  233. public void write(AnnotationsWriter writer) throws IOException {
  234. if (members == null) {
  235. writer.annotation(typeIndex, 0);
  236. return;
  237. }
  238. writer.annotation(typeIndex, members.size());
  239. Iterator it = members.values().iterator();
  240. while (it.hasNext()) {
  241. Pair pair = (Pair)it.next();
  242. writer.memberValuePair(pair.name);
  243. pair.value.write(writer);
  244. }
  245. }
  246. }