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.

BcelAnnotation.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.bcel;
  11. import java.util.Collections;
  12. import java.util.HashSet;
  13. import java.util.List;
  14. import java.util.Set;
  15. import org.aspectj.apache.bcel.classfile.annotation.AnnotationGen;
  16. import org.aspectj.apache.bcel.classfile.annotation.ArrayElementValue;
  17. import org.aspectj.apache.bcel.classfile.annotation.ElementValue;
  18. import org.aspectj.apache.bcel.classfile.annotation.EnumElementValue;
  19. import org.aspectj.apache.bcel.classfile.annotation.NameValuePair;
  20. import org.aspectj.weaver.AbstractAnnotationAJ;
  21. import org.aspectj.weaver.ResolvedType;
  22. import org.aspectj.weaver.UnresolvedType;
  23. import org.aspectj.weaver.World;
  24. /**
  25. * Wraps a Bcel Annotation object and uses it to answer AnnotationAJ method calls. This is cheaper than translating all Bcel
  26. * annotations into AnnotationAJ objects.
  27. *
  28. * @author AndyClement
  29. */
  30. public class BcelAnnotation extends AbstractAnnotationAJ {
  31. private final AnnotationGen bcelAnnotation;
  32. public BcelAnnotation(AnnotationGen theBcelAnnotation, World world) {
  33. super(UnresolvedType.forSignature(theBcelAnnotation.getTypeSignature()).resolve(world));
  34. this.bcelAnnotation = theBcelAnnotation;
  35. }
  36. public BcelAnnotation(AnnotationGen theBcelAnnotation, ResolvedType resolvedAnnotationType) {
  37. super(resolvedAnnotationType);
  38. this.bcelAnnotation = theBcelAnnotation;
  39. }
  40. public String toString() {
  41. StringBuilder sb = new StringBuilder();
  42. List<NameValuePair> nvPairs = bcelAnnotation.getValues();
  43. sb.append("Anno[" + getTypeSignature() + " " + (isRuntimeVisible() ? "rVis" : "rInvis"));
  44. if (nvPairs.size() > 0) {
  45. sb.append(" ");
  46. int i = 0;
  47. for (NameValuePair element : nvPairs) {
  48. if (i > 0) {
  49. sb.append(',');
  50. }
  51. sb.append(element.getNameString()).append("=").append(element.getValue().toString());
  52. i++;
  53. }
  54. }
  55. sb.append("]");
  56. return sb.toString();
  57. }
  58. /**
  59. * {@inheritDoc}
  60. */
  61. @Override
  62. public Set<String> getTargets() {
  63. if (!type.equals(UnresolvedType.AT_TARGET)) {
  64. return Collections.emptySet();
  65. }
  66. List<NameValuePair> values = bcelAnnotation.getValues();
  67. NameValuePair envp = values.get(0);
  68. ArrayElementValue aev = (ArrayElementValue) envp.getValue();
  69. ElementValue[] evs = aev.getElementValuesArray();
  70. Set<String> targets = new HashSet<>();
  71. for (ElementValue elementValue : evs) {
  72. EnumElementValue ev = (EnumElementValue) elementValue;
  73. targets.add(ev.getEnumValueString());
  74. }
  75. return targets;
  76. }
  77. /**
  78. * {@inheritDoc}
  79. */
  80. @Override
  81. public boolean hasNameValuePair(String name, String value) {
  82. return bcelAnnotation.hasNameValuePair(name, value);
  83. }
  84. /**
  85. * {@inheritDoc}
  86. */
  87. @Override
  88. public boolean hasNamedValue(String name) {
  89. return bcelAnnotation.hasNamedValue(name);
  90. }
  91. /**
  92. * {@inheritDoc}
  93. */
  94. @Override
  95. public String stringify() {
  96. StringBuilder sb = new StringBuilder();
  97. sb.append("@").append(type.getClassName());
  98. List<NameValuePair> values = bcelAnnotation.getValues();
  99. if (values != null && values.size() != 0) {
  100. sb.append("(");
  101. for (NameValuePair nvPair : values) {
  102. sb.append(nvPair.getNameString()).append("=").append(nvPair.getValue().stringifyValue());
  103. }
  104. sb.append(")");
  105. }
  106. return sb.toString();
  107. }
  108. /**
  109. * {@inheritDoc}
  110. */
  111. @Override
  112. public boolean isRuntimeVisible() {
  113. return this.bcelAnnotation.isRuntimeVisible();
  114. }
  115. /**
  116. * @return return the real bcel annotation being wrapped
  117. */
  118. public AnnotationGen getBcelAnnotation() {
  119. return bcelAnnotation;
  120. }
  121. /**
  122. * {@inheritDoc}
  123. */
  124. public String getStringFormOfValue(String name) {
  125. List<NameValuePair> annotationValues = this.bcelAnnotation.getValues();
  126. if (annotationValues == null || annotationValues.size() == 0) {
  127. return null;
  128. } else {
  129. for (NameValuePair nvPair : annotationValues) {
  130. if (nvPair.getNameString().equals(name)) {
  131. return nvPair.getValue().stringifyValue();
  132. }
  133. }
  134. return null;
  135. }
  136. }
  137. }