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 package vm.mlvm.meth.share;
  25 
  26 public class PrimitiveTypeConverter {
  27 
  28     /** Unbox, cast and box */
  29     public static Object cast(Object fromValue, Class<?> toType) {
  30 
  31         Class<?> fromType = fromValue.getClass();
  32 #
  33 # String[] ptypes = new String[] { "byte", "short", "char", "int", "long", "float", "double" };
  34 # String[] btypes = new String[] { "Byte", "Short", "Character", "Integer", "Long", "Float", "Double" };
  35 #
  36 # for ( int f = 0; f < ptypes.length; f++ ) {
  37 #   String fb = btypes[f];
  38 #   String fp = ptypes[f];
  39 #   for ( int t = 0; t < ptypes.length; t++ ) {
  40 #     String tp = ptypes[t];
  41 #     String tb = btypes[t];
  42 
  43         if ( @fb.class.equals(fromType) && toType.equals(@tp.class) )
  44 #
  45 #     if ( f == t ) {
  46 #
  47             return fromValue;
  48 #
  49 #     } else {
  50 #
  51             return @tb.valueOf((@tp) ((@fb) fromValue).@(fp)Value());
  52 #
  53 #     }
  54 #   }
  55 # }
  56 
  57         throw new IllegalArgumentException("Can't cast [" + fromValue + "] to " + toType);
  58     }
  59 
  60     /** Unbox, do primitive widening conversion (JLS 5.1.2) and box */
  61     public static Object convert(Object fromValue, Class<?> toType) {
  62         Class<?> fromType = fromValue.getClass();
  63 #
  64 # for ( int t = 0; t < ptypes.length; t++ ) {
  65 #   String tp = ptypes[t];
  66 #   String tb = btypes[t];
  67 #
  68 #   if ( tp.equals("char") ) continue;
  69 #
  70 
  71         if ( @tb.class.equals(fromType) && toType.equals(@tp.class) )
  72             return fromValue;
  73 #
  74 #   for ( int f = 0; f < t; f++ ) {
  75 #     String fp = ptypes[f];
  76 #     String fb = btypes[f];
  77 
  78         if ( @fb.class.equals(fromType) && toType.equals(@tp.class) )
  79             return @tb.valueOf((@tp) ((@fb) fromValue).@(fp)Value());
  80 #   }
  81 # }
  82 
  83         throw new IllegalArgumentException("Can't convert [" + fromValue + "] to " + toType);
  84     }
  85 }