Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ArgsPointcut.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Common Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/cpl-v10.html
  8. *
  9. * Contributors:
  10. * Xerox/PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.patterns;
  13. import java.io.*;
  14. import org.aspectj.bridge.IMessage;
  15. import org.aspectj.util.FuzzyBoolean;
  16. import org.aspectj.weaver.*;
  17. import org.aspectj.weaver.ast.*;
  18. /**
  19. * args(arguments)
  20. *
  21. * @author Erik Hilsdale
  22. * @author Jim Hugunin
  23. */
  24. public class ArgsPointcut extends NameBindingPointcut {
  25. TypePatternList arguments;
  26. public ArgsPointcut(TypePatternList arguments) {
  27. this.arguments = arguments;
  28. }
  29. public FuzzyBoolean match(Shadow shadow) {
  30. int n = shadow.getArgCount();
  31. TypeX[] argTypes = new TypeX[n];
  32. for (int i=0; i < n; i++) {
  33. argTypes[i] = shadow.getArgType(i);
  34. }
  35. FuzzyBoolean ret =
  36. arguments.matches(shadow.getIWorld().resolve(argTypes), TypePattern.DYNAMIC);
  37. return ret;
  38. }
  39. public void write(DataOutputStream s) throws IOException {
  40. s.writeByte(Pointcut.ARGS);
  41. arguments.write(s);
  42. writeLocation(s);
  43. }
  44. public static Pointcut read(DataInputStream s, ISourceContext context) throws IOException {
  45. ArgsPointcut ret = new ArgsPointcut(TypePatternList.read(s, context));
  46. ret.readLocation(context, s);
  47. return ret;
  48. }
  49. public boolean equals(Object other) {
  50. if (!(other instanceof ArgsPointcut)) return false;
  51. ArgsPointcut o = (ArgsPointcut)other;
  52. return o.arguments.equals(this.arguments);
  53. }
  54. public int hashCode() {
  55. return arguments.hashCode();
  56. }
  57. public void resolveBindings(IScope scope, Bindings bindings) {
  58. arguments.resolveBindings(scope, bindings, true);
  59. if (arguments.ellipsisCount > 1) {
  60. scope.message(IMessage.ERROR, this,
  61. "uses more than one .. in args (compiler limitation)");
  62. }
  63. }
  64. public void postRead(ResolvedTypeX enclosingType) {
  65. arguments.postRead(enclosingType);
  66. }
  67. public Pointcut concretize1(ResolvedTypeX inAspect, IntMap bindings) {
  68. return new ArgsPointcut(arguments.resolveReferences(bindings));
  69. }
  70. private Test findResidueNoEllipsis(Shadow shadow, ExposedState state, TypePattern[] patterns) {
  71. int len = shadow.getArgCount();
  72. //System.err.println("boudn to : " + len + ", " + patterns.length);
  73. if (patterns.length != len) {
  74. throw new RuntimeException("this should never happen");
  75. //return Literal.FALSE; //??? this should never happen
  76. }
  77. Test ret = Literal.TRUE;
  78. for (int i=0; i < len; i++) {
  79. TypeX argType = shadow.getArgType(i);
  80. TypePattern type = patterns[i];
  81. if (!(type instanceof BindingTypePattern)) {
  82. if (type.matchesInstanceof(shadow.getIWorld().resolve(argType)).alwaysTrue()) {
  83. continue;
  84. }
  85. }
  86. ret = Test.makeAnd(ret,
  87. exposeStateForVar(shadow.getArgVar(i), type, state,shadow.getIWorld()));
  88. }
  89. return ret;
  90. }
  91. public Test findResidue(Shadow shadow, ExposedState state) {
  92. int ellipsisCount = arguments.ellipsisCount;
  93. if (ellipsisCount == 0) {
  94. return findResidueNoEllipsis(shadow, state, arguments.getTypePatterns());
  95. } else if (ellipsisCount == 1) {
  96. TypePattern[] patternsWithEllipsis = arguments.getTypePatterns();
  97. TypePattern[] patternsWithoutEllipsis = new TypePattern[shadow.getArgCount()];
  98. int lenWithEllipsis = patternsWithEllipsis.length;
  99. int lenWithoutEllipsis = patternsWithoutEllipsis.length;
  100. // l1+1 >= l0
  101. int indexWithEllipsis = 0;
  102. int indexWithoutEllipsis = 0;
  103. while (indexWithoutEllipsis < lenWithoutEllipsis) {
  104. TypePattern p = patternsWithEllipsis[indexWithEllipsis++];
  105. if (p == TypePattern.ELLIPSIS) {
  106. int newLenWithoutEllipsis =
  107. lenWithoutEllipsis - (lenWithEllipsis-indexWithEllipsis);
  108. while (indexWithoutEllipsis < newLenWithoutEllipsis) {
  109. patternsWithoutEllipsis[indexWithoutEllipsis++] = TypePattern.ANY;
  110. }
  111. } else {
  112. patternsWithoutEllipsis[indexWithoutEllipsis++] = p;
  113. }
  114. }
  115. return findResidueNoEllipsis(shadow, state, patternsWithoutEllipsis);
  116. } else {
  117. throw new BetaException("unimplemented");
  118. }
  119. }
  120. public String toString() {
  121. return "args" + arguments.toString() + "";
  122. }
  123. }