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.

BindingAnnotationTypePattern.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* *******************************************************************
  2. * Copyright (c) 2004 IBM Corporation.
  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.patterns;
  11. import java.io.IOException;
  12. import java.util.Map;
  13. import org.aspectj.bridge.IMessage;
  14. import org.aspectj.bridge.MessageUtil;
  15. import org.aspectj.weaver.BCException;
  16. import org.aspectj.weaver.CompressingDataOutputStream;
  17. import org.aspectj.weaver.ISourceContext;
  18. import org.aspectj.weaver.IntMap;
  19. import org.aspectj.weaver.ResolvedType;
  20. import org.aspectj.weaver.TypeVariableReference;
  21. import org.aspectj.weaver.UnresolvedType;
  22. import org.aspectj.weaver.VersionedDataInputStream;
  23. import org.aspectj.weaver.WeaverMessages;
  24. import org.aspectj.weaver.World;
  25. public class BindingAnnotationTypePattern extends ExactAnnotationTypePattern implements BindingPattern {
  26. protected int formalIndex;
  27. /**
  28. * @param annotationType
  29. */
  30. public BindingAnnotationTypePattern(UnresolvedType annotationType, int index) {
  31. super(annotationType, null);
  32. this.formalIndex = index;
  33. }
  34. public BindingAnnotationTypePattern(FormalBinding binding) {
  35. this(binding.getType(), binding.getIndex());
  36. }
  37. public void resolveBinding(World world) {
  38. if (resolved) {
  39. return;
  40. }
  41. resolved = true;
  42. annotationType = annotationType.resolve(world);
  43. ResolvedType resolvedAnnotationType = (ResolvedType) annotationType;
  44. if (!resolvedAnnotationType.isAnnotation()) {
  45. IMessage m = MessageUtil.error(WeaverMessages.format(WeaverMessages.REFERENCE_TO_NON_ANNOTATION_TYPE, annotationType
  46. .getName()), getSourceLocation());
  47. world.getMessageHandler().handleMessage(m);
  48. resolved = false;
  49. }
  50. if (annotationType.isTypeVariableReference()) {
  51. return; // we'll deal with this next check when the type var is actually bound...
  52. }
  53. verifyRuntimeRetention(world, resolvedAnnotationType);
  54. }
  55. private void verifyRuntimeRetention(World world, ResolvedType resolvedAnnotationType) {
  56. if (!resolvedAnnotationType.isAnnotationWithRuntimeRetention()) { // default is class visibility
  57. // default is class visibility
  58. IMessage m = MessageUtil.error(WeaverMessages.format(WeaverMessages.BINDING_NON_RUNTIME_RETENTION_ANNOTATION,
  59. annotationType.getName()), getSourceLocation());
  60. world.getMessageHandler().handleMessage(m);
  61. resolved = false;
  62. }
  63. }
  64. public AnnotationTypePattern parameterizeWith(Map typeVariableMap, World w) {
  65. UnresolvedType newAnnotationType = annotationType;
  66. if (annotationType.isTypeVariableReference()) {
  67. TypeVariableReference t = (TypeVariableReference) annotationType;
  68. String key = t.getTypeVariable().getName();
  69. if (typeVariableMap.containsKey(key)) {
  70. newAnnotationType = (UnresolvedType) typeVariableMap.get(key);
  71. }
  72. } else if (annotationType.isParameterizedType()) {
  73. newAnnotationType = annotationType.parameterize(typeVariableMap);
  74. }
  75. BindingAnnotationTypePattern ret = new BindingAnnotationTypePattern(newAnnotationType, this.formalIndex);
  76. if (newAnnotationType instanceof ResolvedType) {
  77. ResolvedType rat = (ResolvedType) newAnnotationType;
  78. verifyRuntimeRetention(rat.getWorld(), rat);
  79. }
  80. ret.copyLocationFrom(this);
  81. return ret;
  82. }
  83. public int getFormalIndex() {
  84. return formalIndex;
  85. }
  86. public boolean equals(Object obj) {
  87. if (!(obj instanceof BindingAnnotationTypePattern)) {
  88. return false;
  89. }
  90. BindingAnnotationTypePattern btp = (BindingAnnotationTypePattern) obj;
  91. return (super.equals(btp) && (btp.formalIndex == formalIndex));
  92. }
  93. public int hashCode() {
  94. return super.hashCode() * 37 + formalIndex;
  95. }
  96. public AnnotationTypePattern remapAdviceFormals(IntMap bindings) {
  97. if (!bindings.hasKey(formalIndex)) {
  98. return new ExactAnnotationTypePattern(annotationType, null);
  99. } else {
  100. int newFormalIndex = bindings.get(formalIndex);
  101. return new BindingAnnotationTypePattern(annotationType, newFormalIndex);
  102. }
  103. }
  104. private static final byte VERSION = 1; // rev if serialised form changed
  105. @Override
  106. public void write(CompressingDataOutputStream s) throws IOException {
  107. s.writeByte(AnnotationTypePattern.BINDING);
  108. s.writeByte(VERSION);
  109. annotationType.write(s);
  110. s.writeShort((short) formalIndex);
  111. writeLocation(s);
  112. }
  113. public static AnnotationTypePattern read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  114. byte version = s.readByte();
  115. if (version > VERSION) {
  116. throw new BCException("BindingAnnotationTypePattern was written by a more recent version of AspectJ");
  117. }
  118. AnnotationTypePattern ret = new BindingAnnotationTypePattern(UnresolvedType.read(s), s.readShort());
  119. ret.readLocation(context, s);
  120. return ret;
  121. }
  122. }