1/* 2 * Copyright (c) 1998, 2006, 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.*; 29 30public abstract class PrimitiveValueImpl extends ValueImpl 31 implements PrimitiveValue { 32 33 PrimitiveValueImpl(VirtualMachine aVm) { 34 super(aVm); 35 } 36 37 abstract public boolean booleanValue(); 38 abstract public byte byteValue(); 39 abstract public char charValue(); 40 abstract public short shortValue(); 41 abstract public int intValue(); 42 abstract public long longValue(); 43 abstract public float floatValue(); 44 abstract public double doubleValue(); 45 46 /* 47 * The checked versions of the value accessors throw 48 * InvalidTypeException if the required conversion is 49 * narrowing and would result in the loss of information 50 * (either magnitude or precision). 51 * 52 * Default implementations here do no checking; subclasses 53 * override as necessary to do the proper checking. 54 */ 55 byte checkedByteValue() throws InvalidTypeException { 56 return byteValue(); 57 } 58 char checkedCharValue() throws InvalidTypeException { 59 return charValue(); 60 } 61 short checkedShortValue() throws InvalidTypeException { 62 return shortValue(); 63 } 64 int checkedIntValue() throws InvalidTypeException { 65 return intValue(); 66 } 67 long checkedLongValue() throws InvalidTypeException { 68 return longValue(); 69 } 70 float checkedFloatValue() throws InvalidTypeException { 71 return floatValue(); 72 } 73 74 final boolean checkedBooleanValue() throws InvalidTypeException { 75 /* 76 * Always disallow a conversion to boolean from any other 77 * primitive 78 */ 79 if (this instanceof BooleanValue) { 80 return booleanValue(); 81 } else { 82 throw new InvalidTypeException("Can't convert non-boolean value to boolean"); 83 } 84 } 85 86 final double checkedDoubleValue() throws InvalidTypeException { 87 /* 88 * Can't overflow by converting to double, so this method 89 * is never overridden 90 */ 91 return doubleValue(); 92 } 93 94 ValueImpl prepareForAssignmentTo(ValueContainer destination) 95 throws InvalidTypeException { 96 97 return convertForAssignmentTo(destination); 98 } 99 100 ValueImpl convertForAssignmentTo(ValueContainer destination) 101 throws InvalidTypeException { 102 103 /* 104 * TO DO: Centralize JNI signature knowledge 105 */ 106 if (destination.signature().length() > 1) { 107 throw new InvalidTypeException("Can't assign primitive value to object"); 108 } 109 110 if ((destination.signature().charAt(0) == 'Z') && 111 (type().signature().charAt(0) != 'Z')) { 112 throw new InvalidTypeException("Can't assign non-boolean value to a boolean"); 113 } 114 115 if ((destination.signature().charAt(0) != 'Z') && 116 (type().signature().charAt(0) == 'Z')) { 117 throw new InvalidTypeException("Can't assign boolean value to an non-boolean"); 118 } 119 120 if ("void".equals(destination.typeName())) { 121 throw new InvalidTypeException("Can't assign primitive value to a void"); 122 } 123 124 try { 125 PrimitiveTypeImpl primitiveType = (PrimitiveTypeImpl)destination.type(); 126 return (ValueImpl)(primitiveType.convert(this)); 127 } catch (ClassNotLoadedException e) { 128 throw new InternalException("Signature and type inconsistent for: " + 129 destination.typeName()); 130 } 131 } 132} 133