Skip to content

Commit

Permalink
fix: Handle UnresolvedReferenceBinding in ReferenceBuilder (#5294)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinWitt committed Jul 3, 2023
1 parent c497d71 commit 863c9a3
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/main/java/spoon/support/compiler/jdt/ReferenceBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding;
import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
import org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding;
import org.eclipse.jdt.internal.compiler.lookup.UnresolvedReferenceBinding;
import org.eclipse.jdt.internal.compiler.lookup.VariableBinding;
import org.eclipse.jdt.internal.compiler.lookup.VoidTypeBinding;
import org.eclipse.jdt.internal.compiler.lookup.WildcardBinding;
Expand Down Expand Up @@ -867,6 +868,8 @@ <T> CtTypeReference<T> getTypeReference(TypeBinding binding, boolean resolveGene
ref = getTypeReferenceFromProblemReferenceBinding((ProblemReferenceBinding) binding);
} else if (binding instanceof IntersectionTypeBinding18) {
ref = getTypeReferenceFromIntersectionTypeBinding((IntersectionTypeBinding18) binding);
} else if (binding instanceof UnresolvedReferenceBinding) {
ref = getTypeReferenceFromUnresolvedReferenceBinding((UnresolvedReferenceBinding) binding);
} else {
throw new RuntimeException("Unknown TypeBinding: " + binding.getClass() + " " + binding);
}
Expand All @@ -875,6 +878,25 @@ <T> CtTypeReference<T> getTypeReference(TypeBinding binding, boolean resolveGene
return (CtTypeReference<T>) ref;
}

/**
* Resolves a {@link UnresolvedReferenceBinding} to their closest match.
* For this we use the {@link UnresolvedReferenceBinding#closestMatch()} method. This is a best effort approach and can fail.
*
* @param binding the binding to resolve to a type reference.
* @return a type reference or null if the binding has no closest match
*/
@SuppressWarnings("ReturnOfNull")
private CtTypeReference<?> getTypeReferenceFromUnresolvedReferenceBinding(UnresolvedReferenceBinding binding) {
TypeBinding closestMatch = binding.closestMatch();
if (closestMatch != null) {
CtTypeReference<?> ref = this.jdtTreeBuilder.getFactory().Core().createTypeReference();
ref.setSimpleName(new String(binding.sourceName()));
ref.setPackage(getPackageReference(binding.getPackage()));
return ref;
}
return null;
}

private static boolean isParameterizedProblemReferenceBinding(TypeBinding binding) {
String sourceName = String.valueOf(binding.sourceName());
return binding instanceof ProblemReferenceBinding && typeRefContainsTypeArgs(sourceName);
Expand Down

0 comments on commit 863c9a3

Please sign in to comment.