Skip to content

Commit a011049

Browse files
authored
Improve lambda method discovery Fixes: #477 (#478)
* Improve lambda method discovery Fixes: #477
1 parent 31f79a5 commit a011049

File tree

4 files changed

+23
-30
lines changed

4 files changed

+23
-30
lines changed

com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/BindingUtils.java

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
package com.microsoft.java.debug;
1313

1414
import org.eclipse.jdt.core.dom.IMethodBinding;
15+
import org.eclipse.jdt.internal.debug.core.breakpoints.LambdaLocationLocatorHelper;
1516

1617
/**
1718
* Utility methods around working with JDT Bindings.
1819
*/
20+
@SuppressWarnings("restriction")
1921
public final class BindingUtils {
2022
private BindingUtils() {
2123

@@ -48,31 +50,14 @@ public static String getMethodName(IMethodBinding binding, boolean fromKey) {
4850
}
4951

5052
/**
51-
* Returns the method signature of the method represented by the binding. Since
52-
* this implementation use the {@link IMethodBinding#getKey()} to extract the
53-
* signature from, the method name must be passed in.
53+
* Returns the method signature of the method represented by the binding
54+
* including the synthetic outer locals.
5455
*
5556
* @param binding the binding which the signature must be resolved for.
56-
* @param name the name of the method.
57-
* @return the signature or null if the signature could not be resolved from the
58-
* key.
57+
* @return the signature or null if the signature could not be resolved.
5958
*/
60-
public static String toSignature(IMethodBinding binding, String name) {
61-
// use key for now until JDT core provides a public API for this.
62-
// "Ljava/util/Arrays;.asList<T:Ljava/lang/Object;>([TT;)Ljava/util/List<TT;>;"
63-
// "([Ljava/lang/String;)V|Ljava/lang/InterruptedException;"
64-
String signatureString = binding.getKey();
65-
if (signatureString != null) {
66-
name = "." + name;
67-
int index = signatureString.indexOf(name);
68-
if (index > -1) {
69-
int exceptionIndex = signatureString.indexOf("|", signatureString.lastIndexOf(")"));
70-
if (exceptionIndex > -1) {
71-
return signatureString.substring(index + name.length(), exceptionIndex);
72-
}
73-
return signatureString.substring(index + name.length());
74-
}
75-
}
76-
return null;
59+
public static String toSignature(IMethodBinding binding) {
60+
return LambdaLocationLocatorHelper.toMethodSignature(binding);
7761
}
62+
7863
}

com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/BreakpointLocationLocator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public String getMethodSignature() {
4646
if (this.methodBinding == null) {
4747
return null;
4848
}
49-
return BindingUtils.toSignature(this.methodBinding, getMethodName());
49+
return BindingUtils.toSignature(this.methodBinding);
5050
}
5151

5252
/**

com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/LambdaExpressionLocator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public String getMethodSignature() {
7171
if (!this.found) {
7272
return null;
7373
}
74-
return BindingUtils.toSignature(this.lambdaMethodBinding, getMethodName());
74+
return BindingUtils.toSignature(this.lambdaMethodBinding);
7575
}
7676

7777
/**

com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JdtSourceLookUpProvider.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@
2929
import java.util.jar.Manifest;
3030
import java.util.logging.Level;
3131
import java.util.logging.Logger;
32-
import java.util.stream.Stream;
3332
import java.util.stream.Collectors;
33+
import java.util.stream.Stream;
3434

35+
import org.eclipse.core.resources.IFile;
3536
import org.eclipse.core.resources.IResource;
37+
import org.eclipse.core.resources.ResourcesPlugin;
3638
import org.eclipse.core.runtime.CoreException;
3739
import org.eclipse.core.runtime.URIUtil;
3840
import org.eclipse.debug.core.sourcelookup.ISourceContainer;
@@ -50,10 +52,10 @@
5052
import org.eclipse.jdt.core.dom.ASTParser;
5153
import org.eclipse.jdt.core.dom.ASTVisitor;
5254
import org.eclipse.jdt.core.dom.CompilationUnit;
53-
import org.eclipse.jdt.core.dom.LambdaExpression;
54-
import org.eclipse.jdt.core.manipulation.CoreASTProvider;
5555
import org.eclipse.jdt.core.dom.IMethodBinding;
5656
import org.eclipse.jdt.core.dom.ITypeBinding;
57+
import org.eclipse.jdt.core.dom.LambdaExpression;
58+
import org.eclipse.jdt.core.manipulation.CoreASTProvider;
5759
import org.eclipse.jdt.internal.core.JarPackageFragmentRoot;
5860
import org.eclipse.jdt.launching.IVMInstall;
5961
import org.eclipse.jdt.launching.JavaRuntime;
@@ -276,7 +278,13 @@ private CompilationUnit asCompilationUnit(String uri) {
276278
* setEnvironment(String [], String [], String [], boolean)
277279
* and a unit name setUnitName(String).
278280
*/
279-
parser.setEnvironment(new String[0], new String[0], null, true);
281+
IFile resource = (IFile) JDTUtils.findResource(JDTUtils.toURI(uri),
282+
ResourcesPlugin.getWorkspace().getRoot()::findFilesForLocationURI);
283+
if (resource != null && JdtUtils.isJavaProject(resource.getProject())) {
284+
parser.setProject(JavaCore.create(resource.getProject()));
285+
} else {
286+
parser.setEnvironment(new String[0], new String[0], null, true);
287+
}
280288
parser.setUnitName(Paths.get(filePath).getFileName().toString());
281289
/**
282290
* See the java doc for { @link ASTParser#setSource(char[]) },
@@ -492,7 +500,7 @@ public List<MethodInvocation> findMethodInvocations(String uri, int line) {
492500
// Keep consistent with JDI since JDI uses binary class name
493501
invocation.declaringTypeName = binding.getDeclaringClass().getBinaryName();
494502
}
495-
invocation.methodGenericSignature = BindingUtils.toSignature(binding, BindingUtils.getMethodName(binding, true));
503+
invocation.methodGenericSignature = BindingUtils.toSignature(binding);
496504
invocation.methodSignature = Signature.getTypeErasure(invocation.methodGenericSignature);
497505
int startOffset = astNode.getStartPosition();
498506
if (astNode instanceof org.eclipse.jdt.core.dom.MethodInvocation) {

0 commit comments

Comments
 (0)