chaokunyang commented on code in PR #1553:
URL: https://github.com/apache/incubator-fury/pull/1553#discussion_r1577871049


##########
java/fury-core/src/main/java/org/apache/fury/reflect/TypeRef.java:
##########
@@ -0,0 +1,828 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Stream;
+import org.apache.fury.type.TypeUtils;
+
+public class TypeRef<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 TypeRef() {
+    this.type = capture();
+  }
+
+  private TypeRef(Class<T> declaringClass) {
+    this.type = declaringClass;
+  }
+
+  private TypeRef(Type type) {
+    this.type = type;
+  }
+
+  /** Returns an instance of type token that wraps {@code type}. */
+  public static <T> TypeRef<T> of(Class<T> clazz) {
+    return new TypeRef<>(clazz);
+  }
+
+  /** Returns an instance of type token that wraps {@code type}. */
+  public static <T> TypeRef<T> of(Type type) {
+    return new TypeRef<>(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() {
+    @SuppressWarnings("unchecked")
+    Class<? super T> result = (Class<? super T>) TypeUtils.getRawType(type);

Review Comment:
   How about use a field to hold raw types. This may be called multiple times



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to