|
| 1 | +/******************************************************************************* |
| 2 | +* Copyright (c) 2022 Microsoft Corporation and others. |
| 3 | +* All rights reserved. This program and the accompanying materials |
| 4 | +* are made available under the terms of the Eclipse Public License v1.0 |
| 5 | +* which accompanies this distribution, and is available at |
| 6 | +* http://www.eclipse.org/legal/epl-v10.html |
| 7 | +* |
| 8 | +* Contributors: |
| 9 | +* Gayan Perera (gayanper@gmail.com) - initial API and implementation |
| 10 | +*******************************************************************************/ |
| 11 | +package com.microsoft.java.debug; |
| 12 | + |
| 13 | +import org.eclipse.jdt.core.dom.ASTNode; |
| 14 | +import org.eclipse.jdt.core.dom.ASTVisitor; |
| 15 | +import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; |
| 16 | +import org.eclipse.jdt.core.dom.CompilationUnit; |
| 17 | +import org.eclipse.jdt.core.dom.IMethodBinding; |
| 18 | +import org.eclipse.jdt.core.dom.LambdaExpression; |
| 19 | + |
| 20 | +public class LambdaExpressionLocator extends ASTVisitor { |
| 21 | + private CompilationUnit compilationUnit; |
| 22 | + private int line; |
| 23 | + private int column; |
| 24 | + private boolean found; |
| 25 | + |
| 26 | + private IMethodBinding lambdaMethodBinding; |
| 27 | + private LambdaExpression lambdaExpression; |
| 28 | + |
| 29 | + public LambdaExpressionLocator(CompilationUnit compilationUnit, int line, int column) { |
| 30 | + this.compilationUnit = compilationUnit; |
| 31 | + this.line = line; |
| 32 | + this.column = column; |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public boolean visit(LambdaExpression node) { |
| 37 | + // we only support inline breakpoints which are added before the expression part of the |
| 38 | + // lambda. And we don't support lambda blocks since they can be debugged using line |
| 39 | + // breakpoints. |
| 40 | + if (column > -1) { |
| 41 | + int startPosition = node.getStartPosition(); |
| 42 | + int endPosition = node.getBody().getStartPosition(); |
| 43 | + int offset = this.compilationUnit.getPosition(line, column); |
| 44 | + // lambda on same line: |
| 45 | + // list.stream().map(i -> i + 1); |
| 46 | + // |
| 47 | + // lambda on multiple lines: |
| 48 | + // list.stream().map(user |
| 49 | + // -> user.isSystem() ? new SystemUser(user) : new EndUser(user)); |
| 50 | + |
| 51 | + if (offset >= startPosition && offset <= endPosition) { |
| 52 | + this.lambdaMethodBinding = node.resolveMethodBinding(); |
| 53 | + this.found = true; |
| 54 | + this.lambdaExpression = node; |
| 55 | + return false; |
| 56 | + } |
| 57 | + } |
| 58 | + return super.visit(node); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Returns <code>true</code> if a lambda is found at given location. |
| 63 | + */ |
| 64 | + public boolean isFound() { |
| 65 | + return found; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Returns the signature of lambda method otherwise return null. |
| 70 | + */ |
| 71 | + public String getMethodSignature() { |
| 72 | + if (!this.found) { |
| 73 | + return null; |
| 74 | + } |
| 75 | + return BreakpointLocationLocator.toSignature(this.lambdaMethodBinding, getMethodName()); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Returns the name of lambda method otherwise return null. |
| 80 | + */ |
| 81 | + public String getMethodName() { |
| 82 | + if (!this.found) { |
| 83 | + return null; |
| 84 | + } |
| 85 | + String key = this.lambdaMethodBinding.getKey(); |
| 86 | + return key.substring(key.indexOf('.') + 1, key.indexOf('(')); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Returns the name of the type which the lambda method is found. |
| 91 | + */ |
| 92 | + public String getFullyQualifiedTypeName() { |
| 93 | + if (this.found) { |
| 94 | + ASTNode parent = lambdaExpression.getParent(); |
| 95 | + while (parent != null) { |
| 96 | + if (parent instanceof AbstractTypeDeclaration) { |
| 97 | + AbstractTypeDeclaration declaration = (AbstractTypeDeclaration) parent; |
| 98 | + return declaration.resolveBinding().getBinaryName(); |
| 99 | + } |
| 100 | + parent = parent.getParent(); |
| 101 | + } |
| 102 | + } |
| 103 | + return null; |
| 104 | + } |
| 105 | +} |
0 commit comments