flink-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fhue...@apache.org
Subject [3/4] flink git commit: [FLINK-2874] Fix Avro getter/setter recognition
Date Fri, 23 Oct 2015 08:11:28 GMT
[FLINK-2874] Fix Avro getter/setter recognition

This closes #1252


Project: http://git-wip-us.apache.org/repos/asf/flink/repo
Commit: http://git-wip-us.apache.org/repos/asf/flink/commit/42b5ead5
Tree: http://git-wip-us.apache.org/repos/asf/flink/tree/42b5ead5
Diff: http://git-wip-us.apache.org/repos/asf/flink/diff/42b5ead5

Branch: refs/heads/release-0.10
Commit: 42b5ead564f1dde16892b85c8fe04fcd2e819cc1
Parents: 5c3eb8b
Author: Ulf Karlsson <ukarlsson@spotify.com>
Authored: Sun Oct 11 00:04:57 2015 +0200
Committer: Fabian Hueske <fhueske@apache.org>
Committed: Fri Oct 23 10:09:34 2015 +0200

----------------------------------------------------------------------
 .../flink/api/java/typeutils/TypeExtractor.java | 17 +++++---
 .../api/java/typeutils/PojoTypeInfoTest.java    | 44 +++++++++++++++++++-
 2 files changed, 53 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flink/blob/42b5ead5/flink-java/src/main/java/org/apache/flink/api/java/typeutils/TypeExtractor.java
----------------------------------------------------------------------
diff --git a/flink-java/src/main/java/org/apache/flink/api/java/typeutils/TypeExtractor.java
b/flink-java/src/main/java/org/apache/flink/api/java/typeutils/TypeExtractor.java
index 1dec90b..0281da6 100644
--- a/flink-java/src/main/java/org/apache/flink/api/java/typeutils/TypeExtractor.java
+++ b/flink-java/src/main/java/org/apache/flink/api/java/typeutils/TypeExtractor.java
@@ -30,6 +30,7 @@ import java.util.ArrayList;
 import java.util.List;
 
 import org.apache.avro.specific.SpecificRecordBase;
+import org.apache.commons.lang3.ClassUtils;
 import org.apache.flink.api.common.functions.CoGroupFunction;
 import org.apache.flink.api.common.functions.CrossFunction;
 import org.apache.flink.api.common.functions.FlatJoinFunction;
@@ -1299,22 +1300,26 @@ public class TypeExtractor {
 			return true;
 		} else {
 			boolean hasGetter = false, hasSetter = false;
-			final String fieldNameLow = f.getName().toLowerCase();
-			
+			final String fieldNameLow = f.getName().toLowerCase().replaceAll("_", "");
+
 			Type fieldType = f.getGenericType();
+			Class<?> fieldTypeWrapper = ClassUtils.primitiveToWrapper(f.getType());
+
 			TypeVariable<?> fieldTypeGeneric = null;
 			if(fieldType instanceof TypeVariable) {
 				fieldTypeGeneric = (TypeVariable<?>) fieldType;
 				fieldType = materializeTypeVariable(typeHierarchy, (TypeVariable<?>)fieldType);
 			}
 			for(Method m : clazz.getMethods()) {
+				final String methodNameLow = m.getName().toLowerCase().replaceAll("_", "");
+
 				// check for getter
 				if(	// The name should be "get<FieldName>" or "<fieldName>" (for scala) or
"is<fieldName>" for boolean fields.
-					(m.getName().toLowerCase().equals("get"+fieldNameLow) || m.getName().toLowerCase().equals("is"+fieldNameLow)
|| m.getName().toLowerCase().equals(fieldNameLow)) &&
+					(methodNameLow.equals("get"+fieldNameLow) || methodNameLow.equals("is"+fieldNameLow)
|| methodNameLow.equals(fieldNameLow)) &&
 					// no arguments for the getter
 					m.getParameterTypes().length == 0 &&
 					// return type is same as field type (or the generic variant of it)
-					(m.getGenericReturnType().equals( fieldType ) || (fieldTypeGeneric != null &&
m.getGenericReturnType().equals(fieldTypeGeneric)) )
+					(m.getGenericReturnType().equals( fieldType ) || (fieldTypeWrapper != null &&
m.getReturnType().equals( fieldTypeWrapper )) || (fieldTypeGeneric != null && m.getGenericReturnType().equals(fieldTypeGeneric))
)
 				) {
 					if(hasGetter) {
 						throw new IllegalStateException("Detected more than one getter");
@@ -1322,9 +1327,9 @@ public class TypeExtractor {
 					hasGetter = true;
 				}
 				// check for setters (<FieldName>_$eq for scala)
-				if((m.getName().toLowerCase().equals("set"+fieldNameLow) || m.getName().toLowerCase().equals(fieldNameLow+"_$eq"))
&&
+				if((methodNameLow.equals("set"+fieldNameLow) || methodNameLow.equals(fieldNameLow+"_$eq"))
&&
 					m.getParameterTypes().length == 1 && // one parameter of the field's type
-					( m.getGenericParameterTypes()[0].equals( fieldType ) || (fieldTypeGeneric != null &&
m.getGenericParameterTypes()[0].equals(fieldTypeGeneric) ) )&&
+					(m.getGenericParameterTypes()[0].equals( fieldType ) || (fieldTypeWrapper != null &&
m.getParameterTypes()[0].equals( fieldTypeWrapper )) || (fieldTypeGeneric != null &&
m.getGenericParameterTypes()[0].equals(fieldTypeGeneric) ) )&&
 					// return type is void.
 					m.getReturnType().equals(Void.TYPE)
 				) {

http://git-wip-us.apache.org/repos/asf/flink/blob/42b5ead5/flink-java/src/test/java/org/apache/flink/api/java/typeutils/PojoTypeInfoTest.java
----------------------------------------------------------------------
diff --git a/flink-java/src/test/java/org/apache/flink/api/java/typeutils/PojoTypeInfoTest.java
b/flink-java/src/test/java/org/apache/flink/api/java/typeutils/PojoTypeInfoTest.java
index 2fe1357..dbe5115 100644
--- a/flink-java/src/test/java/org/apache/flink/api/java/typeutils/PojoTypeInfoTest.java
+++ b/flink-java/src/test/java/org/apache/flink/api/java/typeutils/PojoTypeInfoTest.java
@@ -74,11 +74,25 @@ public class PojoTypeInfoTest {
 
 		assertEquals(pojoTypeInfo, deserializedPojoTypeInfo);
 	}
-	
+
+	@Test
+	public void testPrimitivePojo() {
+		TypeInformation<PrimitivePojo> info1 = TypeExtractor.getForClass(PrimitivePojo.class);
+
+		assertTrue(info1 instanceof PojoTypeInfo);
+	}
+
+	@Test
+	public void testUnderscorePojo() {
+		TypeInformation<UnderscorePojo> info1 = TypeExtractor.getForClass(UnderscorePojo.class);
+
+		assertTrue(info1 instanceof PojoTypeInfo);
+	}
+
 	public static final class TestPojo {
 		
 		public int someInt;
-		
+
 		private String aString;
 		
 		public Double[] doubleArray;
@@ -110,4 +124,30 @@ public class PojoTypeInfoTest {
 			return aString;
 		}
 	}
+
+	public static final class PrimitivePojo {
+
+		private int someInt;
+
+		public void setSomeInt(Integer someInt) {
+			this.someInt = someInt;
+		}
+
+		public Integer getSomeInt() {
+			return this.someInt;
+		}
+	}
+
+	public static final class UnderscorePojo {
+
+		private int some_int;
+
+		public void setSomeInt(int some_int) {
+			this.some_int = some_int;
+		}
+
+		public Integer getSomeInt() {
+			return this.some_int;
+		}
+	}
 }


Mime
View raw message