1/*
2 * Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.tools.jdi;
27
28import com.sun.jdi.BooleanValue;
29import com.sun.jdi.ClassNotLoadedException;
30import com.sun.jdi.InternalException;
31import com.sun.jdi.InvalidTypeException;
32import com.sun.jdi.PrimitiveValue;
33import com.sun.jdi.VirtualMachine;
34
35public abstract class PrimitiveValueImpl extends ValueImpl
36                                         implements PrimitiveValue
37{
38    PrimitiveValueImpl(VirtualMachine aVm) {
39        super(aVm);
40    }
41
42    abstract public boolean booleanValue();
43    abstract public byte byteValue();
44    abstract public char charValue();
45    abstract public short shortValue();
46    abstract public int intValue();
47    abstract public long longValue();
48    abstract public float floatValue();
49    abstract public double doubleValue();
50
51    /*
52     * The checked versions of the value accessors throw
53     * InvalidTypeException if the required conversion is
54     * narrowing and would result in the loss of information
55     * (either magnitude or precision).
56     *
57     * Default implementations here do no checking; subclasses
58     * override as necessary to do the proper checking.
59     */
60    byte checkedByteValue() throws InvalidTypeException {
61        return byteValue();
62    }
63    char checkedCharValue() throws InvalidTypeException {
64        return charValue();
65    }
66    short checkedShortValue() throws InvalidTypeException {
67        return shortValue();
68    }
69    int checkedIntValue() throws InvalidTypeException {
70        return intValue();
71    }
72    long checkedLongValue() throws InvalidTypeException {
73        return longValue();
74    }
75    float checkedFloatValue() throws InvalidTypeException {
76        return floatValue();
77    }
78
79    final boolean checkedBooleanValue() throws InvalidTypeException {
80        /*
81         * Always disallow a conversion to boolean from any other
82         * primitive
83         */
84        if (this instanceof BooleanValue) {
85            return booleanValue();
86        } else {
87            throw new InvalidTypeException("Can't convert non-boolean value to boolean");
88        }
89    }
90
91    final double checkedDoubleValue() throws InvalidTypeException {
92        /*
93         * Can't overflow by converting to double, so this method
94         * is never overridden
95         */
96        return doubleValue();
97    }
98
99    ValueImpl prepareForAssignmentTo(ValueContainer destination)
100        throws InvalidTypeException
101    {
102        return convertForAssignmentTo(destination);
103    }
104
105    ValueImpl convertForAssignmentTo(ValueContainer destination)
106        throws InvalidTypeException
107    {
108        /*
109         * TO DO: Centralize JNI signature knowledge
110         */
111        if (destination.signature().length() > 1) {
112            throw new InvalidTypeException("Can't assign primitive value to object");
113        }
114
115        if ((destination.signature().charAt(0) == 'Z') &&
116            (type().signature().charAt(0) != 'Z')) {
117            throw new InvalidTypeException("Can't assign non-boolean value to a boolean");
118        }
119
120        if ((destination.signature().charAt(0) != 'Z') &&
121            (type().signature().charAt(0) == 'Z')) {
122            throw new InvalidTypeException("Can't assign boolean value to an non-boolean");
123        }
124
125        if ("void".equals(destination.typeName())) {
126            throw new InvalidTypeException("Can't assign primitive value to a void");
127        }
128
129        try {
130            PrimitiveTypeImpl primitiveType = (PrimitiveTypeImpl)destination.type();
131            return (ValueImpl)(primitiveType.convert(this));
132        } catch (ClassNotLoadedException e) {
133            throw new InternalException("Signature and type inconsistent for: " +
134                                        destination.typeName());
135        }
136    }
137}
138