use of com.intellij.debugger.SourcePosition in project intellij-community by JetBrains.
the class LambdaAsyncMethodFilter method onReached.
@Override
public int onReached(SuspendContextImpl context, RequestHint hint) {
try {
StackFrameProxyImpl proxy = context.getFrameProxy();
if (proxy != null) {
Value lambdaReference = ContainerUtil.getOrElse(proxy.getArgumentValues(), myParamNo, null);
if (lambdaReference instanceof ObjectReference) {
final SourcePosition pos = myMethodFilter.getBreakpointPosition();
if (pos != null) {
Project project = context.getDebugProcess().getProject();
long lambdaId = ((ObjectReference) lambdaReference).uniqueID();
StepIntoBreakpoint breakpoint = new LambdaInstanceBreakpoint(project, lambdaId, pos, myMethodFilter);
ClassInstanceMethodFilter.setUpStepIntoBreakpoint(context, breakpoint, hint);
return RequestHint.RESUME;
}
}
}
} catch (EvaluateException ignore) {
}
return RequestHint.STOP;
}
use of com.intellij.debugger.SourcePosition in project intellij-community by JetBrains.
the class DefaultSourcePositionProvider method getSourcePositionForField.
@Nullable
private static SourcePosition getSourcePositionForField(@NotNull FieldDescriptor descriptor, @NotNull Project project, @NotNull DebuggerContextImpl context, boolean nearest) {
final ReferenceType type = descriptor.getField().declaringType();
final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
final String fieldName = descriptor.getField().name();
if (fieldName.startsWith(FieldDescriptorImpl.OUTER_LOCAL_VAR_FIELD_PREFIX)) {
// this field actually mirrors a local variable in the outer class
String varName = fieldName.substring(fieldName.lastIndexOf('$') + 1);
PsiElement element = PositionUtil.getContextElement(context);
if (element == null) {
return null;
}
PsiClass aClass = PsiTreeUtil.getParentOfType(element, PsiClass.class, false);
if (aClass == null) {
return null;
}
PsiElement navigationElement = aClass.getNavigationElement();
if (!(navigationElement instanceof PsiClass)) {
return null;
}
aClass = (PsiClass) navigationElement;
PsiVariable psiVariable = facade.getResolveHelper().resolveReferencedVariable(varName, aClass);
if (psiVariable == null) {
return null;
}
if (nearest) {
return DebuggerContextUtil.findNearest(context, psiVariable, aClass.getContainingFile());
}
return SourcePosition.createFromElement(psiVariable);
} else {
final DebuggerSession session = context.getDebuggerSession();
final GlobalSearchScope scope = session != null ? session.getSearchScope() : GlobalSearchScope.allScope(project);
PsiClass aClass = facade.findClass(type.name().replace('$', '.'), scope);
if (aClass == null) {
// trying to search, assuming declaring class is an anonymous class
final DebugProcessImpl debugProcess = context.getDebugProcess();
if (debugProcess != null) {
try {
final List<Location> locations = type.allLineLocations();
if (!locations.isEmpty()) {
// important: use the last location to be sure the position will be within the anonymous class
final Location lastLocation = locations.get(locations.size() - 1);
final SourcePosition position = debugProcess.getPositionManager().getSourcePosition(lastLocation);
aClass = JVMNameUtil.getClassAt(position);
}
} catch (AbsentInformationException | ClassNotPreparedException ignored) {
}
}
}
if (aClass != null) {
PsiField field = aClass.findFieldByName(fieldName, false);
if (field == null)
return null;
if (nearest) {
return DebuggerContextUtil.findNearest(context, field.getNavigationElement(), aClass.getContainingFile());
}
return SourcePosition.createFromElement(field);
}
return null;
}
}
use of com.intellij.debugger.SourcePosition in project intellij-community by JetBrains.
the class PositionManagerImpl method getSourcePosition.
@Nullable
public SourcePosition getSourcePosition(final Location location) throws NoDataException {
DebuggerManagerThreadImpl.assertIsManagerThread();
if (location == null) {
return null;
}
Project project = getDebugProcess().getProject();
PsiFile psiFile = getPsiFileByLocation(project, location);
if (psiFile == null) {
return null;
}
LOG.assertTrue(myDebugProcess != null);
int lineNumber = DebuggerUtilsEx.getLineNumber(location, true);
String qName = location.declaringType().name();
// replace file with alternative
String altFileUrl = DebuggerUtilsEx.getAlternativeSourceUrl(qName, project);
if (altFileUrl != null) {
VirtualFile altFile = VirtualFileManager.getInstance().findFileByUrl(altFileUrl);
if (altFile != null) {
PsiFile altPsiFile = psiFile.getManager().findFile(altFile);
if (altPsiFile != null) {
psiFile = altPsiFile;
}
}
}
SourcePosition sourcePosition = null;
if (lineNumber > -1) {
sourcePosition = calcLineMappedSourcePosition(psiFile, lineNumber);
}
final Method method = DebuggerUtilsEx.getMethod(location);
if (sourcePosition == null && (psiFile instanceof PsiCompiledElement || lineNumber < 0)) {
if (method != null && method.name() != null && method.signature() != null) {
PsiClass psiClass = findPsiClassByName(qName, null);
PsiMethod compiledMethod = findMethod(psiClass != null ? psiClass : psiFile, qName, method.name(), method.signature());
if (compiledMethod != null) {
sourcePosition = SourcePosition.createFromElement(compiledMethod);
if (lineNumber >= 0) {
sourcePosition = new ClsSourcePosition(sourcePosition, lineNumber);
}
}
} else {
return SourcePosition.createFromLine(psiFile, -1);
}
}
if (sourcePosition == null) {
sourcePosition = SourcePosition.createFromLine(psiFile, lineNumber);
}
int lambdaOrdinal = -1;
if (DebuggerUtilsEx.isLambda(method)) {
Set<Method> lambdas = ContainerUtil.map2SetNotNull(locationsOfLine(location.declaringType(), sourcePosition), location1 -> {
Method method1 = location1.method();
if (DebuggerUtilsEx.isLambda(method1)) {
return method1;
}
return null;
});
if (lambdas.size() > 1) {
ArrayList<Method> lambdasList = new ArrayList<>(lambdas);
lambdasList.sort(DebuggerUtilsEx.LAMBDA_ORDINAL_COMPARATOR);
lambdaOrdinal = lambdasList.indexOf(method);
}
}
return new JavaSourcePosition(sourcePosition, location.declaringType(), method, lambdaOrdinal);
}
use of com.intellij.debugger.SourcePosition in project intellij-community by JetBrains.
the class RequestHint method getNextStepDepth.
public int getNextStepDepth(final SuspendContextImpl context) {
try {
final StackFrameProxyImpl frameProxy = context.getFrameProxy();
// smart step feature stop check
if (myMethodFilter != null && frameProxy != null && !(myMethodFilter instanceof BreakpointStepMethodFilter) && myMethodFilter.locationMatches(context.getDebugProcess(), frameProxy.location()) && !isTheSameFrame(context)) {
myTargetMethodMatched = true;
return myMethodFilter.onReached(context, this);
}
if ((myDepth == StepRequest.STEP_OVER || myDepth == StepRequest.STEP_INTO) && myPosition != null) {
SourcePosition locationPosition = ContextUtil.getSourcePosition(context);
if (locationPosition != null) {
Integer resultDepth = ApplicationManager.getApplication().runReadAction(new Computable<Integer>() {
public Integer compute() {
if (myPosition.getFile().equals(locationPosition.getFile()) && isTheSameFrame(context) && !mySteppedOut) {
return isOnTheSameLine(locationPosition) ? myDepth : STOP;
}
return null;
}
});
if (resultDepth != null) {
return resultDepth.intValue();
}
}
}
// Now check filters
final DebuggerSettings settings = DebuggerSettings.getInstance();
if ((myMethodFilter != null || (settings.SKIP_SYNTHETIC_METHODS && !myIgnoreFilters)) && frameProxy != null) {
final Location location = frameProxy.location();
if (location != null) {
if (DebuggerUtils.isSynthetic(location.method())) {
return myDepth;
}
}
}
if (!myIgnoreFilters) {
if (settings.SKIP_GETTERS) {
boolean isGetter = ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {
public Boolean compute() {
PsiElement contextElement = ContextUtil.getContextElement(context);
return contextElement != null && DebuggerUtils.isInsideSimpleGetter(contextElement);
}
}).booleanValue();
if (isGetter) {
return StepRequest.STEP_OUT;
}
}
if (frameProxy != null) {
if (settings.SKIP_CONSTRUCTORS) {
final Location location = frameProxy.location();
if (location != null) {
final Method method = location.method();
if (method != null && method.isConstructor()) {
return StepRequest.STEP_OUT;
}
}
}
if (settings.SKIP_CLASSLOADERS) {
final Location location = frameProxy.location();
if (location != null && DebuggerUtilsEx.isAssignableFrom("java.lang.ClassLoader", location.declaringType())) {
return StepRequest.STEP_OUT;
}
}
}
for (ExtraSteppingFilter filter : ExtraSteppingFilter.EP_NAME.getExtensions()) {
try {
if (filter.isApplicable(context))
return filter.getStepRequestDepth(context);
} catch (Exception | AssertionError e) {
LOG.error(e);
}
}
}
// smart step feature
if (myMethodFilter != null && !mySteppedOut) {
return StepRequest.STEP_OUT;
}
} catch (VMDisconnectedException ignored) {
} catch (EvaluateException e) {
LOG.error(e);
}
return STOP;
}
use of com.intellij.debugger.SourcePosition in project intellij-community by JetBrains.
the class JavaSmartStepIntoHandler method findSmartStepTargets.
protected List<SmartStepTarget> findSmartStepTargets(final SourcePosition position, @Nullable SuspendContextImpl suspendContext, @NotNull DebuggerContextImpl debuggerContext) {
final int line = position.getLine();
if (line < 0) {
// the document has been changed
return Collections.emptyList();
}
final PsiFile file = position.getFile();
final VirtualFile vFile = file.getVirtualFile();
if (vFile == null) {
// the file is not physical
return Collections.emptyList();
}
final Document doc = FileDocumentManager.getInstance().getDocument(vFile);
if (doc == null)
return Collections.emptyList();
if (line >= doc.getLineCount()) {
// the document has been changed
return Collections.emptyList();
}
TextRange curLineRange = DocumentUtil.getLineTextRange(doc, line);
PsiElement element = position.getElementAt();
PsiElement body = DebuggerUtilsEx.getBody(DebuggerUtilsEx.getContainingMethod(element));
final TextRange lineRange = (body != null) ? curLineRange.intersection(body.getTextRange()) : curLineRange;
if (lineRange == null || lineRange.isEmpty()) {
return Collections.emptyList();
}
if (element != null && !(element instanceof PsiCompiledElement)) {
do {
final PsiElement parent = element.getParent();
if (parent == null || (parent.getTextOffset() < lineRange.getStartOffset())) {
break;
}
element = parent;
} while (true);
//noinspection unchecked
final List<SmartStepTarget> targets = new OrderedSet<>();
final Ref<TextRange> textRange = new Ref<>(lineRange);
final PsiElementVisitor methodCollector = new JavaRecursiveElementVisitor() {
final Deque<PsiMethod> myContextStack = new LinkedList<>();
final Deque<String> myParamNameStack = new LinkedList<>();
private int myNextLambdaExpressionOrdinal = 0;
private boolean myInsideLambda = false;
@Nullable
private String getCurrentParamName() {
return myParamNameStack.peekFirst();
}
@Override
public void visitAnonymousClass(PsiAnonymousClass aClass) {
for (PsiMethod psiMethod : aClass.getMethods()) {
targets.add(0, new MethodSmartStepTarget(psiMethod, getCurrentParamName(), psiMethod.getBody(), true, null));
}
}
public void visitLambdaExpression(PsiLambdaExpression expression) {
boolean inLambda = myInsideLambda;
myInsideLambda = true;
super.visitLambdaExpression(expression);
myInsideLambda = inLambda;
targets.add(0, new LambdaSmartStepTarget(expression, getCurrentParamName(), expression.getBody(), myNextLambdaExpressionOrdinal++, null, !myInsideLambda));
}
@Override
public void visitMethodReferenceExpression(PsiMethodReferenceExpression expression) {
PsiElement element = expression.resolve();
if (element instanceof PsiMethod) {
PsiElement navMethod = element.getNavigationElement();
if (navMethod instanceof PsiMethod) {
targets.add(0, new MethodSmartStepTarget(((PsiMethod) navMethod), null, expression, true, null));
}
}
}
@Override
public void visitField(PsiField field) {
if (checkTextRange(field, false)) {
super.visitField(field);
}
}
@Override
public void visitMethod(PsiMethod method) {
if (checkTextRange(method, false)) {
super.visitMethod(method);
}
}
@Override
public void visitStatement(PsiStatement statement) {
if (checkTextRange(statement, true)) {
super.visitStatement(statement);
}
}
@Override
public void visitIfStatement(PsiIfStatement statement) {
visitConditional(statement.getCondition(), statement.getThenBranch(), statement.getElseBranch());
}
@Override
public void visitConditionalExpression(PsiConditionalExpression expression) {
visitConditional(expression.getCondition(), expression.getThenExpression(), expression.getElseExpression());
}
private void visitConditional(@Nullable PsiElement condition, @Nullable PsiElement thenBranch, @Nullable PsiElement elseBranch) {
if (condition != null && checkTextRange(condition, true)) {
condition.accept(this);
}
ThreeState conditionRes = evaluateCondition(condition);
if (conditionRes != ThreeState.NO && thenBranch != null && checkTextRange(thenBranch, true)) {
thenBranch.accept(this);
}
if (conditionRes != ThreeState.YES && elseBranch != null && checkTextRange(elseBranch, true)) {
elseBranch.accept(this);
}
}
private ThreeState evaluateCondition(@Nullable PsiElement condition) {
if (condition != null && !DebuggerUtils.hasSideEffects(condition)) {
try {
ExpressionEvaluator evaluator = EvaluatorBuilderImpl.getInstance().build(condition, position);
return ThreeState.fromBoolean(DebuggerUtilsEx.evaluateBoolean(evaluator, debuggerContext.createEvaluationContext()));
} catch (EvaluateException e) {
LOG.info(e);
}
}
return ThreeState.UNSURE;
}
@Override
public void visitExpression(PsiExpression expression) {
checkTextRange(expression, true);
super.visitExpression(expression);
}
boolean checkTextRange(@NotNull PsiElement expression, boolean expand) {
TextRange range = expression.getTextRange();
if (lineRange.intersects(range)) {
if (expand) {
textRange.set(textRange.get().union(range));
}
return true;
}
return false;
}
public void visitExpressionList(PsiExpressionList expressionList) {
PsiMethod psiMethod = myContextStack.peekFirst();
if (psiMethod != null) {
final String methodName = psiMethod.getName();
final PsiExpression[] expressions = expressionList.getExpressions();
final PsiParameter[] parameters = psiMethod.getParameterList().getParameters();
for (int idx = 0; idx < expressions.length; idx++) {
final String paramName = (idx < parameters.length && !parameters[idx].isVarArgs()) ? parameters[idx].getName() : "arg" + (idx + 1);
myParamNameStack.push(methodName + ": " + paramName + ".");
final PsiExpression argExpression = expressions[idx];
try {
argExpression.accept(this);
} finally {
myParamNameStack.pop();
}
}
} else {
super.visitExpressionList(expressionList);
}
}
@Override
public void visitCallExpression(final PsiCallExpression expression) {
final PsiMethod psiMethod = expression.resolveMethod();
if (psiMethod != null) {
myContextStack.push(psiMethod);
targets.add(new MethodSmartStepTarget(psiMethod, null, expression instanceof PsiMethodCallExpression ? ((PsiMethodCallExpression) expression).getMethodExpression().getReferenceNameElement() : expression instanceof PsiNewExpression ? ((PsiNewExpression) expression).getClassOrAnonymousClassReference() : expression, myInsideLambda, null));
}
try {
super.visitCallExpression(expression);
} finally {
if (psiMethod != null) {
myContextStack.pop();
}
}
}
};
element.accept(methodCollector);
for (PsiElement sibling = element.getNextSibling(); sibling != null; sibling = sibling.getNextSibling()) {
if (!lineRange.intersects(sibling.getTextRange())) {
break;
}
sibling.accept(methodCollector);
}
Range<Integer> lines = new Range<>(doc.getLineNumber(textRange.get().getStartOffset()), doc.getLineNumber(textRange.get().getEndOffset()));
targets.forEach(t -> t.setCallingExpressionLines(lines));
if (!targets.isEmpty()) {
StackFrameProxyImpl frameProxy = suspendContext != null ? suspendContext.getFrameProxy() : null;
if (frameProxy != null) {
try {
Location location = frameProxy.location();
MethodBytecodeUtil.visit(location.method(), location.codeIndex(), new MethodVisitor(Opcodes.API_VERSION) {
boolean myLineMatch = false;
@Override
public void visitLineNumber(int line, Label start) {
myLineMatch = lines.isWithin(line - 1);
}
@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
if (myLineMatch) {
targets.removeIf(t -> {
if (t instanceof MethodSmartStepTarget) {
return DebuggerUtilsEx.methodMatches(((MethodSmartStepTarget) t).getMethod(), owner.replace("/", "."), name, desc, suspendContext.getDebugProcess());
}
return false;
});
}
}
}, true);
} catch (Exception e) {
LOG.info(e);
}
}
return targets;
}
}
return Collections.emptyList();
}
Aggregations