diff options
author | Alexander Kriegisch <Alexander@Kriegisch.name> | 2023-01-09 18:03:50 +0100 |
---|---|---|
committer | Alexander Kriegisch <Alexander@Kriegisch.name> | 2023-01-15 14:41:51 +0100 |
commit | 6e79467e0a24c9e903d0d06948615b6fe9250efa (patch) | |
tree | 68bdea953bbd3f30895ef4d1072c40fdc35cd017 | |
parent | a0bcb6ba4b4b81eef4fc95d949cd81538b17aa8f (diff) | |
download | aspectj-6e79467e0a24c9e903d0d06948615b6fe9250efa.tar.gz aspectj-6e79467e0a24c9e903d0d06948615b6fe9250efa.zip |
Handle one- and multi-dimensional array return types correctly
Fixes https://github.com/eclipse/org.aspectj/issues/24, both the array
return type matching as such as well as matching dimensionality patterns
correctly. E.g., 'Foo*[]' is not the same as 'Foo*[][]'. This also works
correctly in combination with asterisks, even for primitive types, i.e.
'in*[][]' correctly matches a 2-dimensional array of 'int'.
Signed-off-by: Alexander Kriegisch <Alexander@Kriegisch.name>
5 files changed, 33 insertions, 6 deletions
diff --git a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/UnresolvedType.java b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/UnresolvedType.java index 16e3b9879..d0611868f 100644 --- a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/UnresolvedType.java +++ b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/UnresolvedType.java @@ -161,6 +161,10 @@ public class UnresolvedType implements Traceable, TypeVariableDeclaringElement { return signature.length() > 0 && signature.charAt(0) == '['; } + public final int getDimensions() { + return signature.replaceAll("^(\\[*).*", "$1").length(); + } + /** * Equality is checked based on the underlying signature. */ @@ -169,7 +173,8 @@ public class UnresolvedType implements Traceable, TypeVariableDeclaringElement { if (!(other instanceof UnresolvedType)) { return false; } - return signature.equals(((UnresolvedType) other).signature); + final UnresolvedType unresolvedOther = (UnresolvedType) other; + return signature.equals(unresolvedOther.signature) && getDimensions() == unresolvedOther.getDimensions(); } /** @@ -178,7 +183,10 @@ public class UnresolvedType implements Traceable, TypeVariableDeclaringElement { */ @Override public int hashCode() { - return signature.hashCode(); + int result = 17; + result = 37 * result + signature.hashCode(); + result = 37 * result + getDimensions(); + return result; } protected UnresolvedType(String signature) { diff --git a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/ExactTypePattern.java b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/ExactTypePattern.java index bedbc4732..9cd6ef6e2 100644 --- a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/ExactTypePattern.java +++ b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/ExactTypePattern.java @@ -95,6 +95,10 @@ public class ExactTypePattern extends TypePattern { return type.isArray(); } + public int getDimensions() { + return type.getDimensions(); + } + /* * (non-Javadoc) * @@ -122,6 +126,9 @@ public class ExactTypePattern extends TypePattern { @Override protected boolean matchesExactly(ResolvedType matchType) { + if (!matchesArray(matchType)) { + return false; + } boolean typeMatch = this.type.equals(matchType); if (!typeMatch && (matchType.isParameterizedType() || matchType.isGenericType())) { typeMatch = this.type.equals(matchType.getRawType()); @@ -150,6 +157,9 @@ public class ExactTypePattern extends TypePattern { @Override protected boolean matchesExactly(ResolvedType matchType, ResolvedType annotatedType) { + if (!matchesArray(matchType)) { + return false; + } boolean typeMatch = this.type.equals(matchType); if (!typeMatch && (matchType.isParameterizedType() || matchType.isGenericType())) { typeMatch = this.type.equals(matchType.getRawType()); diff --git a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/SignaturePattern.java b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/SignaturePattern.java index 250d125b8..5b2e021c1 100644 --- a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/SignaturePattern.java +++ b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/SignaturePattern.java @@ -465,6 +465,9 @@ public class SignaturePattern extends PatternNode implements ISignaturePattern { * Matches on name, declaring type, return type, parameter types, throws types */ private FuzzyBoolean matchesExactlyMethod(JoinPointSignature aMethod, World world, boolean subjectMatch) { + if (aMethod.getReturnType().getDimensions() != returnType.getDimensions()) { + return FuzzyBoolean.NO; + } if (parametersCannotMatch(aMethod)) { // System.err.println("Parameter types pattern " + parameterTypes + " pcount: " + aMethod.getParameterTypes().length); return FuzzyBoolean.NO; diff --git a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/TypePattern.java b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/TypePattern.java index 8deea5abf..8554e28a1 100644 --- a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/TypePattern.java +++ b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/TypePattern.java @@ -90,6 +90,10 @@ public abstract class TypePattern extends PatternNode { return false; } + public int getDimensions() { + return 0; + } + protected TypePattern(boolean includeSubtypes) { this(includeSubtypes, false); } @@ -159,6 +163,10 @@ public abstract class TypePattern extends PatternNode { protected abstract boolean matchesExactly(ResolvedType type, ResolvedType annotatedType); + protected boolean matchesArray(ResolvedType type) { + return type.getDimensions() == getDimensions(); + } + protected boolean matchesSubtypes(ResolvedType type) { // System.out.println("matching: " + this + " to " + type); if (matchesExactly(type)) { @@ -355,6 +363,3 @@ public abstract class TypePattern extends PatternNode { } } - - - diff --git a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/WildTypePattern.java b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/WildTypePattern.java index 37653a9cc..512b72b26 100644 --- a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/WildTypePattern.java +++ b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/WildTypePattern.java @@ -234,6 +234,7 @@ public class WildTypePattern extends TypePattern { annotationPattern.resolve(type.getWorld()); return matchesExactlyByName(targetTypeName, type.isAnonymous(), type.isNested()) && matchesParameters(type, STATIC) + && matchesArray(type) && matchesBounds(type, STATIC) && annotationPattern.matches(annotatedType, type.temporaryAnnotationTypes).alwaysTrue(); } @@ -368,7 +369,7 @@ public class WildTypePattern extends TypePattern { } } - return innerMatchesExactly(targetTypeName, isAnonymous, isNested); + return innerMatchesExactly(targetTypeName.substring(0, targetTypeName.length() - dim * 2), isAnonymous, isNested); } private int lastIndexOfDotOrDollar(String string) { |