Skip to content

Commit

Permalink
Improve array matching for all TypePattern subclasses
Browse files Browse the repository at this point in the history
Relates to #24.

Signed-off-by: Alexander Kriegisch <Alexander@Kriegisch.name>
  • Loading branch information
kriegaex committed Jan 15, 2023
1 parent 6e79467 commit 9cf956c
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public boolean matchesStatically(ResolvedType type) {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public FuzzyBoolean matchesInstanceof(ResolvedType type) {
return FuzzyBoolean.YES;
}

@Override
protected boolean matchesArray(UnresolvedType type) {
return true;
}

@Override
public void write(CompressingDataOutputStream s) throws IOException {
s.writeByte(ANY_KEY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ public FuzzyBoolean matchesInstanceof(ResolvedType type) {
return FuzzyBoolean.MAYBE;
}

@Override
protected boolean matchesArray(UnresolvedType type) {
return true;
}

@Override
public TypePattern parameterizeWith(Map<String,UnresolvedType> typeVariableMap, World w) {
AnyWithAnnotationTypePattern ret = new AnyWithAnnotationTypePattern(this.annotationPattern.parameterizeWith(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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 {
Expand Down Expand Up @@ -59,6 +60,11 @@ public FuzzyBoolean matchesInstanceof(ResolvedType type) {
return FuzzyBoolean.NO;
}

@Override
protected boolean matchesArray(UnresolvedType type) {
return false;
}

@Override
public void write(CompressingDataOutputStream s) throws IOException {
s.writeByte(ELLIPSIS_KEY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ protected boolean matchesExactly(ResolvedType matchType, ResolvedType annotatedT
return (typeMatch && annMatch);
}

@Override
protected boolean matchesArray(UnresolvedType type) {
return type.getDimensions() == getDimensions();
}

public UnresolvedType getType() {
return type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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;
Expand Down Expand Up @@ -121,6 +122,11 @@ protected boolean matchesExactly(ResolvedType type, ResolvedType annotatedType)
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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ protected boolean matchesExactly(ResolvedType type, ResolvedType annotatedType)
return false;
}

@Override
protected boolean matchesArray(UnresolvedType type) {
return false;
}

/**
* @see org.aspectj.weaver.patterns.TypePattern#matchesInstanceof(ResolvedType)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ protected boolean matchesExactly(ResolvedType type, ResolvedType annotatedType)
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ protected boolean matchesExactly(ResolvedType type, ResolvedType annotatedType)
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ private boolean parametersCannotMatch(JoinPointSignature methodJoinpoint) {
* 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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ protected boolean matchesExactly(ResolvedType type, ResolvedType annotatedType)
return isRightCategory(type);
}

@Override
protected boolean matchesArray(UnresolvedType type) {
return true;
}

@Override
public FuzzyBoolean matchesInstanceof(ResolvedType type) {
return FuzzyBoolean.fromBoolean(isRightCategory(type));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,7 @@ public final FuzzyBoolean matches(ResolvedType type, MatchKind kind) {

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ protected boolean matchesExactly(ResolvedType type, ResolvedType annotatedType)
// 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();
Expand All @@ -252,6 +252,12 @@ private boolean matchesParameters(ResolvedType aType, MatchKind staticOrDynamic)
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) {
Expand Down Expand Up @@ -318,19 +324,6 @@ private boolean matchesExactlyByName(String targetTypeName, boolean isAnonymous,
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) {
Expand Down Expand Up @@ -369,7 +362,7 @@ private boolean matchesExactlyByName(String targetTypeName, boolean isAnonymous,
}
}

return innerMatchesExactly(targetTypeName.substring(0, targetTypeName.length() - dim * 2), isAnonymous, isNested);
return innerMatchesExactly(targetTypeName, isAnonymous, isNested);
}

private int lastIndexOfDotOrDollar(String string) {
Expand Down

0 comments on commit 9cf956c

Please sign in to comment.