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.

PerTypeWithinTargetTypeMunger.java 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* *******************************************************************
  2. * Copyright (c) 2005 IBM, 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;
  13. import java.io.IOException;
  14. import org.aspectj.util.FuzzyBoolean;
  15. import org.aspectj.weaver.patterns.PerTypeWithin;
  16. import org.aspectj.weaver.patterns.Pointcut;
  17. // PTWIMPL Target type munger adds the localAspectOf() method
  18. public class PerTypeWithinTargetTypeMunger extends ResolvedTypeMunger {
  19. private UnresolvedType aspectType;
  20. private PerTypeWithin testPointcut;
  21. public PerTypeWithinTargetTypeMunger(UnresolvedType aspectType, PerTypeWithin testPointcut) {
  22. super(PerTypeWithinInterface, null);
  23. this.aspectType = aspectType;
  24. this.testPointcut = testPointcut;
  25. }
  26. public boolean equals(Object other) {
  27. if (!(other instanceof PerTypeWithinTargetTypeMunger)) {
  28. return false;
  29. }
  30. PerTypeWithinTargetTypeMunger o = (PerTypeWithinTargetTypeMunger) other;
  31. return ((o.testPointcut == null) ? (testPointcut == null) : testPointcut.equals(o.testPointcut))
  32. && ((o.aspectType == null) ? (aspectType == null) : aspectType.equals(o.aspectType));
  33. }
  34. private volatile int hashCode = 0;
  35. public int hashCode() {
  36. if (hashCode == 0) {
  37. int result = 17;
  38. result = 37 * result + ((testPointcut == null) ? 0 : testPointcut.hashCode());
  39. result = 37 * result + ((aspectType == null) ? 0 : aspectType.hashCode());
  40. hashCode = result;
  41. }
  42. return hashCode;
  43. }
  44. public void write(CompressingDataOutputStream s) throws IOException {
  45. throw new RuntimeException("shouldn't be serialized");
  46. }
  47. public UnresolvedType getAspectType() {
  48. return aspectType;
  49. }
  50. public Pointcut getTestPointcut() {
  51. return testPointcut;
  52. }
  53. // This is a lexical within() so if you say PerTypeWithin(Test) and matchType is an
  54. // inner type (e.g. Test$NestedType) then it should match successfully
  55. // Does not match if the target is an interface
  56. public boolean matches(ResolvedType matchType, ResolvedType aspectType) {
  57. return isWithinType(matchType).alwaysTrue() && !matchType.isInterface();
  58. }
  59. private FuzzyBoolean isWithinType(ResolvedType type) {
  60. while (type != null) {
  61. if (testPointcut.getTypePattern().matchesStatically(type)) {
  62. return FuzzyBoolean.YES;
  63. }
  64. type = type.getDeclaringType();
  65. }
  66. return FuzzyBoolean.NO;
  67. }
  68. }