Skip to content

Commit

Permalink
Handle one- and multi-dimensional array return types correctly
Browse files Browse the repository at this point in the history
Fixes #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>
  • Loading branch information
kriegaex committed Jan 15, 2023
1 parent a0bcb6b commit 6e79467
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ public final boolean isArray() {
return signature.length() > 0 && signature.charAt(0) == '[';
}

public final int getDimensions() {
return signature.replaceAll("^(\\[*).*", "$1").length();
}

/**
* Equality is checked based on the underlying signature.
*/
Expand All @@ -169,7 +173,8 @@ public boolean equals(Object other) {
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();
}

/**
Expand All @@ -178,7 +183,10 @@ public boolean equals(Object other) {
*/
@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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ public boolean isArray() {
return type.isArray();
}

public int getDimensions() {
return type.getDimensions();
}

/*
* (non-Javadoc)
*
Expand Down Expand Up @@ -122,6 +126,9 @@ protected boolean couldEverMatchSameTypesAs(TypePattern other) {

@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());
Expand Down Expand Up @@ -150,6 +157,9 @@ private boolean matchesTypeVariable(TypeVariableReferenceType matchType) {

@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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,9 @@ 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()) {
return FuzzyBoolean.NO;
}
if (parametersCannotMatch(aMethod)) {
// System.err.println("Parameter types pattern " + parameterTypes + " pcount: " + aMethod.getParameterTypes().length);
return FuzzyBoolean.NO;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ public boolean isArray() {
return false;
}

public int getDimensions() {
return 0;
}

protected TypePattern(boolean includeSubtypes) {
this(includeSubtypes, false);
}
Expand Down Expand Up @@ -159,6 +163,10 @@ 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 boolean matchesSubtypes(ResolvedType type) {
// System.out.println("matching: " + this + " to " + type);
if (matchesExactly(type)) {
Expand Down Expand Up @@ -355,6 +363,3 @@ public boolean hasFailedResolution() {
}

}



Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ protected boolean matchesExactly(ResolvedType type, ResolvedType annotatedType)
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();
}
Expand Down Expand Up @@ -368,7 +369,7 @@ private boolean matchesExactlyByName(String targetTypeName, boolean isAnonymous,
}
}

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

private int lastIndexOfDotOrDollar(String string) {
Expand Down

0 comments on commit 6e79467

Please sign in to comment.