]> source.dussan.org Git - aspectj.git/commitdiff
WildTypePattern: fix hashCode and toString methods
authorAlexander Kriegisch <Alexander@Kriegisch.name>
Sun, 15 Jan 2023 00:16:24 +0000 (01:16 +0100)
committerAlexander Kriegisch <Alexander@Kriegisch.name>
Sun, 15 Jan 2023 13:41:51 +0000 (14:41 +0100)
Especially 'hashCode' did not correspond to 'equals', disregarding
several fields, array dimension information being only one of them. This
led to parts of pointcuts being ignored, because they were regarded as
duplicates. Example:

execution(Foo* *(..)) && !execution(Foo*[] *(..))

Here, the negated pattern was falsely regarded as equal to the first
pattern, leading to an "A && !A" situation, i.e. no match at all.

Furthermore, 'toString' did not print array strings, i.e. instead of
"Foo*[][]" something like "Foo*" was printed. This false information was
also present in annotations generated by the weaver.

FuzzilyMatchingAspect was adjusted to actually match exactly once, as
expected, for the "Foo*" return types, i.e. exclusions for the array
return types have been added.

Relates to #24.

Signed-off-by: Alexander Kriegisch <Alexander@Kriegisch.name>
org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/WildTypePattern.java
tests/bugs1919/github_24/FuzzilyMatchingAspect.aj

index e43b6ca941b13846439c4b7df4e3e4db95dc151a..212378bf71ffa13e357360fdd347d7614d4a3c3b 100644 (file)
@@ -1172,7 +1172,7 @@ public class WildTypePattern extends TypePattern {
                StringBuilder buf = new StringBuilder();
                if (annotationPattern != AnnotationTypePattern.ANY) {
                        buf.append('(');
-                       buf.append(annotationPattern.toString());
+                       buf.append(annotationPattern);
                        buf.append(' ');
                }
                for (int i = 0, len = namePatterns.length; i < len; i++) {
@@ -1183,16 +1183,16 @@ public class WildTypePattern extends TypePattern {
                                if (i > 0) {
                                        buf.append(".");
                                }
-                               buf.append(name.toString());
+                               buf.append(name);
                        }
                }
                if (upperBound != null) {
                        buf.append(" extends ");
-                       buf.append(upperBound.toString());
+                       buf.append(upperBound);
                }
                if (lowerBound != null) {
                        buf.append(" super ");
-                       buf.append(lowerBound.toString());
+                       buf.append(lowerBound);
                }
                if (typeParameters != null && typeParameters.size() != 0) {
                        buf.append("<");
@@ -1202,6 +1202,9 @@ public class WildTypePattern extends TypePattern {
                if (includeSubtypes) {
                        buf.append('+');
                }
+               for (int i = 0; i < getDimensions(); i++) {
+                       buf.append("[]");
+               }
                if (isVarArgs) {
                        buf.append("...");
                }
@@ -1271,7 +1274,11 @@ public class WildTypePattern extends TypePattern {
                for (NamePattern namePattern : namePatterns) {
                        result = 37 * result + namePattern.hashCode();
                }
+               result = 37 * result + (includeSubtypes ? 1 : 0);
+               result = 37 * result + dim;
+               result = 37 * result + (isVarArgs ? 1 : 0);
                result = 37 * result + annotationPattern.hashCode();
+               result = 37 * result + typeParameters.hashCode();
                if (upperBound != null) {
                        result = 37 * result + upperBound.hashCode();
                }
index d7c32a70a2a8ed870668ab4a1798df8de373adc7..b40c13c629a20d1b37328f826a64da4d1c6e0ca2 100644 (file)
@@ -1,25 +1,39 @@
 public aspect FuzzilyMatchingAspect {
-  after() : execution(public MaybeMissing* MaybeMissing*.*()) {
+
+  pointcut returnRefTypeSimpleOrArray() : execution(public MaybeMissing* MaybeMissing*.*());
+  pointcut return1DimRefTypeArray() : execution(public MaybeMissing*[] MaybeMissing*.*());
+  pointcut return2DimRefTypeArray() : execution(public MaybeMissing*[][] MaybeMissing*.*());
+
+  // Return type 'MaybeMissing*' also matches array types due to the '*' at the end.
+  // Therefore, explicitly exclude array pointcuts in order to only match the method returning the simple type.
+  after() : returnRefTypeSimpleOrArray() && !return1DimRefTypeArray() && !return2DimRefTypeArray() {
     System.out.println(thisJoinPoint);
   }
 
-  after() : execution(public MaybeMissing*[] MaybeMissing*.*()) {
+  after() : return1DimRefTypeArray() {
     System.out.println(thisJoinPoint);
   }
 
-  after() : execution(public MaybeMissing*[][] MaybeMissing*.*()) {
+  after() : return2DimRefTypeArray() {
     System.out.println(thisJoinPoint);
   }
 
-  after() : execution(public in* MaybeMissing*.*()) {
+  pointcut returnPrimitiveTypeSimpleOrArray() : execution(public in* MaybeMissing*.*());
+  pointcut return1DimPrimitiveTypeArray() : execution(public in*[] MaybeMissing*.*());
+  pointcut return2DimPrimitiveTypeArray() : execution(public in*[][] MaybeMissing*.*());
+
+  // Return type 'in*' also matches array types due to the '*' at the end.
+  // Therefore, explicitly exclude array pointcuts in order to only match the method returning the simple type.
+  after() : returnPrimitiveTypeSimpleOrArray() && !return1DimPrimitiveTypeArray() && !return2DimPrimitiveTypeArray() {
     System.out.println(thisJoinPoint);
   }
 
-  after() : execution(public in*[] MaybeMissing*.*()) {
+  after() : return1DimPrimitiveTypeArray() {
     System.out.println(thisJoinPoint);
   }
 
-  after() : execution(public in*[][] MaybeMissing*.*()) {
+  after() : return2DimPrimitiveTypeArray() {
     System.out.println(thisJoinPoint);
   }
+
 }