|
| 1 | +/* |
| 2 | + * Copyright 2020 Restream |
| 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 ru.rt.restream.reindexer.convert.util; |
| 17 | + |
| 18 | +import org.apache.commons.lang3.reflect.TypeUtils; |
| 19 | +import ru.rt.restream.reindexer.util.Pair; |
| 20 | + |
| 21 | +import java.lang.reflect.Type; |
| 22 | +import java.lang.reflect.TypeVariable; |
| 23 | +import java.util.Collection; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Objects; |
| 26 | +import java.util.concurrent.ConcurrentHashMap; |
| 27 | + |
| 28 | +/** |
| 29 | + * For internal use only, as this contract is likely to change. |
| 30 | + */ |
| 31 | +public final class ConversionUtils { |
| 32 | + |
| 33 | + private static final Map<Class<?>, Pair<ResolvableType, ResolvableType>> CONVERTIBLE_PAIR_CACHE = new ConcurrentHashMap<>(); |
| 34 | + |
| 35 | + /** |
| 36 | + * Resolves source and target types based on {@code [0]} and {@code [1]} parameters from {@code converterType} interface implementation. |
| 37 | + * If {@code converterType} source or target type is an array, the type is determined by {@code Class#getComponentType}, |
| 38 | + * in case of a generic type, only {@code java.util.Collection} assignable types are considered, nested generic types |
| 39 | + * (i.e. {@code List<List<String>>}) are not supported, such types will not be recognized and IllegalArgumentException will be thrown. |
| 40 | + * @param converterClass the {@code converterType} interface implementation class to use |
| 41 | + * @param converterType the converter interface to use |
| 42 | + * @return the {@link Pair} of source and target {@link ResolvableType}s to use |
| 43 | + */ |
| 44 | + public static Pair<ResolvableType, ResolvableType> resolveConvertiblePair(Class<?> converterClass, Class<?> converterType) { |
| 45 | + Objects.requireNonNull(converterClass, "converterClass must not be null"); |
| 46 | + // https://bugs.openjdk.java.net/browse/JDK-8161372 |
| 47 | + Pair<ResolvableType, ResolvableType> typePair = CONVERTIBLE_PAIR_CACHE.get(converterClass); |
| 48 | + if (typePair != null) { |
| 49 | + return typePair; |
| 50 | + } |
| 51 | + Objects.requireNonNull(converterType, "converterType must not be null"); |
| 52 | + TypeVariable<? extends Class<?>>[] typeParameters = converterType.getTypeParameters(); |
| 53 | + if (typeParameters.length < 2) { |
| 54 | + throw new IllegalArgumentException( |
| 55 | + String.format("Converter type: %s must have 2 type arguments", converterType.getName())); |
| 56 | + } |
| 57 | + return CONVERTIBLE_PAIR_CACHE.computeIfAbsent(converterClass, k -> { |
| 58 | + Map<TypeVariable<?>, Type> typeArguments = TypeUtils.getTypeArguments(converterClass, converterType); |
| 59 | + Type sourceType = typeArguments.get(typeParameters[0]); |
| 60 | + ResolvableType resolvableSourceType = resolveType(sourceType); |
| 61 | + if (resolvableSourceType == null) { |
| 62 | + throw new IllegalArgumentException( |
| 63 | + String.format("Cannot resolve source type: %s of converter type: %s for converter class: %s", |
| 64 | + sourceType, converterType.getName(), converterClass.getName())); |
| 65 | + } |
| 66 | + Type targetType = typeArguments.get(typeParameters[1]); |
| 67 | + ResolvableType resolvableTargetType = resolveType(targetType); |
| 68 | + if (resolvableTargetType == null) { |
| 69 | + throw new IllegalArgumentException( |
| 70 | + String.format("Cannot resolve target type: %s of converter type: %s for converter class: %s", |
| 71 | + targetType, converterType.getName(), converterClass.getName())); |
| 72 | + } |
| 73 | + return new Pair<>(resolvableSourceType, resolvableTargetType); |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + private static ResolvableType resolveType(Type type) { |
| 78 | + if (TypeUtils.isAssignable(type, Collection.class)) { |
| 79 | + Type typeArgument = TypeUtils.getTypeArguments(type, Collection.class) |
| 80 | + .get(Collection.class.getTypeParameters()[0]); |
| 81 | + if (typeArgument instanceof Class<?>) { |
| 82 | + Class<?> containerType = TypeUtils.getRawType(type, Collection.class); |
| 83 | + return new ResolvableType(containerType, (Class<?>) typeArgument, true); |
| 84 | + } |
| 85 | + return null; |
| 86 | + } |
| 87 | + if (type instanceof Class<?>) { |
| 88 | + Class<?> targetType = (Class<?>) type; |
| 89 | + return new ResolvableType(targetType, targetType.getComponentType(), targetType.isArray()); |
| 90 | + } |
| 91 | + return null; |
| 92 | + } |
| 93 | + |
| 94 | + private ConversionUtils() { |
| 95 | + // utils |
| 96 | + } |
| 97 | +} |
0 commit comments