dawidwys commented on a change in pull request #10342: [FLINK-14967][table] Add a utility for creating data types via reflection URL: https://github.com/apache/flink/pull/10342#discussion_r351676300
########## File path: flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/extraction/utils/ExtractionUtils.java ########## @@ -0,0 +1,565 @@ +/* + * 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.flink.table.types.extraction.utils; + +import org.apache.flink.api.common.typeutils.TypeSerializer; +import org.apache.flink.table.api.DataTypes; +import org.apache.flink.table.api.ValidationException; +import org.apache.flink.table.catalog.DataTypeLookup; +import org.apache.flink.table.types.DataType; +import org.apache.flink.table.types.logical.StructuredType; + +import org.apache.flink.shaded.asm7.org.objectweb.asm.ClassReader; +import org.apache.flink.shaded.asm7.org.objectweb.asm.ClassVisitor; +import org.apache.flink.shaded.asm7.org.objectweb.asm.Label; +import org.apache.flink.shaded.asm7.org.objectweb.asm.MethodVisitor; +import org.apache.flink.shaded.asm7.org.objectweb.asm.Opcodes; + +import javax.annotation.Nullable; + +import java.io.IOException; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.lang.reflect.Parameter; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.lang.reflect.TypeVariable; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.apache.flink.shaded.asm7.org.objectweb.asm.Type.getConstructorDescriptor; + +/** + * Utilities for performing reflection tasks. + */ +public final class ExtractionUtils { + + /** + * Helper method for creating consistent exceptions during extraction. + */ + public static ValidationException extractionError(String message, Object... args) { + return extractionError(null, message, args); + } + + /** + * Helper method for creating consistent exceptions during extraction. + */ + public static ValidationException extractionError(Throwable cause, String message, Object... args) { + return new ValidationException( + String.format( + message, + args + ), + cause + ); + } + + /** + * Collects the type hierarchy from the given type to the base class. The base class is included + * in the returned hierarchy if it is not {@link Object}. + */ + public static List<Type> collectTypeHierarchy(Class<?> baseClass, Type type) { + Type currentType = type; + Class<?> currentClass = toClass(type); + if (currentClass == null || !baseClass.isAssignableFrom(currentClass)) { + throw extractionError( + "Base class %s is not a super class of type %s.", + baseClass.getName(), + type.toString()); + } + final List<Type> typeHierarchy = new ArrayList<>(); + while (currentClass != baseClass) { + assert currentClass != null; + typeHierarchy.add(currentType); + currentType = currentClass.getGenericSuperclass(); Review comment: Is this a conscious decision not to support generic interfaces? ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
