Skip to content

Commit

Permalink
Use recovered method symbol
Browse files Browse the repository at this point in the history
Needed to use reflection to access the field containing the recovered
symbol.

- Fixes "Remove argument", "Add parameter", "Qualify with enclosing
  type" quick fixes for the following snippet:

```java
package io.github.datho7561;

public class MyCoolCodeActions {
	public void run(int i) {

	}
	public void foo() {
		new Runnable() {
			void run() {
				run(1);
			}
		}
	}
}
```

- Should fix `UnresolvedMethodsQuickFixTest.testMethodInAnonymousCovering1` in jdt-ls test suite

Signed-off-by: David Thompson <[email protected]>
  • Loading branch information
datho7561 committed Jul 2, 2024
1 parent a29a33f commit 9710acf
Showing 1 changed file with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package org.eclipse.jdt.core.dom;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -162,6 +163,10 @@ public JavacLambdaBinding getLambdaBinding(JavacMethodBinding javacMethodBinding
}

public IBinding getBinding(final Symbol owner, final com.sun.tools.javac.code.Type type) {
Symbol recoveredSymbol = getRecoveredSymbol(type);
if (recoveredSymbol != null) {
return getBinding(recoveredSymbol, recoveredSymbol.type);
}
if (owner instanceof final PackageSymbol other) {
return getPackageBinding(other);
} else if (owner instanceof ModuleSymbol typeSymbol) {
Expand Down Expand Up @@ -693,6 +698,17 @@ public ITypeBinding resolveExpressionType(Expression expr) {
if (jcExpr.type instanceof PackageType) {
return null;
}
Symbol recoveredSymbol = getRecoveredSymbol(jcExpr.type);
if (recoveredSymbol != null) {
IBinding recoveredBinding = this.bindings.getBinding(recoveredSymbol, recoveredSymbol.type);
switch (recoveredBinding) {
case IVariableBinding variableBinding: return variableBinding.getType();
case ITypeBinding typeBinding: return typeBinding;
case IMethodBinding methodBinding: return methodBinding.getReturnType();
default:
return null;
}
}
return this.bindings.getTypeBinding(jcExpr.type);
}
return null;
Expand Down Expand Up @@ -921,5 +937,20 @@ IBinding resolveReference(MemberRef ref) {
}
return null;
}
private static Symbol getRecoveredSymbol(com.sun.tools.javac.code.Type type) {
if (type instanceof ErrorType) {
try {
Field candidateSymbolField = type.getClass().getField("candidateSymbol");
candidateSymbolField.setAccessible(true);

Object symbolFieldValue = candidateSymbolField.get(type);
if (symbolFieldValue instanceof Symbol symbol) {
return symbol;
}
} catch (NoSuchFieldException | IllegalAccessException unused) {
// fall through to null
}
}
return null;
}
}

0 comments on commit 9710acf

Please sign in to comment.