From 9cf956c2838cbb4f5fd3ad46064e579df3752456 Mon Sep 17 00:00:00 2001 From: Alexander Kriegisch Date: Sun, 15 Jan 2023 01:05:47 +0100 Subject: [PATCH] Improve array matching for all TypePattern subclasses Relates to #24. Signed-off-by: Alexander Kriegisch --- .../weaver/patterns/AndTypePattern.java | 5 ++++ .../weaver/patterns/AnyTypePattern.java | 5 ++++ .../AnyWithAnnotationTypePattern.java | 5 ++++ .../weaver/patterns/EllipsisTypePattern.java | 6 +++++ .../weaver/patterns/ExactTypePattern.java | 5 ++++ .../weaver/patterns/HasMemberTypePattern.java | 6 +++++ .../weaver/patterns/NoTypePattern.java | 5 ++++ .../weaver/patterns/NotTypePattern.java | 5 ++++ .../weaver/patterns/OrTypePattern.java | 5 ++++ .../weaver/patterns/SignaturePattern.java | 2 +- .../patterns/TypeCategoryTypePattern.java | 5 ++++ .../aspectj/weaver/patterns/TypePattern.java | 4 +--- .../weaver/patterns/WildTypePattern.java | 23 +++++++------------ 13 files changed, 62 insertions(+), 19 deletions(-) diff --git a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/AndTypePattern.java b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/AndTypePattern.java index 83004d468..e4099a692 100644 --- a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/AndTypePattern.java +++ b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/AndTypePattern.java @@ -68,6 +68,11 @@ public class AndTypePattern extends TypePattern { return left.matchesStatically(type) && right.matchesStatically(type); } + @Override + protected boolean matchesArray(UnresolvedType type) { + return left.matchesArray(type) && right.matchesArray(type); + } + @Override public void setIsVarArgs(boolean isVarArgs) { this.isVarArgs = isVarArgs; diff --git a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/AnyTypePattern.java b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/AnyTypePattern.java index 2f71e7312..4201f3cfb 100644 --- a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/AnyTypePattern.java +++ b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/AnyTypePattern.java @@ -60,6 +60,11 @@ public class AnyTypePattern extends TypePattern { return FuzzyBoolean.YES; } + @Override + protected boolean matchesArray(UnresolvedType type) { + return true; + } + @Override public void write(CompressingDataOutputStream s) throws IOException { s.writeByte(ANY_KEY); diff --git a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/AnyWithAnnotationTypePattern.java b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/AnyWithAnnotationTypePattern.java index eafc0ebc1..f0583ffcf 100644 --- a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/AnyWithAnnotationTypePattern.java +++ b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/AnyWithAnnotationTypePattern.java @@ -82,6 +82,11 @@ public class AnyWithAnnotationTypePattern extends TypePattern { return FuzzyBoolean.MAYBE; } + @Override + protected boolean matchesArray(UnresolvedType type) { + return true; + } + @Override public TypePattern parameterizeWith(Map typeVariableMap, World w) { AnyWithAnnotationTypePattern ret = new AnyWithAnnotationTypePattern(this.annotationPattern.parameterizeWith( diff --git a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/EllipsisTypePattern.java b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/EllipsisTypePattern.java index 745edb9f1..34df30df5 100644 --- a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/EllipsisTypePattern.java +++ b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/EllipsisTypePattern.java @@ -17,6 +17,7 @@ import java.util.Map; import org.aspectj.util.FuzzyBoolean; import org.aspectj.weaver.CompressingDataOutputStream; import org.aspectj.weaver.ResolvedType; +import org.aspectj.weaver.UnresolvedType; import org.aspectj.weaver.World; public class EllipsisTypePattern extends TypePattern { @@ -59,6 +60,11 @@ public class EllipsisTypePattern extends TypePattern { return FuzzyBoolean.NO; } + @Override + protected boolean matchesArray(UnresolvedType type) { + return false; + } + @Override public void write(CompressingDataOutputStream s) throws IOException { s.writeByte(ELLIPSIS_KEY); 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 9cd6ef6e2..6a747a3ec 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 @@ -177,6 +177,11 @@ public class ExactTypePattern extends TypePattern { return (typeMatch && annMatch); } + @Override + protected boolean matchesArray(UnresolvedType type) { + return type.getDimensions() == getDimensions(); + } + public UnresolvedType getType() { return type; } diff --git a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/HasMemberTypePattern.java b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/HasMemberTypePattern.java index 84730b900..be09f77ba 100644 --- a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/HasMemberTypePattern.java +++ b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/HasMemberTypePattern.java @@ -26,6 +26,7 @@ import org.aspectj.weaver.ISourceContext; import org.aspectj.weaver.Member; import org.aspectj.weaver.ResolvedMember; import org.aspectj.weaver.ResolvedType; +import org.aspectj.weaver.UnresolvedType; import org.aspectj.weaver.VersionedDataInputStream; import org.aspectj.weaver.WeaverMessages; import org.aspectj.weaver.World; @@ -121,6 +122,11 @@ public class HasMemberTypePattern extends TypePattern { return matchesExactly(type); } + @Override + protected boolean matchesArray(UnresolvedType type) { + return true; + } + @Override public FuzzyBoolean matchesInstanceof(ResolvedType type) { throw new UnsupportedOperationException("hasmethod/field do not support instanceof matching"); diff --git a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/NoTypePattern.java b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/NoTypePattern.java index aac64a1d6..585bf34d1 100644 --- a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/NoTypePattern.java +++ b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/NoTypePattern.java @@ -49,6 +49,11 @@ public class NoTypePattern extends TypePattern { return false; } + @Override + protected boolean matchesArray(UnresolvedType type) { + return false; + } + /** * @see org.aspectj.weaver.patterns.TypePattern#matchesInstanceof(ResolvedType) */ diff --git a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/NotTypePattern.java b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/NotTypePattern.java index dca24c8f3..b5c5aee5a 100644 --- a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/NotTypePattern.java +++ b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/NotTypePattern.java @@ -68,6 +68,11 @@ public class NotTypePattern extends TypePattern { return (!negatedPattern.matchesExactly(type, annotatedType) && annotationPattern.matches(annotatedType).alwaysTrue()); } + @Override + protected boolean matchesArray(UnresolvedType type) { + return !negatedPattern.matchesArray(type); + } + @Override public boolean matchesStatically(ResolvedType type) { return !negatedPattern.matchesStatically(type); diff --git a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/OrTypePattern.java b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/OrTypePattern.java index 4d0f9157f..627c4a7d0 100644 --- a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/OrTypePattern.java +++ b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/OrTypePattern.java @@ -73,6 +73,11 @@ public class OrTypePattern extends TypePattern { return left.matchesExactly(type, annotatedType) || right.matchesExactly(type, annotatedType); } + @Override + protected boolean matchesArray(UnresolvedType type) { + return left.matchesArray(type) || right.matchesArray(type); + } + public boolean matchesStatically(ResolvedType type) { return left.matchesStatically(type) || right.matchesStatically(type); } 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 5b2e021c1..4285da575 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,7 +465,7 @@ 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()) { + if (!returnType.matchesArray(aMethod.getReturnType())) { return FuzzyBoolean.NO; } if (parametersCannotMatch(aMethod)) { diff --git a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/TypeCategoryTypePattern.java b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/TypeCategoryTypePattern.java index df8345e96..1f98d074a 100644 --- a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/TypeCategoryTypePattern.java +++ b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/TypeCategoryTypePattern.java @@ -67,6 +67,11 @@ public class TypeCategoryTypePattern extends TypePattern { return isRightCategory(type); } + @Override + protected boolean matchesArray(UnresolvedType type) { + return true; + } + @Override public FuzzyBoolean matchesInstanceof(ResolvedType type) { return FuzzyBoolean.fromBoolean(isRightCategory(type)); 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 8554e28a1..b0e058d45 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 @@ -163,9 +163,7 @@ public abstract class TypePattern extends PatternNode { protected abstract boolean matchesExactly(ResolvedType type, ResolvedType annotatedType); - protected boolean matchesArray(ResolvedType type) { - return type.getDimensions() == getDimensions(); - } + protected abstract boolean matchesArray(UnresolvedType type); protected boolean matchesSubtypes(ResolvedType type) { // System.out.println("matching: " + this + " to " + type); 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 512b72b26..e43b6ca94 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 @@ -233,7 +233,7 @@ public class WildTypePattern extends TypePattern { // Ensure the annotation pattern is resolved annotationPattern.resolve(type.getWorld()); - return matchesExactlyByName(targetTypeName, type.isAnonymous(), type.isNested()) && matchesParameters(type, STATIC) + return matchesExactlyByName(targetTypeName.replaceFirst("(\\[\\])+$", ""), type.isAnonymous(), type.isNested()) && matchesParameters(type, STATIC) && matchesArray(type) && matchesBounds(type, STATIC) && annotationPattern.matches(annotatedType, type.temporaryAnnotationTypes).alwaysTrue(); @@ -252,6 +252,12 @@ public class WildTypePattern extends TypePattern { return true; } + @Override + protected boolean matchesArray(UnresolvedType type) { + return type.getDimensions() == getDimensions() || + getDimensions() == 0 && namePatterns.length > 0 && namePatterns[namePatterns.length-1].toString().endsWith("*"); + } + // we've matched against the base (or raw) type, but if this type pattern specifies bounds because // it is a ? extends or ? super deal then we have to match them too. private boolean matchesBounds(ResolvedType aType, MatchKind staticOrDynamic) { @@ -318,19 +324,6 @@ public class WildTypePattern extends TypePattern { return innerMatchesExactly(targetTypeName, isAnonymous, isNested); } - if (isNamePatternStar()) { - // we match if the dimensions match - int numDimensionsInTargetType = 0; - if (dim > 0) { - int index; - while ((index = targetTypeName.indexOf('[')) != -1) { - numDimensionsInTargetType++; - targetTypeName = targetTypeName.substring(index + 1); - } - return numDimensionsInTargetType == dim; - } - } - // if our pattern is length 1, then known matches are exact matches // if it's longer than that, then known matches are prefixes of a sort if (namePatterns.length == 1) { @@ -369,7 +362,7 @@ public class WildTypePattern extends TypePattern { } } - return innerMatchesExactly(targetTypeName.substring(0, targetTypeName.length() - dim * 2), isAnonymous, isNested); + return innerMatchesExactly(targetTypeName, isAnonymous, isNested); } private int lastIndexOfDotOrDollar(String string) { -- 2.39.5