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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. * or the Apache License Version 2.0.
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. */
  16. package javassist.bytecode.annotation;
  17. import javassist.bytecode.ConstPool;
  18. import javassist.bytecode.Descriptor;
  19. import javassist.ClassPool;
  20. import javassist.CtClass;
  21. import javassist.CtMethod;
  22. import javassist.NotFoundException;
  23. import java.io.IOException;
  24. import java.util.LinkedHashMap;
  25. import java.util.Set;
  26. import java.util.Iterator;
  27. /**
  28. * The <code>annotation</code> structure.
  29. *
  30. * <p>An instance of this class is returned by
  31. * <code>getAnnotations()</code> in <code>AnnotationsAttribute</code>
  32. * or in <code>ParameterAnnotationsAttribute</code>.
  33. *
  34. * @see javassist.bytecode.AnnotationsAttribute#getAnnotations()
  35. * @see javassist.bytecode.ParameterAnnotationsAttribute#getAnnotations()
  36. * @see MemberValue
  37. * @see MemberValueVisitor
  38. * @see AnnotationsWriter
  39. *
  40. * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
  41. * @author Shigeru Chiba
  42. * @author <a href="mailto:adrian@jboss.org">Adrian Brock</a>
  43. */
  44. public class Annotation {
  45. static class Pair {
  46. int name;
  47. MemberValue value;
  48. }
  49. ConstPool pool;
  50. int typeIndex;
  51. LinkedHashMap members; // this sould be LinkedHashMap
  52. // but it is not supported by JDK 1.3.
  53. /**
  54. * Constructs an annotation including no members. A member can be
  55. * later added to the created annotation by <code>addMemberValue()</code>.
  56. *
  57. * @param type the index into the constant pool table.
  58. * the entry at that index must be the
  59. * <code>CONSTANT_Utf8_Info</code> structure
  60. * repreenting the name of the annotation interface type.
  61. * @param cp the constant pool table.
  62. *
  63. * @see #addMemberValue(String, MemberValue)
  64. */
  65. public Annotation(int type, ConstPool cp) {
  66. pool = cp;
  67. typeIndex = type;
  68. members = null;
  69. }
  70. /**
  71. * Constructs an annotation including no members. A member can be
  72. * later added to the created annotation by <code>addMemberValue()</code>.
  73. *
  74. * @param typeName the fully-qualified name of the annotation interface type.
  75. * @param cp the constant pool table.
  76. *
  77. * @see #addMemberValue(String, MemberValue)
  78. */
  79. public Annotation(String typeName, ConstPool cp) {
  80. this(cp.addUtf8Info(Descriptor.of(typeName)), cp);
  81. }
  82. /**
  83. * Constructs an annotation that can be accessed through the interface
  84. * represented by <code>clazz</code>. The values of the members are
  85. * not specified.
  86. *
  87. * @param cp the constant pool table.
  88. * @param clazz the interface.
  89. * @throws NotFoundException when the clazz is not found
  90. */
  91. public Annotation(ConstPool cp, CtClass clazz)
  92. throws NotFoundException
  93. {
  94. // todo Enums are not supported right now.
  95. this(cp.addUtf8Info(Descriptor.of(clazz.getName())), cp);
  96. if (!clazz.isInterface())
  97. throw new RuntimeException(
  98. "Only interfaces are allowed for Annotation creation.");
  99. CtMethod methods[] = clazz.getDeclaredMethods();
  100. if (methods.length > 0) {
  101. members = new LinkedHashMap();
  102. }
  103. for (int i = 0; i < methods.length; i++) {
  104. CtClass returnType = methods[i].getReturnType();
  105. addMemberValue(methods[i].getName(),
  106. createMemberValue(cp, returnType));
  107. }
  108. }
  109. /**
  110. * Makes an instance of <code>MemberValue</code>.
  111. *
  112. * @param cp the constant pool table.
  113. * @param type the type of the member.
  114. * @return the member value
  115. * @throws NotFoundException when the type is not found
  116. */
  117. public static MemberValue createMemberValue(ConstPool cp, CtClass type)
  118. throws NotFoundException
  119. {
  120. if (type == CtClass.booleanType)
  121. return new BooleanMemberValue(cp);
  122. else if (type == CtClass.byteType)
  123. return new ByteMemberValue(cp);
  124. else if (type == CtClass.charType)
  125. return new CharMemberValue(cp);
  126. else if (type == CtClass.shortType)
  127. return new ShortMemberValue(cp);
  128. else if (type == CtClass.intType)
  129. return new IntegerMemberValue(cp);
  130. else if (type == CtClass.longType)
  131. return new LongMemberValue(cp);
  132. else if (type == CtClass.floatType)
  133. return new FloatMemberValue(cp);
  134. else if (type == CtClass.doubleType)
  135. return new DoubleMemberValue(cp);
  136. else if (type.getName().equals("java.lang.Class"))
  137. return new ClassMemberValue(cp);
  138. else if (type.getName().equals("java.lang.String"))
  139. return new StringMemberValue(cp);
  140. else if (type.isArray()) {
  141. CtClass arrayType = type.getComponentType();
  142. MemberValue member = createMemberValue(cp, arrayType);
  143. return new ArrayMemberValue(member, cp);
  144. }
  145. else if (type.isInterface()) {
  146. Annotation info = new Annotation(cp, type);
  147. return new AnnotationMemberValue(info, cp);
  148. }
  149. else {
  150. // treat as enum. I know this is not typed,
  151. // but JBoss has an Annotation Compiler for JDK 1.4
  152. // and I want it to work with that. - Bill Burke
  153. EnumMemberValue emv = new EnumMemberValue(cp);
  154. emv.setType(type.getName());
  155. return emv;
  156. }
  157. }
  158. /**
  159. * Adds a new member.
  160. *
  161. * @param nameIndex the index into the constant pool table.
  162. * The entry at that index must be
  163. * a <code>CONSTANT_Utf8_info</code> structure.
  164. * structure representing the member name.
  165. * @param value the member value.
  166. */
  167. public void addMemberValue(int nameIndex, MemberValue value) {
  168. Pair p = new Pair();
  169. p.name = nameIndex;
  170. p.value = value;
  171. addMemberValue(p);
  172. }
  173. /**
  174. * Adds a new member.
  175. *
  176. * @param name the member name.
  177. * @param value the member value.
  178. */
  179. public void addMemberValue(String name, MemberValue value) {
  180. Pair p = new Pair();
  181. p.name = pool.addUtf8Info(name);
  182. p.value = value;
  183. if (members == null)
  184. members = new LinkedHashMap();
  185. members.put(name, p);
  186. }
  187. private void addMemberValue(Pair pair) {
  188. String name = pool.getUtf8Info(pair.name);
  189. if (members == null)
  190. members = new LinkedHashMap();
  191. members.put(name, pair);
  192. }
  193. /**
  194. * Returns a string representation of the annotation.
  195. */
  196. public String toString() {
  197. StringBuffer buf = new StringBuffer("@");
  198. buf.append(getTypeName());
  199. if (members != null) {
  200. buf.append("(");
  201. Iterator mit = members.keySet().iterator();
  202. while (mit.hasNext()) {
  203. String name = (String)mit.next();
  204. buf.append(name).append("=").append(getMemberValue(name));
  205. if (mit.hasNext())
  206. buf.append(", ");
  207. }
  208. buf.append(")");
  209. }
  210. return buf.toString();
  211. }
  212. /**
  213. * Obtains the name of the annotation type.
  214. *
  215. * @return the type name
  216. */
  217. public String getTypeName() {
  218. return Descriptor.toClassName(pool.getUtf8Info(typeIndex));
  219. }
  220. /**
  221. * Obtains all the member names.
  222. *
  223. * @return null if no members are defined.
  224. */
  225. public Set getMemberNames() {
  226. if (members == null)
  227. return null;
  228. else
  229. return members.keySet();
  230. }
  231. /**
  232. * Obtains the member value with the given name.
  233. *
  234. * <p>If this annotation does not have a value for the
  235. * specified member,
  236. * this method returns null. It does not return a
  237. * <code>MemberValue</code> with the default value.
  238. * The default value can be obtained from the annotation type.
  239. *
  240. * @param name the member name
  241. * @return null if the member cannot be found or if the value is
  242. * the default value.
  243. *
  244. * @see javassist.bytecode.AnnotationDefaultAttribute
  245. */
  246. public MemberValue getMemberValue(String name) {
  247. if (members == null)
  248. return null;
  249. else {
  250. Pair p = (Pair)members.get(name);
  251. if (p == null)
  252. return null;
  253. else
  254. return p.value;
  255. }
  256. }
  257. /**
  258. * Constructs an annotation-type object representing this annotation.
  259. * For example, if this annotation represents <code>@Author</code>,
  260. * this method returns an <code>Author</code> object.
  261. *
  262. * @param cl class loader for loading an annotation type.
  263. * @param cp class pool for obtaining class files.
  264. * @return the annotation
  265. * @throws ClassNotFoundException if the class cannot found.
  266. * @throws NoSuchClassError if the class linkage fails.
  267. */
  268. public Object toAnnotationType(ClassLoader cl, ClassPool cp)
  269. throws ClassNotFoundException, NoSuchClassError
  270. {
  271. return AnnotationImpl.make(cl,
  272. MemberValue.loadClass(cl, getTypeName()),
  273. cp, this);
  274. }
  275. /**
  276. * Writes this annotation.
  277. *
  278. * @param writer the output.
  279. * @throws IOException for an error during the write
  280. */
  281. public void write(AnnotationsWriter writer) throws IOException {
  282. String typeName = pool.getUtf8Info(typeIndex);
  283. if (members == null) {
  284. writer.annotation(typeName, 0);
  285. return;
  286. }
  287. writer.annotation(typeName, members.size());
  288. Iterator it = members.values().iterator();
  289. while (it.hasNext()) {
  290. Pair pair = (Pair)it.next();
  291. writer.memberValuePair(pair.name);
  292. pair.value.write(writer);
  293. }
  294. }
  295. /**
  296. * Returns true if the given object represents the same annotation
  297. * as this object. The equality test checks the member values.
  298. */
  299. public boolean equals(Object obj) {
  300. if (obj == this)
  301. return true;
  302. if (obj == null || obj instanceof Annotation == false)
  303. return false;
  304. Annotation other = (Annotation) obj;
  305. if (getTypeName().equals(other.getTypeName()) == false)
  306. return false;
  307. LinkedHashMap otherMembers = other.members;
  308. if (members == otherMembers)
  309. return true;
  310. else if (members == null)
  311. return otherMembers == null;
  312. else
  313. if (otherMembers == null)
  314. return false;
  315. else
  316. return members.equals(otherMembers);
  317. }
  318. }