|
| 1 | +/* |
| 2 | + * Copyright (C) 2023 Elytrium |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Lesser General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU Lesser General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Lesser General Public License |
| 15 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +package net.elytrium.serializer.utils; |
| 19 | + |
| 20 | +import java.lang.reflect.ParameterizedType; |
| 21 | +import java.lang.reflect.Type; |
| 22 | +import java.lang.reflect.TypeVariable; |
| 23 | + |
| 24 | +public class GenericUtils { |
| 25 | + |
| 26 | + public static int getParameterIndex(TypeVariable<?>[] variables, TypeVariable<?> expected) { |
| 27 | + for (int i = 0; i < variables.length; ++i) { |
| 28 | + if (variables[i].getName().equals(expected.getName())) { |
| 29 | + return i; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + return -1; |
| 34 | + } |
| 35 | + |
| 36 | + public static Type getParameterTypeFromSuperclass(Class<?> parent, Type type, Type superclass, int searchIndex) { |
| 37 | + Type superclassType = GenericUtils.getParameterType(parent, superclass, searchIndex); |
| 38 | + if (superclassType instanceof TypeVariable<?> typeVariable && type instanceof ParameterizedType parameterizedType) { |
| 39 | + Class<?> rawSuperclass = (Class<?>) parameterizedType.getRawType(); |
| 40 | + int index = GenericUtils.getParameterIndex(rawSuperclass.getTypeParameters(), typeVariable); |
| 41 | + if (index != -1) { |
| 42 | + return parameterizedType.getActualTypeArguments()[index]; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + return superclassType; |
| 47 | + } |
| 48 | + |
| 49 | + public static Type getParameterType(Class<?> parent, Type type, int index) { |
| 50 | + if (type == null) { |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + Class<?> clazz = null; |
| 55 | + |
| 56 | + if (type instanceof ParameterizedType parameterizedType) { |
| 57 | + clazz = (Class<?>) parameterizedType.getRawType(); |
| 58 | + if (clazz.equals(parent)) { |
| 59 | + return parameterizedType.getActualTypeArguments()[index]; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + if (type instanceof Class<?> typeClazz) { |
| 64 | + clazz = typeClazz; |
| 65 | + } |
| 66 | + |
| 67 | + if (clazz != null) { |
| 68 | + Type genericSuperclass = clazz.getGenericSuperclass(); |
| 69 | + Type superclassType = GenericUtils.getParameterTypeFromSuperclass(parent, type, genericSuperclass, index); |
| 70 | + if (superclassType != null) { |
| 71 | + return superclassType; |
| 72 | + } |
| 73 | + |
| 74 | + for (Type genericInterface : clazz.getGenericInterfaces()) { |
| 75 | + Type genericInterfaceType = GenericUtils.getParameterTypeFromSuperclass(parent, type, genericInterface, index); |
| 76 | + if (genericInterfaceType != null) { |
| 77 | + return genericInterfaceType; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return null; |
| 83 | + } |
| 84 | +} |
0 commit comments