Skip to content

Commit

Permalink
Enable type parameter traversal in exact type patterns
Browse files Browse the repository at this point in the history
Closes #221

Signed-off-by: Alexander Kriegisch <[email protected]>
  • Loading branch information
kriegaex committed Apr 12, 2024
1 parent 4f3e990 commit 97d8f73
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class BindingTypePattern extends ExactTypePattern implements BindingPatte
private String bindingName;

public BindingTypePattern(UnresolvedType type, int index, boolean isVarArgs) {
super(type, false, isVarArgs);
super(type, false, isVarArgs, null);
this.formalIndex = index;
}

Expand Down Expand Up @@ -89,7 +89,7 @@ public static TypePattern read(VersionedDataInputStream s, ISourceContext contex

public TypePattern remapAdviceFormals(IntMap bindings) {
if (!bindings.hasKey(formalIndex)) {
return new ExactTypePattern(type, false, isVarArgs);
return new ExactTypePattern(type, false, isVarArgs, null);
} else {
int newFormalIndex = bindings.get(formalIndex);
return new BindingTypePattern(type, newFormalIndex, isVarArgs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ protected boolean matchesSubtypes(ResolvedType type) {
if (type.isArray() && this.type.isArray()) {
ResolvedType componentType = type.getComponentType().resolve(type.getWorld());
UnresolvedType newPatternType = this.type.getComponentType();
ExactTypePattern etp = new ExactTypePattern(newPatternType, includeSubtypes, false);
ExactTypePattern etp = new ExactTypePattern(newPatternType, includeSubtypes, false, typeParameters);
return etp.matchesSubtypes(componentType, type);
}
return match;
}

public ExactTypePattern(UnresolvedType type, boolean includeSubtypes, boolean isVarArgs) {
super(includeSubtypes, isVarArgs);
public ExactTypePattern(UnresolvedType type, boolean includeSubtypes, boolean isVarArgs, TypePatternList typeParams) {
super(includeSubtypes, isVarArgs, typeParams);
this.type = type;
}

Expand Down Expand Up @@ -283,16 +283,20 @@ public static TypePattern readTypePattern150(VersionedDataInputStream s, ISource
if (version > EXACT_VERSION) {
throw new BCException("ExactTypePattern was written by a more recent version of AspectJ");
}
TypePattern ret = new ExactTypePattern(s.isAtLeast169() ? s.readSignatureAsUnresolvedType() : UnresolvedType.read(s), s
.readBoolean(), s.readBoolean());
TypePattern ret = new ExactTypePattern(
s.isAtLeast169() ? s.readSignatureAsUnresolvedType() : UnresolvedType.read(s),
s.readBoolean(),
s.readBoolean(),
null // set null first, use 'setTypeParameters' below
);
ret.setAnnotationTypePattern(AnnotationTypePattern.read(s, context));
ret.setTypeParameters(TypePatternList.read(s, context));
ret.readLocation(context, s);
return ret;
}

public static TypePattern readTypePatternOldStyle(DataInputStream s, ISourceContext context) throws IOException {
TypePattern ret = new ExactTypePattern(UnresolvedType.read(s), s.readBoolean(), false);
TypePattern ret = new ExactTypePattern(UnresolvedType.read(s), s.readBoolean(), false, null);
ret.readLocation(context, s);
return ret;
}
Expand Down Expand Up @@ -342,7 +346,7 @@ public TypePattern parameterizeWith(Map<String,UnresolvedType> typeVariableMap,
} else if (type.isParameterizedType()) {
newType = w.resolve(type).parameterize(typeVariableMap);
}
ExactTypePattern ret = new ExactTypePattern(newType, includeSubtypes, isVarArgs);
ExactTypePattern ret = new ExactTypePattern(newType, includeSubtypes, isVarArgs, typeParameters);
ret.annotationPattern = annotationPattern.parameterizeWith(typeVariableMap, w);
ret.copyLocationFrom(this);
return ret;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,10 @@ public Object accept(PatternNodeVisitor visitor, Object data) {
return visitor.visit(this, data);
}

@Override
public Object traverse(PatternNodeVisitor visitor, Object data) {
Object ret = accept(visitor, data);
super.traverse(visitor, ret);
if (this.signaturePattern != null)
this.signaturePattern.traverse(visitor, ret);
return ret;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,13 @@ public boolean hasFailedResolution() {
return false;
}

@Override
public Object traverse(PatternNodeVisitor visitor, Object data) {
Object ret = accept(visitor, data);
if (annotationPattern != null)
annotationPattern.traverse(visitor, ret);
if (typeParameters != null)
typeParameters.traverse(visitor, ret);
return ret;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class TypeVariablePattern extends PatternNode {
*/
public TypeVariablePattern(String variableName) {
this.name = variableName;
this.upperBound = new ExactTypePattern(UnresolvedType.OBJECT, false, false);
this.upperBound = new ExactTypePattern(UnresolvedType.OBJECT, false, false, null);
this.lowerBound = null;
this.interfaceBounds = null;
}
Expand All @@ -67,7 +67,7 @@ public TypeVariablePattern(String variableName, TypePattern upperLimit, TypePatt
this.name = variableName;
this.upperBound = upperLimit;
if (upperBound == null) {
upperBound = new ExactTypePattern(UnresolvedType.OBJECT, false, false);
upperBound = new ExactTypePattern(UnresolvedType.OBJECT, false, false, null);
}
this.interfaceBounds = interfaceBounds;
this.lowerBound = lowerBound;
Expand All @@ -77,7 +77,21 @@ public Object accept(PatternNodeVisitor visitor, Object data) {
return visitor.visit(this, data);
}

public String getName() {
public Object traverse(PatternNodeVisitor visitor, Object data) {
Object ret = accept(visitor, data);
if (lowerBound != null)
lowerBound.traverse(visitor, ret);
if (upperBound != null)
upperBound.traverse(visitor, ret);
if (interfaceBounds != null) {
for (TypePattern pattern : interfaceBounds) {
pattern.traverse(visitor, ret);
}
}
return ret;
}

public String getName() {
return name;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public Object accept(PatternNodeVisitor visitor, Object data) {
return visitor.visit(this, data);
}

@Override
public Object traverse(PatternNodeVisitor visitor, Object data) {
Object ret = accept(visitor, data);
if (patterns != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public void resolve(World world) {
if (fullyQualifiedName != null && fullyQualifiedName.contains(".")) {
ResolvedType resolvedType = world.resolve(UnresolvedType.forName(fullyQualifiedName));
if (resolvedType != null && !resolvedType.isMissing()) {
typePattern = new ExactTypePattern(resolvedType, false, false);
typePattern = new ExactTypePattern(resolvedType, false, false, null);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ private TypePattern resolveBindingsForExactType(IScope scope, UnresolvedType aTy
if (dim != 0) {
aType = UnresolvedType.makeArray(aType, dim);
}
ret = new ExactTypePattern(aType, includeSubtypes, isVarArgs);
ret = new ExactTypePattern(aType, includeSubtypes, isVarArgs, typeParameters);
}
ret.setAnnotationTypePattern(annotationPattern);
ret.copyLocationFrom(this);
Expand Down Expand Up @@ -841,7 +841,7 @@ private TypePattern resolveGenericWildcard(IScope scope, UnresolvedType aType) {
}
if (canBeExact) {
// might have changed if we find out include subtypes is set on one of the bounds...
return new ExactTypePattern(type, includeSubtypes, isVarArgs);
return new ExactTypePattern(type, includeSubtypes, isVarArgs, typeParameters);
}
}

Expand Down Expand Up @@ -878,7 +878,7 @@ private TypePattern resolveParameterizedType(IScope scope, UnresolvedType aType,
if (dim != 0) {
type = ResolvedType.makeArray(type, dim);
}
return new ExactTypePattern(type, includeSubtypes, isVarArgs);
return new ExactTypePattern(type, includeSubtypes, isVarArgs, typeParameters);
} else {
// AMC... just leave it as a wild type pattern then?
importedPrefixes = scope.getImportedPrefixes();
Expand Down Expand Up @@ -940,7 +940,7 @@ private TypePattern resolveBindingsForTypeVariable(IScope scope, UnresolvedTypeV
if (dim != 0) {
rType = ResolvedType.makeArray(rType, dim);
}
return new ExactTypePattern(rType, includeSubtypes, isVarArgs);
return new ExactTypePattern(rType, includeSubtypes, isVarArgs, typeParameters);
} else {
// we have to set bounds on the TypeVariable held by tvrType before resolving it
boolean canCreateExactTypePattern = true;
Expand Down Expand Up @@ -973,7 +973,7 @@ private TypePattern resolveBindingsForTypeVariable(IScope scope, UnresolvedTypeV
if (dim != 0) {
rType = ResolvedType.makeArray(rType, dim);
}
return new ExactTypePattern(rType, includeSubtypes, isVarArgs);
return new ExactTypePattern(rType, includeSubtypes, isVarArgs, typeParameters);
}
return this; // leave as wild type pattern then
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public void testParseTypeParameterListWithSeveralTypeParameters() {
public void testParseAllowedSuperInTypeVariable() {
PatternParser parser = new PatternParser("T super Number+");
TypeVariablePattern tv = parser.parseTypeVariable();
TypeVariablePattern expected = new TypeVariablePattern("T", new ExactTypePattern(UnresolvedType.OBJECT, false, false),
TypeVariablePattern expected = new TypeVariablePattern("T", new ExactTypePattern(UnresolvedType.OBJECT, false, false, null),
null, new PatternParser("Number+").parseTypePattern());
assertEquals("Expected type variable T super Number+", expected, tv);
}
Expand Down

0 comments on commit 97d8f73

Please sign in to comment.