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.

StandardAnnotation.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* *******************************************************************
  2. * Copyright (c) 2008 Contributors
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * ******************************************************************/
  10. package org.aspectj.weaver;
  11. import java.util.ArrayList;
  12. import java.util.Collections;
  13. import java.util.HashSet;
  14. import java.util.Iterator;
  15. import java.util.List;
  16. import java.util.Set;
  17. /**
  18. * This type represents the weavers abstraction of an annotation - it is not tied to any underlying BCI toolkit. The weaver actualy
  19. * handles these through AnnotationX wrapper objects - until we start transforming the BCEL annotations into this form (expensive)
  20. * or offer a clever visitor mechanism over the BCEL annotation stuff that builds these annotation types directly.
  21. *
  22. * @author AndyClement
  23. */
  24. public class StandardAnnotation extends AbstractAnnotationAJ {
  25. private final boolean isRuntimeVisible;
  26. private List<AnnotationNameValuePair> nvPairs = null;
  27. public StandardAnnotation(ResolvedType type, boolean isRuntimeVisible) {
  28. super(type);
  29. this.isRuntimeVisible = isRuntimeVisible;
  30. }
  31. /**
  32. * {@inheritDoc}
  33. */
  34. public boolean isRuntimeVisible() {
  35. return isRuntimeVisible;
  36. }
  37. /**
  38. * {@inheritDoc}
  39. */
  40. public String stringify() {
  41. StringBuffer sb = new StringBuffer();
  42. sb.append("@").append(type.getClassName());
  43. if (hasNameValuePairs()) {
  44. sb.append("(");
  45. for (AnnotationNameValuePair nvPair : nvPairs) {
  46. sb.append(nvPair.stringify());
  47. }
  48. sb.append(")");
  49. }
  50. return sb.toString();
  51. }
  52. public String toString() {
  53. StringBuffer sb = new StringBuffer();
  54. sb.append("Anno[" + getTypeSignature() + " " + (isRuntimeVisible ? "rVis" : "rInvis"));
  55. if (nvPairs != null) {
  56. sb.append(" ");
  57. for (Iterator<AnnotationNameValuePair> iter = nvPairs.iterator(); iter.hasNext();) {
  58. AnnotationNameValuePair element = iter.next();
  59. sb.append(element.toString());
  60. if (iter.hasNext()) {
  61. sb.append(",");
  62. }
  63. }
  64. }
  65. sb.append("]");
  66. return sb.toString();
  67. }
  68. /**
  69. * {@inheritDoc}
  70. */
  71. public boolean hasNamedValue(String n) {
  72. if (nvPairs == null) {
  73. return false;
  74. }
  75. for (int i = 0; i < nvPairs.size(); i++) {
  76. AnnotationNameValuePair pair = nvPairs.get(i);
  77. if (pair.getName().equals(n)) {
  78. return true;
  79. }
  80. }
  81. return false;
  82. }
  83. /**
  84. * {@inheritDoc}
  85. */
  86. public boolean hasNameValuePair(String n, String v) {
  87. if (nvPairs == null) {
  88. return false;
  89. }
  90. for (int i = 0; i < nvPairs.size(); i++) {
  91. AnnotationNameValuePair pair = nvPairs.get(i);
  92. if (pair.getName().equals(n)) {
  93. if (pair.getValue().stringify().equals(v)) {
  94. return true;
  95. }
  96. }
  97. }
  98. return false;
  99. }
  100. /**
  101. * {@inheritDoc}
  102. */
  103. public Set<String> getTargets() {
  104. if (!type.equals(UnresolvedType.AT_TARGET)) {
  105. return Collections.emptySet();
  106. }
  107. AnnotationNameValuePair nvp = nvPairs.get(0);
  108. ArrayAnnotationValue aav = (ArrayAnnotationValue) nvp.getValue();
  109. AnnotationValue[] avs = aav.getValues();
  110. Set<String> targets = new HashSet<String>();
  111. for (int i = 0; i < avs.length; i++) {
  112. EnumAnnotationValue value = (EnumAnnotationValue)avs[i];
  113. targets.add(value.getValue());
  114. }
  115. return targets;
  116. }
  117. public List<AnnotationNameValuePair> getNameValuePairs() {
  118. return nvPairs;
  119. }
  120. public boolean hasNameValuePairs() {
  121. return nvPairs != null && nvPairs.size() != 0;
  122. }
  123. public void addNameValuePair(AnnotationNameValuePair pair) {
  124. if (nvPairs == null) {
  125. nvPairs = new ArrayList<AnnotationNameValuePair>();
  126. }
  127. nvPairs.add(pair);
  128. }
  129. /**
  130. * {@inheritDoc}
  131. */
  132. public String getStringFormOfValue(String name) {
  133. if (hasNameValuePairs()) {
  134. for (AnnotationNameValuePair nvPair : nvPairs) {
  135. if (nvPair.getName().equals(name)) {
  136. return nvPair.getValue().stringify();
  137. }
  138. }
  139. }
  140. return null;
  141. }
  142. }