chaokunyang commented on code in PR #1553: URL: https://github.com/apache/incubator-fury/pull/1553#discussion_r1575708279
########## java/fury-core/src/main/java/org/apache/fury/reflect/TypeToken.java: ########## @@ -0,0 +1,836 @@ +/* + * Copyright (C) 2006 The Guava Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package org.apache.fury.reflect; + +import static org.apache.fury.reflect.Types.asTypeVariableKeyOrNull; +import static org.apache.fury.reflect.Types.newArrayType; +import static org.apache.fury.reflect.Types.typeVariablesEquals; + +import java.lang.reflect.Array; +import java.lang.reflect.GenericArrayType; +import java.lang.reflect.Modifier; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.lang.reflect.TypeVariable; +import java.lang.reflect.WildcardType; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.apache.fury.type.TypeUtils; + +public class TypeToken<T> { + + private final Type type; + + /** + * Constructs a new type token of {@code T}. + * + * <p>Clients create an empty anonymous subclass. This embeds the type parameter in the anonymous + * class's type hierarchy, so we can reconstitute it at runtime despite erasure. + * + * <p>For example: + * + * <pre>{@code + * TypeToken<List<String>> t = new TypeToken<List<String>>() {}; + * }</pre> + */ + protected TypeToken() { + this.type = capture(); + } + + private TypeToken(Class<T> declaringClass) { + this.type = declaringClass; + } + + private TypeToken(Type type) { + this.type = type; + } + + /** Returns an instance of type token that wraps {@code type}. */ + public static <T> TypeToken<T> of(Class<T> clazz) { + return new TypeToken<>(clazz); + } + + /** Returns an instance of type token that wraps {@code type}. */ + public static <T> TypeToken<T> of(Type type) { + return new TypeToken<>(type); + } + + /** Returns the captured type. */ + private Type capture() { + final Type superclass = getClass().getGenericSuperclass(); + if (!(superclass instanceof ParameterizedType)) { + throw new IllegalArgumentException(superclass + " isn't parameterized"); + } + return ((ParameterizedType) superclass).getActualTypeArguments()[0]; + } + + /** Returns the represented type. */ + public Type getType() { + return type; + } + + /** + * Returns the raw type of {@code T}. Formally speaking, if {@code T} is returned by {@link + * java.lang.reflect.Method#getGenericReturnType}, the raw type is what's returned by {@link + * java.lang.reflect.Method#getReturnType} of the same method object. Specifically: + * + * <ul> + * <li>If {@code T} is a {@code Class} itself, {@code T} itself is returned. + * <li>If {@code T} is a {@link ParameterizedType}, the raw type of the parameterized type is + * returned. + * <li>If {@code T} is a {@link GenericArrayType}, the returned type is the corresponding array + * class. For example: {@code List<Integer>[] => List[]}. + * <li>If {@code T} is a type variable or a wildcard type, the raw type of the first upper bound + * is returned. For example: {@code <X extends Foo> => Foo}. + * </ul> + */ + public Class<? super T> getRawType() { + // For wildcard or type variable, the first bound determines the runtime type. + Class<?> rawType = getRawTypes(type).iterator().next(); Review Comment: Actually we have `org.apache.fury.type.TypeUtils#getRawType(com.google.common.reflect.TypeToken<?>)` to reduce this method cost. If we can optimize here, we don't need `TypeUtils#getRawType` anymore -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@fury.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@fury.apache.org For additional commands, e-mail: commits-h...@fury.apache.org