Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

StandardAnnotation.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 v 2.0
  6. * which accompanies this distribution and is available at
  7. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  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. StringBuilder sb = new StringBuilder();
  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. StringBuilder sb = new StringBuilder();
  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 (AnnotationNameValuePair pair : nvPairs) {
  76. if (pair.getName().equals(n)) {
  77. return true;
  78. }
  79. }
  80. return false;
  81. }
  82. /**
  83. * {@inheritDoc}
  84. */
  85. public boolean hasNameValuePair(String n, String v) {
  86. if (nvPairs == null) {
  87. return false;
  88. }
  89. for (AnnotationNameValuePair pair : nvPairs) {
  90. if (pair.getName().equals(n)) {
  91. if (pair.getValue().stringify().equals(v)) {
  92. return true;
  93. }
  94. }
  95. }
  96. return false;
  97. }
  98. /**
  99. * {@inheritDoc}
  100. */
  101. public Set<String> getTargets() {
  102. if (!type.equals(UnresolvedType.AT_TARGET)) {
  103. return Collections.emptySet();
  104. }
  105. AnnotationNameValuePair nvp = nvPairs.get(0);
  106. ArrayAnnotationValue aav = (ArrayAnnotationValue) nvp.getValue();
  107. AnnotationValue[] avs = aav.getValues();
  108. Set<String> targets = new HashSet<>();
  109. for (AnnotationValue av : avs) {
  110. EnumAnnotationValue value = (EnumAnnotationValue) av;
  111. targets.add(value.getValue());
  112. }
  113. return targets;
  114. }
  115. public List<AnnotationNameValuePair> getNameValuePairs() {
  116. return nvPairs;
  117. }
  118. public boolean hasNameValuePairs() {
  119. return nvPairs != null && nvPairs.size() != 0;
  120. }
  121. public void addNameValuePair(AnnotationNameValuePair pair) {
  122. if (nvPairs == null) {
  123. nvPairs = new ArrayList<>();
  124. }
  125. nvPairs.add(pair);
  126. }
  127. /**
  128. * {@inheritDoc}
  129. */
  130. public String getStringFormOfValue(String name) {
  131. if (hasNameValuePairs()) {
  132. for (AnnotationNameValuePair nvPair : nvPairs) {
  133. if (nvPair.getName().equals(name)) {
  134. return nvPair.getValue().stringify();
  135. }
  136. }
  137. }
  138. return null;
  139. }
  140. }