View Javadoc
1   /*
2    * Copyright (c) 2006 JMockit developers
3    * This file is subject to the terms of the MIT license (see LICENSE.txt).
4    */
5   package mockit.internal.util;
6   
7   import static mockit.internal.util.AutoBoxing.isWrapperOfPrimitiveType;
8   
9   import edu.umd.cs.findbugs.annotations.NonNull;
10  import edu.umd.cs.findbugs.annotations.Nullable;
11  
12  import java.math.BigDecimal;
13  import java.math.BigInteger;
14  import java.util.concurrent.atomic.AtomicInteger;
15  import java.util.concurrent.atomic.AtomicLong;
16  
17  public final class TypeConversion {
18      private TypeConversion() {
19      }
20  
21      @Nullable
22      public static Object convertFromString(@NonNull Class<?> targetType, @NonNull String value) {
23          if (targetType == String.class) {
24              return value;
25          }
26          if (isCharacter(targetType)) {
27              return value.charAt(0);
28          }
29          if (targetType.isPrimitive() || isWrapperOfPrimitiveType(targetType)) {
30              return newWrapperInstance(targetType, value);
31          }
32          if (targetType == BigDecimal.class) {
33              return new BigDecimal(value.trim());
34          }
35          if (targetType == BigInteger.class) {
36              return new BigInteger(value.trim());
37          }
38          if (targetType == AtomicInteger.class) {
39              return new AtomicInteger(Integer.parseInt(value.trim()));
40          }
41          if (targetType == AtomicLong.class) {
42              return new AtomicLong(Long.parseLong(value.trim()));
43          }
44          if (targetType.isEnum()) {
45              // noinspection unchecked
46              return enumValue(targetType, value);
47          }
48  
49          return null;
50      }
51  
52      private static boolean isCharacter(@NonNull Class<?> targetType) {
53          return targetType == char.class || targetType == Character.class;
54      }
55  
56      @NonNull
57      private static Object newWrapperInstance(@NonNull Class<?> targetType, @NonNull String value) {
58          String trimmedValue = value.trim();
59  
60          try {
61              if (targetType == int.class || targetType == Integer.class) {
62                  return Integer.valueOf(trimmedValue);
63              }
64              if (targetType == long.class || targetType == Long.class) {
65                  return Long.valueOf(trimmedValue);
66              }
67              if (targetType == short.class || targetType == Short.class) {
68                  return Short.valueOf(trimmedValue);
69              }
70              if (targetType == byte.class || targetType == Byte.class) {
71                  return Byte.valueOf(trimmedValue);
72              }
73              if (targetType == double.class || targetType == Double.class) {
74                  return Double.valueOf(trimmedValue);
75              }
76              if (targetType == float.class || targetType == Float.class) {
77                  return Float.valueOf(trimmedValue);
78              }
79          } catch (NumberFormatException e) {
80              throw new IllegalArgumentException("Invalid value \"" + trimmedValue + "\" for " + targetType);
81          }
82  
83          return Boolean.valueOf(trimmedValue);
84      }
85  
86      @NonNull
87      private static <E extends Enum<E>> Object enumValue(Class<?> targetType, @NonNull String value) {
88          @SuppressWarnings("unchecked")
89          Class<E> enumType = (Class<E>) targetType;
90          return Enum.valueOf(enumType, value);
91      }
92  }