diff --git a/src/main/java/spoon/support/compiler/jdt/ReferenceBuilder.java b/src/main/java/spoon/support/compiler/jdt/ReferenceBuilder.java index d8db2f91b48..9b566123e13 100644 --- a/src/main/java/spoon/support/compiler/jdt/ReferenceBuilder.java +++ b/src/main/java/spoon/support/compiler/jdt/ReferenceBuilder.java @@ -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; @@ -867,6 +868,8 @@ CtTypeReference 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); } @@ -875,6 +878,25 @@ CtTypeReference getTypeReference(TypeBinding binding, boolean resolveGene return (CtTypeReference) 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);