From 624c448fbef20a1a2fad2289f622b468c25763d1 Mon Sep 17 00:00:00 2001 From: Max Cai Date: Mon, 29 Jul 2013 17:20:50 +0100 Subject: Fix outer classname for javamicro/javanano. - File class name is defined as the java_outer_classname option value or the file name ToCamelCase; never the single message's ClassName. - File-scope enums are translated to constants in the file class, regardless of java_multiple_files. - If java_multiple_files=true, and file's class name equals a message's class name, no error. This is done by detecting that the outer class is not needed and skipping the outer class codegen and clash checks. Note: there is a disparity between java[lite] and the previous java{micr|nan}o: when generating code for a single-message proto, the outer class is omitted by java{micr|nan}o if the file does not have java_outer_classname. This change makes java{micr|nan}o align with java[lite] codegen and create the outer class, but will print some info to warn of potential change of code. - Also fixed the "is_own_file" detection and made all parseX() methods static. Previously, all messages in a java_multiple_files=true file are (incorrectly) considered to be in their own files, including nested messages, causing them to become inner classes (instance- bound) and forcing the parseX() methods to lose the static modifier. - This change supersedes c/60164 and c/60086, which causes javanano to put enum values into enum shell classes if java_multiple_files=true. We now always use the parent class to host the enum values. A future change will add a command line option to provide more flexibility. - Elaborated in java/README.txt. Change-Id: I684932f90e0a028ef37c662b221def5ffa202439 --- .../test/java/com/google/protobuf/MicroTest.java | 46 ++++++++++++++++++++-- .../test/java/com/google/protobuf/NanoTest.java | 45 ++++++++++++++++++++- 2 files changed, 86 insertions(+), 5 deletions(-) (limited to 'java/src') diff --git a/java/src/test/java/com/google/protobuf/MicroTest.java b/java/src/test/java/com/google/protobuf/MicroTest.java index 03005a7..1e43f07 100644 --- a/java/src/test/java/com/google/protobuf/MicroTest.java +++ b/java/src/test/java/com/google/protobuf/MicroTest.java @@ -32,14 +32,19 @@ package com.google.protobuf; import com.google.protobuf.micro.ByteStringMicro; import com.google.protobuf.micro.CodedInputStreamMicro; +import com.google.protobuf.micro.FileScopeEnumRefMicro; +import com.google.protobuf.micro.MessageScopeEnumRefMicro; import com.google.protobuf.micro.MicroOuterClass; import com.google.protobuf.micro.MicroOuterClass.TestAllTypesMicro; import com.google.protobuf.micro.MultipleImportingNonMultipleMicro1; import com.google.protobuf.micro.MultipleImportingNonMultipleMicro2; -import com.google.protobuf.micro.RecursiveMessageMicro; -import com.google.protobuf.micro.SimpleMessageMicro; -import com.google.protobuf.micro.StringUtf8; +import com.google.protobuf.micro.MultipleNameClashMicro; import com.google.protobuf.micro.UnittestImportMicro; +import com.google.protobuf.micro.UnittestMultipleMicro; +import com.google.protobuf.micro.UnittestRecursiveMicro.RecursiveMessageMicro; +import com.google.protobuf.micro.UnittestSimpleMicro.SimpleMessageMicro; +import com.google.protobuf.micro.UnittestSingleMicro.SingleMessageMicro; +import com.google.protobuf.micro.UnittestStringutf8Micro.StringUtf8; import junit.framework.TestCase; @@ -2104,6 +2109,41 @@ public class MicroTest extends TestCase { } /** + * Tests that code generation correctly wraps a single message into its outer + * class. The class {@code SingleMessageMicro} is imported from the outer + * class {@code UnittestSingleMicro}, whose name is implicit. Any error would + * cause this method to fail compilation. + */ + public void testMicroSingle() throws Exception { + SingleMessageMicro msg = new SingleMessageMicro(); + } + + /** + * Tests that code generation correctly skips generating the outer class if + * unnecessary, letting a file-scope entity have the same name. The class + * {@code MultipleNameClashMicro} shares the same name with the file's outer + * class defined explicitly, but the file contains no other entities and has + * java_multiple_files set. Any error would cause this method to fail + * compilation. + */ + public void testMicroMultipleNameClash() throws Exception { + MultipleNameClashMicro msg = new MultipleNameClashMicro(); + msg.setField(0); + } + + /** + * Tests that code generation correctly handles enums in different scopes in + * a source file with the option java_multiple_files set to true. Any error + * would cause this method to fail compilation. + */ + public void testMicroMultipleEnumScoping() throws Exception { + FileScopeEnumRefMicro msg1 = new FileScopeEnumRefMicro(); + msg1.setEnumField(UnittestMultipleMicro.ONE); + MessageScopeEnumRefMicro msg2 = new MessageScopeEnumRefMicro(); + msg2.setEnumField(MessageScopeEnumRefMicro.TWO); + } + + /** * Tests that code generation with mixed values of the java_multiple_files * options between the main source file and the imported source files would * generate correct references. Any error would cause this method to fail diff --git a/java/src/test/java/com/google/protobuf/NanoTest.java b/java/src/test/java/com/google/protobuf/NanoTest.java index 80f091b..b1251f8 100644 --- a/java/src/test/java/com/google/protobuf/NanoTest.java +++ b/java/src/test/java/com/google/protobuf/NanoTest.java @@ -33,16 +33,22 @@ package com.google.protobuf; import com.google.protobuf.nano.CodedInputByteBufferNano; import com.google.protobuf.nano.Extensions; import com.google.protobuf.nano.Extensions.AnotherMessage; +import com.google.protobuf.nano.FileScopeEnumRefNano; import com.google.protobuf.nano.InternalNano; import com.google.protobuf.nano.MessageNano; +import com.google.protobuf.nano.MessageScopeEnumRefNano; import com.google.protobuf.nano.MultipleImportingNonMultipleNano1; import com.google.protobuf.nano.MultipleImportingNonMultipleNano2; +import com.google.protobuf.nano.MultipleNameClashNano; import com.google.protobuf.nano.NanoHasOuterClass.TestAllTypesNanoHas; import com.google.protobuf.nano.NanoOuterClass; import com.google.protobuf.nano.NanoOuterClass.TestAllTypesNano; -import com.google.protobuf.nano.RecursiveMessageNano; -import com.google.protobuf.nano.SimpleMessageNano; import com.google.protobuf.nano.UnittestImportNano; +import com.google.protobuf.nano.UnittestMultipleNano; +import com.google.protobuf.nano.UnittestRecursiveNano.RecursiveMessageNano; +import com.google.protobuf.nano.UnittestSimpleNano.SimpleMessageNano; +import com.google.protobuf.nano.UnittestSingleNano.SingleMessageNano; +import com.google.protobuf.nano.UnittestStringutf8Nano.StringUtf8; import junit.framework.TestCase; @@ -2043,6 +2049,41 @@ public class NanoTest extends TestCase { } /** + * Tests that code generation correctly wraps a single message into its outer + * class. The class {@code SingleMessageNano} is imported from the outer + * class {@code UnittestSingleNano}, whose name is implicit. Any error would + * cause this method to fail compilation. + */ + public void testNanoSingle() throws Exception { + SingleMessageNano msg = new SingleMessageNano(); + } + + /** + * Tests that code generation correctly skips generating the outer class if + * unnecessary, letting a file-scope entity have the same name. The class + * {@code MultipleNameClashNano} shares the same name with the file's outer + * class defined explicitly, but the file contains no other entities and has + * java_multiple_files set. Any error would cause this method to fail + * compilation. + */ + public void testNanoMultipleNameClash() throws Exception { + MultipleNameClashNano msg = new MultipleNameClashNano(); + msg.field = 0; + } + + /** + * Tests that code generation correctly handles enums in different scopes in + * a source file with the option java_multiple_files set to true. Any error + * would cause this method to fail compilation. + */ + public void testNanoMultipleEnumScoping() throws Exception { + FileScopeEnumRefNano msg1 = new FileScopeEnumRefNano(); + msg1.enumField = UnittestMultipleNano.ONE; + MessageScopeEnumRefNano msg2 = new MessageScopeEnumRefNano(); + msg2.enumField = MessageScopeEnumRefNano.TWO; + } + + /** * Tests that code generation with mixed values of the java_multiple_files * options between the main source file and the imported source files would * generate correct references. Any error would cause this method to fail -- cgit v1.1