Repository: crunch Updated Branches: refs/heads/master fb218002e -> 983019aab
CRUNCH-612: Add support of private ctors to AvroDeepCopier Signed-off-by: Micah Whitacre <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/crunch/repo Commit: http://git-wip-us.apache.org/repos/asf/crunch/commit/983019aa Tree: http://git-wip-us.apache.org/repos/asf/crunch/tree/983019aa Diff: http://git-wip-us.apache.org/repos/asf/crunch/diff/983019aa Branch: refs/heads/master Commit: 983019aab946a0505569f34ef925640244845f6d Parents: fb21800 Author: Clément MATHIEU <[email protected]> Authored: Tue Jul 19 21:01:20 2016 +0200 Committer: Micah Whitacre <[email protected]> Committed: Fri Jul 29 16:33:05 2016 -0500 ---------------------------------------------------------------------- .../crunch/types/avro/AvroDeepCopier.java | 9 ++--- .../crunch/types/avro/AvroDeepCopierTest.java | 13 +++++++ .../crunch/types/avro/PojoWithPrivateCtor.java | 37 ++++++++++++++++++++ 3 files changed, 55 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/crunch/blob/983019aa/crunch-core/src/main/java/org/apache/crunch/types/avro/AvroDeepCopier.java ---------------------------------------------------------------------- diff --git a/crunch-core/src/main/java/org/apache/crunch/types/avro/AvroDeepCopier.java b/crunch-core/src/main/java/org/apache/crunch/types/avro/AvroDeepCopier.java index e6b878a..44ae9c7 100644 --- a/crunch-core/src/main/java/org/apache/crunch/types/avro/AvroDeepCopier.java +++ b/crunch-core/src/main/java/org/apache/crunch/types/avro/AvroDeepCopier.java @@ -19,6 +19,7 @@ package org.apache.crunch.types.avro; import java.io.ByteArrayOutputStream; import java.io.Serializable; +import java.lang.reflect.Constructor; import java.nio.ByteBuffer; import org.apache.avro.Schema; @@ -149,10 +150,10 @@ abstract class AvroDeepCopier<T> implements DeepCopier<T>, Serializable { protected T createNewInstance(Class<T> targetClass) { try { - return targetClass.newInstance(); - } catch (InstantiationException e) { - throw new CrunchRuntimeException(e); - } catch (IllegalAccessException e) { + Constructor<T> ctor = targetClass.getDeclaredConstructor(); + ctor.setAccessible(true); + return ctor.newInstance(); + } catch (ReflectiveOperationException e) { throw new CrunchRuntimeException(e); } } http://git-wip-us.apache.org/repos/asf/crunch/blob/983019aa/crunch-core/src/test/java/org/apache/crunch/types/avro/AvroDeepCopierTest.java ---------------------------------------------------------------------- diff --git a/crunch-core/src/test/java/org/apache/crunch/types/avro/AvroDeepCopierTest.java b/crunch-core/src/test/java/org/apache/crunch/types/avro/AvroDeepCopierTest.java index e3717c8..46eba4e 100644 --- a/crunch-core/src/test/java/org/apache/crunch/types/avro/AvroDeepCopierTest.java +++ b/crunch-core/src/test/java/org/apache/crunch/types/avro/AvroDeepCopierTest.java @@ -29,6 +29,8 @@ import java.io.ObjectOutputStream; import java.nio.ByteBuffer; import com.google.common.collect.Lists; + +import org.apache.avro.Schema; import org.apache.avro.generic.GenericData.Record; import org.apache.crunch.test.Person; import org.apache.crunch.types.PType; @@ -89,7 +91,18 @@ public class AvroDeepCopierTest { assertEquals(person, deepCopyPerson); assertNotSame(person, deepCopyPerson); + } + + @Test + public void testDeepCopyReflect_privateCtor() { + Schema schema = Avros.reflects(PojoWithPrivateCtor.class).getSchema(); + AvroDeepCopier<PojoWithPrivateCtor> avroDeepCopier = new AvroDeepCopier.AvroReflectDeepCopier<>(schema); + avroDeepCopier.initialize(new Configuration()); + + PojoWithPrivateCtor orig = new PojoWithPrivateCtor("foo"); + PojoWithPrivateCtor deepCopy = avroDeepCopier.deepCopy(orig); + assertEquals(orig.getField(), deepCopy.getField()); } @Test http://git-wip-us.apache.org/repos/asf/crunch/blob/983019aa/crunch-core/src/test/java/org/apache/crunch/types/avro/PojoWithPrivateCtor.java ---------------------------------------------------------------------- diff --git a/crunch-core/src/test/java/org/apache/crunch/types/avro/PojoWithPrivateCtor.java b/crunch-core/src/test/java/org/apache/crunch/types/avro/PojoWithPrivateCtor.java new file mode 100644 index 0000000..2d0a705 --- /dev/null +++ b/crunch-core/src/test/java/org/apache/crunch/types/avro/PojoWithPrivateCtor.java @@ -0,0 +1,37 @@ +/* + * 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.crunch.types.avro; + +/** + * A test helper class to check that Avro reflect support private constructors + */ +class PojoWithPrivateCtor { + + private String field; + + public PojoWithPrivateCtor(String field) { + this.field = field; + } + + private PojoWithPrivateCtor() {} + + public String getField() { + return field; + } +}
