autoboxing support for debugger evaluators: binary/unary expressions, assignments...
[fedora-idea.git] / java / debugger / impl / src / com / intellij / debugger / engine / evaluation / expression / BoxingEvaluator.java
blob060bc902b4a0a96de1754686dcc8d78f85126797
1 /*
2 * Copyright 2000-2010 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.debugger.engine.evaluation.expression;
18 import com.intellij.debugger.engine.DebugProcessImpl;
19 import com.intellij.debugger.engine.JVMNameUtil;
20 import com.intellij.debugger.engine.evaluation.EvaluateException;
21 import com.intellij.debugger.engine.evaluation.EvaluationContextImpl;
22 import com.sun.jdi.*;
23 import org.jetbrains.annotations.Nullable;
25 import java.util.ArrayList;
26 import java.util.List;
28 /**
29 * @author Eugene Zhuravlev
30 * Date: Feb 8, 2010
32 public class BoxingEvaluator implements Evaluator{
33 private final Evaluator myOperand;
35 public BoxingEvaluator(Evaluator operand) {
36 myOperand = operand;
39 public Object evaluate(EvaluationContextImpl context) throws EvaluateException {
40 final Object result = myOperand.evaluate(context);
41 if (result == null || result instanceof ObjectReference) {
42 return result;
45 if (result instanceof BooleanValue) {
46 return convertToWrapper(context, (BooleanValue)result, "java.lang.Boolean");
48 if (result instanceof ByteValue) {
49 return convertToWrapper(context, (ByteValue)result, "java.lang.Byte");
51 if (result instanceof CharValue) {
52 return convertToWrapper(context, (CharValue)result, "java.lang.Character");
54 if (result instanceof ShortValue) {
55 return convertToWrapper(context, (ShortValue)result, "java.lang.Short");
57 if (result instanceof IntegerValue) {
58 return convertToWrapper(context, (IntegerValue)result, "java.lang.Integer");
60 if (result instanceof LongValue) {
61 return convertToWrapper(context, (LongValue)result, "java.lang.Long");
63 if (result instanceof FloatValue) {
64 return convertToWrapper(context, (FloatValue)result, "java.lang.Float");
66 if (result instanceof DoubleValue) {
67 return convertToWrapper(context, (DoubleValue)result, "java.lang.Double");
69 throw new EvaluateException("Cannot perform boxing conversion for a value of type " + ((Value)result).type().name());
72 @Nullable
73 public Modifier getModifier() {
74 return null;
77 private static Value convertToWrapper(EvaluationContextImpl context, PrimitiveValue value, String wrapperTypeName) throws
78 EvaluateException {
79 final DebugProcessImpl process = context.getDebugProcess();
80 final ClassType wrapperClass = (ClassType)process.findClass(context, wrapperTypeName, null);
81 final String methodSignature = "(" + JVMNameUtil.getPrimitiveSignature(value.type().name()) + ")L" + wrapperTypeName.replace('.', '/') + ";";
83 List<Method> methods = wrapperClass.methodsByName("valueOf", methodSignature);
84 if (methods.size() == 0) { // older JDK version
85 methods = wrapperClass.methodsByName("<init>", methodSignature);
87 if (methods.size() == 0) {
88 throw new EvaluateException("Cannot construct wrapper object for value of type " + value.type() + ": Unable to find either valueOf() or constructor method");
91 final Method factoryMethod = methods.get(0);
93 final ArrayList args = new ArrayList();
94 args.add(value);
96 return process.invokeMethod(context, wrapperClass, factoryMethod, args);