1 /*
   2  * Copyright (c) 2011, 2018, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 // generated from vm/mlvm/meth/share/PrimitiveTypeConverter.jmpp
  25 
  26 package vm.mlvm.meth.share;
  27 
  28 public class PrimitiveTypeConverter {
  29 
  30     /** Unbox, cast and box */
  31     public static Object cast(Object fromValue, Class<?> toType) {
  32 
  33         Class<?> fromType = fromValue.getClass();
  34 
  35         if ( Byte.class.equals(fromType) && toType.equals(byte.class) )
  36             return fromValue;
  37 
  38         if ( Byte.class.equals(fromType) && toType.equals(short.class) )
  39             return Short.valueOf((short) ((Byte) fromValue).byteValue());
  40 
  41         if ( Byte.class.equals(fromType) && toType.equals(char.class) )
  42             return Character.valueOf((char) ((Byte) fromValue).byteValue());
  43 
  44         if ( Byte.class.equals(fromType) && toType.equals(int.class) )
  45             return Integer.valueOf((int) ((Byte) fromValue).byteValue());
  46 
  47         if ( Byte.class.equals(fromType) && toType.equals(long.class) )
  48             return Long.valueOf((long) ((Byte) fromValue).byteValue());
  49 
  50         if ( Byte.class.equals(fromType) && toType.equals(float.class) )
  51             return Float.valueOf((float) ((Byte) fromValue).byteValue());
  52 
  53         if ( Byte.class.equals(fromType) && toType.equals(double.class) )
  54             return Double.valueOf((double) ((Byte) fromValue).byteValue());
  55 
  56         if ( Short.class.equals(fromType) && toType.equals(byte.class) )
  57             return Byte.valueOf((byte) ((Short) fromValue).shortValue());
  58 
  59         if ( Short.class.equals(fromType) && toType.equals(short.class) )
  60             return fromValue;
  61 
  62         if ( Short.class.equals(fromType) && toType.equals(char.class) )
  63             return Character.valueOf((char) ((Short) fromValue).shortValue());
  64 
  65         if ( Short.class.equals(fromType) && toType.equals(int.class) )
  66             return Integer.valueOf((int) ((Short) fromValue).shortValue());
  67 
  68         if ( Short.class.equals(fromType) && toType.equals(long.class) )
  69             return Long.valueOf((long) ((Short) fromValue).shortValue());
  70 
  71         if ( Short.class.equals(fromType) && toType.equals(float.class) )
  72             return Float.valueOf((float) ((Short) fromValue).shortValue());
  73 
  74         if ( Short.class.equals(fromType) && toType.equals(double.class) )
  75             return Double.valueOf((double) ((Short) fromValue).shortValue());
  76 
  77         if ( Character.class.equals(fromType) && toType.equals(byte.class) )
  78             return Byte.valueOf((byte) ((Character) fromValue).charValue());
  79 
  80         if ( Character.class.equals(fromType) && toType.equals(short.class) )
  81             return Short.valueOf((short) ((Character) fromValue).charValue());
  82 
  83         if ( Character.class.equals(fromType) && toType.equals(char.class) )
  84             return fromValue;
  85 
  86         if ( Character.class.equals(fromType) && toType.equals(int.class) )
  87             return Integer.valueOf((int) ((Character) fromValue).charValue());
  88 
  89         if ( Character.class.equals(fromType) && toType.equals(long.class) )
  90             return Long.valueOf((long) ((Character) fromValue).charValue());
  91 
  92         if ( Character.class.equals(fromType) && toType.equals(float.class) )
  93             return Float.valueOf((float) ((Character) fromValue).charValue());
  94 
  95         if ( Character.class.equals(fromType) && toType.equals(double.class) )
  96             return Double.valueOf((double) ((Character) fromValue).charValue());
  97 
  98         if ( Integer.class.equals(fromType) && toType.equals(byte.class) )
  99             return Byte.valueOf((byte) ((Integer) fromValue).intValue());
 100 
 101         if ( Integer.class.equals(fromType) && toType.equals(short.class) )
 102             return Short.valueOf((short) ((Integer) fromValue).intValue());
 103 
 104         if ( Integer.class.equals(fromType) && toType.equals(char.class) )
 105             return Character.valueOf((char) ((Integer) fromValue).intValue());
 106 
 107         if ( Integer.class.equals(fromType) && toType.equals(int.class) )
 108             return fromValue;
 109 
 110         if ( Integer.class.equals(fromType) && toType.equals(long.class) )
 111             return Long.valueOf((long) ((Integer) fromValue).intValue());
 112 
 113         if ( Integer.class.equals(fromType) && toType.equals(float.class) )
 114             return Float.valueOf((float) ((Integer) fromValue).intValue());
 115 
 116         if ( Integer.class.equals(fromType) && toType.equals(double.class) )
 117             return Double.valueOf((double) ((Integer) fromValue).intValue());
 118 
 119         if ( Long.class.equals(fromType) && toType.equals(byte.class) )
 120             return Byte.valueOf((byte) ((Long) fromValue).longValue());
 121 
 122         if ( Long.class.equals(fromType) && toType.equals(short.class) )
 123             return Short.valueOf((short) ((Long) fromValue).longValue());
 124 
 125         if ( Long.class.equals(fromType) && toType.equals(char.class) )
 126             return Character.valueOf((char) ((Long) fromValue).longValue());
 127 
 128         if ( Long.class.equals(fromType) && toType.equals(int.class) )
 129             return Integer.valueOf((int) ((Long) fromValue).longValue());
 130 
 131         if ( Long.class.equals(fromType) && toType.equals(long.class) )
 132             return fromValue;
 133 
 134         if ( Long.class.equals(fromType) && toType.equals(float.class) )
 135             return Float.valueOf((float) ((Long) fromValue).longValue());
 136 
 137         if ( Long.class.equals(fromType) && toType.equals(double.class) )
 138             return Double.valueOf((double) ((Long) fromValue).longValue());
 139 
 140         if ( Float.class.equals(fromType) && toType.equals(byte.class) )
 141             return Byte.valueOf((byte) ((Float) fromValue).floatValue());
 142 
 143         if ( Float.class.equals(fromType) && toType.equals(short.class) )
 144             return Short.valueOf((short) ((Float) fromValue).floatValue());
 145 
 146         if ( Float.class.equals(fromType) && toType.equals(char.class) )
 147             return Character.valueOf((char) ((Float) fromValue).floatValue());
 148 
 149         if ( Float.class.equals(fromType) && toType.equals(int.class) )
 150             return Integer.valueOf((int) ((Float) fromValue).floatValue());
 151 
 152         if ( Float.class.equals(fromType) && toType.equals(long.class) )
 153             return Long.valueOf((long) ((Float) fromValue).floatValue());
 154 
 155         if ( Float.class.equals(fromType) && toType.equals(float.class) )
 156             return fromValue;
 157 
 158         if ( Float.class.equals(fromType) && toType.equals(double.class) )
 159             return Double.valueOf((double) ((Float) fromValue).floatValue());
 160 
 161         if ( Double.class.equals(fromType) && toType.equals(byte.class) )
 162             return Byte.valueOf((byte) ((Double) fromValue).doubleValue());
 163 
 164         if ( Double.class.equals(fromType) && toType.equals(short.class) )
 165             return Short.valueOf((short) ((Double) fromValue).doubleValue());
 166 
 167         if ( Double.class.equals(fromType) && toType.equals(char.class) )
 168             return Character.valueOf((char) ((Double) fromValue).doubleValue());
 169 
 170         if ( Double.class.equals(fromType) && toType.equals(int.class) )
 171             return Integer.valueOf((int) ((Double) fromValue).doubleValue());
 172 
 173         if ( Double.class.equals(fromType) && toType.equals(long.class) )
 174             return Long.valueOf((long) ((Double) fromValue).doubleValue());
 175 
 176         if ( Double.class.equals(fromType) && toType.equals(float.class) )
 177             return Float.valueOf((float) ((Double) fromValue).doubleValue());
 178 
 179         if ( Double.class.equals(fromType) && toType.equals(double.class) )
 180             return fromValue;
 181 
 182         throw new IllegalArgumentException("Can't cast [" + fromValue + "] to " + toType);
 183     }
 184 
 185     /** Unbox, do primitive widening conversion (JLS 5.1.2) and box */
 186     public static Object convert(Object fromValue, Class<?> toType) {
 187         Class<?> fromType = fromValue.getClass();
 188 
 189         if ( Byte.class.equals(fromType) && toType.equals(byte.class) )
 190             return fromValue;
 191 
 192         if ( Short.class.equals(fromType) && toType.equals(short.class) )
 193             return fromValue;
 194 
 195         if ( Byte.class.equals(fromType) && toType.equals(short.class) )
 196             return Short.valueOf((short) ((Byte) fromValue).byteValue());
 197 
 198         if ( Integer.class.equals(fromType) && toType.equals(int.class) )
 199             return fromValue;
 200 
 201         if ( Byte.class.equals(fromType) && toType.equals(int.class) )
 202             return Integer.valueOf((int) ((Byte) fromValue).byteValue());
 203 
 204         if ( Short.class.equals(fromType) && toType.equals(int.class) )
 205             return Integer.valueOf((int) ((Short) fromValue).shortValue());
 206 
 207         if ( Character.class.equals(fromType) && toType.equals(int.class) )
 208             return Integer.valueOf((int) ((Character) fromValue).charValue());
 209 
 210         if ( Long.class.equals(fromType) && toType.equals(long.class) )
 211             return fromValue;
 212 
 213         if ( Byte.class.equals(fromType) && toType.equals(long.class) )
 214             return Long.valueOf((long) ((Byte) fromValue).byteValue());
 215 
 216         if ( Short.class.equals(fromType) && toType.equals(long.class) )
 217             return Long.valueOf((long) ((Short) fromValue).shortValue());
 218 
 219         if ( Character.class.equals(fromType) && toType.equals(long.class) )
 220             return Long.valueOf((long) ((Character) fromValue).charValue());
 221 
 222         if ( Integer.class.equals(fromType) && toType.equals(long.class) )
 223             return Long.valueOf((long) ((Integer) fromValue).intValue());
 224 
 225         if ( Float.class.equals(fromType) && toType.equals(float.class) )
 226             return fromValue;
 227 
 228         if ( Byte.class.equals(fromType) && toType.equals(float.class) )
 229             return Float.valueOf((float) ((Byte) fromValue).byteValue());
 230 
 231         if ( Short.class.equals(fromType) && toType.equals(float.class) )
 232             return Float.valueOf((float) ((Short) fromValue).shortValue());
 233 
 234         if ( Character.class.equals(fromType) && toType.equals(float.class) )
 235             return Float.valueOf((float) ((Character) fromValue).charValue());
 236 
 237         if ( Integer.class.equals(fromType) && toType.equals(float.class) )
 238             return Float.valueOf((float) ((Integer) fromValue).intValue());
 239 
 240         if ( Long.class.equals(fromType) && toType.equals(float.class) )
 241             return Float.valueOf((float) ((Long) fromValue).longValue());
 242 
 243         if ( Double.class.equals(fromType) && toType.equals(double.class) )
 244             return fromValue;
 245 
 246         if ( Byte.class.equals(fromType) && toType.equals(double.class) )
 247             return Double.valueOf((double) ((Byte) fromValue).byteValue());
 248 
 249         if ( Short.class.equals(fromType) && toType.equals(double.class) )
 250             return Double.valueOf((double) ((Short) fromValue).shortValue());
 251 
 252         if ( Character.class.equals(fromType) && toType.equals(double.class) )
 253             return Double.valueOf((double) ((Character) fromValue).charValue());
 254 
 255         if ( Integer.class.equals(fromType) && toType.equals(double.class) )
 256             return Double.valueOf((double) ((Integer) fromValue).intValue());
 257 
 258         if ( Long.class.equals(fromType) && toType.equals(double.class) )
 259             return Double.valueOf((double) ((Long) fromValue).longValue());
 260 
 261         if ( Float.class.equals(fromType) && toType.equals(double.class) )
 262             return Double.valueOf((double) ((Float) fromValue).floatValue());
 263 
 264         throw new IllegalArgumentException("Can't convert [" + fromValue + "] to " + toType);
 265     }
 266 }