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.

AbstractAnnotationAJ.java 3.4KB

15 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
15 years ago
14 years ago
15 years ago
15 years ago
15 years ago
14 years ago
15 years ago
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 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.Collections;
  12. import java.util.Iterator;
  13. import java.util.Set;
  14. public abstract class AbstractAnnotationAJ implements AnnotationAJ {
  15. protected final ResolvedType type;
  16. private Set<String> supportedTargets = null; // @target meta annotation
  17. public AbstractAnnotationAJ(ResolvedType type) {
  18. this.type = type;
  19. }
  20. /**
  21. * {@inheritDoc}
  22. */
  23. public final ResolvedType getType() {
  24. return type;
  25. }
  26. /**
  27. * {@inheritDoc}
  28. */
  29. public final String getTypeSignature() {
  30. return type.getSignature();
  31. }
  32. /**
  33. * {@inheritDoc}
  34. */
  35. public final String getTypeName() {
  36. return type.getName();
  37. }
  38. /**
  39. * {@inheritDoc}
  40. */
  41. public final boolean allowedOnAnnotationType() {
  42. ensureAtTargetInitialized();
  43. if (supportedTargets.isEmpty()) {
  44. return true;
  45. }
  46. return supportedTargets.contains("ANNOTATION_TYPE");
  47. }
  48. /**
  49. * {@inheritDoc}
  50. */
  51. public final boolean allowedOnField() {
  52. ensureAtTargetInitialized();
  53. if (supportedTargets.isEmpty()) {
  54. return true;
  55. }
  56. return supportedTargets.contains("FIELD");
  57. }
  58. /**
  59. * {@inheritDoc}
  60. */
  61. public final boolean allowedOnRegularType() {
  62. ensureAtTargetInitialized();
  63. if (supportedTargets.isEmpty()) {
  64. return true;
  65. }
  66. return supportedTargets.contains("TYPE");
  67. }
  68. /**
  69. * {@inheritDoc}
  70. */
  71. public final void ensureAtTargetInitialized() {
  72. if (supportedTargets == null) {
  73. AnnotationAJ atTargetAnnotation = retrieveAnnotationOnAnnotation(UnresolvedType.AT_TARGET);
  74. if (atTargetAnnotation == null) {
  75. supportedTargets = Collections.emptySet();
  76. } else {
  77. supportedTargets = atTargetAnnotation.getTargets();
  78. }
  79. }
  80. }
  81. /**
  82. * {@inheritDoc}
  83. */
  84. public final String getValidTargets() {
  85. StringBuilder sb = new StringBuilder();
  86. sb.append("{");
  87. for (Iterator<String> iter = supportedTargets.iterator(); iter.hasNext();) {
  88. String evalue = iter.next();
  89. sb.append(evalue);
  90. if (iter.hasNext()) {
  91. sb.append(",");
  92. }
  93. }
  94. sb.append("}");
  95. return sb.toString();
  96. }
  97. /**
  98. * {@inheritDoc}
  99. */
  100. public final boolean specifiesTarget() {
  101. ensureAtTargetInitialized();
  102. return !supportedTargets.isEmpty();
  103. }
  104. /**
  105. * Helper method to retrieve an annotation on an annotation e.g. retrieveAnnotationOnAnnotation(UnresolvedType.AT_TARGET)
  106. */
  107. private final AnnotationAJ retrieveAnnotationOnAnnotation(UnresolvedType requiredAnnotationSignature) {
  108. AnnotationAJ[] annos = type.getAnnotations();
  109. for (AnnotationAJ a : annos) {
  110. if (a.getTypeSignature().equals(requiredAnnotationSignature.getSignature())) {
  111. return a;
  112. }
  113. }
  114. return null;
  115. }
  116. /**
  117. * {@inheritDoc}
  118. */
  119. public abstract boolean isRuntimeVisible();
  120. /**
  121. * {@inheritDoc}
  122. */
  123. public abstract Set<String> getTargets();
  124. /**
  125. * {@inheritDoc}
  126. */
  127. public abstract boolean hasNameValuePair(String name, String value);
  128. /**
  129. * {@inheritDoc}
  130. */
  131. public abstract boolean hasNamedValue(String name);
  132. /**
  133. * {@inheritDoc}
  134. */
  135. public abstract String stringify();
  136. }