|
| 1 | +/* |
| 2 | + * Copyright © 2025-present Stefano Cordio |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.github.scordio.junit.converters; |
| 17 | + |
| 18 | +import java.util.Collections; |
| 19 | +import java.util.IdentityHashMap; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.Objects; |
| 22 | + |
| 23 | +/** |
| 24 | + * Methods borrowed from {@link org.junit.platform.commons.util.ReflectionUtils}. |
| 25 | + */ |
| 26 | +class ReflectionUtils { |
| 27 | + |
| 28 | + private static final Map<Class<?>, Class<?>> primitiveToWrapperMap; |
| 29 | + |
| 30 | + static { |
| 31 | + @SuppressWarnings("IdentityHashMapUsage") |
| 32 | + Map<Class<?>, Class<?>> primitivesToWrappers = new IdentityHashMap<>(9); |
| 33 | + |
| 34 | + primitivesToWrappers.put(boolean.class, Boolean.class); |
| 35 | + primitivesToWrappers.put(byte.class, Byte.class); |
| 36 | + primitivesToWrappers.put(char.class, Character.class); |
| 37 | + primitivesToWrappers.put(short.class, Short.class); |
| 38 | + primitivesToWrappers.put(int.class, Integer.class); |
| 39 | + primitivesToWrappers.put(long.class, Long.class); |
| 40 | + primitivesToWrappers.put(float.class, Float.class); |
| 41 | + primitivesToWrappers.put(double.class, Double.class); |
| 42 | + |
| 43 | + primitiveToWrapperMap = Collections.unmodifiableMap(primitivesToWrappers); |
| 44 | + } |
| 45 | + |
| 46 | + // https://github.com/junit-team/junit-framework/issues/5641 |
| 47 | + static boolean isAssignableTo(Class<?> sourceType, Class<?> targetType) { |
| 48 | + Objects.requireNonNull(sourceType, "source type must not be null"); |
| 49 | + Objects.requireNonNull(targetType, "target type must not be null"); |
| 50 | + |
| 51 | + if (sourceType.isPrimitive()) { |
| 52 | + throw new IllegalArgumentException("source type must not be a primitive type"); |
| 53 | + } |
| 54 | + |
| 55 | + if (targetType.isAssignableFrom(sourceType)) { |
| 56 | + return true; |
| 57 | + } |
| 58 | + |
| 59 | + if (targetType.isPrimitive()) { |
| 60 | + return sourceType == primitiveToWrapperMap.get(targetType) || isWideningConversion(sourceType, targetType); |
| 61 | + } |
| 62 | + |
| 63 | + return false; |
| 64 | + } |
| 65 | + |
| 66 | + private static boolean isWideningConversion(Class<?> sourceType, Class<?> targetType) { |
| 67 | + if (!targetType.isPrimitive()) { |
| 68 | + throw new IllegalArgumentException("targetType must be primitive"); |
| 69 | + } |
| 70 | + |
| 71 | + boolean isPrimitive = sourceType.isPrimitive(); |
| 72 | + boolean isWrapper = primitiveToWrapperMap.containsValue(sourceType); |
| 73 | + |
| 74 | + // Neither a primitive nor a wrapper? |
| 75 | + if (!isPrimitive && !isWrapper) { |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + if (isPrimitive) { |
| 80 | + sourceType = primitiveToWrapperMap.get(sourceType); |
| 81 | + } |
| 82 | + |
| 83 | + // @formatter:off |
| 84 | + if (sourceType == Byte.class) { |
| 85 | + return |
| 86 | + targetType == short.class || |
| 87 | + targetType == int.class || |
| 88 | + targetType == long.class || |
| 89 | + targetType == float.class || |
| 90 | + targetType == double.class; |
| 91 | + } |
| 92 | + |
| 93 | + if (sourceType == Short.class || sourceType == Character.class) { |
| 94 | + return |
| 95 | + targetType == int.class || |
| 96 | + targetType == long.class || |
| 97 | + targetType == float.class || |
| 98 | + targetType == double.class; |
| 99 | + } |
| 100 | + |
| 101 | + if (sourceType == Integer.class) { |
| 102 | + return |
| 103 | + targetType == long.class || |
| 104 | + targetType == float.class || |
| 105 | + targetType == double.class; |
| 106 | + } |
| 107 | + |
| 108 | + if (sourceType == Long.class) { |
| 109 | + return |
| 110 | + targetType == float.class || |
| 111 | + targetType == double.class; |
| 112 | + } |
| 113 | + |
| 114 | + if (sourceType == Float.class) { |
| 115 | + return |
| 116 | + targetType == double.class; |
| 117 | + } |
| 118 | + // @formatter:on |
| 119 | + |
| 120 | + return false; |
| 121 | + } |
| 122 | + |
| 123 | + static String[] parseFullyQualifiedMethodName(String fullyQualifiedMethodName) { |
| 124 | + if (fullyQualifiedMethodName.isEmpty()) { |
| 125 | + throw new IllegalArgumentException("fullyQualifiedMethodName must not be null or blank"); |
| 126 | + } |
| 127 | + |
| 128 | + int indexOfFirstHashtag = fullyQualifiedMethodName.indexOf('#'); |
| 129 | + boolean validSyntax = (indexOfFirstHashtag > 0) |
| 130 | + && (indexOfFirstHashtag < fullyQualifiedMethodName.length() - 1); |
| 131 | + |
| 132 | + if (!validSyntax) { |
| 133 | + throw new IllegalArgumentException("[" + fullyQualifiedMethodName |
| 134 | + + "] is not a valid fully qualified method name: " |
| 135 | + + "it must start with a fully qualified class name followed by a '#' " |
| 136 | + + "and then the method name, optionally followed by a parameter list enclosed in parentheses."); |
| 137 | + } |
| 138 | + |
| 139 | + String className = fullyQualifiedMethodName.substring(0, indexOfFirstHashtag); |
| 140 | + String methodPart = fullyQualifiedMethodName.substring(indexOfFirstHashtag + 1); |
| 141 | + String methodName = methodPart; |
| 142 | + String methodParameters = ""; |
| 143 | + |
| 144 | + if (methodPart.endsWith("()")) { |
| 145 | + methodName = methodPart.substring(0, methodPart.length() - 2); |
| 146 | + } |
| 147 | + else if (methodPart.endsWith(")")) { |
| 148 | + int indexOfLastOpeningParenthesis = methodPart.lastIndexOf('('); |
| 149 | + if ((indexOfLastOpeningParenthesis > 0) && (indexOfLastOpeningParenthesis < methodPart.length() - 1)) { |
| 150 | + methodName = methodPart.substring(0, indexOfLastOpeningParenthesis); |
| 151 | + methodParameters = methodPart.substring(indexOfLastOpeningParenthesis + 1, methodPart.length() - 1); |
| 152 | + } |
| 153 | + } |
| 154 | + return new String[] { className, methodName, methodParameters }; |
| 155 | + } |
| 156 | + |
| 157 | +} |
0 commit comments