• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hazendaz / jmockit1 / 496

15 Nov 2025 05:33PM UTC coverage: 72.192% (-0.008%) from 72.2%
496

push

github

web-flow
Merge pull request #412 from hazendaz/renovate/major-spring-core

Update spring core to v7 (major)

5677 of 8360 branches covered (67.91%)

Branch coverage included in aggregate %.

11922 of 16018 relevant lines covered (74.43%)

0.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.18
/main/src/main/java/mockit/internal/util/TypeConversion.java
1
/*
2
 * MIT License
3
 * Copyright (c) 2006-2025 JMockit developers
4
 * See LICENSE file for full license text.
5
 */
6
package mockit.internal.util;
7

8
import static mockit.internal.util.AutoBoxing.isWrapperOfPrimitiveType;
9

10
import edu.umd.cs.findbugs.annotations.NonNull;
11
import edu.umd.cs.findbugs.annotations.Nullable;
12

13
import java.math.BigDecimal;
14
import java.math.BigInteger;
15
import java.util.concurrent.atomic.AtomicInteger;
16
import java.util.concurrent.atomic.AtomicLong;
17

18
public final class TypeConversion {
19
    private TypeConversion() {
20
    }
21

22
    @Nullable
23
    public static Object convertFromString(@NonNull Class<?> targetType, @NonNull String value) {
24
        if (targetType == String.class) {
1✔
25
            return value;
1✔
26
        }
27
        if (isCharacter(targetType)) {
1✔
28
            return value.charAt(0);
1✔
29
        }
30
        if (targetType.isPrimitive() || isWrapperOfPrimitiveType(targetType)) {
1✔
31
            return newWrapperInstance(targetType, value);
1✔
32
        }
33
        if (targetType == BigDecimal.class) {
1✔
34
            return new BigDecimal(value.trim());
1✔
35
        }
36
        if (targetType == BigInteger.class) {
1✔
37
            return new BigInteger(value.trim());
1✔
38
        }
39
        if (targetType == AtomicInteger.class) {
1✔
40
            return new AtomicInteger(Integer.parseInt(value.trim()));
1✔
41
        }
42
        if (targetType == AtomicLong.class) {
1✔
43
            return new AtomicLong(Long.parseLong(value.trim()));
1✔
44
        }
45
        if (targetType.isEnum()) {
1!
46
            // noinspection unchecked
47
            return enumValue(targetType, value);
1✔
48
        }
49

50
        return null;
×
51
    }
52

53
    private static boolean isCharacter(@NonNull Class<?> targetType) {
54
        return targetType == char.class || targetType == Character.class;
1✔
55
    }
56

57
    @NonNull
58
    private static Object newWrapperInstance(@NonNull Class<?> targetType, @NonNull String value) {
59
        String trimmedValue = value.trim();
1✔
60

61
        try {
62
            if (targetType == int.class || targetType == Integer.class) {
1✔
63
                return Integer.valueOf(trimmedValue);
1✔
64
            }
65
            if (targetType == long.class || targetType == Long.class) {
1✔
66
                return Long.valueOf(trimmedValue);
1✔
67
            }
68
            if (targetType == short.class || targetType == Short.class) {
1✔
69
                return Short.valueOf(trimmedValue);
1✔
70
            }
71
            if (targetType == byte.class || targetType == Byte.class) {
1✔
72
                return Byte.valueOf(trimmedValue);
1✔
73
            }
74
            if (targetType == double.class || targetType == Double.class) {
1✔
75
                return Double.valueOf(trimmedValue);
1✔
76
            }
77
            if (targetType == float.class || targetType == Float.class) {
1✔
78
                return Float.valueOf(trimmedValue);
1✔
79
            }
80
        } catch (NumberFormatException e) {
×
81
            throw new IllegalArgumentException("Invalid value \"" + trimmedValue + "\" for " + targetType);
×
82
        }
1✔
83

84
        return Boolean.valueOf(trimmedValue);
1✔
85
    }
86

87
    @NonNull
88
    private static <E extends Enum<E>> Object enumValue(Class<?> targetType, @NonNull String value) {
89
        @SuppressWarnings("unchecked")
90
        Class<E> enumType = (Class<E>) targetType;
1✔
91
        return Enum.valueOf(enumType, value);
1✔
92
    }
93
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc