Skip to content

Commit

Permalink
fixed ProtobufWriter writeValue setter method not found issue
Browse files Browse the repository at this point in the history
when domain value type is a subclass of setter parameter type
  • Loading branch information
ppoffice committed Nov 20, 2017
1 parent c0aa141 commit 02bd149
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,26 @@ protected void write(final Object destination, final FieldResolver fieldResolver


private void writeValue(final Object destination, final FieldResolver fieldResolver, final Object value) throws WriteException {
String setterName = FieldUtils.createProtobufSetterName(fieldResolver);
try {
destinationClass.getMethod(setterName, extractValueClass(value)).invoke(destination, value);
} catch (IllegalAccessException e) {
throw new WriteException(
String.format("Access denied. '%s.%s()'", destinationClass.getName(), setterName));
} catch (InvocationTargetException e) {
throw new WriteException(
String.format("Can't set field value through '%s.%s()'", destinationClass.getName(), setterName));
} catch (NoSuchMethodException e) {
throw new WriteException(
String.format("Setter not found: '%s.%s()'", destinationClass.getName(), setterName));
Class<?> valueClass = extractValueClass(value);
while (valueClass != null) {
String setterName = FieldUtils.createProtobufSetterName(fieldResolver);
try {
destinationClass.getMethod(setterName, valueClass).invoke(destination, value);
break;
} catch (IllegalAccessException e) {
throw new WriteException(
String.format("Access denied. '%s.%s(%s)'", destinationClass.getName(), setterName, valueClass));
} catch (InvocationTargetException e) {
throw new WriteException(
String.format("Can't set field value through '%s.%s(%s)'", destinationClass.getName(), setterName, valueClass));
} catch (NoSuchMethodException e) {
if (valueClass.getSuperclass() != null) {
valueClass = valueClass.getSuperclass();
} else {
throw new WriteException(
String.format("Setter not found: '%s.%s(%s)'", destinationClass.getName(), setterName, valueClass));
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.badata.protobuf.converter;

import com.google.protobuf.ByteString;
import net.badata.protobuf.converter.domain.ConverterDomain;
import net.badata.protobuf.converter.proto.ConverterProto;
import org.junit.Assert;
Expand Down Expand Up @@ -55,6 +56,7 @@ private void createTestProtobuf() {
.addStringListValue("10")
.addComplexListValue(ConverterProto.PrimitiveTest.newBuilder().setIntValue(1001))
.addComplexSetValue(ConverterProto.PrimitiveTest.newBuilder().setIntValue(1002))
.setBytesValue(ByteString.copyFrom(new byte[]{ 0, 1, 3, 7 }))
.build();
}

Expand Down Expand Up @@ -91,6 +93,8 @@ private void createTestDomain() {
primitiveTestItem.setIntValue(-1002);
testDomain.setComplexSetValue(new HashSet<ConverterDomain.PrimitiveTest>(Arrays.asList(primitiveTestSItem)));
testDomain.setComplexNullableCollectionValue(null);

testDomain.setBytesValue(ByteString.copyFrom(new byte[]{ 0, 1, 3, 7 }));
}

private void createIgnoredFieldsMap() {
Expand Down Expand Up @@ -146,6 +150,8 @@ public void testProtobufToDomain() {
result.getComplexSetValue().iterator().next().getIntValue());

Assert.assertTrue(result.getComplexNullableCollectionValue().isEmpty());

Assert.assertEquals(testProtobuf.getBytesValue(), testDomain.getBytesValue());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.badata.protobuf.converter.domain;

import com.google.protobuf.ByteString;
import com.google.protobuf.Message;
import net.badata.protobuf.converter.annotation.ProtoClass;
import net.badata.protobuf.converter.annotation.ProtoField;
Expand Down Expand Up @@ -55,6 +56,8 @@ public static class Test {
private Set<PrimitiveTest> complexSetValue;
@ProtoField
private List<PrimitiveTest> complexNullableCollectionValue;
@ProtoField
private ByteString bytesValue;


public Long getLongValue() {
Expand Down Expand Up @@ -161,6 +164,14 @@ public List<PrimitiveTest> getComplexNullableCollectionValue() {
public void setComplexNullableCollectionValue(final List<PrimitiveTest> complexNullableCollectionValue) {
this.complexNullableCollectionValue = complexNullableCollectionValue;
}

public ByteString getBytesValue() {
return bytesValue;
}

public void setBytesValue(ByteString bytesValue) {
this.bytesValue = bytesValue;
}
}

@ProtoClass(ConverterProto.PrimitiveTest.class)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/test/proto/converter_test.proto
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ message ConverterTest {
repeated PrimitiveTest complexListValue = 11;
repeated PrimitiveTest complexSetValue = 12;
repeated PrimitiveTest complexNullableCollectionValue = 13;
bytes bytesValue = 14;
}

0 comments on commit 02bd149

Please sign in to comment.