PageRenderTime 147ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/aspectj-1.6.9/aspectjtools1.6.9/org/aspectj/weaver/patterns/BindingAnnotationFieldTypePattern.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 176 lines | 105 code | 16 blank | 55 comment | 16 complexity | ba566d8a2c955692217bd6f47fd6be9b MD5 | raw file
  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. * Contributors:
  10. * Andy Clement initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.patterns;
  13. import java.io.IOException;
  14. import java.util.Map;
  15. import org.aspectj.bridge.IMessage;
  16. import org.aspectj.bridge.MessageUtil;
  17. import org.aspectj.util.FuzzyBoolean;
  18. import org.aspectj.weaver.AnnotatedElement;
  19. import org.aspectj.weaver.BCException;
  20. import org.aspectj.weaver.CompressingDataOutputStream;
  21. import org.aspectj.weaver.ISourceContext;
  22. import org.aspectj.weaver.IntMap;
  23. import org.aspectj.weaver.ReferenceType;
  24. import org.aspectj.weaver.ResolvedMember;
  25. import org.aspectj.weaver.ResolvedType;
  26. import org.aspectj.weaver.UnresolvedType;
  27. import org.aspectj.weaver.VersionedDataInputStream;
  28. import org.aspectj.weaver.WeaverMessages;
  29. import org.aspectj.weaver.World;
  30. /**
  31. * Represents an attempt to bind the field of an annotation within a pointcut. For example:<br>
  32. * <code><pre>
  33. * before(Level lev): execution(* *(..)) &amp;&amp; @annotation(TraceAnnotation(lev))
  34. * </pre></code><br>
  35. * This binding annotation type pattern will be for 'lev'.
  36. */
  37. public class BindingAnnotationFieldTypePattern extends ExactAnnotationTypePattern implements BindingPattern {
  38. protected int formalIndex;
  39. UnresolvedType formalType; // In this construct the formal type differs from the annotation type
  40. public BindingAnnotationFieldTypePattern(UnresolvedType formalType, int formalIndex, UnresolvedType theAnnotationType) {
  41. super(theAnnotationType, null);
  42. this.formalIndex = formalIndex;
  43. this.formalType = formalType;
  44. }
  45. public void resolveBinding(World world) {
  46. if (resolved) {
  47. return;
  48. }
  49. resolved = true;
  50. formalType = world.resolve(formalType);
  51. annotationType = world.resolve(annotationType);
  52. ResolvedType annoType = (ResolvedType) annotationType;
  53. if (!annoType.isAnnotation()) {
  54. IMessage m = MessageUtil.error(WeaverMessages.format(WeaverMessages.REFERENCE_TO_NON_ANNOTATION_TYPE, annoType
  55. .getName()), getSourceLocation());
  56. world.getMessageHandler().handleMessage(m);
  57. resolved = false;
  58. }
  59. }
  60. public AnnotationTypePattern parameterizeWith(Map typeVariableMap, World w) {
  61. throw new BCException("Parameterization not implemented for annotation field binding construct (compiler limitation)");
  62. // UnresolvedType newAnnotationType = annotationType;
  63. // if (annotationType.isTypeVariableReference()) {
  64. // TypeVariableReference t = (TypeVariableReference) annotationType;
  65. // String key = t.getTypeVariable().getName();
  66. // if (typeVariableMap.containsKey(key)) {
  67. // newAnnotationType = (UnresolvedType) typeVariableMap.get(key);
  68. // }
  69. // } else if (annotationType.isParameterizedType()) {
  70. // newAnnotationType = annotationType.parameterize(typeVariableMap);
  71. // }
  72. // BindingAnnotationTypePattern ret = new BindingAnnotationTypePattern(newAnnotationType, this.formalIndex);
  73. // if (newAnnotationType instanceof ResolvedType) {
  74. // ResolvedType rat = (ResolvedType) newAnnotationType;
  75. // verifyRuntimeRetention(rat.getWorld(), rat);
  76. // }
  77. // ret.copyLocationFrom(this);
  78. // return ret;
  79. }
  80. public int getFormalIndex() {
  81. return formalIndex;
  82. }
  83. public boolean equals(Object obj) {
  84. if (!(obj instanceof BindingAnnotationFieldTypePattern)) {
  85. return false;
  86. }
  87. BindingAnnotationFieldTypePattern btp = (BindingAnnotationFieldTypePattern) obj;
  88. return (btp.formalIndex == formalIndex) && (annotationType.equals(btp.annotationType))
  89. && (formalType.equals(btp.formalType));
  90. }
  91. public int hashCode() {
  92. return (annotationType.hashCode() * 37 + formalIndex * 37) + formalType.hashCode();
  93. }
  94. public AnnotationTypePattern remapAdviceFormals(IntMap bindings) {
  95. if (!bindings.hasKey(formalIndex)) {
  96. throw new BCException("Annotation field binding reference must be bound (compiler limitation)");
  97. // must be something like returning the unbound form: return new ExactAnnotationTypePattern(annotationType,
  98. // null);
  99. } else {
  100. int newFormalIndex = bindings.get(formalIndex);
  101. return new BindingAnnotationFieldTypePattern(formalType, newFormalIndex, annotationType);
  102. }
  103. }
  104. public void write(CompressingDataOutputStream s) throws IOException {
  105. s.writeByte(AnnotationTypePattern.BINDINGFIELD);
  106. formalType.write(s); // the type of the field within the annotation
  107. s.writeShort((short) formalIndex);
  108. annotationType.write(s); // the annotation type
  109. writeLocation(s);
  110. }
  111. public static AnnotationTypePattern read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  112. AnnotationTypePattern ret = new BindingAnnotationFieldTypePattern(UnresolvedType.read(s), s.readShort(), UnresolvedType
  113. .read(s));
  114. ret.readLocation(context, s);
  115. return ret;
  116. }
  117. public FuzzyBoolean matches(AnnotatedElement annotated, ResolvedType[] parameterAnnotations) {
  118. // Inheritance irrelevant because @annotation(Anno(x)) only supported at method execution join points (compiler limitation)
  119. // boolean checkSupers = false;
  120. // if (getResolvedAnnotationType().hasAnnotation(UnresolvedType.AT_INHERITED)) {
  121. // if (annotated instanceof ResolvedType) {
  122. // checkSupers = true;
  123. // }
  124. // }
  125. //
  126. if (annotated.hasAnnotation(annotationType)) {
  127. if (annotationType instanceof ReferenceType) {
  128. ReferenceType rt = (ReferenceType) annotationType;
  129. if (rt.getRetentionPolicy() != null && rt.getRetentionPolicy().equals("SOURCE")) {
  130. rt.getWorld().getMessageHandler().handleMessage(
  131. MessageUtil.warn(WeaverMessages.format(WeaverMessages.NO_MATCH_BECAUSE_SOURCE_RETENTION,
  132. annotationType, annotated), getSourceLocation()));
  133. return FuzzyBoolean.NO;
  134. }
  135. ResolvedMember[] methods = rt.getDeclaredMethods();
  136. boolean found = false;
  137. for (int i = 0; i < methods.length && !found; i++) {
  138. if (methods[i].getReturnType().equals(formalType)) {
  139. found = true;
  140. }
  141. }
  142. return (found ? FuzzyBoolean.YES : FuzzyBoolean.NO);
  143. }
  144. }
  145. // else if (checkSupers) {
  146. // ResolvedType toMatchAgainst = ((ResolvedType) annotated).getSuperclass();
  147. // while (toMatchAgainst != null) {
  148. // if (toMatchAgainst.hasAnnotation(annotationType)) {
  149. // return FuzzyBoolean.YES;
  150. // }
  151. // toMatchAgainst = toMatchAgainst.getSuperclass();
  152. // }
  153. // }
  154. //
  155. return FuzzyBoolean.NO;
  156. }
  157. public UnresolvedType getFormalType() {
  158. return formalType;
  159. }
  160. }