Skip to content

Commit

Permalink
Fix some "wrong type" quick fixes
Browse files Browse the repository at this point in the history
- add arguments to the created problem
  - This is needed to get the quick fixes to show up in JDT UI
- Fix resolveBinding(Expression) for method invocations (it now properly
  returns the binding for the return type)
  - This fixes the "change variable type" quickfix

Signed-off-by: David Thompson <[email protected]>
  • Loading branch information
datho7561 authored and mickaelistria committed Jul 5, 2024
1 parent d0cc34d commit 1d841cc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,9 @@ public ITypeBinding resolveExpressionType(Expression expr) {
}
}
var jcTree = this.converter.domToJavac.get(expr);
if (jcTree instanceof JCMethodInvocation javacMethodInvocation) {
return this.bindings.getTypeBinding(javacMethodInvocation.meth.type.asMethodType().getReturnType());
}
if (jcTree instanceof JCFieldAccess jcFieldAccess) {
if (jcFieldAccess.type instanceof PackageType) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.IOException;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Stream;

import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;
Expand Down Expand Up @@ -78,12 +79,13 @@ public JavacProblem createJavacProblem(Diagnostic<? extends JavaFileObject> diag
return null;
}
org.eclipse.jface.text.Position diagnosticPosition = getDiagnosticPosition(diagnostic, context);
String[] arguments = getDiagnosticStringArguments(diagnostic);
return new JavacProblem(
diagnostic.getSource().getName().toCharArray(),
diagnostic.getMessage(Locale.getDefault()),
diagnostic.getCode(),
problemId,
new String[0],
arguments,
severity,
diagnosticPosition.getOffset(),
diagnosticPosition.getOffset() + diagnosticPosition.getLength() - 1,
Expand Down Expand Up @@ -489,6 +491,27 @@ private Object[] getDiagnosticArguments(Diagnostic<?> diagnostic) {
return jcDiagnostic.getArgs();
}

private String[] getDiagnosticStringArguments(Diagnostic<?> diagnostic) {
if (!(diagnostic instanceof JCDiagnostic jcDiagnostic)) {
return new String[0];
}

if (!jcDiagnostic.getSubdiagnostics().isEmpty()) {
jcDiagnostic = jcDiagnostic.getSubdiagnostics().get(0);
}

if (jcDiagnostic.getArgs().length != 0
&& jcDiagnostic.getArgs()[0] instanceof JCDiagnostic argDiagnostic) {
return Stream.of(argDiagnostic.getArgs()) //
.map(Object::toString) //
.toArray(String[]::new);
}

return Stream.of(jcDiagnostic.getArgs()) //
.map(Object::toString) //
.toArray(String[]::new);
}

// compiler.err.prob.found.req -> TypeMismatch, ReturnTypeMismatch, IllegalCast, VoidMethodReturnsValue...
private int convertTypeMismatch(Diagnostic<?> diagnostic) {
Diagnostic<?> diagnosticArg = getDiagnosticArgumentByType(diagnostic, Diagnostic.class);
Expand Down

0 comments on commit 1d841cc

Please sign in to comment.