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 4.0KB

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