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.

NamePattern.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.patterns;
  13. import java.io.DataOutputStream;
  14. import java.io.IOException;
  15. import org.aspectj.weaver.VersionedDataInputStream;
  16. public class NamePattern extends PatternNode {
  17. char[] pattern;
  18. int starCount = 0;
  19. public static final NamePattern ELLIPSIS = new NamePattern("");
  20. public static final NamePattern ANY = new NamePattern("*");
  21. public NamePattern(String name) {
  22. this(name.toCharArray());
  23. }
  24. public NamePattern(char[] pattern) {
  25. this.pattern = pattern;
  26. for (int i=0, len=pattern.length; i<len; i++) {
  27. if (pattern[i] == '*') starCount++;
  28. }
  29. }
  30. public boolean matches(char[] a2) {
  31. char[] a1 = pattern;
  32. int len1 = a1.length;
  33. int len2 = a2.length;
  34. if (starCount == 0) {
  35. if (len1 != len2) return false;
  36. for (int i=0; i<len1; i++) {
  37. if (a1[i] != a2[i]) return false;
  38. }
  39. return true;
  40. } else if (starCount == 1) {
  41. // just '*' matches anything
  42. if (len1 == 1) return true;
  43. if (len1 > len2+1) return false;
  44. int i2=0;
  45. for (int i1=0; i1<len1; i1++) {
  46. char c1 = a1[i1];
  47. if (c1 == '*') {
  48. i2 = len2 - (len1-(i1+1));
  49. } else if (c1 != a2[i2++]) {
  50. return false;
  51. }
  52. }
  53. return true;
  54. } else {
  55. // String pattern = new String(a1);
  56. // String target = new String(a2);
  57. // System.err.print("match(\"" + pattern + "\", \"" + target + "\") -> ");
  58. boolean b = outOfStar(a1, a2, 0, 0, len1 - starCount, len2, starCount);
  59. // System.err.println(b);
  60. return b;
  61. }
  62. }
  63. private static boolean outOfStar(final char[] pattern, final char[] target,
  64. int pi, int ti,
  65. int pLeft, int tLeft,
  66. final int starsLeft) {
  67. if (pLeft > tLeft) return false;
  68. while (true) {
  69. // invariant: if (tLeft > 0) then (ti < target.length && pi < pattern.length)
  70. if (tLeft == 0) return true;
  71. if (pLeft == 0) {
  72. return (starsLeft > 0);
  73. }
  74. if (pattern[pi] == '*') {
  75. return inStar(pattern, target, pi+1, ti, pLeft, tLeft, starsLeft-1);
  76. }
  77. if (target[ti] != pattern[pi]) {
  78. return false;
  79. }
  80. pi++; ti++; pLeft--; tLeft--;
  81. }
  82. }
  83. private static boolean inStar(final char[] pattern, final char[] target,
  84. int pi, int ti,
  85. final int pLeft, int tLeft,
  86. int starsLeft) {
  87. // invariant: pLeft > 0, so we know we'll run out of stars and find a real char in pattern
  88. char patternChar = pattern[pi];
  89. while (patternChar == '*') {
  90. starsLeft--;
  91. patternChar = pattern[++pi];
  92. }
  93. while (true) {
  94. // invariant: if (tLeft > 0) then (ti < target.length)
  95. if (pLeft > tLeft) return false;
  96. if (target[ti] == patternChar) {
  97. if (outOfStar(pattern, target, pi+1, ti+1, pLeft-1, tLeft-1, starsLeft)) return true;
  98. }
  99. ti++; tLeft--;
  100. }
  101. }
  102. public boolean matches(String other) {
  103. return matches(other.toCharArray());
  104. }
  105. public String toString() {
  106. return new String(pattern);
  107. }
  108. public boolean equals(Object other) {
  109. if (other instanceof NamePattern) {
  110. NamePattern otherPat = (NamePattern)other;
  111. return otherPat.starCount == this.starCount &&
  112. new String(otherPat.pattern).equals(new String(this.pattern));
  113. }
  114. return false;
  115. }
  116. public int hashCode() {
  117. return new String(pattern).hashCode();
  118. }
  119. public void write(DataOutputStream out) throws IOException {
  120. out.writeUTF(new String(pattern));
  121. }
  122. public static NamePattern read(VersionedDataInputStream in) throws IOException {
  123. String s = in.readUTF();
  124. if (s.length() == 0) return ELLIPSIS;
  125. return new NamePattern(s);
  126. }
  127. /**
  128. * Method maybeGetSimpleName.
  129. * @return String
  130. */
  131. public String maybeGetSimpleName() {
  132. if (starCount == 0 && pattern.length > 0) return new String(pattern);
  133. return null;
  134. }
  135. /**
  136. * Method isAny.
  137. * @return boolean
  138. */
  139. public boolean isAny() {
  140. return starCount == 1 && pattern.length == 1;
  141. }
  142. }