|
| 1 | +/* |
| 2 | + * Copyright 2015-2023 the original author or authors. |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials are |
| 5 | + * made available under the terms of the Eclipse Public License v2.0 which |
| 6 | + * accompanies this distribution and is available at |
| 7 | + * |
| 8 | + * https://www.eclipse.org/legal/epl-v20.html |
| 9 | + */ |
| 10 | + |
| 11 | +package org.junit.platform.commons.support.conversion; |
| 12 | + |
| 13 | +import static org.junit.platform.commons.util.ReflectionUtils.HierarchyTraversalMode.BOTTOM_UP; |
| 14 | +import static org.junit.platform.commons.util.ReflectionUtils.findConstructors; |
| 15 | +import static org.junit.platform.commons.util.ReflectionUtils.findMethods; |
| 16 | +import static org.junit.platform.commons.util.ReflectionUtils.invokeMethod; |
| 17 | +import static org.junit.platform.commons.util.ReflectionUtils.isNotPrivate; |
| 18 | +import static org.junit.platform.commons.util.ReflectionUtils.isNotStatic; |
| 19 | +import static org.junit.platform.commons.util.ReflectionUtils.newInstance; |
| 20 | + |
| 21 | +import java.lang.reflect.Constructor; |
| 22 | +import java.lang.reflect.Executable; |
| 23 | +import java.lang.reflect.Method; |
| 24 | +import java.util.List; |
| 25 | +import java.util.concurrent.ConcurrentHashMap; |
| 26 | +import java.util.function.Function; |
| 27 | +import java.util.function.Predicate; |
| 28 | + |
| 29 | +import org.junit.platform.commons.util.Preconditions; |
| 30 | + |
| 31 | +/** |
| 32 | + * {@code FallbackStringToObjectConverter} is a {@link StringToObjectConverter} |
| 33 | + * that provides a fallback conversion strategy for converting from a |
| 34 | + * {@link String} to a given target type by invoking a static factory method |
| 35 | + * or factory constructor defined in the target type. |
| 36 | + * |
| 37 | + * <h2>Search Algorithm</h2> |
| 38 | + * |
| 39 | + * <ol> |
| 40 | + * <li>Search for a single, non-private static factory method in the target |
| 41 | + * type that converts from a String to the target type. Use the factory method |
| 42 | + * if present.</li> |
| 43 | + * <li>Search for a single, non-private constructor in the target type that |
| 44 | + * accepts a String. Use the constructor if present.</li> |
| 45 | + * </ol> |
| 46 | + * |
| 47 | + * <p>If multiple suitable factory methods are discovered they will be ignored. |
| 48 | + * If neither a single factory method nor a single constructor is found, this |
| 49 | + * converter acts as a no-op. |
| 50 | + * |
| 51 | + * @since 1.11 |
| 52 | + * @see StringConversionSupport |
| 53 | + */ |
| 54 | +class FallbackStringToObjectConverter implements StringToObjectConverter { |
| 55 | + |
| 56 | + /** |
| 57 | + * Implementation of the NULL Object Pattern. |
| 58 | + */ |
| 59 | + private static final Function<String, Object> NULL_EXECUTABLE = source -> source; |
| 60 | + |
| 61 | + /** |
| 62 | + * Cache for factory methods and factory constructors. |
| 63 | + * |
| 64 | + * <p>Searches that do not find a factory method or constructor are tracked |
| 65 | + * by the presence of a {@link #NULL_EXECUTABLE} object stored in the map. |
| 66 | + * This prevents the framework from repeatedly searching for things which |
| 67 | + * are already known not to exist. |
| 68 | + */ |
| 69 | + private static final ConcurrentHashMap<Class<?>, Function<String, Object>> factoryExecutableCache // |
| 70 | + = new ConcurrentHashMap<>(64); |
| 71 | + |
| 72 | + @Override |
| 73 | + public boolean canConvert(Class<?> targetType) { |
| 74 | + return findFactoryExecutable(targetType) != NULL_EXECUTABLE; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public Object convert(String source, Class<?> targetType) throws Exception { |
| 79 | + Function<String, Object> executable = findFactoryExecutable(targetType); |
| 80 | + Preconditions.condition(executable != NULL_EXECUTABLE, |
| 81 | + "Illegal state: convert() must not be called if canConvert() returned false"); |
| 82 | + |
| 83 | + return executable.apply(source); |
| 84 | + } |
| 85 | + |
| 86 | + private static Function<String, Object> findFactoryExecutable(Class<?> targetType) { |
| 87 | + return factoryExecutableCache.computeIfAbsent(targetType, type -> { |
| 88 | + Method factoryMethod = findFactoryMethod(type); |
| 89 | + if (factoryMethod != null) { |
| 90 | + return source -> invokeMethod(factoryMethod, null, source); |
| 91 | + } |
| 92 | + Constructor<?> constructor = findFactoryConstructor(type); |
| 93 | + if (constructor != null) { |
| 94 | + return source -> newInstance(constructor, source); |
| 95 | + } |
| 96 | + return NULL_EXECUTABLE; |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | + private static Method findFactoryMethod(Class<?> targetType) { |
| 101 | + List<Method> factoryMethods = findMethods(targetType, new IsFactoryMethod(targetType), BOTTOM_UP); |
| 102 | + if (factoryMethods.size() == 1) { |
| 103 | + return factoryMethods.get(0); |
| 104 | + } |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + private static Constructor<?> findFactoryConstructor(Class<?> targetType) { |
| 109 | + List<Constructor<?>> constructors = findConstructors(targetType, new IsFactoryConstructor(targetType)); |
| 110 | + if (constructors.size() == 1) { |
| 111 | + return constructors.get(0); |
| 112 | + } |
| 113 | + return null; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * {@link Predicate} that determines if the {@link Method} supplied to |
| 118 | + * {@link #test(Method)} is a non-private static factory method for the |
| 119 | + * supplied {@link #targetType}. |
| 120 | + */ |
| 121 | + static class IsFactoryMethod implements Predicate<Method> { |
| 122 | + |
| 123 | + private final Class<?> targetType; |
| 124 | + |
| 125 | + IsFactoryMethod(Class<?> targetType) { |
| 126 | + this.targetType = targetType; |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public boolean test(Method method) { |
| 131 | + // Please do not collapse the following into a single statement. |
| 132 | + if (!method.getReturnType().equals(this.targetType)) { |
| 133 | + return false; |
| 134 | + } |
| 135 | + if (isNotStatic(method)) { |
| 136 | + return false; |
| 137 | + } |
| 138 | + return isNotPrivateAndAcceptsSingleStringArgument(method); |
| 139 | + } |
| 140 | + |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * {@link Predicate} that determines if the {@link Constructor} supplied to |
| 145 | + * {@link #test(Constructor)} is a non-private factory constructor for the |
| 146 | + * supplied {@link #targetType}. |
| 147 | + */ |
| 148 | + static class IsFactoryConstructor implements Predicate<Constructor<?>> { |
| 149 | + |
| 150 | + private final Class<?> targetType; |
| 151 | + |
| 152 | + IsFactoryConstructor(Class<?> targetType) { |
| 153 | + this.targetType = targetType; |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public boolean test(Constructor<?> constructor) { |
| 158 | + // Please do not collapse the following into a single statement. |
| 159 | + if (!constructor.getDeclaringClass().equals(this.targetType)) { |
| 160 | + return false; |
| 161 | + } |
| 162 | + return isNotPrivateAndAcceptsSingleStringArgument(constructor); |
| 163 | + } |
| 164 | + |
| 165 | + } |
| 166 | + |
| 167 | + private static boolean isNotPrivateAndAcceptsSingleStringArgument(Executable executable) { |
| 168 | + return isNotPrivate(executable) // |
| 169 | + && (executable.getParameterCount() == 1) // |
| 170 | + && (executable.getParameterTypes()[0] == String.class); |
| 171 | + } |
| 172 | + |
| 173 | +} |
0 commit comments