diff --git a/src/main/java/net/badata/protobuf/converter/Converter.java b/src/main/java/net/badata/protobuf/converter/Converter.java index 6446c92..6665d61 100644 --- a/src/main/java/net/badata/protobuf/converter/Converter.java +++ b/src/main/java/net/badata/protobuf/converter/Converter.java @@ -192,14 +192,13 @@ private void fillDomainField(final FieldResolver fieldResolver, final MappingRes throws WriteException { DomainWriter fieldWriter = new DomainWriter(mappingResult.getDestination()); Object mappedValue = mappingResult.getValue(); - Field field = fieldResolver.getField(); switch (mappingResult.getCode()) { case NESTED_MAPPING: - fieldWriter.write(fieldResolver, createNestedConverter().toDomain(field.getType(), (Message) - mappedValue)); + fieldWriter.write(fieldResolver, createNestedConverter().toDomain(fieldResolver.getDomainType(), + (Message) mappedValue)); break; case COLLECTION_MAPPING: - Class collectionType = FieldUtils.extractCollectionType(field); + Class collectionType = FieldUtils.extractCollectionType(fieldResolver.getField()); if (FieldUtils.isComplexType(collectionType)) { mappedValue = createDomainValueList(collectionType, mappedValue); } @@ -302,20 +301,18 @@ private void fillProtobufField(final FieldResolver fieldResolver, final MappingR throws WriteException { ProtobufWriter fieldWriter = new ProtobufWriter((Message.Builder) mappingResult.getDestination()); Object mappedValue = mappingResult.getValue(); - Field field = fieldResolver.getField(); switch (mappingResult.getCode()) { case NESTED_MAPPING: - Class protobufClass = MessageUtils - .getMessageType(mappingResult.getDestination(), - FieldUtils.createProtobufGetterName(fieldResolver)); + Class protobufClass = MessageUtils.getMessageType(mappingResult.getDestination(), + FieldUtils.createProtobufGetterName(fieldResolver)); fieldWriter.write(fieldResolver, createNestedConverter().toProtobuf(protobufClass, mappedValue)); break; case COLLECTION_MAPPING: - Class collectionType = FieldUtils.extractCollectionType(field); + Class collectionType = FieldUtils.extractCollectionType(fieldResolver.getField()); if (FieldUtils.isComplexType(collectionType)) { Class protobufCollectionClass = MessageUtils.getMessageCollectionType( mappingResult.getDestination(), FieldUtils.createProtobufGetterName(fieldResolver)); - mappedValue = createProtobufValueList(protobufCollectionClass, field.getType(), + mappedValue = createProtobufValueList(protobufCollectionClass, fieldResolver.getDomainType(), (Collection) mappedValue); } case MAPPED: diff --git a/src/main/java/net/badata/protobuf/converter/resolver/AnnotatedFieldResolverFactoryImpl.java b/src/main/java/net/badata/protobuf/converter/resolver/AnnotatedFieldResolverFactoryImpl.java index 74c984c..7fa7d8c 100644 --- a/src/main/java/net/badata/protobuf/converter/resolver/AnnotatedFieldResolverFactoryImpl.java +++ b/src/main/java/net/badata/protobuf/converter/resolver/AnnotatedFieldResolverFactoryImpl.java @@ -21,6 +21,7 @@ import net.badata.protobuf.converter.exception.ConverterException; import net.badata.protobuf.converter.exception.WriteException; import net.badata.protobuf.converter.utils.AnnotationUtils; +import net.badata.protobuf.converter.utils.FieldUtils; import java.lang.reflect.Field; @@ -40,18 +41,25 @@ public class AnnotatedFieldResolverFactoryImpl implements FieldResolverFactory { public FieldResolver createResolver(final Field field) { DefaultFieldResolverImpl fieldResolver = new DefaultFieldResolverImpl(field); if (field.isAnnotationPresent(ProtoField.class)) { - ProtoField protoField = field.getAnnotation(ProtoField.class); - if (!"".equals(protoField.name())) { - fieldResolver.setProtobufName(protoField.name()); - } try { - fieldResolver.setConverter(AnnotationUtils.createTypeConverter(protoField)); - fieldResolver.setNullValueInspector(AnnotationUtils.createNullValueInspector(protoField)); - fieldResolver.setDefaultValue(AnnotationUtils.createDefaultValue(protoField)); + initializeFieldResolver(fieldResolver, field.getAnnotation(ProtoField.class)); } catch (WriteException e) { - throw new ConverterException("Can't create field resolver", e); + throw new ConverterException("Can't initialize field resolver", e); } } return fieldResolver; } + + private void initializeFieldResolver(final DefaultFieldResolverImpl resolver, final ProtoField annotation) throws + WriteException { + if (!"".equals(annotation.name())) { + resolver.setProtobufName(annotation.name()); + } + Class protobufType = FieldUtils.extractProtobufFieldType(annotation.converter(), resolver.getProtobufType()); + resolver.setProtobufType(protobufType); + resolver.setConverter(AnnotationUtils.createTypeConverter(annotation)); + resolver.setNullValueInspector(AnnotationUtils.createNullValueInspector(annotation)); + resolver.setDefaultValue(AnnotationUtils.createDefaultValue(annotation)); + } + } diff --git a/src/main/java/net/badata/protobuf/converter/resolver/DefaultFieldResolverImpl.java b/src/main/java/net/badata/protobuf/converter/resolver/DefaultFieldResolverImpl.java index 4767100..604a17d 100644 --- a/src/main/java/net/badata/protobuf/converter/resolver/DefaultFieldResolverImpl.java +++ b/src/main/java/net/badata/protobuf/converter/resolver/DefaultFieldResolverImpl.java @@ -36,6 +36,8 @@ public class DefaultFieldResolverImpl implements FieldResolver { private final Field field; private String domainName; private String protobufName; + private Class domainType; + private Class protobufType; private TypeConverter converter; private NullValueInspector nullValueInspector; private DefaultValue defaultValue; @@ -50,6 +52,8 @@ public DefaultFieldResolverImpl(final Field field) { this.field = field; this.domainName = field.getName(); this.protobufName = field.getName(); + this.domainType = field.getType(); + this.protobufType = field.getType(); this.converter = new DefaultConverterImpl(); this.nullValueInspector = new DefaultNullValueInspectorImpl(); this.defaultValue = new SimpleDefaultValueImpl(); @@ -79,6 +83,16 @@ public String getProtobufName() { return protobufName; } + @Override + public Class getDomainType() { + return domainType; + } + + @Override + public Class getProtobufType() { + return protobufType; + } + /** * {@inheritDoc} */ @@ -103,19 +117,51 @@ public DefaultValue getDefaultValue() { return defaultValue; } + /** + * Setter for protobuf field name. + * + * @param protobufName name of the protobuf field. + */ public void setProtobufName(final String protobufName) { this.protobufName = protobufName; } + /** + * Setter for protobuf field type. + * + * @param protobufType type of the protobuf field. + */ + public void setProtobufType(final Class protobufType) { + this.protobufType = protobufType; + } + + /** + * Setter for converter instance. + * + * @param converter Instance of field converter. + */ public void setConverter(final TypeConverter converter) { this.converter = converter; } + + /** + * Setter for protobuf field nullability inspector. + * + * @param nullValueInspector Instance of class that perform nullability check for protobuf field value. + */ public void setNullValueInspector(final NullValueInspector nullValueInspector) { this.nullValueInspector = nullValueInspector; } + /** + * Setter for domain field default value generator. + * + * @param defaultValue Instance of class that generates default value for domain object field. + */ public void setDefaultValue(final DefaultValue defaultValue) { this.defaultValue = defaultValue; } } + + diff --git a/src/main/java/net/badata/protobuf/converter/resolver/FieldResolver.java b/src/main/java/net/badata/protobuf/converter/resolver/FieldResolver.java index 23d6df3..692d0b7 100644 --- a/src/main/java/net/badata/protobuf/converter/resolver/FieldResolver.java +++ b/src/main/java/net/badata/protobuf/converter/resolver/FieldResolver.java @@ -51,6 +51,20 @@ public interface FieldResolver { */ String getProtobufName(); + /** + * Getter for domain class field type. + * + * @return String with domain field type. + */ + Class getDomainType(); + + /** + * Getter for protobuf message field type. + * + * @return String with protobuf field type. + */ + Class getProtobufType(); + /** * Getter for field type converter. * diff --git a/src/main/java/net/badata/protobuf/converter/utils/FieldUtils.java b/src/main/java/net/badata/protobuf/converter/utils/FieldUtils.java index a81bae1..0688d81 100644 --- a/src/main/java/net/badata/protobuf/converter/utils/FieldUtils.java +++ b/src/main/java/net/badata/protobuf/converter/utils/FieldUtils.java @@ -20,9 +20,11 @@ import net.badata.protobuf.converter.annotation.ProtoClass; import net.badata.protobuf.converter.annotation.ProtoClasses; import net.badata.protobuf.converter.resolver.FieldResolver; +import net.badata.protobuf.converter.type.TypeConverter; import java.lang.reflect.Field; import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; import java.util.Collection; /** @@ -30,14 +32,11 @@ */ public final class FieldUtils { - private static final String GETTER_PREFIX = "get"; private static final String SETTER_PREFIX = "set"; private static final String BOOLEAN_GETTER_PREFIX = "is"; private static final String PROTOBUF_LIST_GETTER_POSTFIX = "List"; private static final String PROTOBUF_LIST_SETTER_PREFIX = "addAll"; - private static final String PROTOBUF_NESTED_BUILDER_POSTFIX = "Builder"; - /** * Check whether field has own mapper. @@ -68,7 +67,17 @@ public static boolean isComplexType(final Class type) { * @return true if field type implements {@link java.util.Collection}, otherwise false. */ public static boolean isCollectionType(final Field field) { - return Collection.class.isAssignableFrom(field.getType()); + return isCollectionType(field.getType()); + } + + /** + * Check whether class implements Collection interface. + * + * @param type Testing class. + * @return true if class implements {@link java.util.Collection}, otherwise false. + */ + public static boolean isCollectionType(final Class type) { + return Collection.class.isAssignableFrom(type); } /** @@ -79,7 +88,7 @@ public static boolean isCollectionType(final Field field) { */ public static String createProtobufGetterName(final FieldResolver fieldResolver) { String getterName = StringUtils.createMethodName(GETTER_PREFIX, fieldResolver.getProtobufName()); - if (isCollectionType(fieldResolver.getField())) { + if (isCollectionType(fieldResolver.getProtobufType())) { return getterName + PROTOBUF_LIST_GETTER_POSTFIX; } return getterName; @@ -92,23 +101,12 @@ public static String createProtobufGetterName(final FieldResolver fieldResolver) * @return Protobuf field setter name. */ public static String createProtobufSetterName(final FieldResolver fieldResolver) { - if (isCollectionType(fieldResolver.getField())) { + if (isCollectionType(fieldResolver.getProtobufType())) { return StringUtils.createMethodName(PROTOBUF_LIST_SETTER_PREFIX, fieldResolver.getProtobufName()); } return StringUtils.createMethodName(SETTER_PREFIX, fieldResolver.getProtobufName()); } - /** - * Create protobuf builder getter name for complex domain field. - * - * @param fieldResolver Domain object field resolver. - * @return Protobuf field builder getter name. - */ - public static String createProtobufBuilderName(final FieldResolver fieldResolver) { - String getterName = StringUtils.createMethodName(GETTER_PREFIX, fieldResolver.getProtobufName()); - return getterName + PROTOBUF_NESTED_BUILDER_POSTFIX; - } - /** * Create domain field getter name. * @@ -116,7 +114,7 @@ public static String createProtobufBuilderName(final FieldResolver fieldResolver * @return Domain field getter name. */ public static String createDomainGetterName(final FieldResolver fieldResolver) { - if (fieldResolver.getField().getType() == boolean.class) { + if (fieldResolver.getDomainType() == boolean.class) { return StringUtils.createMethodName(BOOLEAN_GETTER_PREFIX, fieldResolver.getDomainName()); } return StringUtils.createMethodName(GETTER_PREFIX, fieldResolver.getDomainName()); @@ -136,11 +134,35 @@ public static String createDomainSetterName(final FieldResolver fieldResolver) { * Extract parameter type of the collection. * * @param field Field with type derived from {@link java.util.Collection}. - * @return Collection parameter. + * @return Collection generic type. */ public static Class extractCollectionType(final Field field) { - ParameterizedType stringListType = (ParameterizedType) field.getGenericType(); - return (Class) stringListType.getActualTypeArguments()[0]; + ParameterizedType genericType = (ParameterizedType) field.getGenericType(); + return (Class) genericType.getActualTypeArguments()[0]; + } + + /** + * Extract protobuf field type from type converter. + * + * @param typeConverterClass field converter type. + * @param defaultType Default protobuf field type. + * @return Protobuf field type declared in the type converter class or default type when it is unable to extract + * field type from converter. + */ + public static Class extractProtobufFieldType(final Class typeConverterClass, + final Class defaultType) { + Type[] interfaceTypes = typeConverterClass.getGenericInterfaces(); + for (Type interfaceType : interfaceTypes) { + ParameterizedType parameterizedType = (ParameterizedType) interfaceType; + if (parameterizedType.getRawType().equals(TypeConverter.class)) { + Type extractedType = parameterizedType.getActualTypeArguments()[1]; + if (extractedType instanceof ParameterizedType) { + return (Class) ((ParameterizedType) extractedType).getRawType(); + } + return Object.class.equals(extractedType) ? defaultType : (Class) extractedType; + } + } + return defaultType; } private FieldUtils() { diff --git a/src/main/java/net/badata/protobuf/converter/writer/DomainWriter.java b/src/main/java/net/badata/protobuf/converter/writer/DomainWriter.java index 332da68..49e4c2b 100644 --- a/src/main/java/net/badata/protobuf/converter/writer/DomainWriter.java +++ b/src/main/java/net/badata/protobuf/converter/writer/DomainWriter.java @@ -55,7 +55,7 @@ protected void write(final Object destination, final FieldResolver fieldResolver DefaultValue defaultValueCreator = fieldResolver.getDefaultValue(); TypeConverter typeConverter = fieldResolver.getTypeConverter(); if (nullInspector.isNull(value)) { - writeValue(destination, fieldResolver, defaultValueCreator.generateValue(fieldResolver.getField().getType())); + writeValue(destination, fieldResolver, defaultValueCreator.generateValue(fieldResolver.getDomainType())); } else { writeValue(destination, fieldResolver, typeConverter.toDomainValue(value)); } @@ -64,7 +64,7 @@ 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.createDomainSetterName(fieldResolver); try { - destinationClass.getMethod(setterName, fieldResolver.getField().getType()).invoke(destination, value); + destinationClass.getMethod(setterName, fieldResolver.getDomainType()).invoke(destination, value); } catch (IllegalAccessException e) { throw new WriteException( String.format("Access denied. '%s.%s()'", destinationClass.getName(), setterName)); diff --git a/src/test/java/net/badata/protobuf/converter/ResolverTest.java b/src/test/java/net/badata/protobuf/converter/ResolverTest.java new file mode 100644 index 0000000..5a0d592 --- /dev/null +++ b/src/test/java/net/badata/protobuf/converter/ResolverTest.java @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2017 BAData Creative Studio + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + */ + +package net.badata.protobuf.converter; + +import net.badata.protobuf.converter.domain.ResolverDomain; +import net.badata.protobuf.converter.proto.ResolverProto; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +/** + * Created by jsjem on 10.03.2017. + * + * @author jsjem + */ +public class ResolverTest { + private static final String DELIMITED_STRING = "one,two,three"; + private static final List STRING_LIST = Arrays.asList("four", "five", "six"); + + private ResolverDomain.Test testDomain; + private ResolverProto.ResolverTest testProtobuf; + + @Before + public void setUp() { + createTestDomain(); + createTestProtobuf(); + } + + private void createTestDomain() { + testDomain = new ResolverDomain.Test(); + testDomain.setCommaDelimitedStringValue(DELIMITED_STRING); + testDomain.setStringList(STRING_LIST); + } + + private void createTestProtobuf() { + testProtobuf = ResolverProto.ResolverTest.newBuilder() + .addAllStringListValue(STRING_LIST) + .setDelimitedStringValue(DELIMITED_STRING) + .build(); + } + + @Test + public void testDomainToProtobuf() { + ResolverProto.ResolverTest result = Converter.create().toProtobuf(ResolverProto.ResolverTest.class, + testDomain); + + Assert.assertNotNull(result); + Assert.assertEquals(testDomain.getCommaDelimitedStringValue(), + String.join(",", result.getStringListValueList())); + Assert.assertEquals(testDomain.getStringList(), Arrays.asList(result.getDelimitedStringValue().split(","))); + } + + + @Test + public void testProtobufToDomain() { + ResolverDomain.Test result = Converter.create().toDomain(ResolverDomain.Test.class, testProtobuf); + + Assert.assertNotNull(result); + Assert.assertEquals(testProtobuf.getStringListValueList(), + Arrays.asList(result.getCommaDelimitedStringValue().split(","))); + Assert.assertEquals(testProtobuf.getDelimitedStringValue(), String.join(",", result.getStringList())); + } +} diff --git a/src/test/java/net/badata/protobuf/converter/domain/ResolverDomain.java b/src/test/java/net/badata/protobuf/converter/domain/ResolverDomain.java new file mode 100644 index 0000000..64c8719 --- /dev/null +++ b/src/test/java/net/badata/protobuf/converter/domain/ResolverDomain.java @@ -0,0 +1,105 @@ +/* + * Copyright (C) 2017 BAData Creative Studio + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + */ + +package net.badata.protobuf.converter.domain; + +import net.badata.protobuf.converter.annotation.ProtoClass; +import net.badata.protobuf.converter.annotation.ProtoField; +import net.badata.protobuf.converter.proto.ResolverProto; +import net.badata.protobuf.converter.type.TypeConverter; + +import java.util.LinkedList; +import java.util.List; + +/** + * Created by jsjem on 10.03.2017. + * + * @author jsjem + */ +public class ResolverDomain { + + + @ProtoClass(ResolverProto.ResolverTest.class) + public static class Test { + + @ProtoField(name = "stringListValue", converter = CommaDelimitedStringListConverter.class) + private String commaDelimitedStringValue; + @ProtoField(name = "delimitedStringValue", converter = ListCommaDelimitedStringConverter.class) + private List stringList; + + public String getCommaDelimitedStringValue() { + return commaDelimitedStringValue; + } + + public void setCommaDelimitedStringValue(String commaDelimitedStringValue) { + this.commaDelimitedStringValue = commaDelimitedStringValue; + } + + public List getStringList() { + return stringList; + } + + public void setStringList(final List stringList) { + this.stringList = stringList; + } + } + + + public static class CommaDelimitedStringListConverter implements TypeConverter> { + + @Override + public String toDomainValue(Object instance) { + @SuppressWarnings("unchecked") + List stringList = (List) instance; + return String.join(",", stringList); + } + + @Override + public List toProtobufValue(Object instance) { + String stringValue = (String) instance; + String[] splitStrings = stringValue.split(","); + List stringList = new LinkedList<>(); + for (String s : splitStrings) { + stringList.add(s); + } + return stringList; + } + + } + + public static class ListCommaDelimitedStringConverter implements TypeConverter, String> { + + @Override + public List toDomainValue(Object instance) { + String stringValue = (String) instance; + String[] splitStrings = stringValue.split(","); + List stringList = new LinkedList<>(); + for (String s : splitStrings) { + stringList.add(s); + } + return stringList; + } + + @Override + public String toProtobufValue(Object instance) { + @SuppressWarnings("unchecked") + List stringList = (List) instance; + return String.join(",", stringList); + } + + } +} diff --git a/src/test/java/net/badata/protobuf/converter/proto/ResolverProto.java b/src/test/java/net/badata/protobuf/converter/proto/ResolverProto.java new file mode 100644 index 0000000..f6a0af2 --- /dev/null +++ b/src/test/java/net/badata/protobuf/converter/proto/ResolverProto.java @@ -0,0 +1,689 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: field_resolver_test.proto + +package net.badata.protobuf.converter.proto; + +public final class ResolverProto { + private ResolverProto() {} + public static void registerAllExtensions( + com.google.protobuf.ExtensionRegistry registry) { + } + public interface ResolverTestOrBuilder extends + // @@protoc_insertion_point(interface_extends:net.badata.protobuf.converter.proto.ResolverTest) + com.google.protobuf.MessageOrBuilder { + + /** + * repeated string stringListValue = 1; + */ + com.google.protobuf.ProtocolStringList + getStringListValueList(); + /** + * repeated string stringListValue = 1; + */ + int getStringListValueCount(); + /** + * repeated string stringListValue = 1; + */ + java.lang.String getStringListValue(int index); + /** + * repeated string stringListValue = 1; + */ + com.google.protobuf.ByteString + getStringListValueBytes(int index); + + /** + * optional string delimitedStringValue = 3; + */ + java.lang.String getDelimitedStringValue(); + /** + * optional string delimitedStringValue = 3; + */ + com.google.protobuf.ByteString + getDelimitedStringValueBytes(); + } + /** + * Protobuf type {@code net.badata.protobuf.converter.proto.ResolverTest} + */ + public static final class ResolverTest extends + com.google.protobuf.GeneratedMessage implements + // @@protoc_insertion_point(message_implements:net.badata.protobuf.converter.proto.ResolverTest) + ResolverTestOrBuilder { + // Use ResolverTest.newBuilder() to construct. + private ResolverTest(com.google.protobuf.GeneratedMessage.Builder builder) { + super(builder); + } + private ResolverTest() { + stringListValue_ = com.google.protobuf.LazyStringArrayList.EMPTY; + delimitedStringValue_ = ""; + } + + @java.lang.Override + public final com.google.protobuf.UnknownFieldSet + getUnknownFields() { + return com.google.protobuf.UnknownFieldSet.getDefaultInstance(); + } + private ResolverTest( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) { + this(); + int mutable_bitField0_ = 0; + try { + boolean done = false; + while (!done) { + int tag = input.readTag(); + switch (tag) { + case 0: + done = true; + break; + default: { + if (!input.skipField(tag)) { + done = true; + } + break; + } + case 10: { + String s = input.readStringRequireUtf8(); + if (!((mutable_bitField0_ & 0x00000001) == 0x00000001)) { + stringListValue_ = new com.google.protobuf.LazyStringArrayList(); + mutable_bitField0_ |= 0x00000001; + } + stringListValue_.add(s); + break; + } + case 26: { + String s = input.readStringRequireUtf8(); + + delimitedStringValue_ = s; + break; + } + } + } + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + throw new RuntimeException(e.setUnfinishedMessage(this)); + } catch (java.io.IOException e) { + throw new RuntimeException( + new com.google.protobuf.InvalidProtocolBufferException( + e.getMessage()).setUnfinishedMessage(this)); + } finally { + if (((mutable_bitField0_ & 0x00000001) == 0x00000001)) { + stringListValue_ = stringListValue_.getUnmodifiableView(); + } + makeExtensionsImmutable(); + } + } + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return net.badata.protobuf.converter.proto.ResolverProto.internal_static_net_badata_protobuf_converter_proto_ResolverTest_descriptor; + } + + protected com.google.protobuf.GeneratedMessage.FieldAccessorTable + internalGetFieldAccessorTable() { + return net.badata.protobuf.converter.proto.ResolverProto.internal_static_net_badata_protobuf_converter_proto_ResolverTest_fieldAccessorTable + .ensureFieldAccessorsInitialized( + net.badata.protobuf.converter.proto.ResolverProto.ResolverTest.class, net.badata.protobuf.converter.proto.ResolverProto.ResolverTest.Builder.class); + } + + private int bitField0_; + public static final int STRINGLISTVALUE_FIELD_NUMBER = 1; + private com.google.protobuf.LazyStringList stringListValue_; + /** + * repeated string stringListValue = 1; + */ + public com.google.protobuf.ProtocolStringList + getStringListValueList() { + return stringListValue_; + } + /** + * repeated string stringListValue = 1; + */ + public int getStringListValueCount() { + return stringListValue_.size(); + } + /** + * repeated string stringListValue = 1; + */ + public java.lang.String getStringListValue(int index) { + return stringListValue_.get(index); + } + /** + * repeated string stringListValue = 1; + */ + public com.google.protobuf.ByteString + getStringListValueBytes(int index) { + return stringListValue_.getByteString(index); + } + + public static final int DELIMITEDSTRINGVALUE_FIELD_NUMBER = 3; + private volatile java.lang.Object delimitedStringValue_; + /** + * optional string delimitedStringValue = 3; + */ + public java.lang.String getDelimitedStringValue() { + java.lang.Object ref = delimitedStringValue_; + if (ref instanceof java.lang.String) { + return (java.lang.String) ref; + } else { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + delimitedStringValue_ = s; + return s; + } + } + /** + * optional string delimitedStringValue = 3; + */ + public com.google.protobuf.ByteString + getDelimitedStringValueBytes() { + java.lang.Object ref = delimitedStringValue_; + if (ref instanceof java.lang.String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (java.lang.String) ref); + delimitedStringValue_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + + private byte memoizedIsInitialized = -1; + public final boolean isInitialized() { + byte isInitialized = memoizedIsInitialized; + if (isInitialized == 1) return true; + if (isInitialized == 0) return false; + + memoizedIsInitialized = 1; + return true; + } + + public void writeTo(com.google.protobuf.CodedOutputStream output) + throws java.io.IOException { + for (int i = 0; i < stringListValue_.size(); i++) { + com.google.protobuf.GeneratedMessage.writeString(output, 1, stringListValue_.getRaw(i)); + } + if (!getDelimitedStringValueBytes().isEmpty()) { + com.google.protobuf.GeneratedMessage.writeString(output, 3, delimitedStringValue_); + } + } + + public int getSerializedSize() { + int size = memoizedSize; + if (size != -1) return size; + + size = 0; + { + int dataSize = 0; + for (int i = 0; i < stringListValue_.size(); i++) { + dataSize += computeStringSizeNoTag(stringListValue_.getRaw(i)); + } + size += dataSize; + size += 1 * getStringListValueList().size(); + } + if (!getDelimitedStringValueBytes().isEmpty()) { + size += com.google.protobuf.GeneratedMessage.computeStringSize(3, delimitedStringValue_); + } + memoizedSize = size; + return size; + } + + private static final long serialVersionUID = 0L; + public static net.badata.protobuf.converter.proto.ResolverProto.ResolverTest parseFrom( + com.google.protobuf.ByteString data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static net.badata.protobuf.converter.proto.ResolverProto.ResolverTest parseFrom( + com.google.protobuf.ByteString data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static net.badata.protobuf.converter.proto.ResolverProto.ResolverTest parseFrom(byte[] data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static net.badata.protobuf.converter.proto.ResolverProto.ResolverTest parseFrom( + byte[] data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static net.badata.protobuf.converter.proto.ResolverProto.ResolverTest parseFrom(java.io.InputStream input) + throws java.io.IOException { + return PARSER.parseFrom(input); + } + public static net.badata.protobuf.converter.proto.ResolverProto.ResolverTest parseFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return PARSER.parseFrom(input, extensionRegistry); + } + public static net.badata.protobuf.converter.proto.ResolverProto.ResolverTest parseDelimitedFrom(java.io.InputStream input) + throws java.io.IOException { + return PARSER.parseDelimitedFrom(input); + } + public static net.badata.protobuf.converter.proto.ResolverProto.ResolverTest parseDelimitedFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return PARSER.parseDelimitedFrom(input, extensionRegistry); + } + public static net.badata.protobuf.converter.proto.ResolverProto.ResolverTest parseFrom( + com.google.protobuf.CodedInputStream input) + throws java.io.IOException { + return PARSER.parseFrom(input); + } + public static net.badata.protobuf.converter.proto.ResolverProto.ResolverTest parseFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return PARSER.parseFrom(input, extensionRegistry); + } + + public Builder newBuilderForType() { return newBuilder(); } + public static Builder newBuilder() { + return DEFAULT_INSTANCE.toBuilder(); + } + public static Builder newBuilder(net.badata.protobuf.converter.proto.ResolverProto.ResolverTest prototype) { + return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); + } + public Builder toBuilder() { + return this == DEFAULT_INSTANCE + ? new Builder() : new Builder().mergeFrom(this); + } + + @java.lang.Override + protected Builder newBuilderForType( + com.google.protobuf.GeneratedMessage.BuilderParent parent) { + Builder builder = new Builder(parent); + return builder; + } + /** + * Protobuf type {@code net.badata.protobuf.converter.proto.ResolverTest} + */ + public static final class Builder extends + com.google.protobuf.GeneratedMessage.Builder implements + // @@protoc_insertion_point(builder_implements:net.badata.protobuf.converter.proto.ResolverTest) + net.badata.protobuf.converter.proto.ResolverProto.ResolverTestOrBuilder { + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return net.badata.protobuf.converter.proto.ResolverProto.internal_static_net_badata_protobuf_converter_proto_ResolverTest_descriptor; + } + + protected com.google.protobuf.GeneratedMessage.FieldAccessorTable + internalGetFieldAccessorTable() { + return net.badata.protobuf.converter.proto.ResolverProto.internal_static_net_badata_protobuf_converter_proto_ResolverTest_fieldAccessorTable + .ensureFieldAccessorsInitialized( + net.badata.protobuf.converter.proto.ResolverProto.ResolverTest.class, net.badata.protobuf.converter.proto.ResolverProto.ResolverTest.Builder.class); + } + + // Construct using net.badata.protobuf.converter.proto.ResolverProto.ResolverTest.newBuilder() + private Builder() { + maybeForceBuilderInitialization(); + } + + private Builder( + com.google.protobuf.GeneratedMessage.BuilderParent parent) { + super(parent); + maybeForceBuilderInitialization(); + } + private void maybeForceBuilderInitialization() { + if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) { + } + } + public Builder clear() { + super.clear(); + stringListValue_ = com.google.protobuf.LazyStringArrayList.EMPTY; + bitField0_ = (bitField0_ & ~0x00000001); + delimitedStringValue_ = ""; + + return this; + } + + public com.google.protobuf.Descriptors.Descriptor + getDescriptorForType() { + return net.badata.protobuf.converter.proto.ResolverProto.internal_static_net_badata_protobuf_converter_proto_ResolverTest_descriptor; + } + + public net.badata.protobuf.converter.proto.ResolverProto.ResolverTest getDefaultInstanceForType() { + return net.badata.protobuf.converter.proto.ResolverProto.ResolverTest.getDefaultInstance(); + } + + public net.badata.protobuf.converter.proto.ResolverProto.ResolverTest build() { + net.badata.protobuf.converter.proto.ResolverProto.ResolverTest result = buildPartial(); + if (!result.isInitialized()) { + throw newUninitializedMessageException(result); + } + return result; + } + + public net.badata.protobuf.converter.proto.ResolverProto.ResolverTest buildPartial() { + net.badata.protobuf.converter.proto.ResolverProto.ResolverTest result = new net.badata.protobuf.converter.proto.ResolverProto.ResolverTest(this); + int from_bitField0_ = bitField0_; + int to_bitField0_ = 0; + if (((bitField0_ & 0x00000001) == 0x00000001)) { + stringListValue_ = stringListValue_.getUnmodifiableView(); + bitField0_ = (bitField0_ & ~0x00000001); + } + result.stringListValue_ = stringListValue_; + result.delimitedStringValue_ = delimitedStringValue_; + result.bitField0_ = to_bitField0_; + onBuilt(); + return result; + } + + public Builder mergeFrom(com.google.protobuf.Message other) { + if (other instanceof net.badata.protobuf.converter.proto.ResolverProto.ResolverTest) { + return mergeFrom((net.badata.protobuf.converter.proto.ResolverProto.ResolverTest)other); + } else { + super.mergeFrom(other); + return this; + } + } + + public Builder mergeFrom(net.badata.protobuf.converter.proto.ResolverProto.ResolverTest other) { + if (other == net.badata.protobuf.converter.proto.ResolverProto.ResolverTest.getDefaultInstance()) return this; + if (!other.stringListValue_.isEmpty()) { + if (stringListValue_.isEmpty()) { + stringListValue_ = other.stringListValue_; + bitField0_ = (bitField0_ & ~0x00000001); + } else { + ensureStringListValueIsMutable(); + stringListValue_.addAll(other.stringListValue_); + } + onChanged(); + } + if (!other.getDelimitedStringValue().isEmpty()) { + delimitedStringValue_ = other.delimitedStringValue_; + onChanged(); + } + onChanged(); + return this; + } + + public final boolean isInitialized() { + return true; + } + + public Builder mergeFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + net.badata.protobuf.converter.proto.ResolverProto.ResolverTest parsedMessage = null; + try { + parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + parsedMessage = (net.badata.protobuf.converter.proto.ResolverProto.ResolverTest) e.getUnfinishedMessage(); + throw e; + } finally { + if (parsedMessage != null) { + mergeFrom(parsedMessage); + } + } + return this; + } + private int bitField0_; + + private com.google.protobuf.LazyStringList stringListValue_ = com.google.protobuf.LazyStringArrayList.EMPTY; + private void ensureStringListValueIsMutable() { + if (!((bitField0_ & 0x00000001) == 0x00000001)) { + stringListValue_ = new com.google.protobuf.LazyStringArrayList(stringListValue_); + bitField0_ |= 0x00000001; + } + } + /** + * repeated string stringListValue = 1; + */ + public com.google.protobuf.ProtocolStringList + getStringListValueList() { + return stringListValue_.getUnmodifiableView(); + } + /** + * repeated string stringListValue = 1; + */ + public int getStringListValueCount() { + return stringListValue_.size(); + } + /** + * repeated string stringListValue = 1; + */ + public java.lang.String getStringListValue(int index) { + return stringListValue_.get(index); + } + /** + * repeated string stringListValue = 1; + */ + public com.google.protobuf.ByteString + getStringListValueBytes(int index) { + return stringListValue_.getByteString(index); + } + /** + * repeated string stringListValue = 1; + */ + public Builder setStringListValue( + int index, java.lang.String value) { + if (value == null) { + throw new NullPointerException(); + } + ensureStringListValueIsMutable(); + stringListValue_.set(index, value); + onChanged(); + return this; + } + /** + * repeated string stringListValue = 1; + */ + public Builder addStringListValue( + java.lang.String value) { + if (value == null) { + throw new NullPointerException(); + } + ensureStringListValueIsMutable(); + stringListValue_.add(value); + onChanged(); + return this; + } + /** + * repeated string stringListValue = 1; + */ + public Builder addAllStringListValue( + java.lang.Iterable values) { + ensureStringListValueIsMutable(); + com.google.protobuf.AbstractMessageLite.Builder.addAll( + values, stringListValue_); + onChanged(); + return this; + } + /** + * repeated string stringListValue = 1; + */ + public Builder clearStringListValue() { + stringListValue_ = com.google.protobuf.LazyStringArrayList.EMPTY; + bitField0_ = (bitField0_ & ~0x00000001); + onChanged(); + return this; + } + /** + * repeated string stringListValue = 1; + */ + public Builder addStringListValueBytes( + com.google.protobuf.ByteString value) { + if (value == null) { + throw new NullPointerException(); + } + checkByteStringIsUtf8(value); + ensureStringListValueIsMutable(); + stringListValue_.add(value); + onChanged(); + return this; + } + + private java.lang.Object delimitedStringValue_ = ""; + /** + * optional string delimitedStringValue = 3; + */ + public java.lang.String getDelimitedStringValue() { + java.lang.Object ref = delimitedStringValue_; + if (!(ref instanceof java.lang.String)) { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + delimitedStringValue_ = s; + return s; + } else { + return (java.lang.String) ref; + } + } + /** + * optional string delimitedStringValue = 3; + */ + public com.google.protobuf.ByteString + getDelimitedStringValueBytes() { + java.lang.Object ref = delimitedStringValue_; + if (ref instanceof String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (java.lang.String) ref); + delimitedStringValue_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + /** + * optional string delimitedStringValue = 3; + */ + public Builder setDelimitedStringValue( + java.lang.String value) { + if (value == null) { + throw new NullPointerException(); + } + + delimitedStringValue_ = value; + onChanged(); + return this; + } + /** + * optional string delimitedStringValue = 3; + */ + public Builder clearDelimitedStringValue() { + + delimitedStringValue_ = getDefaultInstance().getDelimitedStringValue(); + onChanged(); + return this; + } + /** + * optional string delimitedStringValue = 3; + */ + public Builder setDelimitedStringValueBytes( + com.google.protobuf.ByteString value) { + if (value == null) { + throw new NullPointerException(); + } + checkByteStringIsUtf8(value); + + delimitedStringValue_ = value; + onChanged(); + return this; + } + public final Builder setUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return this; + } + + public final Builder mergeUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return this; + } + + + // @@protoc_insertion_point(builder_scope:net.badata.protobuf.converter.proto.ResolverTest) + } + + // @@protoc_insertion_point(class_scope:net.badata.protobuf.converter.proto.ResolverTest) + private static final net.badata.protobuf.converter.proto.ResolverProto.ResolverTest DEFAULT_INSTANCE; + static { + DEFAULT_INSTANCE = new net.badata.protobuf.converter.proto.ResolverProto.ResolverTest(); + } + + public static net.badata.protobuf.converter.proto.ResolverProto.ResolverTest getDefaultInstance() { + return DEFAULT_INSTANCE; + } + + private static final com.google.protobuf.Parser + PARSER = new com.google.protobuf.AbstractParser() { + public ResolverTest parsePartialFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + try { + return new ResolverTest(input, extensionRegistry); + } catch (RuntimeException e) { + if (e.getCause() instanceof + com.google.protobuf.InvalidProtocolBufferException) { + throw (com.google.protobuf.InvalidProtocolBufferException) + e.getCause(); + } + throw e; + } + } + }; + + public static com.google.protobuf.Parser parser() { + return PARSER; + } + + @java.lang.Override + public com.google.protobuf.Parser getParserForType() { + return PARSER; + } + + public net.badata.protobuf.converter.proto.ResolverProto.ResolverTest getDefaultInstanceForType() { + return DEFAULT_INSTANCE; + } + + } + + private static com.google.protobuf.Descriptors.Descriptor + internal_static_net_badata_protobuf_converter_proto_ResolverTest_descriptor; + private static + com.google.protobuf.GeneratedMessage.FieldAccessorTable + internal_static_net_badata_protobuf_converter_proto_ResolverTest_fieldAccessorTable; + + public static com.google.protobuf.Descriptors.FileDescriptor + getDescriptor() { + return descriptor; + } + private static com.google.protobuf.Descriptors.FileDescriptor + descriptor; + static { + java.lang.String[] descriptorData = { + "\n\031field_resolver_test.proto\022#net.badata." + + "protobuf.converter.proto\"E\n\014ResolverTest" + + "\022\027\n\017stringListValue\030\001 \003(\t\022\034\n\024delimitedSt" + + "ringValue\030\003 \001(\tB4\n#net.badata.protobuf.c" + + "onverter.protoB\rResolverProtob\006proto3" + }; + com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner = + new com.google.protobuf.Descriptors.FileDescriptor. InternalDescriptorAssigner() { + public com.google.protobuf.ExtensionRegistry assignDescriptors( + com.google.protobuf.Descriptors.FileDescriptor root) { + descriptor = root; + return null; + } + }; + com.google.protobuf.Descriptors.FileDescriptor + .internalBuildGeneratedFileFrom(descriptorData, + new com.google.protobuf.Descriptors.FileDescriptor[] { + }, assigner); + internal_static_net_badata_protobuf_converter_proto_ResolverTest_descriptor = + getDescriptor().getMessageTypes().get(0); + internal_static_net_badata_protobuf_converter_proto_ResolverTest_fieldAccessorTable = new + com.google.protobuf.GeneratedMessage.FieldAccessorTable( + internal_static_net_badata_protobuf_converter_proto_ResolverTest_descriptor, + new java.lang.String[] { "StringListValue", "DelimitedStringValue", }); + } + + // @@protoc_insertion_point(outer_class_scope) +} diff --git a/src/test/proto/field_resolver_test.proto b/src/test/proto/field_resolver_test.proto new file mode 100644 index 0000000..33062d6 --- /dev/null +++ b/src/test/proto/field_resolver_test.proto @@ -0,0 +1,13 @@ +syntax = "proto3"; + +package net.badata.protobuf.converter.proto; + +option java_package = "net.badata.protobuf.converter.proto"; +option java_outer_classname = "ResolverProto"; + +message ResolverTest { + repeated string stringListValue = 1; + string delimitedStringValue = 3; +} + +