aboutsummaryrefslogtreecommitdiffstats
path: root/java
diff options
context:
space:
mode:
authorAndrew Flynn <flynn@google.com>2013-12-04 13:10:07 -0800
committerAndrew Flynn <flynn@google.com>2013-12-09 11:40:03 -0800
commit102cfe65b9b9e2b458c3d0ec69d47e8fc40dc726 (patch)
tree196b5f09c23bb1dddc242ef002106b78afdaff1a /java
parentc4c4a562672068fb89ea4e65408dd53de2ed8f1f (diff)
downloadexternal_protobuf-102cfe65b9b9e2b458c3d0ec69d47e8fc40dc726.zip
external_protobuf-102cfe65b9b9e2b458c3d0ec69d47e8fc40dc726.tar.gz
external_protobuf-102cfe65b9b9e2b458c3d0ec69d47e8fc40dc726.tar.bz2
Nano: don't generate accessor methods for nested methods
For nested message objects, don't generate accessor methods because they have a default value that is not a valid value (null), so there is no reason to have get/set/has/clear methods for them. Clients and protos (while serializing) can check against the invalid value to see if it's been set. Change-Id: Ic63400889581271b8cbcd9c45c84519d4921fd4b
Diffstat (limited to 'java')
-rw-r--r--java/README.txt6
-rw-r--r--java/src/test/java/com/google/protobuf/NanoTest.java60
2 files changed, 47 insertions, 19 deletions
diff --git a/java/README.txt b/java/README.txt
index 976ec84..ced78d2 100644
--- a/java/README.txt
+++ b/java/README.txt
@@ -507,9 +507,9 @@ optional_field_style={default,accessors,reftypes} (default: default)
useful when you need to serialize a field with the default value, or check
if a field has been explicitly set to its default value from the wire.
- In the 'accessors' style, required fields are still translated to one
- public mutable Java field each, and repeated fields are still translated
- to arrays. No accessors are generated for them.
+ In the 'accessors' style, required and nested message fields are still
+ translated to one public mutable Java field each, repeated fields are still
+ translated to arrays. No accessors are generated for them.
IMPORTANT: When using the 'accessors' style, ProGuard should always
be enabled with optimization (don't use -dontoptimize) and allowing
diff --git a/java/src/test/java/com/google/protobuf/NanoTest.java b/java/src/test/java/com/google/protobuf/NanoTest.java
index 0e1c9bc..4b8d63d 100644
--- a/java/src/test/java/com/google/protobuf/NanoTest.java
+++ b/java/src/test/java/com/google/protobuf/NanoTest.java
@@ -2271,9 +2271,10 @@ public class NanoTest extends TestCase {
public void testNanoWithAccessorsBasic() throws Exception {
TestNanoAccessors msg = new TestNanoAccessors();
- // Makes sure required and repeated fields are still public fields
+ // Makes sure required, repeated, and message fields are still public
msg.id = 3;
msg.repeatedBytes = new byte[2][3];
+ msg.optionalNestedMessage = null;
// Test accessors
assertEquals(0, msg.getOptionalInt32());
@@ -2295,10 +2296,6 @@ public class NanoTest extends TestCase {
msg.setOptionalString(null);
fail();
} catch (NullPointerException expected) {}
- try {
- msg.setOptionalNestedMessage(null);
- fail();
- } catch (NullPointerException expected) {}
// Test has bit on bytes field with defaults and clear() re-clones the default array
assertFalse(msg.hasDefaultBytes());
@@ -2352,8 +2349,8 @@ public class NanoTest extends TestCase {
assertFalse(msg.hasDefaultBytes());
assertFalse(msg.hasDefaultFloatNan());
assertFalse(msg.hasDefaultNestedEnum());
- msg.setOptionalNestedMessage(new TestNanoAccessors.NestedMessage());
- msg.getOptionalNestedMessage().setBb(2);
+ msg.optionalNestedMessage = new TestNanoAccessors.NestedMessage();
+ msg.optionalNestedMessage.setBb(2);
msg.setOptionalNestedEnum(TestNanoAccessors.BAZ);
msg.setDefaultInt32(msg.getDefaultInt32());
}
@@ -2366,8 +2363,8 @@ public class NanoTest extends TestCase {
// Has fields true upon parse.
TestNanoAccessors newMsg = TestNanoAccessors.parseFrom(result);
- assertEquals(2, newMsg.getOptionalNestedMessage().getBb());
- assertTrue(newMsg.getOptionalNestedMessage().hasBb());
+ assertEquals(2, newMsg.optionalNestedMessage.getBb());
+ assertTrue(newMsg.optionalNestedMessage.hasBb());
assertEquals(TestNanoAccessors.BAZ, newMsg.getOptionalNestedEnum());
assertTrue(newMsg.hasOptionalNestedEnum());
@@ -2376,6 +2373,38 @@ public class NanoTest extends TestCase {
assertEquals(41, newMsg.getDefaultInt32());
}
+ public void testNanoWithAccessorsPublicFieldTypes() throws Exception {
+ TestNanoAccessors msg = new TestNanoAccessors();
+ assertNull(msg.optionalNestedMessage);
+ assertEquals(0, msg.id);
+ assertEquals(0, msg.repeatedNestedEnum.length);
+
+ TestNanoAccessors newMsg = TestNanoAccessors.parseFrom(MessageNano.toByteArray(msg));
+ assertNull(newMsg.optionalNestedMessage);
+ assertEquals(0, newMsg.id);
+ assertEquals(0, newMsg.repeatedNestedEnum.length);
+
+ TestNanoAccessors.NestedMessage nestedMessage = new TestNanoAccessors.NestedMessage();
+ nestedMessage.setBb(5);
+ newMsg.optionalNestedMessage = nestedMessage;
+ newMsg.id = -1;
+ newMsg.repeatedNestedEnum = new int[] { TestAllTypesNano.FOO };
+
+ TestNanoAccessors newMsg2 = TestNanoAccessors.parseFrom(MessageNano.toByteArray(newMsg));
+ assertEquals(nestedMessage.getBb(), newMsg2.optionalNestedMessage.getBb());
+ assertEquals(-1, newMsg2.id);
+ assertEquals(TestAllTypesNano.FOO, newMsg2.repeatedNestedEnum[0]);
+
+ newMsg2.optionalNestedMessage = null;
+ newMsg2.id = 0;
+ newMsg2.repeatedNestedEnum = null;
+
+ TestNanoAccessors newMsg3 = TestNanoAccessors.parseFrom(MessageNano.toByteArray(newMsg2));
+ assertNull(newMsg3.optionalNestedMessage);
+ assertEquals(0, newMsg3.id);
+ assertEquals(0, newMsg3.repeatedNestedEnum.length);
+ }
+
public void testNanoWithAccessorsSerialize() throws Exception {
TestNanoAccessors msg = new TestNanoAccessors();
msg.setOptionalInt32(msg.getOptionalInt32());
@@ -2383,7 +2412,7 @@ public class NanoTest extends TestCase {
msg.setOptionalBytes(msg.getOptionalBytes());
TestNanoAccessors.NestedMessage nestedMessage = new TestNanoAccessors.NestedMessage();
nestedMessage.setBb(nestedMessage.getBb());
- msg.setOptionalNestedMessage(nestedMessage);
+ msg.optionalNestedMessage = nestedMessage;
msg.setOptionalNestedEnum(msg.getOptionalNestedEnum());
msg.setDefaultInt32(msg.getDefaultInt32());
msg.setDefaultString(msg.getDefaultString());
@@ -2400,8 +2429,7 @@ public class NanoTest extends TestCase {
assertTrue(newMsg.hasOptionalInt32());
assertTrue(newMsg.hasOptionalString());
assertTrue(newMsg.hasOptionalBytes());
- assertTrue(newMsg.hasOptionalNestedMessage());
- assertTrue(newMsg.getOptionalNestedMessage().hasBb());
+ assertTrue(newMsg.optionalNestedMessage.hasBb());
assertTrue(newMsg.hasOptionalNestedEnum());
assertTrue(newMsg.hasDefaultInt32());
assertTrue(newMsg.hasDefaultString());
@@ -2411,7 +2439,7 @@ public class NanoTest extends TestCase {
assertEquals(0, newMsg.getOptionalInt32());
assertEquals(0, newMsg.getOptionalString().length());
assertEquals(0, newMsg.getOptionalBytes().length);
- assertEquals(0, newMsg.getOptionalNestedMessage().getBb());
+ assertEquals(0, newMsg.optionalNestedMessage.getBb());
assertEquals(TestNanoAccessors.FOO, newMsg.getOptionalNestedEnum());
assertEquals(41, newMsg.getDefaultInt32());
assertEquals("hello", newMsg.getDefaultString());
@@ -2856,15 +2884,15 @@ public class NanoTest extends TestCase {
.setOptionalInt32(5)
.setOptionalString("Hello")
.setOptionalBytes(new byte[] {1, 2, 3})
- .setOptionalNestedMessage(new TestNanoAccessors.NestedMessage().setBb(27))
.setOptionalNestedEnum(TestNanoAccessors.BAR)
.setDefaultFloatNan(1.0f);
+ message.optionalNestedMessage = new TestNanoAccessors.NestedMessage().setBb(27);
message.repeatedInt32 = new int[] { 5, 6, 7, 8 };
message.repeatedString = new String[] { "One", "Two" };
message.repeatedBytes = new byte[][] { { 2, 7 }, { 2, 7 } };
message.repeatedNestedMessage = new TestNanoAccessors.NestedMessage[] {
- message.getOptionalNestedMessage(),
- message.getOptionalNestedMessage()
+ message.optionalNestedMessage,
+ message.optionalNestedMessage
};
message.repeatedNestedEnum = new int[] {
TestAllTypesNano.BAR,