|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2026 IBM Corporation. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * IBM Corporation - initial API and implementation |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.jdt.internal.debug.ui.heapwalking; |
| 15 | + |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +import org.eclipse.core.runtime.CoreException; |
| 19 | +import org.eclipse.debug.core.DebugException; |
| 20 | +import org.eclipse.debug.core.model.IVariable; |
| 21 | +import org.eclipse.debug.internal.ui.views.variables.IndexedVariablePartition; |
| 22 | +import org.eclipse.debug.ui.actions.IWatchExpressionFactoryAdapter2; |
| 23 | +import org.eclipse.debug.ui.actions.IWatchExpressionFactoryAdapterExtension; |
| 24 | +import org.eclipse.jdt.internal.debug.core.logicalstructures.JDIPlaceholderVariable; |
| 25 | +import org.eclipse.jdt.internal.debug.core.model.JDIArrayEntryVariable; |
| 26 | +import org.eclipse.jdt.internal.debug.core.model.JDIPlaceholderValue; |
| 27 | +import org.eclipse.jdt.internal.debug.core.model.JDIReferenceListEntryVariable; |
| 28 | +import org.eclipse.jdt.internal.debug.core.model.JDIReferenceListVariable; |
| 29 | + |
| 30 | +/** |
| 31 | + * Uses the {@link IWatchExpressionFactoryAdapterExtension} to filter when the watch expression action is available based on the variable selected. |
| 32 | + * |
| 33 | + * Currently removes the action from {@link JDIPlaceholderVariable}s and {@link JDIReferenceListVariable}s. |
| 34 | + */ |
| 35 | +public class JavaNestedWatchExpressionFilter implements IWatchExpressionFactoryAdapter2 { |
| 36 | + |
| 37 | + @Override |
| 38 | + public String createWatchExpression(Object element) throws CoreException { |
| 39 | + if (element instanceof List<?> expVariablesList) { |
| 40 | + StringBuilder expresion = new StringBuilder(); |
| 41 | + for (Object ob : expVariablesList) { |
| 42 | + if (ob instanceof IVariable variable) { |
| 43 | + String current = variable.getName(); |
| 44 | + expresion.append(current); |
| 45 | + expresion.append('.'); |
| 46 | + } |
| 47 | + } |
| 48 | + expresion.deleteCharAt(expresion.length() - 1); |
| 49 | + return expresion.toString(); |
| 50 | + } |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public boolean canCreateWatchExpression(Object variable) { |
| 56 | + if (variable instanceof List<?> expVariablesList) { |
| 57 | + for (Object ob : expVariablesList) { |
| 58 | + if (ob instanceof IVariable variable2) { |
| 59 | + return checkForValidVariable(variable2); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + return false; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Checks whether the given {@link IVariable} is suitable for creating a watch expression. |
| 68 | + * <p> |
| 69 | + * Filters out variables that represent internal or synthetic structures (such as reference lists, array entries, indexed partitions) or |
| 70 | + * placeholder values that do not have a concrete value. |
| 71 | + * </p> |
| 72 | + * |
| 73 | + * @param variable |
| 74 | + * the variable to check |
| 75 | + * @return {@code true} if the variable can be used to create a watch expression, {@code false} otherwise |
| 76 | + */ |
| 77 | + private boolean checkForValidVariable(IVariable variable) { |
| 78 | + if (variable instanceof JDIReferenceListVariable || variable instanceof JDIReferenceListEntryVariable |
| 79 | + || variable instanceof JDIArrayEntryVariable || variable instanceof IndexedVariablePartition) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + try { |
| 83 | + if (variable.getValue() instanceof JDIPlaceholderValue) { |
| 84 | + return false; |
| 85 | + } |
| 86 | + } catch (DebugException e) { |
| 87 | + } |
| 88 | + return true; |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments