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_r351695997
########## File path: flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/extraction/DataTypeExtractor.java ########## @@ -0,0 +1,574 @@ +/* + * 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; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.table.annotation.DataTypeHint; +import org.apache.flink.table.api.DataTypes; +import org.apache.flink.table.catalog.DataTypeLookup; +import org.apache.flink.table.types.DataType; +import org.apache.flink.table.types.FieldsDataType; +import org.apache.flink.table.types.extraction.utils.DataTypeTemplate; +import org.apache.flink.table.types.extraction.utils.ExtractionUtils; +import org.apache.flink.table.types.logical.LogicalType; +import org.apache.flink.table.types.logical.RawType; +import org.apache.flink.table.types.logical.StructuredType; +import org.apache.flink.table.types.logical.StructuredType.StructuredAttribute; +import org.apache.flink.table.types.utils.ClassDataTypeConverter; + +import javax.annotation.Nullable; + +import java.lang.reflect.Field; +import java.lang.reflect.GenericArrayType; +import java.lang.reflect.Method; +import java.lang.reflect.Parameter; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.lang.reflect.TypeVariable; +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import static org.apache.flink.table.types.extraction.utils.ExtractionUtils.collectStructuredFields; +import static org.apache.flink.table.types.extraction.utils.ExtractionUtils.collectTypeHierarchy; +import static org.apache.flink.table.types.extraction.utils.ExtractionUtils.createRawType; +import static org.apache.flink.table.types.extraction.utils.ExtractionUtils.extractAssigningConstructor; +import static org.apache.flink.table.types.extraction.utils.ExtractionUtils.extractionError; +import static org.apache.flink.table.types.extraction.utils.ExtractionUtils.isStructuredFieldMutable; +import static org.apache.flink.table.types.extraction.utils.ExtractionUtils.resolveVariable; +import static org.apache.flink.table.types.extraction.utils.ExtractionUtils.toClass; +import static org.apache.flink.table.types.extraction.utils.ExtractionUtils.validateStructuredClass; +import static org.apache.flink.table.types.extraction.utils.ExtractionUtils.validateStructuredFieldReadability; + +/** + * Reflection-based utility that analyzes a given {@link java.lang.reflect.Type}, method, or class to + * extract a (possibly nested) {@link DataType} from it. + */ +@Internal +public final class DataTypeExtractor { + + private final DataTypeLookup lookup; + + private DataTypeExtractor(DataTypeLookup lookup) { + this.lookup = lookup; + } + + /** + * Extracts a data type from a type without considering surrounding classes or templates. + */ + public static DataType extractFromType( + DataTypeLookup lookup, + Type type) { + return extractDataTypeWithClassContext( + lookup, + DataTypeTemplate.fromDefaults(), + null, + type, + ""); + } + + /** + * Extracts a data type from a type without considering surrounding classes but templates. + */ + public static DataType extractFromType( + DataTypeLookup lookup, + DataTypeTemplate template, + Type type) { + return extractDataTypeWithClassContext( + lookup, + template, + null, + type, + ""); + } + + /** + * Extracts a data type from a type variable at {@code genericPos} of {@code baseClass} using + * the information of the most specific type {@code contextType}. + */ + public static DataType extractFromGeneric( + DataTypeLookup lookup, + Class<?> baseClass, + int genericPos, + Type contextType) { + final TypeVariable<?> variable = baseClass.getTypeParameters()[genericPos]; + return extractDataTypeWithClassContext( + lookup, + DataTypeTemplate.fromDefaults(), + contextType, + variable, + String.format( + " in generic class '%s' in %s", + baseClass.getName(), + contextType.toString())); + } + + /** + * Extracts a data type from a method parameter by considering surrounding classes and parameter + * annotation. + */ + public static DataType extractFromMethodParameter( + DataTypeLookup lookup, + Class<?> baseClass, + Method method, + int paramPos) { + final Parameter parameter = method.getParameters()[paramPos]; + final DataTypeHint hint = parameter.getAnnotation(DataTypeHint.class); + final DataTypeTemplate template; + if (hint != null) { + template = DataTypeTemplate.fromAnnotation(lookup, hint); + } else { + template = DataTypeTemplate.fromDefaults(); + } + return extractDataTypeWithClassContext( + lookup, + template, + baseClass, + parameter.getParameterizedType(), + String.format( + " in parameter %d of method '%s' in class '%s'", + paramPos, + method.getName(), + baseClass.getName())); + } + + /** + * Extracts a data type from a method return type by considering surrounding classes and method + * annotation. + */ + public static DataType extractFromMethodOutput( + DataTypeLookup lookup, + Class<?> baseClass, + Method method) { + final DataTypeHint hint = method.getAnnotation(DataTypeHint.class); + final DataTypeTemplate template; + if (hint != null) { + template = DataTypeTemplate.fromAnnotation(lookup, hint); + } else { + template = DataTypeTemplate.fromDefaults(); + } + return extractDataTypeWithClassContext( + lookup, + template, + baseClass, + method.getGenericReturnType(), + String.format( + " in return type of method '%s' in class '%s'", + method.getName(), + baseClass.getName())); + } + + private static DataType extractDataTypeWithClassContext( + DataTypeLookup lookup, + DataTypeTemplate outerTemplate, + @Nullable Type contextType, + Type type, + String contextExplanation) { + final DataTypeExtractor extractor = new DataTypeExtractor(lookup); + try { + final List<Type> typeHierarchy; + if (contextType != null) { + typeHierarchy = collectTypeHierarchy(Object.class, contextType); + } else { + typeHierarchy = Collections.singletonList(type); + } + return extractor.extractDataTypeOrRaw(outerTemplate, typeHierarchy, type); + } catch (Throwable t) { + throw extractionError( + t, + "Could not extract a data type from '%s'%s. " + + "Please pass the required data type manually or allow RAW types.", Review comment: I think the statement `or allow RAW types` is a bit too high in the call stack. There may be so many problems with building the type hierarchy, merging annotations, probably other problems that cannot be solved simply with allowing the RAW types. Imho we could throw that exception in `extractDataTypeOrRawWithTemplate` If the problem is you need the `contextExplanation` make it a field of the `DataTypeExtractor`. ---------------------------------------------------------------- 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
