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_r351874871
########## 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) { + // 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) { + final ParameterizedType currentParameterized = (ParameterizedType) currentType; + final Class<?> currentRaw = (Class<?>) currentParameterized.getRawType(); + final TypeVariable<?>[] currentVariables = currentRaw.getTypeParameters(); + // search for matching type variable + for (int paramPos = 0; paramPos < currentVariables.length; paramPos++) { + final TypeVariable<?> currentVariable = currentVariables[paramPos]; + if (currentVariable.getGenericDeclaration().equals(variable.getGenericDeclaration()) && + currentVariable.getName().equals(variable.getName())) { + final Type resolvedType = currentParameterized.getActualTypeArguments()[paramPos]; + // follow type variables transitively + if (resolvedType instanceof TypeVariable) { + variable = (TypeVariable<?>) resolvedType; + } else { + return resolvedType; + } + } + } + } + } + // unresolved variable + return variable; + } + + /** + * Validates the characteristics of a class for a {@link StructuredType} such as accessibility. + */ + public static void validateStructuredClass(Class<?> clazz) { + final int m = clazz.getModifiers(); + if (Modifier.isAbstract(m)) { + throw extractionError("Class '%s' must not be abstract.", clazz.getName()); + } + if (!Modifier.isPublic(m)) { + throw extractionError("Class '%s' is not public.", clazz.getName()); + } + if (clazz.getEnclosingClass() != null && + (clazz.getDeclaringClass() == null || !Modifier.isStatic(m))) { + throw extractionError("Class '%s' is a not a static, globally accessible class.", clazz.getName()); + } + } + + /** + * Returns the fields of a class for a {@link StructuredType}. + */ + public static List<Field> collectStructuredFields(Class<?> clazz) { + final List<Field> fields = new ArrayList<>(); + while (clazz != Object.class) { + final Field[] declaredFields = clazz.getDeclaredFields(); + Stream.of(declaredFields) + .filter(field -> { + final int m = field.getModifiers(); + return !Modifier.isStatic(m) && !Modifier.isTransient(m); + }) + .forEach(fields::add); + clazz = clazz.getSuperclass(); + } + return fields; + } + + /** + * Validates if a field is properly readable either directly or through a getter. + */ + public static void validateStructuredFieldReadability(Class<?> clazz, Field field) { + final int m = field.getModifiers(); + + // field is accessible + if (Modifier.isPublic(m)) { + return; + } + + // field needs a getter + if (!hasStructuredFieldGetter(clazz, field)) { + throw extractionError( + "Field '%s' of class '%s' is neither publicly accessible nor does it have " + + "a corresponding getter method.", + field.getName(), + clazz.getName()); + } + } + + /** + * Checks if a field is mutable or immutable. Returns {@code true} if the field is properly + * mutable. Returns {@code false} if it is properly immutable. + */ + public static boolean isStructuredFieldMutable(Class<?> clazz, Field field) { + final int m = field.getModifiers(); + + // field is immutable + if (Modifier.isFinal(m)) { + return false; + } + // field is directly mutable + if (Modifier.isPublic(m)) { + return true; + } + // field has setters by which it is mutable + if (hasFieldSetter(clazz, field)) { + return true; + } + + throw extractionError( + "Field '%s' of class '%s' is mutable but is neither publicly accessible nor does it have " + + "a corresponding setter method.", + field.getName(), + clazz.getName()); + } + + /** + * Checks for a field setters. The logic is as broad as possible to support both Java and Scala + * in different flavors. + */ + public static boolean hasFieldSetter(Class<?> clazz, Field field) { + final String normalizedFieldName = field.getName().toUpperCase().replaceAll("_", ""); + + final List<Method> methods = collectStructuredMethods(clazz); + for (Method method : methods) { + + // check name: + // set<Name>(type) + // <Name>(type) + // <Name>_$eq(type) for Scala + final String normalizedMethodName = method.getName().toUpperCase().replaceAll("_", ""); + final boolean hasName = normalizedMethodName.equals("SET" + normalizedFieldName) || + normalizedMethodName.equals(normalizedFieldName) || + normalizedMethodName.equals(normalizedFieldName + "$EQ"); + if (!hasName) { + continue; + } + + // check return type: + // void or the declaring class + final Class<?> returnType = method.getReturnType(); + final boolean hasReturnType = returnType == Void.TYPE || returnType == clazz; + if (!hasReturnType) { + continue; + } + + // check parameters: + // one parameter that has the same (or primitive) type of the field + final boolean hasParameter = method.getParameterCount() == 1 && + (method.getGenericParameterTypes()[0].equals(field.getGenericType()) || + boxPrimitive(method.getGenericParameterTypes()[0]).equals(field.getGenericType())); + if (!hasParameter) { + continue; + } + + // matching setter found + return true; + } + + // no setter found + return false; + } + + private static final Map<Class<?>, Class<?>> primitiveWrapperMap = new HashMap<>(); + static { + primitiveWrapperMap.put(Boolean.TYPE, Boolean.class); + primitiveWrapperMap.put(Byte.TYPE, Byte.class); + primitiveWrapperMap.put(Character.TYPE, Character.class); + primitiveWrapperMap.put(Short.TYPE, Short.class); + primitiveWrapperMap.put(Integer.TYPE, Integer.class); + primitiveWrapperMap.put(Long.TYPE, Long.class); + primitiveWrapperMap.put(Double.TYPE, Double.class); + primitiveWrapperMap.put(Float.TYPE, Float.class); + } + + /** + * Returns the boxed type of a primitive type. + */ + public static Type boxPrimitive(Type type) { + if (type instanceof Class && ((Class) type).isPrimitive()) { + return primitiveWrapperMap.get(type); + } + return type; + } + + /** + * Checks for a field getter. The logic is as broad as possible to support both Java and Scala + * in different flavors. + */ + public static boolean hasStructuredFieldGetter(Class<?> clazz, Field field) { + final String normalizedFieldName = field.getName().toUpperCase().replaceAll("_", ""); + + final List<Method> methods = collectStructuredMethods(clazz); + for (Method method : methods) { + // check name: + // get<Name>() + // is<Name>() + // <Name>() for Scala + final String normalizedMethodName = method.getName().toUpperCase().replaceAll("_", ""); + final boolean hasName = normalizedMethodName.equals("GET" + normalizedFieldName) || + normalizedMethodName.equals("IS" + normalizedFieldName) || + normalizedMethodName.equals(normalizedFieldName); + if (!hasName) { + continue; + } + + // check return type: + // equal to field type + final Type returnType = method.getGenericReturnType(); + final boolean hasReturnType = returnType.equals(field.getGenericType()); + if (!hasReturnType) { + continue; + } + + // check parameters: + // no parameters + final boolean hasNoParameters = method.getParameterCount() == 0; + if (!hasNoParameters) { + continue; + } + + // matching getter found + return true; + } + + // no getter found + return false; + } + + /** + * Collects all methods that qualify as methods of a {@link StructuredType}. + */ + public static List<Method> collectStructuredMethods(Class<?> clazz) { + final List<Method> methods = new ArrayList<>(); + while (clazz != Object.class) { + final Method[] declaredMethods = clazz.getDeclaredMethods(); + Stream.of(declaredMethods) + .filter(field -> { + final int m = field.getModifiers(); + return Modifier.isPublic(m) && !Modifier.isNative(m) && !Modifier.isAbstract(m); + }) + .forEach(methods::add); + clazz = clazz.getSuperclass(); + } + return methods; + } + + // -------------------------------------------------------------------------------------------- + // Assignable Constructor Utilities + // -------------------------------------------------------------------------------------------- + + /** + * Result of the extraction in {@link #extractAssigningConstructor(Class, List)}. + */ + public static class AssigningConstructor { + public final Constructor<?> constructor; + public final List<String> parameterNames; + + private AssigningConstructor(Constructor<?> constructor, List<String> parameterNames) { + this.constructor = constructor; + this.parameterNames = parameterNames; + } + } + + /** + * Checks whether the given constructor takes all of the given fields with matching (possibly + * primitive) type and name. An assigning constructor can define the order of fields. + */ + public static @Nullable AssigningConstructor extractAssigningConstructor( + Class<?> clazz, + List<Field> fields) { + AssigningConstructor foundConstructor = null; + for (Constructor<?> constructor : clazz.getDeclaredConstructors()) { + if (!Modifier.isPublic(constructor.getModifiers())) { + continue; + } + final List<String> parameterNames = extractConstructorParameterNames(clazz, constructor, fields); + if (parameterNames != null) { + if (foundConstructor != null) { + throw extractionError( + "Multiple constructors found that assign all fields for class '%s'.", + clazz.getName()); + } + foundConstructor = new AssigningConstructor(constructor, parameterNames); + } + } + return foundConstructor; + } + + /** + * Extracts ordered parameter names from a constructor that takes all of the given fields with + * matching (possibly primitive) type and name. + */ + private static @Nullable List<String> extractConstructorParameterNames( + Class<?> clazz, + Constructor<?> constructor, + List<Field> fields) { + final Type[] parameterTypes = constructor.getGenericParameterTypes(); + List<String> parameterNames = Stream.of(constructor.getParameters()) + .map(Parameter::getName) + .collect(Collectors.toList()); + + // by default parameter names are "arg0, arg1, arg2, ..." if compiler flag is not set + // so we need to extract them manually if possible + if (parameterNames.stream().allMatch(n -> n.startsWith("arg"))) { + final ParameterExtractor extractor = new ParameterExtractor(constructor); + getClassReader(clazz).accept(extractor, 0); + + final List<String> extractedNames = extractor.getParameterNames(); + if (extractedNames.size() == 0 || !extractedNames.get(0).equals("this")) { + return null; + } + // remove "this" and additional local variables + parameterNames = extractedNames.subList(1, Math.min(fields.size() + 1, extractedNames.size())); Review comment: Is the removing trailing names correct? Does it mean that we accept a ctor that has more arguments than just the fields? ---------------------------------------------------------------- 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
