1 11 package org.eclipse.jdt.internal.debug.ui.actions; 12 13 import java.lang.reflect.InvocationTargetException ; 14 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.core.runtime.IAdaptable; 17 import org.eclipse.core.runtime.IProgressMonitor; 18 import org.eclipse.core.runtime.IStatus; 19 import org.eclipse.core.runtime.Status; 20 import org.eclipse.debug.core.DebugEvent; 21 import org.eclipse.debug.core.DebugException; 22 import org.eclipse.debug.core.ILaunch; 23 import org.eclipse.debug.core.model.ISourceLocator; 24 import org.eclipse.debug.core.model.IValue; 25 import org.eclipse.debug.core.model.IVariable; 26 import org.eclipse.debug.ui.DebugUITools; 27 import org.eclipse.debug.ui.IDebugUIConstants; 28 import org.eclipse.debug.ui.actions.IVariableValueEditor; 29 import org.eclipse.jdt.core.IJavaElement; 30 import org.eclipse.jdt.core.IJavaProject; 31 import org.eclipse.jdt.debug.core.IJavaDebugTarget; 32 import org.eclipse.jdt.debug.core.IJavaStackFrame; 33 import org.eclipse.jdt.debug.core.IJavaThread; 34 import org.eclipse.jdt.debug.core.IJavaVariable; 35 import org.eclipse.jdt.debug.eval.IAstEvaluationEngine; 36 import org.eclipse.jdt.debug.eval.IEvaluationListener; 37 import org.eclipse.jdt.debug.eval.IEvaluationResult; 38 import org.eclipse.jdt.internal.debug.core.JDIDebugPlugin; 39 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; 40 import org.eclipse.jface.operation.IRunnableWithProgress; 41 import org.eclipse.jface.window.Window; 42 import org.eclipse.swt.widgets.Shell; 43 import org.eclipse.ui.PlatformUI; 44 import org.eclipse.ui.progress.IProgressService; 45 46 import com.ibm.icu.text.MessageFormat; 47 import com.sun.jdi.InvalidTypeException; 48 import com.sun.jdi.InvocationException; 49 import com.sun.jdi.ObjectReference; 50 51 55 public class JavaObjectValueEditor implements IVariableValueEditor { 56 57 60 public boolean editVariable(IVariable variable, Shell shell) { 61 try { 62 IJavaVariable javaVariable = (IJavaVariable) variable; 63 String signature = javaVariable.getSignature(); 64 if ("Ljava/lang/String;".equals(signature)) { StringValueInputDialog dialog= new StringValueInputDialog(shell, javaVariable); 66 if (dialog.open() == Window.OK) { 67 String result = dialog.getResult(); 68 if (dialog.isUseLiteralValue()) { 69 variable.setValue(result); 70 } else { 71 setValue(variable, result); 72 } 73 } 74 } else { 75 ExpressionInputDialog dialog= new ExpressionInputDialog(shell, javaVariable); 76 if (dialog.open() == Window.OK) { 77 String result = dialog.getResult(); 78 setValue(variable, result); 79 } 80 } 81 } catch (DebugException e) { 82 handleException(e); 83 } 84 return true; 85 } 86 87 90 public boolean saveVariable(IVariable variable, String expression, Shell shell) { 91 IJavaVariable javaVariable = (IJavaVariable) variable; 92 String signature= null; 93 try { 94 signature = javaVariable.getSignature(); 95 if ("Ljava/lang/String;".equals(signature)) { return false; 97 } 98 setValue(variable, expression); 99 } catch (DebugException e) { 100 handleException(e); 101 } 102 return true; 103 } 104 105 114 protected void setValue(final IVariable variable, final String expression) throws DebugException { 115 IProgressService service= PlatformUI.getWorkbench().getProgressService(); 116 117 IRunnableWithProgress runnable = new IRunnableWithProgress() { 118 public void run(IProgressMonitor monitor) throws InvocationTargetException { 119 try { 120 IValue newValue = evaluate(expression); 121 if (newValue != null) { 122 variable.setValue(newValue); 123 } 124 } catch (DebugException de) { 125 throw new InvocationTargetException (de); 126 } 127 } 128 }; 129 130 try { 131 service.busyCursorWhile(runnable); 132 } catch (InvocationTargetException e) { 133 if (e.getTargetException() instanceof DebugException) { 134 throw (DebugException)e.getTargetException(); 135 } 136 } catch (InterruptedException e) { 137 } 138 } 139 140 143 protected void handleException(DebugException e) { 144 Throwable cause = e.getStatus().getException(); 145 if (cause instanceof InvalidTypeException) { 146 IStatus status = new Status(IStatus.ERROR, JDIDebugUIPlugin.getUniqueIdentifier(), IDebugUIConstants.INTERNAL_ERROR, cause.getMessage(), null); 147 JDIDebugUIPlugin.statusDialog(ActionMessages.JavaObjectValueEditor_3, status); 148 } else { 149 JDIDebugUIPlugin.statusDialog(e.getStatus()); 150 } 151 } 152 153 158 private IValue evaluate(String stringValue) throws DebugException { 159 IAdaptable adaptable = DebugUITools.getDebugContext(); 160 IJavaStackFrame frame= (IJavaStackFrame) adaptable.getAdapter(IJavaStackFrame.class); 161 if (frame != null) { 162 IJavaThread thread = (IJavaThread) frame.getThread(); 163 IJavaProject project= getProject(frame); 164 if (project != null) { 165 final IEvaluationResult[] results= new IEvaluationResult[1]; 166 IAstEvaluationEngine engine = JDIDebugPlugin.getDefault().getEvaluationEngine(project, (IJavaDebugTarget) thread.getDebugTarget()); 167 IEvaluationListener listener= new IEvaluationListener() { 168 public void evaluationComplete(IEvaluationResult result) { 169 synchronized (JavaObjectValueEditor.this) { 170 results[0]= result; 171 JavaObjectValueEditor.this.notifyAll(); 172 } 173 } 174 }; 175 synchronized(this) { 176 engine.evaluate(stringValue, frame, listener, DebugEvent.EVALUATION_IMPLICIT, false); 177 try { 178 this.wait(); 179 } catch (InterruptedException e) { 180 } 181 } 182 IEvaluationResult result= results[0]; 183 if (result == null) { 184 return null; 185 } 186 if (result.hasErrors()) { 187 DebugException exception = result.getException(); 188 StringBuffer buffer = new StringBuffer (); 189 if (exception == null) { 190 String [] messages = result.getErrorMessages(); 191 for (int i = 0; i < messages.length; i++) { 192 buffer.append(messages[i]).append("\n "); } 194 } else { 195 buffer.append(EvaluateAction.getExceptionMessage(exception)); 196 } 197 IStatus status= new Status(IStatus.ERROR, JDIDebugUIPlugin.getUniqueIdentifier(), IStatus.ERROR, buffer.toString(), null); 198 throw new DebugException(status); 199 } 200 return result.getValue(); 201 } 202 } 203 return null; 204 } 205 206 209 protected String getExceptionMessage(Throwable exception) { 210 if (exception instanceof CoreException) { 211 CoreException ce = (CoreException)exception; 212 Throwable throwable= ce.getStatus().getException(); 213 if (throwable instanceof com.sun.jdi.InvocationException) { 214 return getInvocationExceptionMessage((com.sun.jdi.InvocationException)throwable); 215 } else if (throwable instanceof CoreException) { 216 return getExceptionMessage(throwable); 218 } 219 return ce.getStatus().getMessage(); 220 } 221 String message= MessageFormat.format(ActionMessages.Evaluate_error_message_direct_exception, new Object [] { exception.getClass() }); 222 if (exception.getMessage() != null) { 223 message= MessageFormat.format(ActionMessages.Evaluate_error_message_exception_pattern, new Object [] { message, exception.getMessage() }); 224 } 225 return message; 226 } 227 228 231 protected String getInvocationExceptionMessage(com.sun.jdi.InvocationException exception) { 232 InvocationException ie= exception; 233 ObjectReference ref= ie.exception(); 234 return MessageFormat.format(ActionMessages.Evaluate_error_message_wrapped_exception, new Object [] { ref.referenceType().name() }); 235 } 236 237 241 private IJavaProject getProject(IJavaStackFrame javaStackFrame) { 242 ILaunch launch = javaStackFrame.getLaunch(); 243 if (launch == null) { 244 return null; 245 } 246 ISourceLocator locator= launch.getSourceLocator(); 247 if (locator == null) { 248 return null; 249 } 250 251 Object sourceElement = locator.getSourceElement(javaStackFrame); 252 if (!(sourceElement instanceof IJavaElement) && sourceElement instanceof IAdaptable) { 253 sourceElement = ((IAdaptable)sourceElement).getAdapter(IJavaElement.class); 254 } 255 if (sourceElement instanceof IJavaElement) { 256 return ((IJavaElement) sourceElement).getJavaProject(); 257 } 258 return null; 259 } 260 } 261 | Popular Tags |