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.

AnyWithAnnotationTypePattern.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* *******************************************************************
  2. * Copyright (c) 2002, 2010 Palo Alto Research Center, Incorporated (PARC) and others.
  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. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.patterns;
  13. import java.io.IOException;
  14. import java.lang.reflect.Modifier;
  15. import java.util.Map;
  16. import org.aspectj.bridge.MessageUtil;
  17. import org.aspectj.util.FuzzyBoolean;
  18. import org.aspectj.weaver.CompressingDataOutputStream;
  19. import org.aspectj.weaver.ISourceContext;
  20. import org.aspectj.weaver.ResolvedType;
  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. /**
  26. * This type represents a type pattern of '*' but with an annotation specified, e.g. '@Color *'
  27. */
  28. public class AnyWithAnnotationTypePattern extends TypePattern {
  29. public AnyWithAnnotationTypePattern(AnnotationTypePattern atp) {
  30. super(false, false);
  31. annotationPattern = atp;
  32. }
  33. @Override
  34. public Object accept(PatternNodeVisitor visitor, Object data) {
  35. return visitor.visit(this, data);
  36. }
  37. @Override
  38. protected boolean couldEverMatchSameTypesAs(TypePattern other) {
  39. return true;
  40. }
  41. @Override
  42. protected boolean matchesExactly(ResolvedType type) {
  43. annotationPattern.resolve(type.getWorld());
  44. boolean b = false;
  45. if (type.temporaryAnnotationTypes != null) {
  46. b = annotationPattern.matches(type, type.temporaryAnnotationTypes).alwaysTrue();
  47. } else {
  48. b = annotationPattern.matches(type).alwaysTrue();
  49. }
  50. return b;
  51. }
  52. @Override
  53. public TypePattern resolveBindings(IScope scope, Bindings bindings, boolean allowBinding, boolean requireExactType) {
  54. if (requireExactType) {
  55. scope.getWorld().getMessageHandler().handleMessage(
  56. MessageUtil.error(WeaverMessages.format(WeaverMessages.WILDCARD_NOT_ALLOWED), getSourceLocation()));
  57. return NO;
  58. }
  59. return super.resolveBindings(scope, bindings, allowBinding, requireExactType);
  60. }
  61. @Override
  62. protected boolean matchesExactly(ResolvedType type, ResolvedType annotatedType) {
  63. annotationPattern.resolve(type.getWorld());
  64. return annotationPattern.matches(annotatedType).alwaysTrue();
  65. }
  66. @Override
  67. public FuzzyBoolean matchesInstanceof(ResolvedType type) {
  68. if (Modifier.isFinal(type.getModifiers())) {
  69. return FuzzyBoolean.fromBoolean(matchesExactly(type));
  70. }
  71. return FuzzyBoolean.MAYBE;
  72. }
  73. @Override
  74. public TypePattern parameterizeWith(Map<String,UnresolvedType> typeVariableMap, World w) {
  75. AnyWithAnnotationTypePattern ret = new AnyWithAnnotationTypePattern(this.annotationPattern.parameterizeWith(
  76. typeVariableMap, w));
  77. ret.copyLocationFrom(this);
  78. return ret;
  79. }
  80. @Override
  81. public void write(CompressingDataOutputStream s) throws IOException {
  82. s.writeByte(TypePattern.ANY_WITH_ANNO);
  83. annotationPattern.write(s);
  84. writeLocation(s);
  85. }
  86. public static TypePattern read(VersionedDataInputStream s, ISourceContext c) throws IOException {
  87. AnnotationTypePattern annPatt = AnnotationTypePattern.read(s, c);
  88. AnyWithAnnotationTypePattern ret = new AnyWithAnnotationTypePattern(annPatt);
  89. ret.readLocation(c, s);
  90. return ret;
  91. }
  92. // public FuzzyBoolean matches(IType type, MatchKind kind) {
  93. // return FuzzyBoolean.YES;
  94. // }
  95. @Override
  96. protected boolean matchesSubtypes(ResolvedType type) {
  97. return true;
  98. }
  99. @Override
  100. public boolean isStar() {
  101. return false;
  102. }
  103. @Override
  104. public String toString() {
  105. return "(" + annotationPattern + " *)";
  106. }
  107. public AnnotationTypePattern getAnnotationTypePattern() {
  108. return annotationPattern;
  109. }
  110. @Override
  111. public boolean equals(Object obj) {
  112. if (!(obj instanceof AnyWithAnnotationTypePattern)) {
  113. return false;
  114. }
  115. AnyWithAnnotationTypePattern awatp = (AnyWithAnnotationTypePattern) obj;
  116. return (annotationPattern.equals(awatp.annotationPattern));
  117. }
  118. @Override
  119. public int hashCode() {
  120. return annotationPattern.hashCode();
  121. }
  122. }