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.

TypeCategoryTypePattern.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* *******************************************************************
  2. * Copyright (c) 2010 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
  11. * Nieraj Singh
  12. * ******************************************************************/
  13. package org.aspectj.weaver.patterns;
  14. import java.io.IOException;
  15. import java.util.Map;
  16. import org.aspectj.util.FuzzyBoolean;
  17. import org.aspectj.weaver.CompressingDataOutputStream;
  18. import org.aspectj.weaver.ISourceContext;
  19. import org.aspectj.weaver.ResolvedType;
  20. import org.aspectj.weaver.UnresolvedType;
  21. import org.aspectj.weaver.VersionedDataInputStream;
  22. import org.aspectj.weaver.World;
  23. /**
  24. * A TypeCategoryTypePattern matches on the category of a type, one of class/interface/aspect/inner/anonymous/enum/annotation, and
  25. * these are specified in the pointcut via isClass() isInterface() isAspect() isInner() isAnonymous() isEnum() isAnnotation().
  26. *
  27. * @author Andy Clement
  28. * @since 1.6.9
  29. */
  30. public class TypeCategoryTypePattern extends TypePattern {
  31. public static final int CLASS = 1;
  32. public static final int INTERFACE = 2;
  33. public static final int ASPECT = 3;
  34. public static final int INNER = 4;
  35. public static final int ANONYMOUS = 5;
  36. public static final int ENUM = 6;
  37. public static final int ANNOTATION = 7;
  38. private int category;
  39. private int VERSION = 1;
  40. public TypeCategoryTypePattern(int category) {
  41. super(false);
  42. this.category = category;
  43. }
  44. public int getTypeCategory() {
  45. return category;
  46. }
  47. @Override
  48. protected boolean matchesExactly(ResolvedType type) {
  49. return isRightCategory(type);
  50. }
  51. @Override
  52. protected boolean matchesExactly(ResolvedType type, ResolvedType annotatedType) {
  53. return isRightCategory(type);
  54. }
  55. @Override
  56. public FuzzyBoolean matchesInstanceof(ResolvedType type) {
  57. return FuzzyBoolean.fromBoolean(isRightCategory(type));
  58. }
  59. @Override
  60. public TypePattern parameterizeWith(Map<String,UnresolvedType> typeVariableMap, World w) {
  61. return this;
  62. }
  63. @Override
  64. public Object accept(PatternNodeVisitor visitor, Object data) {
  65. return visitor.visit(this, data);
  66. }
  67. @Override
  68. public boolean equals(Object other) {
  69. if (!(other instanceof TypeCategoryTypePattern)) {
  70. return false;
  71. }
  72. TypeCategoryTypePattern o = (TypeCategoryTypePattern) other;
  73. return o.category == category;
  74. }
  75. // TODO is sourcelocation part of the identity or just a 'nice to have' - if important it should be in hashcode/equals
  76. // TODO but if that is the case it needs addressing for all type patterns
  77. @Override
  78. public int hashCode() {
  79. return category * 37;
  80. }
  81. @Override
  82. public void write(CompressingDataOutputStream s) throws IOException {
  83. s.writeByte(TypePattern.TYPE_CATEGORY);
  84. s.writeInt(VERSION);
  85. s.writeInt(category);
  86. writeLocation(s);
  87. }
  88. public static TypePattern read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  89. int version = s.readInt();
  90. int category = s.readInt();
  91. TypeCategoryTypePattern tp = new TypeCategoryTypePattern(category);
  92. tp.readLocation(context, s);
  93. return tp;
  94. }
  95. /**
  96. * @return true if the supplied type is of the category specified for this type pattern
  97. */
  98. private boolean isRightCategory(ResolvedType type) {
  99. switch (category) {
  100. case CLASS:
  101. return type.isClass();
  102. case INTERFACE:
  103. return type.isInterface();
  104. case ASPECT:
  105. return type.isAspect();
  106. case ANONYMOUS:
  107. return type.isAnonymous();
  108. case INNER:
  109. return type.isNested();
  110. case ENUM:
  111. return type.isEnum();
  112. case ANNOTATION:
  113. return type.isAnnotation();
  114. }
  115. return false;
  116. }
  117. }