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

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