KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > eval > ast > instructions > SendMessage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM 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  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.debug.eval.ast.instructions;
12
13
14 import com.ibm.icu.text.MessageFormat;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Status;
19 import org.eclipse.jdt.debug.core.IJavaObject;
20 import org.eclipse.jdt.debug.core.IJavaValue;
21 import org.eclipse.jdt.debug.core.IJavaVariable;
22 import org.eclipse.jdt.internal.debug.core.JDIDebugPlugin;
23  
24 /**
25  * Sends an message to an instance. The arguments are on the
26  * stack in reverse order, followed by the receiver.
27  * Pushes the result, if any, onto the stack
28  */

29 public class SendMessage extends CompoundInstruction {
30     
31     private int fArgCount;
32     private String JavaDoc fSelector;
33     private String JavaDoc fSignature;
34     private String JavaDoc fDeclaringType;
35     
36     public SendMessage(String JavaDoc selector, String JavaDoc signature, int argCount, String JavaDoc declaringType, int start) {
37         super(start);
38         fArgCount= argCount;
39         fSelector= selector;
40         fSignature= signature;
41         fDeclaringType= declaringType;
42     }
43     
44     public void execute() throws CoreException {
45         IJavaValue[] args = new IJavaValue[fArgCount];
46         // args are in reverse order
47
for (int i= fArgCount - 1; i >= 0; i--) {
48             args[i] = popValue();
49         }
50         Object JavaDoc receiver = pop();
51         IJavaValue result = null;
52         
53         if (receiver instanceof IJavaVariable) {
54             receiver = ((IJavaVariable) receiver).getValue();
55         }
56         
57         if (receiver instanceof IJavaObject) {
58             result = ((IJavaObject)receiver).sendMessage(fSelector, fSignature, args, getContext().getThread(), fDeclaringType);
59         } else {
60             throw new CoreException(new Status(IStatus.ERROR, JDIDebugPlugin.getUniqueIdentifier(), IStatus.OK, InstructionsEvaluationMessages.SendMessage_Attempt_to_send_a_message_to_a_non_object_value_1, null));
61         }
62         setLastValue(result);
63         if (!fSignature.endsWith(")V")) { //$NON-NLS-1$
64
// only push the result if not a void method
65
push(result);
66         }
67     }
68     
69     public String JavaDoc toString() {
70         return MessageFormat.format(InstructionsEvaluationMessages.SendMessage_send_message__0___1__2, new String JavaDoc[]{fSelector,fSignature});
71     }
72 }
73
74
Popular Tags