KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.debug.core.DebugException;
15 import org.eclipse.jdt.debug.core.IJavaObject;
16 import org.eclipse.jdt.debug.core.IJavaPrimitiveValue;
17 import org.eclipse.jdt.debug.core.IJavaValue;
18 import org.eclipse.jdt.internal.debug.core.model.JDINullValue;
19
20 public class PlusOperator extends BinaryOperator {
21
22     public static final String JavaDoc NULL= "null"; //$NON-NLS-1$
23
public static final String JavaDoc TOSTRING_SELECTOR= "toString"; //$NON-NLS-1$
24
public static final String JavaDoc TOSTRING_SIGNATURE= "()Ljava/lang/String;"; //$NON-NLS-1$
25

26     public PlusOperator(int resultId, int leftTypeId, int rightTypeId, int start) {
27         this(resultId, leftTypeId, rightTypeId, false, start);
28     }
29     
30     protected PlusOperator(int resultId, int leftTypeId, int rightTypeId, boolean isAssignmentOperator, int start) {
31         super(resultId, leftTypeId, rightTypeId, isAssignmentOperator, start);
32     }
33     
34     private String JavaDoc getString(IJavaValue value, int typeId) throws DebugException {
35         
36         // test if value == null
37
if (value instanceof JDINullValue) {
38             return NULL;
39         }
40         
41         if (value instanceof IJavaObject) {
42             if (typeId == T_String) {
43                 return value.getValueString();
44             }
45             return ((IJavaObject) value).sendMessage(TOSTRING_SELECTOR, TOSTRING_SIGNATURE, null, getContext().getThread(), null).getValueString();
46         }
47         
48         IJavaPrimitiveValue primitiveValue= (IJavaPrimitiveValue)value;
49         switch (typeId) {
50             case T_boolean:
51                 return Boolean.valueOf(primitiveValue.getBooleanValue()).toString();
52             case T_byte:
53                 return new Integer JavaDoc(primitiveValue.getByteValue()).toString();
54             case T_char:
55                 return new Character JavaDoc(primitiveValue.getCharValue()).toString();
56             case T_double:
57                 return new Double JavaDoc(primitiveValue.getDoubleValue()).toString();
58             case T_float:
59                 return new Float JavaDoc(primitiveValue.getFloatValue()).toString();
60             case T_int:
61                 return new Integer JavaDoc(primitiveValue.getIntValue()).toString();
62             case T_long:
63                 return new Long JavaDoc(primitiveValue.getLongValue()).toString();
64             case T_short:
65                 return new Integer JavaDoc(primitiveValue.getShortValue()).toString();
66         }
67         return NULL;
68     }
69     
70     /*
71      * @see BinaryOperator#getBooleanResult(IJavaValue, IJavaValue)
72      */

73     protected boolean getBooleanResult(IJavaValue leftOperand, IJavaValue rightOperand) {
74         return false;
75     }
76
77     /*
78      * @see BinaryOperator#getDoubleResult(IJavaValue, IJavaValue)
79      */

80     protected double getDoubleResult(IJavaValue leftOperand, IJavaValue rightOperand) {
81         return ((IJavaPrimitiveValue)leftOperand).getDoubleValue() + ((IJavaPrimitiveValue)rightOperand).getDoubleValue();
82     }
83
84     /*
85      * @see BinaryOperator#getFloatResult(IJavaValue, IJavaValue)
86      */

87     protected float getFloatResult(IJavaValue leftOperand, IJavaValue rightOperand) {
88         return ((IJavaPrimitiveValue)leftOperand).getFloatValue() + ((IJavaPrimitiveValue)rightOperand).getFloatValue();
89     }
90
91     /*
92      * @see BinaryOperator#getIntResult(IJavaValue, IJavaValue)
93      */

94     protected int getIntResult(IJavaValue leftOperand, IJavaValue rightOperand) {
95         return ((IJavaPrimitiveValue)leftOperand).getIntValue() + ((IJavaPrimitiveValue)rightOperand).getIntValue();
96     }
97
98     /*
99      * @see BinaryOperator#getLongResult(IJavaValue, IJavaValue)
100      */

101     protected long getLongResult(IJavaValue leftOperand, IJavaValue rightOperand) {
102         return ((IJavaPrimitiveValue)leftOperand).getLongValue() + ((IJavaPrimitiveValue)rightOperand).getLongValue();
103     }
104
105     /*
106      * @see BinaryOperator#getStringResult(IJavaValue, IJavaValue)
107      */

108     protected String JavaDoc getStringResult(IJavaValue leftOperand, IJavaValue rightOperand) throws CoreException {
109         return getString(leftOperand, fLeftTypeId) + getString(rightOperand, fRightTypeId);
110     }
111
112     public String JavaDoc toString() {
113         return InstructionsEvaluationMessages.PlusOperator______operator_2;
114     }
115
116 }
117
Popular Tags