Path: blob/master/src/jdk.jdi/share/classes/com/sun/tools/jdi/PrimitiveValueImpl.java
41161 views
/*1* Copyright (c) 1998, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.jdi;2627import com.sun.jdi.BooleanValue;28import com.sun.jdi.ClassNotLoadedException;29import com.sun.jdi.InternalException;30import com.sun.jdi.InvalidTypeException;31import com.sun.jdi.PrimitiveValue;32import com.sun.jdi.VirtualMachine;3334public abstract class PrimitiveValueImpl extends ValueImpl35implements PrimitiveValue36{37PrimitiveValueImpl(VirtualMachine aVm) {38super(aVm);39}4041abstract public boolean booleanValue();42abstract public byte byteValue();43abstract public char charValue();44abstract public short shortValue();45abstract public int intValue();46abstract public long longValue();47abstract public float floatValue();48abstract public double doubleValue();4950/*51* The checked versions of the value accessors throw52* InvalidTypeException if the required conversion is53* narrowing and would result in the loss of information54* (either magnitude or precision).55*56* Default implementations here do no checking; subclasses57* override as necessary to do the proper checking.58*/59byte checkedByteValue() throws InvalidTypeException {60return byteValue();61}62char checkedCharValue() throws InvalidTypeException {63return charValue();64}65short checkedShortValue() throws InvalidTypeException {66return shortValue();67}68int checkedIntValue() throws InvalidTypeException {69return intValue();70}71long checkedLongValue() throws InvalidTypeException {72return longValue();73}74float checkedFloatValue() throws InvalidTypeException {75return floatValue();76}7778final boolean checkedBooleanValue() throws InvalidTypeException {79/*80* Always disallow a conversion to boolean from any other81* primitive82*/83if (this instanceof BooleanValue) {84return booleanValue();85} else {86throw new InvalidTypeException("Can't convert non-boolean value to boolean");87}88}8990final double checkedDoubleValue() throws InvalidTypeException {91/*92* Can't overflow by converting to double, so this method93* is never overridden94*/95return doubleValue();96}9798ValueImpl prepareForAssignmentTo(ValueContainer destination)99throws InvalidTypeException100{101return convertForAssignmentTo(destination);102}103104ValueImpl convertForAssignmentTo(ValueContainer destination)105throws InvalidTypeException106{107JNITypeParser destSig = new JNITypeParser(destination.signature());108JNITypeParser sourceSig = new JNITypeParser(type().signature());109110if (destSig.isReference()) {111throw new InvalidTypeException("Can't assign primitive value to object");112}113114if (destSig.isBoolean() && !sourceSig.isBoolean()) {115throw new InvalidTypeException("Can't assign non-boolean value to a boolean");116}117118if (!destSig.isBoolean() && sourceSig.isBoolean()) {119throw new InvalidTypeException("Can't assign boolean value to an non-boolean");120}121122if (destSig.isVoid()) {123throw new InvalidTypeException("Can't assign primitive value to a void");124}125126try {127PrimitiveTypeImpl primitiveType = (PrimitiveTypeImpl)destination.type();128return (ValueImpl)(primitiveType.convert(this));129} catch (ClassNotLoadedException e) {130throw new InternalException("Signature and type inconsistent for: " +131destination.typeName());132}133}134}135136137