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.

AnnotationGen.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* *******************************************************************
  2. * Copyright (c) 2004 IBM Corporation
  3. *
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * Andy Clement initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.apache.bcel.classfile.annotation;
  14. import java.io.DataInputStream;
  15. import java.io.DataOutputStream;
  16. import java.io.IOException;
  17. import java.util.ArrayList;
  18. import java.util.Collections;
  19. import java.util.List;
  20. import org.aspectj.apache.bcel.classfile.ConstantPool;
  21. import org.aspectj.apache.bcel.classfile.ConstantUtf8;
  22. import org.aspectj.apache.bcel.classfile.Utility;
  23. import org.aspectj.apache.bcel.generic.ObjectType;
  24. public class AnnotationGen {
  25. public static final AnnotationGen[] NO_ANNOTATIONS = new AnnotationGen[0];
  26. private int typeIndex;
  27. private List<NameValuePair> pairs = Collections.emptyList();
  28. private ConstantPool cpool;
  29. private boolean isRuntimeVisible = false;
  30. public AnnotationGen(AnnotationGen a, ConstantPool cpool, boolean copyPoolEntries) {
  31. this.cpool = cpool;
  32. if (copyPoolEntries) {
  33. typeIndex = cpool.addUtf8(a.getTypeSignature());
  34. } else {
  35. typeIndex = a.getTypeIndex();
  36. }
  37. isRuntimeVisible = a.isRuntimeVisible();
  38. pairs = copyValues(a.getValues(), cpool, copyPoolEntries);
  39. }
  40. private List<NameValuePair> copyValues(List<NameValuePair> in, ConstantPool cpool, boolean copyPoolEntries) {
  41. List<NameValuePair> out = new ArrayList<NameValuePair>();
  42. for (NameValuePair nvp : in) {
  43. out.add(new NameValuePair(nvp, cpool, copyPoolEntries));
  44. }
  45. return out;
  46. }
  47. private AnnotationGen(ConstantPool cpool) {
  48. this.cpool = cpool;
  49. }
  50. /**
  51. * Retrieve an immutable version of this AnnotationGen
  52. */
  53. public AnnotationGen(ObjectType type, List<NameValuePair> pairs, boolean runtimeVisible, ConstantPool cpool) {
  54. this.cpool = cpool;
  55. if (type != null) {
  56. this.typeIndex = cpool.addUtf8(type.getSignature()); // Only null for funky *temporary* FakeAnnotation objects
  57. }
  58. this.pairs = pairs;
  59. isRuntimeVisible = runtimeVisible;
  60. }
  61. public static AnnotationGen read(DataInputStream dis, ConstantPool cpool, boolean b) throws IOException {
  62. AnnotationGen a = new AnnotationGen(cpool);
  63. a.typeIndex = dis.readUnsignedShort();
  64. int elemValuePairCount = dis.readUnsignedShort();
  65. for (int i = 0; i < elemValuePairCount; i++) {
  66. int nidx = dis.readUnsignedShort();
  67. a.addElementNameValuePair(new NameValuePair(nidx, ElementValue.readElementValue(dis, cpool), cpool));
  68. }
  69. a.isRuntimeVisible(b);
  70. return a;
  71. }
  72. public void dump(DataOutputStream dos) throws IOException {
  73. dos.writeShort(typeIndex); // u2 index of type name in cpool
  74. dos.writeShort(pairs.size()); // u2 element_value pair count
  75. for (int i = 0; i < pairs.size(); i++) {
  76. NameValuePair envp = pairs.get(i);
  77. envp.dump(dos);
  78. }
  79. }
  80. public void addElementNameValuePair(NameValuePair evp) {
  81. if (pairs == Collections.EMPTY_LIST) {
  82. pairs = new ArrayList<NameValuePair>();
  83. }
  84. pairs.add(evp);
  85. }
  86. public int getTypeIndex() {
  87. return typeIndex;
  88. }
  89. public String getTypeSignature() {
  90. ConstantUtf8 utf8 = (ConstantUtf8) cpool.getConstant(typeIndex);
  91. return utf8.getValue();
  92. }
  93. public String getTypeName() {
  94. return Utility.signatureToString(getTypeSignature());
  95. }
  96. public List<NameValuePair> getValues() {
  97. return pairs;
  98. }
  99. @Override
  100. public String toString() {
  101. StringBuffer s = new StringBuffer();
  102. s.append("AnnotationGen:[" + getTypeName() + " #" + pairs.size() + " {");
  103. for (int i = 0; i < pairs.size(); i++) {
  104. s.append(pairs.get(i));
  105. if (i + 1 < pairs.size())
  106. s.append(",");
  107. }
  108. s.append("}]");
  109. return s.toString();
  110. }
  111. public String toShortString() {
  112. StringBuffer s = new StringBuffer();
  113. s.append("@" + getTypeName() + "(");
  114. for (int i = 0; i < pairs.size(); i++) {
  115. s.append(pairs.get(i));
  116. if (i + 1 < pairs.size())
  117. s.append(",");
  118. }
  119. s.append(")");
  120. return s.toString();
  121. }
  122. private void isRuntimeVisible(boolean b) {
  123. isRuntimeVisible = b;
  124. }
  125. public boolean isRuntimeVisible() {
  126. return isRuntimeVisible;
  127. }
  128. /**
  129. * @return true if the annotation has a value with the specified name and (toString'd) value
  130. */
  131. public boolean hasNameValuePair(String name, String value) {
  132. for (NameValuePair pair : pairs) {
  133. if (pair.getNameString().equals(name)) {
  134. if (pair.getValue().stringifyValue().equals(value)) {
  135. return true;
  136. }
  137. }
  138. }
  139. return false;
  140. }
  141. /**
  142. * @return true if the annotation has a value with the specified name
  143. */
  144. public boolean hasNamedValue(String name) {
  145. for (NameValuePair pair : pairs) {
  146. if (pair.getNameString().equals(name)) {
  147. return true;
  148. }
  149. }
  150. return false;
  151. }
  152. }