8181import org .eclipse .jdt .debug .core .IJavaType ;
8282import org .eclipse .jdt .debug .core .IJavaWatchpoint ;
8383import org .eclipse .jdt .debug .core .JDIDebugModel ;
84+ import org .eclipse .jdt .internal .core .LambdaMethod ;
8485import org .eclipse .jdt .internal .corext .template .java .CompilationUnitContext ;
8586import org .eclipse .jdt .internal .corext .template .java .CompilationUnitContextType ;
8687import org .eclipse .jdt .internal .corext .template .java .JavaContextType ;
@@ -286,7 +287,7 @@ protected IStatus run(IProgressMonitor monitor) {
286287 job .schedule ();
287288 }
288289
289- public void toggleLambdaEntryMethodBreakpoints (final IWorkbenchPart part , final ISelection finalSelection , final String lambdaMethodName , final String lambdaMethodSignature , final LambdaBreakpoint lambdaProperties ) {
290+ static void toggleLambdaEntryMethodBreakpoints (final IWorkbenchPart part , final ISelection finalSelection , final String lambdaMethodName , final String lambdaMethodSignature , final LambdaProperties lambdaProperties ) {
290291 Job job = new Job ("Toggle Lambda Entry Method Breakpoints" ) { //$NON-NLS-1$
291292 @ Override
292293 protected IStatus run (IProgressMonitor monitor ) {
@@ -307,7 +308,7 @@ protected IStatus run(IProgressMonitor monitor) {
307308 job .schedule ();
308309 }
309310
310- static IStatus doToggleLambdaEntryMethodBreakpoints (IWorkbenchPart part , ISelection selection , String lambdaMethodName , String lambdaMethodSignature , LambdaBreakpoint lambdaProperties , IProgressMonitor monitor ) throws CoreException {
311+ static IStatus doToggleLambdaEntryMethodBreakpoints (IWorkbenchPart part , ISelection selection , String lambdaMethodName , String lambdaMethodSignature , LambdaProperties lambdaProperties , IProgressMonitor monitor ) throws CoreException {
311312 ITextEditor textEditor = getTextEditor (part );
312313 if (textEditor == null || !(selection instanceof ITextSelection )) {
313314 return Status .OK_STATUS ;
@@ -412,13 +413,13 @@ private static void doToggleMethodBreakpoint(IMethod member, IWorkbenchPart part
412413 doToggleMethodBreakpoint (member , null , null , null , part , finalSelection , monitor );
413414 }
414415
415- private static void doToggleMethodBreakpoint (IMethod member , String lambdaMethodName , String lambdaMethodSignature , LambdaBreakpoint lambdaProperties , IWorkbenchPart part , ISelection finalSelection , IProgressMonitor monitor ) throws CoreException {
416+ private static void doToggleMethodBreakpoint (IMethod member , String lambdaMethodName , String lambdaMethodSignature , LambdaProperties lambdaProperties , IWorkbenchPart part , ISelection finalSelection , IProgressMonitor monitor ) throws CoreException {
416417 int lambdaPosition = 0 ;
417418 if (lambdaProperties != null ) {
418419 lambdaPosition = lambdaProperties .lambdaPosition ();
419420 }
420421
421- IJavaBreakpoint breakpoint = getMethodBreakpoint (member , lambdaMethodName , lambdaMethodSignature , lambdaPosition );
422+ IJavaBreakpoint breakpoint = getMethodBreakpoint (member , lambdaPosition );
422423 if (breakpoint != null ) {
423424 if (BreakpointToggleUtils .isToggleTracepoint ()) {
424425 deleteTracepoint (breakpoint , part , monitor );
@@ -470,14 +471,11 @@ private static void doToggleMethodBreakpoint(IMethod member, String lambdaMethod
470471 }
471472 BreakpointToggleUtils .setUnsetTracepoint (false );
472473 }
473- if (lambdaMethodName != null || lambdaMethodSignature != null ) {
474+ if (lambdaProperties != null ) {
474475 methodBreakpoint .setLambdaBreakpoint (true );
475476 methodBreakpoint .setInlineLambdaPosition (lambdaPosition );
476- String lambdaName = lambdaProperties .getLambdaName ();
477477 if (lambdaProperties .isInline ()) {
478- methodBreakpoint .setLambdaName (lambdaName );
479- } else {
480- methodBreakpoint .setLambdaName (null );
478+ methodBreakpoint .setLambdaName (lambdaProperties .getLambdaName ());
481479 }
482480 }
483481 if (ValidBreakpointLocationLocator .LOCATION_METHOD_CLOSE ) {
@@ -1372,60 +1370,51 @@ protected static IMethod getMethodHandle(IEditorPart editorPart, String typeName
13721370 *
13731371 * @param element
13741372 * the element to get the breakpoint from
1375- * @param lambdaMethodSignature
1376- * @param lambdaMethodName
13771373 * @param lambdaPosition
1374+ * the position of the lambda expression in the line (if any)
13781375 * @return the current breakpoint from the element or <code>null</code>
13791376 */
13801377 @ SuppressWarnings ("restriction" )
1381- protected static IJavaBreakpoint getMethodBreakpoint (IMember element , String lambdaMethodName , String lambdaMethodSignature , int lambdaPosition ) {
1378+ protected static IJavaBreakpoint getMethodBreakpoint (IMember element , int lambdaPosition ) {
1379+ if (!(element instanceof IMethod method )) {
1380+ return null ;
1381+ }
13821382 IBreakpointManager breakpointManager = DebugPlugin .getDefault ().getBreakpointManager ();
13831383 IBreakpoint [] breakpoints = breakpointManager .getBreakpoints (JDIDebugModel .getPluginIdentifier ());
1384- if (!(element instanceof IMethod )) {
1385- return null ;
1386- }
1387- IMethod method = (IMethod ) element ;
13881384 for (IBreakpoint breakpoint : breakpoints ) {
1389- if (!(breakpoint instanceof IJavaMethodBreakpoint )) {
1385+ if (!(breakpoint instanceof IJavaMethodBreakpoint methodBreakpoint )) {
13901386 continue ;
13911387 }
1392- IJavaMethodBreakpoint methodBreakpoint = (IJavaMethodBreakpoint ) breakpoint ;
1393- IMember container = null ;
1388+ IMember container ;
13941389 try {
13951390 container = BreakpointUtils .getMember (methodBreakpoint );
1396- } catch (CoreException e ) {
1397- JDIDebugUIPlugin .log (e );
1398- return null ;
1399- }
1400- if (container == null ) {
1401- try {
1391+ if (container == null ) {
14021392 if (method .getDeclaringType ().getFullyQualifiedName ().equals (methodBreakpoint .getTypeName ())
14031393 && method .getElementName ().equals (methodBreakpoint .getMethodName ())
14041394 && methodBreakpoint .getMethodSignature ().equals (resolveMethodSignature (method ))) {
14051395 return methodBreakpoint ;
14061396 }
1407- } catch (CoreException e ) {
1408- JDIDebugUIPlugin .log (e );
1397+ continue ;
14091398 }
1410- } else {
1411- if (container instanceof IMethod ) {
1412- if (method instanceof org .eclipse .jdt .internal .core .LambdaMethod ) {
1413- try {
1414- if (methodBreakpoint .getInlineLambdaPosition () == lambdaPosition
1415- && methodBreakpoint .getCharStart () == element .getSourceRange ().getOffset ()) {
1416- return methodBreakpoint ;
1417- }
1418- } catch (CoreException e ) {
1419- JDIDebugUIPlugin .log (e );
1420- return null ;
1421- }
1422-
1423- } else if (method .getDeclaringType ().getFullyQualifiedName ().equals (container .getDeclaringType ().getFullyQualifiedName ())) {
1424- if (method .isSimilar ((IMethod ) container )) {
1425- return methodBreakpoint ;
1426- }
1399+ if (!(container instanceof IMethod )) {
1400+ continue ;
1401+ }
1402+ if (method instanceof LambdaMethod ) {
1403+ ISourceRange sourceRange = element .getSourceRange ();
1404+ if (sourceRange != null && methodBreakpoint .getInlineLambdaPosition () == lambdaPosition
1405+ && methodBreakpoint .getCharStart () == sourceRange .getOffset ()) {
1406+ return methodBreakpoint ;
14271407 }
1408+ continue ;
14281409 }
1410+ if (method .getDeclaringType ().getFullyQualifiedName ().equals (container .getDeclaringType ().getFullyQualifiedName ())) {
1411+ if (method .isSimilar ((IMethod ) container )) {
1412+ return methodBreakpoint ;
1413+ }
1414+ }
1415+ } catch (CoreException e ) {
1416+ JDIDebugUIPlugin .log (e );
1417+ continue ;
14291418 }
14301419 }
14311420 return null ;
@@ -1697,53 +1686,64 @@ private void toggleFieldOrMethodBreakpoints(IWorkbenchPart part, ISelection sele
16971686 toggleWatchpoints (part , ts );
16981687 } else if (loc .getLocationType () == ValidBreakpointLocationLocator .LOCATION_LINE ) {
16991688 if (lambdaEntryBP ) {
1700- IEditorInput editorInput = editor .getEditorInput ();
1701- IDocumentProvider documentProvider = editor .getDocumentProvider ();
1702- if (documentProvider == null ) {
1703- BreakpointToggleUtils .report (ActionMessages .LambdaEntryBreakpointToggleAction_Unavailable , part );
1704- throw new CoreException (Status .CANCEL_STATUS );
1705- }
1706- IDocument document = documentProvider .getDocument (editorInput );
1707- try {
1708- IRegion region = document .getLineInformation (ts .getStartLine ());
1709- List <LambdaExpression > lambdaExpresions = findLambdaExpressions (editor , region );
1710- if (lambdaExpresions .isEmpty ()) {
1711- return ;
1712- }
1713- int lambdaPosition ;
1714- if (lambdaExpresions .size () == 1 ) {
1715- lambdaPosition = 0 ;
1716- } else {
1717- lambdaPosition = selectLambda (lambdaExpresions );
1718- if (lambdaPosition == -1 ) {
1719- return ;
1720- }
1721- }
1722- LambdaExpression selectedLambda = lambdaExpresions .get (lambdaPosition );
1723- IMethodBinding methodBinding = selectedLambda .resolveMethodBinding ();
1724- if (methodBinding == null ) {
1725- BreakpointToggleUtils .report (ActionMessages .LambdaEntryBreakpointToggleAction_Unavailable , part );
1726- return ;
1727- }
1728- String lambdaMethodName = LambdaLocationLocatorHelper .toMethodName (methodBinding );
1729- String lambdaMethodSignature = LambdaLocationLocatorHelper .toMethodSignature (methodBinding );
1730- ITextSelection textSelection = new TextSelection (document , selectedLambda .getStartPosition (), selectedLambda .getLength ());
1731- boolean inline = lambdaExpresions .size () > 1 ? true : false ;
1732- LambdaBreakpoint lambdaBp = new LambdaBreakpoint (lambdaExpresions .get (lambdaPosition ).toString (), lambdaPosition , inline );
1733- toggleLambdaEntryMethodBreakpoints (part , textSelection , lambdaMethodName , lambdaMethodSignature , lambdaBp );
1734- } catch (BadLocationException e ) {
1735- BreakpointToggleUtils .report (ActionMessages .LambdaEntryBreakpointToggleAction_Unavailable , part );
1736- }
1737-
1689+ toggleLambdaBreakpoint (part , ts , editor );
17381690 } else {
17391691 toggleLineBreakpoints (part , ts , false , loc );
17401692 }
17411693 }
17421694 }
17431695
1696+ private static void toggleLambdaBreakpoint (IWorkbenchPart part , ITextSelection ts , ITextEditor editor ) throws CoreException {
1697+ IDocumentProvider documentProvider = editor .getDocumentProvider ();
1698+ if (documentProvider == null ) {
1699+ BreakpointToggleUtils .report (ActionMessages .LambdaEntryBreakpointToggleAction_Unavailable , part );
1700+ throw new CoreException (Status .CANCEL_STATUS );
1701+ }
1702+ IEditorInput editorInput = editor .getEditorInput ();
1703+ IDocument document = documentProvider .getDocument (editorInput );
1704+ if (document == null ) {
1705+ BreakpointToggleUtils .report (ActionMessages .LambdaEntryBreakpointToggleAction_Unavailable , part );
1706+ throw new CoreException (Status .CANCEL_STATUS );
1707+ }
1708+ try {
1709+ IRegion region = document .getLineInformation (ts .getStartLine ());
1710+ List <LambdaExpression > lambdaExpresions = findLambdaExpressions (editor , region );
1711+ if (lambdaExpresions .isEmpty ()) {
1712+ return ;
1713+ }
1714+ int lambdaPosition ;
1715+ if (lambdaExpresions .size () == 1 ) {
1716+ lambdaPosition = 0 ;
1717+ } else {
1718+ lambdaPosition = selectLambda (lambdaExpresions );
1719+ if (lambdaPosition == -1 ) {
1720+ return ;
1721+ }
1722+ }
1723+ LambdaExpression selectedLambda = lambdaExpresions .get (lambdaPosition );
1724+ IMethodBinding methodBinding = selectedLambda .resolveMethodBinding ();
1725+ if (methodBinding == null ) {
1726+ BreakpointToggleUtils .report (ActionMessages .LambdaEntryBreakpointToggleAction_Unavailable , part );
1727+ return ;
1728+ }
1729+ String lambdaMethodName = LambdaLocationLocatorHelper .toMethodName (methodBinding );
1730+ String lambdaMethodSignature = LambdaLocationLocatorHelper .toMethodSignature (methodBinding );
1731+ ITextSelection textSelection = new TextSelection (document , selectedLambda .getStartPosition (), selectedLambda .getLength ());
1732+ boolean inline = lambdaExpresions .size () > 1 ;
1733+ LambdaProperties lambdaProperties = new LambdaProperties (lambdaExpresions .get (lambdaPosition ).toString (), lambdaPosition , inline );
1734+ toggleLambdaEntryMethodBreakpoints (part , textSelection , lambdaMethodName , lambdaMethodSignature , lambdaProperties );
1735+ } catch (BadLocationException e ) {
1736+ BreakpointToggleUtils .report (ActionMessages .LambdaEntryBreakpointToggleAction_Unavailable , part );
1737+ }
1738+ }
1739+
17441740 private static List <LambdaExpression > findLambdaExpressions (ITextEditor editor , IRegion region ) {
17451741 LambdaCollector lambdas = new LambdaCollector (region .getOffset (), region .getOffset () + region .getLength ());
17461742 CompilationUnit unitForLambdas = parseCompilationUnit (editor );
1743+ if (unitForLambdas == null ) {
1744+ JDIDebugUIPlugin .log ("Failed to parse CU for: " + editor .getTitle (), new IllegalStateException ()); //$NON-NLS-1$
1745+ return List .of ();
1746+ }
17471747 unitForLambdas .accept (lambdas );
17481748 return lambdas .getLambdaExpressions ();
17491749 }
@@ -1962,10 +1962,11 @@ private static ICompilationUnit getCompilationUnit(ITextEditor editor) {
19621962 return JavaCore .createCompilationUnitFrom (file );
19631963 }
19641964
1965- public int selectLambda (List <LambdaExpression > lambdaExps ) {
1965+ static int selectLambda (List <LambdaExpression > lambdaExps ) {
19661966 Map <String , Integer > seen = new HashMap <>();
1967- // Generate unique names for lambda expressions. Note that two identical
1968- // lambda expressions in different locations should still have different names.
1967+ // Generate unique labels used also as keys for lambda expressions. Note that two identical
1968+ // lambda expressions in different locations should still have different names
1969+ // otherwise the selection dialog cannot distinguish them.
19691970 List <String > lambdaNames = lambdaExps .stream ().map (expr -> shortenLambdaExpression (expr .toString ())).map (name -> {
19701971 int count = seen .getOrDefault (name , 0 );
19711972 seen .put (name , count + 1 );
@@ -2033,7 +2034,7 @@ protected void setShellStyle(int newShellStyle) {
20332034 }
20342035 }
20352036
2036- private static final record LambdaBreakpoint (String lambdaName , int lambdaPosition , boolean isInline ) {
2037+ private static final record LambdaProperties (String lambdaName , int lambdaPosition , boolean isInline ) {
20372038 public String getLambdaName () {
20382039 return shortenLambdaExpression (lambdaName );
20392040 }
0 commit comments