From e789f65554855ccf92513c7fa7bdb4c74cb2da57 Mon Sep 17 00:00:00 2001 From: "Egor.Ushakov" Date: Tue, 12 Jan 2016 15:34:57 +0300 Subject: [PATCH] IDEA-150258 Debugger: couldn't set line breakpoint in static initializer at line with lambda --- .../actions/JavaSmartStepIntoHandler.java | 5 ++-- .../engine/BasicStepMethodFilter.java | 4 +-- .../debugger/engine/PositionManagerImpl.java | 6 ++--- .../debugger/impl/DebuggerUtilsEx.java | 27 ++++++++++++++++--- .../debugger/jdi/LocalVariablesUtil.java | 18 +++++-------- .../breakpoints/JavaLineBreakpointType.java | 4 +-- 6 files changed, 39 insertions(+), 25 deletions(-) diff --git a/java/debugger/impl/src/com/intellij/debugger/actions/JavaSmartStepIntoHandler.java b/java/debugger/impl/src/com/intellij/debugger/actions/JavaSmartStepIntoHandler.java index 1b78472e256f..c61d48df4714 100644 --- a/java/debugger/impl/src/com/intellij/debugger/actions/JavaSmartStepIntoHandler.java +++ b/java/debugger/impl/src/com/intellij/debugger/actions/JavaSmartStepIntoHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -67,8 +67,7 @@ public class JavaSmartStepIntoHandler extends JvmSmartStepIntoHandler { } TextRange curLineRange = DocumentUtil.getLineTextRange(doc, line); PsiElement element = position.getElementAt(); - PsiParameterListOwner method = DebuggerUtilsEx.getContainingMethod(element); - PsiElement body = method != null ? method.getBody() : null; + PsiElement body = DebuggerUtilsEx.getBody(DebuggerUtilsEx.getContainingMethod(element)); final TextRange lineRange = (body != null) ? curLineRange.intersection(body.getTextRange()) : curLineRange; if (lineRange == null || lineRange.isEmpty()) { diff --git a/java/debugger/impl/src/com/intellij/debugger/engine/BasicStepMethodFilter.java b/java/debugger/impl/src/com/intellij/debugger/engine/BasicStepMethodFilter.java index ff15205d1e9c..cc42a83cd616 100644 --- a/java/debugger/impl/src/com/intellij/debugger/engine/BasicStepMethodFilter.java +++ b/java/debugger/impl/src/com/intellij/debugger/engine/BasicStepMethodFilter.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -73,7 +73,7 @@ public class BasicStepMethodFilter implements NamedMethodFilter { lambdaMatched = ApplicationManager.getApplication().runReadAction(new Computable() { @Override public Boolean compute() { - PsiParameterListOwner psiMethod = DebuggerUtilsEx.getContainingMethod(process.getPositionManager().getSourcePosition(location)); + PsiElement psiMethod = DebuggerUtilsEx.getContainingMethod(process.getPositionManager().getSourcePosition(location)); if (psiMethod instanceof PsiLambdaExpression) { PsiType type = ((PsiLambdaExpression)psiMethod).getFunctionalInterfaceType(); PsiMethod interfaceMethod = LambdaUtil.getFunctionalInterfaceMethod(type); diff --git a/java/debugger/impl/src/com/intellij/debugger/engine/PositionManagerImpl.java b/java/debugger/impl/src/com/intellij/debugger/engine/PositionManagerImpl.java index 1bc5a1af33a5..3c0ed6780f10 100644 --- a/java/debugger/impl/src/com/intellij/debugger/engine/PositionManagerImpl.java +++ b/java/debugger/impl/src/com/intellij/debugger/engine/PositionManagerImpl.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -230,14 +230,14 @@ public class PositionManagerImpl implements PositionManager, MultiRequestPositio if (name != null && !name.equals(myExpectedClassName)) { return null; } - PsiParameterListOwner method = DebuggerUtilsEx.getContainingMethod(element); + PsiElement method = DebuggerUtilsEx.getContainingMethod(element); if (!StringUtil.isEmpty(myExpectedMethodName)) { if (method == null) { return null; } else if (((method instanceof PsiMethod && myExpectedMethodName.equals(((PsiMethod)method).getName())) || (method instanceof PsiLambdaExpression && LambdaMethodFilter.isLambdaName(myExpectedMethodName))) && - insideBody(element, method.getBody())) { + insideBody(element, DebuggerUtilsEx.getBody(method))) { return element; } } diff --git a/java/debugger/impl/src/com/intellij/debugger/impl/DebuggerUtilsEx.java b/java/debugger/impl/src/com/intellij/debugger/impl/DebuggerUtilsEx.java index 7ae4204fe882..68c949e0bb2e 100644 --- a/java/debugger/impl/src/com/intellij/debugger/impl/DebuggerUtilsEx.java +++ b/java/debugger/impl/src/com/intellij/debugger/impl/DebuggerUtilsEx.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -845,6 +845,25 @@ public abstract class DebuggerUtilsEx extends DebuggerUtils { return lambdas; } + @Nullable + public static PsiElement getBody(PsiElement method) { + if (method instanceof PsiParameterListOwner) { + return ((PsiParameterListOwner)method).getBody(); + } + else if (method instanceof PsiClassInitializer) { + return ((PsiClassInitializer)method).getBody(); + } + return null; + } + + @NotNull + public static PsiParameter[] getParameters(PsiElement method) { + if (method instanceof PsiParameterListOwner) { + return ((PsiParameterListOwner)method).getParameterList().getParameters(); + } + return PsiParameter.EMPTY_ARRAY; + } + public static boolean intersects(@NotNull TextRange range, @NotNull PsiElement elem) { TextRange elemRange = elem.getTextRange(); return elemRange != null && elemRange.intersects(range); @@ -888,12 +907,12 @@ public abstract class DebuggerUtilsEx extends DebuggerUtils { } @Nullable - public static PsiParameterListOwner getContainingMethod(@Nullable PsiElement elem) { - return PsiTreeUtil.getContextOfType(elem, PsiMethod.class, PsiLambdaExpression.class); + public static PsiElement getContainingMethod(@Nullable PsiElement elem) { + return PsiTreeUtil.getContextOfType(elem, PsiMethod.class, PsiLambdaExpression.class, PsiClassInitializer.class); } @Nullable - public static PsiParameterListOwner getContainingMethod(@Nullable SourcePosition position) { + public static PsiElement getContainingMethod(@Nullable SourcePosition position) { if (position == null) return null; return getContainingMethod(position.getElementAt()); } diff --git a/java/debugger/impl/src/com/intellij/debugger/jdi/LocalVariablesUtil.java b/java/debugger/impl/src/com/intellij/debugger/jdi/LocalVariablesUtil.java index 50a03a53fd3d..1ee6c1a8ee04 100644 --- a/java/debugger/impl/src/com/intellij/debugger/jdi/LocalVariablesUtil.java +++ b/java/debugger/impl/src/com/intellij/debugger/jdi/LocalVariablesUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -323,18 +323,15 @@ public class LocalVariablesUtil { SourcePosition position = ContextUtil.getSourcePosition(context); if (position != null) { PsiElement element = position.getElementAt(); - PsiParameterListOwner method = DebuggerUtilsEx.getContainingMethod(element); + PsiElement method = DebuggerUtilsEx.getContainingMethod(element); if (method != null) { - PsiParameterList params = method.getParameterList(); MultiMap res = new MultiMap(); - int psiFirstLocalsSlot = getFirstLocalsSlot(method); - int slot = Math.max(0, firstLocalsSlot - psiFirstLocalsSlot); - for (int i = 0; i < params.getParametersCount(); i++) { - PsiParameter parameter = params.getParameters()[i]; + int slot = Math.max(0, firstLocalsSlot - getFirstLocalsSlot(method)); + for (PsiParameter parameter : DebuggerUtilsEx.getParameters(method)) { res.putValue(slot, parameter.getName()); slot += getTypeSlotSize(parameter.getType()); } - PsiElement body = method.getBody(); + PsiElement body = DebuggerUtilsEx.getBody(method); if (body != null) { try { body.accept(new LocalVariableNameFinder(firstLocalsSlot, res, element)); @@ -481,13 +478,12 @@ public class LocalVariablesUtil { } } - private static int getFirstLocalsSlot(PsiParameterListOwner method) { + private static int getFirstLocalsSlot(PsiElement method) { int startSlot = 0; if (method instanceof PsiModifierListOwner) { startSlot = ((PsiModifierListOwner)method).hasModifierProperty(PsiModifier.STATIC) ? 0 : 1; } - PsiParameterList params = method.getParameterList(); - for (PsiParameter parameter : params.getParameters()) { + for (PsiParameter parameter : DebuggerUtilsEx.getParameters(method)) { startSlot += getTypeSlotSize(parameter.getType()); } return startSlot; diff --git a/java/debugger/impl/src/com/intellij/debugger/ui/breakpoints/JavaLineBreakpointType.java b/java/debugger/impl/src/com/intellij/debugger/ui/breakpoints/JavaLineBreakpointType.java index 361101abea5f..50ac2b9474f3 100644 --- a/java/debugger/impl/src/com/intellij/debugger/ui/breakpoints/JavaLineBreakpointType.java +++ b/java/debugger/impl/src/com/intellij/debugger/ui/breakpoints/JavaLineBreakpointType.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -123,7 +123,7 @@ public class JavaLineBreakpointType extends JavaLineBreakpointTypeBase res = new SmartList(); res.add(new JavaBreakpointVariant(position)); //all - if (startMethod instanceof PsiMethod) { + if (!(startMethod instanceof PsiLambdaExpression)) { res.add(new ExactJavaBreakpointVariant(position, startMethod, -1)); // base method }