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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* *******************************************************************
  2. * Copyright (c) 2004, 2013 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 v 2.0
  7. * which accompanies this distribution and is available at
  8. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  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<>();
  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 (NameValuePair envp : pairs) {
  76. envp.dump(dos);
  77. }
  78. }
  79. public void addElementNameValuePair(NameValuePair evp) {
  80. if (pairs == Collections.EMPTY_LIST) {
  81. pairs = new ArrayList<>();
  82. }
  83. pairs.add(evp);
  84. }
  85. public int getTypeIndex() {
  86. return typeIndex;
  87. }
  88. public String getTypeSignature() {
  89. ConstantUtf8 utf8 = (ConstantUtf8) cpool.getConstant(typeIndex);
  90. return utf8.getValue();
  91. }
  92. public String getTypeName() {
  93. return Utility.signatureToString(getTypeSignature());
  94. }
  95. public List<NameValuePair> getValues() {
  96. return pairs;
  97. }
  98. @Override
  99. public String toString() {
  100. StringBuilder s = new StringBuilder();
  101. s.append("AnnotationGen:[" + getTypeName() + " #" + pairs.size() + " {");
  102. for (int i = 0; i < pairs.size(); i++) {
  103. s.append(pairs.get(i));
  104. if (i + 1 < pairs.size())
  105. s.append(",");
  106. }
  107. s.append("}]");
  108. return s.toString();
  109. }
  110. public String toShortString() {
  111. StringBuilder s = new StringBuilder();
  112. s.append("@").append(getTypeName());
  113. if (pairs.size()!=0) {
  114. s.append("(");
  115. for (int i = 0; i < pairs.size(); i++) {
  116. s.append(pairs.get(i));
  117. if (i + 1 < pairs.size())
  118. s.append(",");
  119. }
  120. s.append(")");
  121. }
  122. return s.toString();
  123. }
  124. private void isRuntimeVisible(boolean b) {
  125. isRuntimeVisible = b;
  126. }
  127. public boolean isRuntimeVisible() {
  128. return isRuntimeVisible;
  129. }
  130. /**
  131. * @return true if the annotation has a value with the specified name and (toString'd) value
  132. */
  133. public boolean hasNameValuePair(String name, String value) {
  134. for (NameValuePair pair : pairs) {
  135. if (pair.getNameString().equals(name)) {
  136. if (pair.getValue().stringifyValue().equals(value)) {
  137. return true;
  138. }
  139. }
  140. }
  141. return false;
  142. }
  143. /**
  144. * @return true if the annotation has a value with the specified name
  145. */
  146. public boolean hasNamedValue(String name) {
  147. for (NameValuePair pair : pairs) {
  148. if (pair.getNameString().equals(name)) {
  149. return true;
  150. }
  151. }
  152. return false;
  153. }
  154. }