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_r351774090
 
 

 ##########
 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();
+                       currentClass = toClass(currentType);
+               }
+               if (currentClass != Object.class) {
+                       typeHierarchy.add(currentType);
+               }
+               return typeHierarchy;
+       }
+
+       /**
+        * Converts a {@link Type} to {@link Class} if possible, {@code null} 
otherwise.
+        */
+       public static @Nullable Class<?> toClass(Type type) {
+               if (type instanceof Class) {
+                       return (Class<?>) type;
+               } else if (type instanceof ParameterizedType) {
+                       // this is always a class
+                       return (Class<?>) ((ParameterizedType) 
type).getRawType();
+               }
+               // unsupported: generic arrays, type variables, wildcard types
+               return null;
+       }
+
+       /**
+        * Creates a raw data type.
+        */
+       @SuppressWarnings("unchecked")
+       public static DataType createRawType(
+                       DataTypeLookup lookup,
+                       @Nullable Class<?> rawSerializer,
+                       @Nullable Class<?> conversionClass) {
+               if (rawSerializer != null) {
+                       return DataTypes.RAW((Class) 
createConversionClass(conversionClass), 
instantiateRawSerializer(rawSerializer));
+               }
+               return 
lookup.resolveRawDataType(createConversionClass(conversionClass));
+       }
+
+       private static Class<?> createConversionClass(@Nullable Class<?> 
conversionClass) {
+               if (conversionClass != null) {
+                       return conversionClass;
+               }
+               return Object.class;
+       }
+
+       private static TypeSerializer<?> instantiateRawSerializer(Class<?> 
rawSerializer) {
+               if (!TypeSerializer.class.isAssignableFrom(rawSerializer)) {
+                       throw extractionError(
+                               "Defined class '%s' for RAW serializer does not 
extend '%s'.",
+                               rawSerializer.getName(),
+                               TypeSerializer.class.getName()
+                       );
+               }
+               try {
+                       return (TypeSerializer<?>) rawSerializer.newInstance();
+               } catch (Exception e) {
+                       throw extractionError(
+                               e,
+                               "Cannot instantiate type serializer '%s' for 
RAW type. " +
+                                       "Make sure the class is publicly 
accessible and has a default constructor",
+                               rawSerializer.getName()
+                       );
+               }
+       }
+
+       /**
+        * Resolves a {@link TypeVariable} using the given type hierarchy if 
possible.
+        */
+       public static Type resolveVariable(List<Type> typeHierarchy, 
TypeVariable<?> variable) {
 
 Review comment:
   I had hard time understanding this method. I tried to refactor it a bit. 
Personally found it a bit easier to understand, but that might be subjective. 
   
   There is one substantial difference that it does not iterate through all the 
parameters of a ParameterizedType if a matching variable was found.
   
   ```
        /**
         * Resolves a {@link TypeVariable} using the given type hierarchy if 
possible.
         */
        public static Type resolveVariable(List<Type> typeHierarchy, 
TypeVariable<?> variable) {
                // iterate through hierarchy from top to bottom until type 
variable gets a non-variable assigned
                for (int i = typeHierarchy.size() - 1; i >= 0; i--) {
                        final Type currentType = typeHierarchy.get(i);
   
                        if (currentType instanceof ParameterizedType) {
                                Type resolvedType = 
resolveVariableInParameterizedType(variable, (ParameterizedType) currentType);
                                if (resolvedType instanceof TypeVariable) {
                                        // follow type variables transitively
                                        variable = (TypeVariable<?>) 
resolvedType;
                                } else if (resolvedType != null) {
                                        return resolvedType;
                                }
                        }
                }
                // unresolved variable
                return variable;
        }
   
        private static @Nullable Type resolveVariableInParameterizedType(
                        TypeVariable<?> variable,
                        ParameterizedType currentType) {
                final Class<?> currentRaw = (Class<?>) currentType.getRawType();
                final TypeVariable<?>[] currentVariables = 
currentRaw.getTypeParameters();
                // search for matching type variable
                for (int paramPos = 0; paramPos < currentVariables.length; 
paramPos++) {
                        if (equal(variable, currentVariables[paramPos])) {
                                return 
currentType.getActualTypeArguments()[paramPos];
                        }
                }
                return null;
        }
   
        private static boolean equal(TypeVariable<?> variable, TypeVariable<?> 
currentVariable) {
                return 
currentVariable.getGenericDeclaration().equals(variable.getGenericDeclaration())
 &&
                                
currentVariable.getName().equals(variable.getName());
        }
   ```

----------------------------------------------------------------
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

Reply via email to