reuvenlax commented on code in PR #32757: URL: https://github.com/apache/beam/pull/32757#discussion_r1799844830
########## sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/utils/StaticSchemaInference.java: ########## @@ -85,14 +86,17 @@ enum MethodType { * public getter methods, or special annotations on the class. */ public static Schema schemaFromClass( - TypeDescriptor<?> typeDescriptor, FieldValueTypeSupplier fieldValueTypeSupplier) { - return schemaFromClass(typeDescriptor, fieldValueTypeSupplier, new HashMap<>()); + TypeDescriptor<?> typeDescriptor, + FieldValueTypeSupplier fieldValueTypeSupplier, + Map<Type, Type> boundTypes) { Review Comment: Added Internal annotation. the fact that it wasn't so marked before was an oversight. ########## sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/utils/ReflectUtils.java: ########## @@ -243,4 +255,45 @@ public static TypeDescriptor boxIfPrimitive(TypeDescriptor typeDescriptor) { ? TypeDescriptor.of(Primitives.wrap(typeDescriptor.getRawType())) : typeDescriptor; } + + public static <T> Map<Type, Type> getAllBoundTypes(TypeDescriptor<T> typeDescriptor) { + Map<Type, Type> boundParameters = Maps.newHashMap(); + TypeDescriptor<?> currentType = typeDescriptor; + do { + if (currentType.getType() instanceof ParameterizedType) { + ParameterizedType parameterizedType = (ParameterizedType) currentType.getType(); + TypeVariable<?>[] typeVariables = currentType.getRawType().getTypeParameters(); + Type[] typeArguments = parameterizedType.getActualTypeArguments(); + ; + if (typeArguments.length != typeVariables.length) { + throw new RuntimeException("Unmatching arguments lengths"); + } + for (int i = 0; i < typeVariables.length; ++i) { + boundParameters.put(typeVariables[i], typeArguments[i]); + } + } + Type superClass = currentType.getRawType().getGenericSuperclass(); + if (superClass == null || superClass.equals(Object.class)) { + break; + } + currentType = TypeDescriptor.of(superClass); + } while (true); + return boundParameters; + } + + public static Type resolveType(Type type, Map<Type, Type> boundTypes) { + TypeDescriptor<?> typeDescriptor = TypeDescriptor.of(type); + if (typeDescriptor.isSubtypeOf(TypeDescriptor.of(Iterable.class)) + || typeDescriptor.isSubtypeOf(TypeDescriptor.of(Map.class))) { + // Don't resolve these as we special case map and interable. + return type; + } + + if (type instanceof TypeVariable) { + TypeVariable<?> typeVariable = (TypeVariable<?>) type; + return Preconditions.checkArgumentNotNull(boundTypes.get(typeVariable)); Review Comment: I'd prefer not to, as that would simply be hiding a bug which might cause other problems (often caused by a call to getType() instead of getGenericType()). ########## sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/utils/ReflectUtils.java: ########## @@ -243,4 +255,45 @@ public static TypeDescriptor boxIfPrimitive(TypeDescriptor typeDescriptor) { ? TypeDescriptor.of(Primitives.wrap(typeDescriptor.getRawType())) : typeDescriptor; } + + public static <T> Map<Type, Type> getAllBoundTypes(TypeDescriptor<T> typeDescriptor) { + Map<Type, Type> boundParameters = Maps.newHashMap(); + TypeDescriptor<?> currentType = typeDescriptor; + do { + if (currentType.getType() instanceof ParameterizedType) { + ParameterizedType parameterizedType = (ParameterizedType) currentType.getType(); + TypeVariable<?>[] typeVariables = currentType.getRawType().getTypeParameters(); + Type[] typeArguments = parameterizedType.getActualTypeArguments(); + ; + if (typeArguments.length != typeVariables.length) { + throw new RuntimeException("Unmatching arguments lengths"); Review Comment: Done ########## sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/utils/ReflectUtils.java: ########## @@ -243,4 +255,45 @@ public static TypeDescriptor boxIfPrimitive(TypeDescriptor typeDescriptor) { ? TypeDescriptor.of(Primitives.wrap(typeDescriptor.getRawType())) : typeDescriptor; } + + public static <T> Map<Type, Type> getAllBoundTypes(TypeDescriptor<T> typeDescriptor) { Review Comment: Done -- 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: github-unsubscr...@beam.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org