echauchot commented on a change in pull request #6034:
URL: https://github.com/apache/flink/pull/6034#discussion_r531523021
##########
File path:
flink-core/src/test/java/org/apache/flink/api/java/typeutils/PojoTypeExtractionTest.java
##########
@@ -877,4 +878,57 @@ public void testRecursivePojoWithTypeVariable() {
Assert.assertEquals(GenericTypeInfo.class, ((PojoTypeInfo)
pti).getPojoFieldAt(0).getTypeInformation().getClass());
}
+ /**
+ * POJO with a bounded generic parameter.
+ *
+ * @param <T> generic parameter bounded by one type
+ */
+ public static class BoundedPojo<T extends Tuple2<Integer, String>> {
+
+ public T someKey;
+
+ public BoundedPojo() {}
+
+ public BoundedPojo(T someKey) {
+ this.someKey = someKey;
+ }
+ }
+
+ @Test
+ public void testBoundedPojos() {
+ TypeInformation<?> ti =
TypeExtractor.createTypeInfo(BoundedPojo.class);
+ Assert.assertTrue(ti instanceof PojoTypeInfo);
+ PojoTypeInfo<?> pti = (PojoTypeInfo<?>) ti;
+ Assert.assertEquals(BoundedPojo.class, pti.getTypeClass());
+ Assert.assertTrue(pti.getPojoFieldAt(0).getTypeInformation()
instanceof GenericTypeInfo);
+ TypeInformation<?> fti =
pti.getPojoFieldAt(0).getTypeInformation();
+ Assert.assertEquals(Tuple2.class, fti.getTypeClass());
+ }
+
+ /**
+ * POJO with a bounded generic parameter.
+ *
+ * @param <T> generic parameter bounded by two types
+ */
+ public static class MultiBoundedPojo<T extends Tuple2<Integer, String>
& Serializable> {
+
+ public T someKey;
+
+ public MultiBoundedPojo() {}
+
+ public MultiBoundedPojo(T someKey) {
+ this.someKey = someKey;
+ }
+ }
+
+ @Test
+ public void testMultiBoundedPojos() {
+ TypeInformation<?> ti =
TypeExtractor.createTypeInfo(MultiBoundedPojo.class);
+ Assert.assertTrue(ti instanceof PojoTypeInfo);
+ PojoTypeInfo<?> pti = (PojoTypeInfo<?>) ti;
+ Assert.assertEquals(MultiBoundedPojo.class, pti.getTypeClass());
+ Assert.assertTrue(pti.getPojoFieldAt(0).getTypeInformation()
instanceof GenericTypeInfo);
+ TypeInformation<?> fti =
pti.getPojoFieldAt(0).getTypeInformation();
+ Assert.assertEquals(Tuple2.class, fti.getTypeClass());
Review comment:
ditto
##########
File path:
flink-core/src/test/java/org/apache/flink/api/java/typeutils/PojoTypeExtractionTest.java
##########
@@ -877,4 +878,57 @@ public void testRecursivePojoWithTypeVariable() {
Assert.assertEquals(GenericTypeInfo.class, ((PojoTypeInfo)
pti).getPojoFieldAt(0).getTypeInformation().getClass());
}
+ /**
+ * POJO with a bounded generic parameter.
+ *
+ * @param <T> generic parameter bounded by one type
+ */
+ public static class BoundedPojo<T extends Tuple2<Integer, String>> {
+
+ public T someKey;
+
+ public BoundedPojo() {}
+
+ public BoundedPojo(T someKey) {
+ this.someKey = someKey;
+ }
+ }
+
+ @Test
+ public void testBoundedPojos() {
+ TypeInformation<?> ti =
TypeExtractor.createTypeInfo(BoundedPojo.class);
+ Assert.assertTrue(ti instanceof PojoTypeInfo);
+ PojoTypeInfo<?> pti = (PojoTypeInfo<?>) ti;
+ Assert.assertEquals(BoundedPojo.class, pti.getTypeClass());
+ Assert.assertTrue(pti.getPojoFieldAt(0).getTypeInformation()
instanceof GenericTypeInfo);
+ TypeInformation<?> fti =
pti.getPojoFieldAt(0).getTypeInformation();
+ Assert.assertEquals(Tuple2.class, fti.getTypeClass());
Review comment:
nit: what about adding an error message for clarity? I mean this
particular assert is the feature we want to address so I think it deserves an
error message. The other asserts are self explanatory I think
##########
File path:
flink-core/src/main/java/org/apache/flink/api/java/typeutils/TypeExtractor.java
##########
@@ -1924,7 +1924,16 @@ else if (typeHierarchy.size() <= 1) {
pojoFields.add(new PojoField(field, ti));
} catch (InvalidTypesException e) {
Class<?> genericClass = Object.class;
- if(isClassType(fieldType)) {
+ // use information about bounds if available
+ if (fieldType instanceof TypeVariable) {
+ TypeVariable<?> typeVar =
(TypeVariable<?>) fieldType;
+ // we take the first bound (usually the
class), everything is better than Object
+ if (typeVar.getBounds().length > 0 &&
isClassType(typeVar.getBounds()[0])) {
+ genericClass =
typeToClass(typeVar.getBounds()[0]);
Review comment:
_getBounds()_ can throw runtime exceptions, should we not fall back to
Object if it happens ?
----------------------------------------------------------------
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]