private static void recursivelyRegisterGenericType(Type fieldType, ExecutionConfig config, Set<Class<?>> alreadySeen) { if (fieldType instanceof ParameterizedType) { // field has generics ParameterizedType parameterizedFieldType = (ParameterizedType) fieldType; for (Type t: parameterizedFieldType.getActualTypeArguments()) { if (TypeExtractionUtils.isClassType(t) ) { recursivelyRegisterType(TypeExtractionUtils.typeToClass(t), config, alreadySeen); } } recursivelyRegisterGenericType(parameterizedFieldType.getRawType(), config, alreadySeen); } else if (fieldType instanceof GenericArrayType) { GenericArrayType genericArrayType = (GenericArrayType) fieldType; recursivelyRegisterGenericType(genericArrayType.getGenericComponentType(), config, alreadySeen); } else if (fieldType instanceof Class) { Class<?> clazz = (Class<?>) fieldType; recursivelyRegisterType(clazz, config, alreadySeen); } }
public static void recursivelyRegisterType(TypeInformation<?> typeInfo, ExecutionConfig config, Set<Class<?>> alreadySeen) { if (typeInfo instanceof GenericTypeInfo) { GenericTypeInfo<?> genericTypeInfo = (GenericTypeInfo<?>) typeInfo; Serializers.recursivelyRegisterType(genericTypeInfo.getTypeClass(), config, alreadySeen); } else if (typeInfo instanceof CompositeType) { List<GenericTypeInfo<?>> genericTypesInComposite = new ArrayList<>(); getContainedGenericTypes((CompositeType<?>)typeInfo, genericTypesInComposite); for (GenericTypeInfo<?> gt : genericTypesInComposite) { Serializers.recursivelyRegisterType(gt.getTypeClass(), config, alreadySeen); } } else if (typeInfo instanceof ObjectArrayTypeInfo) { ObjectArrayTypeInfo<?, ?> objectArrayTypeInfo = (ObjectArrayTypeInfo<?, ?>) typeInfo; recursivelyRegisterType(objectArrayTypeInfo.getComponentInfo(), config, alreadySeen); } }
assertEquals(GenericTypeInfo.class, te.getClass()); Serializers.recursivelyRegisterType(te.getTypeClass(), ec, new HashSet<>());
/** * Returns all GenericTypeInfos contained in a composite type. * * @param typeInfo {@link CompositeType} */ private static void getContainedGenericTypes(CompositeType<?> typeInfo, List<GenericTypeInfo<?>> target) { for (int i = 0; i < typeInfo.getArity(); i++) { TypeInformation<?> type = typeInfo.getTypeAt(i); if (type instanceof CompositeType) { getContainedGenericTypes((CompositeType<?>) type, target); } else if (type instanceof GenericTypeInfo) { if (!target.contains(type)) { target.add((GenericTypeInfo<?>) type); } } } }
@Test public void testTypeRegistration() { ExecutionConfig conf = new ExecutionConfig(); Serializers.recursivelyRegisterType(ClassWithNested.class, conf, new HashSet<Class<?>>()); KryoSerializer<String> kryo = new KryoSerializer<>(String.class, conf); // we create Kryo from another type. Assert.assertTrue(kryo.getKryo().getRegistration(FromNested.class).getId() > 0); Assert.assertTrue(kryo.getKryo().getRegistration(ClassWithNested.class).getId() > 0); Assert.assertTrue(kryo.getKryo().getRegistration(Path.class).getId() > 0); // check if the generic type from one field is also registered (its very likely that // generic types are also used as fields somewhere. Assert.assertTrue(kryo.getKryo().getRegistration(FromGeneric1.class).getId() > 0); Assert.assertTrue(kryo.getKryo().getRegistration(FromGeneric2.class).getId() > 0); Assert.assertTrue(kryo.getKryo().getRegistration(Node.class).getId() > 0); // register again and make sure classes are still registered ExecutionConfig conf2 = new ExecutionConfig(); Serializers.recursivelyRegisterType(ClassWithNested.class, conf2, new HashSet<Class<?>>()); KryoSerializer<String> kryo2 = new KryoSerializer<>(String.class, conf); assertTrue(kryo2.getKryo().getRegistration(FromNested.class).getId() > 0); }
/** * Returns all GenericTypeInfos contained in a composite type. * * @param typeInfo {@link CompositeType} */ private static void getContainedGenericTypes(CompositeType<?> typeInfo, List<GenericTypeInfo<?>> target) { for (int i = 0; i < typeInfo.getArity(); i++) { TypeInformation<?> type = typeInfo.getTypeAt(i); if (type instanceof CompositeType) { getContainedGenericTypes((CompositeType<?>) type, target); } else if (type instanceof GenericTypeInfo) { if (!target.contains(type)) { target.add((GenericTypeInfo<?>) type); } } } }
public static void recursivelyRegisterType(Class<?> type, ExecutionConfig config, Set<Class<?>> alreadySeen) { // don't register or remember primitives if (type == null || type.isPrimitive() || type == Object.class) { return; } // prevent infinite recursion for recursive types if (!alreadySeen.add(type)) { return; } if (type.isArray()) { recursivelyRegisterType(type.getComponentType(), config, alreadySeen); } else { config.registerKryoType(type); // add serializers for Avro type if necessary AvroUtils.getAvroUtils().addAvroSerializersIfRequired(config, type); Field[] fields = type.getDeclaredFields(); for (Field field : fields) { if (Modifier.isStatic(field.getModifiers()) || Modifier.isTransient(field.getModifiers())) { continue; } Type fieldType = field.getGenericType(); recursivelyRegisterGenericType(fieldType, config, alreadySeen); } } }
public static void recursivelyRegisterType(TypeInformation<?> typeInfo, ExecutionConfig config, Set<Class<?>> alreadySeen) { if (typeInfo instanceof GenericTypeInfo) { GenericTypeInfo<?> genericTypeInfo = (GenericTypeInfo<?>) typeInfo; Serializers.recursivelyRegisterType(genericTypeInfo.getTypeClass(), config, alreadySeen); } else if (typeInfo instanceof CompositeType) { List<GenericTypeInfo<?>> genericTypesInComposite = new ArrayList<>(); getContainedGenericTypes((CompositeType<?>)typeInfo, genericTypesInComposite); for (GenericTypeInfo<?> gt : genericTypesInComposite) { Serializers.recursivelyRegisterType(gt.getTypeClass(), config, alreadySeen); } } else if (typeInfo instanceof ObjectArrayTypeInfo) { ObjectArrayTypeInfo<?, ?> objectArrayTypeInfo = (ObjectArrayTypeInfo<?, ?>) typeInfo; recursivelyRegisterType(objectArrayTypeInfo.getComponentInfo(), config, alreadySeen); } }
@Test public void testTypeRegistrationFromTypeInfo() { ExecutionConfig conf = new ExecutionConfig(); Serializers.recursivelyRegisterType(new GenericTypeInfo<>(ClassWithNested.class), conf, new HashSet<Class<?>>()); KryoSerializer<String> kryo = new KryoSerializer<>(String.class, conf); // we create Kryo from another type. assertTrue(kryo.getKryo().getRegistration(FromNested.class).getId() > 0); assertTrue(kryo.getKryo().getRegistration(ClassWithNested.class).getId() > 0); assertTrue(kryo.getKryo().getRegistration(Path.class).getId() > 0); // check if the generic type from one field is also registered (its very likely that // generic types are also used as fields somewhere. assertTrue(kryo.getKryo().getRegistration(FromGeneric1.class).getId() > 0); assertTrue(kryo.getKryo().getRegistration(FromGeneric2.class).getId() > 0); assertTrue(kryo.getKryo().getRegistration(Node.class).getId() > 0); } }
/** * Returns all GenericTypeInfos contained in a composite type. * * @param typeInfo {@link CompositeType} */ private static void getContainedGenericTypes(CompositeType<?> typeInfo, List<GenericTypeInfo<?>> target) { for (int i = 0; i < typeInfo.getArity(); i++) { TypeInformation<?> type = typeInfo.getTypeAt(i); if (type instanceof CompositeType) { getContainedGenericTypes((CompositeType<?>) type, target); } else if (type instanceof GenericTypeInfo) { if (!target.contains(type)) { target.add((GenericTypeInfo<?>) type); } } } }
private static void recursivelyRegisterGenericType(Type fieldType, ExecutionConfig config, Set<Class<?>> alreadySeen) { if (fieldType instanceof ParameterizedType) { // field has generics ParameterizedType parameterizedFieldType = (ParameterizedType) fieldType; for (Type t: parameterizedFieldType.getActualTypeArguments()) { if (TypeExtractionUtils.isClassType(t) ) { recursivelyRegisterType(TypeExtractionUtils.typeToClass(t), config, alreadySeen); } } recursivelyRegisterGenericType(parameterizedFieldType.getRawType(), config, alreadySeen); } else if (fieldType instanceof GenericArrayType) { GenericArrayType genericArrayType = (GenericArrayType) fieldType; recursivelyRegisterGenericType(genericArrayType.getGenericComponentType(), config, alreadySeen); } else if (fieldType instanceof Class) { Class<?> clazz = (Class<?>) fieldType; recursivelyRegisterType(clazz, config, alreadySeen); } }
public static void recursivelyRegisterType(TypeInformation<?> typeInfo, ExecutionConfig config, Set<Class<?>> alreadySeen) { if (typeInfo instanceof GenericTypeInfo) { GenericTypeInfo<?> genericTypeInfo = (GenericTypeInfo<?>) typeInfo; Serializers.recursivelyRegisterType(genericTypeInfo.getTypeClass(), config, alreadySeen); } else if (typeInfo instanceof CompositeType) { List<GenericTypeInfo<?>> genericTypesInComposite = new ArrayList<>(); getContainedGenericTypes((CompositeType<?>)typeInfo, genericTypesInComposite); for (GenericTypeInfo<?> gt : genericTypesInComposite) { Serializers.recursivelyRegisterType(gt.getTypeClass(), config, alreadySeen); } } else if (typeInfo instanceof ObjectArrayTypeInfo) { ObjectArrayTypeInfo<?, ?> objectArrayTypeInfo = (ObjectArrayTypeInfo<?, ?>) typeInfo; recursivelyRegisterType(objectArrayTypeInfo.getComponentInfo(), config, alreadySeen); } }
private static void recursivelyRegisterGenericType(Type fieldType, ExecutionConfig config, Set<Class<?>> alreadySeen) { if (fieldType instanceof ParameterizedType) { // field has generics ParameterizedType parameterizedFieldType = (ParameterizedType) fieldType; for (Type t: parameterizedFieldType.getActualTypeArguments()) { if (TypeExtractionUtils.isClassType(t) ) { recursivelyRegisterType(TypeExtractionUtils.typeToClass(t), config, alreadySeen); } } recursivelyRegisterGenericType(parameterizedFieldType.getRawType(), config, alreadySeen); } else if (fieldType instanceof GenericArrayType) { GenericArrayType genericArrayType = (GenericArrayType) fieldType; recursivelyRegisterGenericType(genericArrayType.getGenericComponentType(), config, alreadySeen); } else if (fieldType instanceof Class) { Class<?> clazz = (Class<?>) fieldType; recursivelyRegisterType(clazz, config, alreadySeen); } }
public static void recursivelyRegisterType(Class<?> type, ExecutionConfig config, Set<Class<?>> alreadySeen) { // don't register or remember primitives if (type == null || type.isPrimitive() || type == Object.class) { return; } // prevent infinite recursion for recursive types if (!alreadySeen.add(type)) { return; } if (type.isArray()) { recursivelyRegisterType(type.getComponentType(), config, alreadySeen); } else { config.registerKryoType(type); // add serializers for Avro type if necessary AvroUtils.getAvroUtils().addAvroSerializersIfRequired(config, type); Field[] fields = type.getDeclaredFields(); for (Field field : fields) { if (Modifier.isStatic(field.getModifiers()) || Modifier.isTransient(field.getModifiers())) { continue; } Type fieldType = field.getGenericType(); recursivelyRegisterGenericType(fieldType, config, alreadySeen); } } }
public static void recursivelyRegisterType(Class<?> type, ExecutionConfig config, Set<Class<?>> alreadySeen) { // don't register or remember primitives if (type == null || type.isPrimitive() || type == Object.class) { return; } // prevent infinite recursion for recursive types if (!alreadySeen.add(type)) { return; } if (type.isArray()) { recursivelyRegisterType(type.getComponentType(), config, alreadySeen); } else { config.registerKryoType(type); // add serializers for Avro type if necessary AvroUtils.getAvroUtils().addAvroSerializersIfRequired(config, type); Field[] fields = type.getDeclaredFields(); for (Field field : fields) { if (Modifier.isStatic(field.getModifiers()) || Modifier.isTransient(field.getModifiers())) { continue; } Type fieldType = field.getGenericType(); recursivelyRegisterGenericType(fieldType, config, alreadySeen); } } }