diff options
author | Max Cai <maxtroy@google.com> | 2013-07-29 17:20:50 +0100 |
---|---|---|
committer | Max Cai <maxtroy@google.com> | 2013-08-05 21:54:58 +0100 |
commit | 06eed37ec6a0bc912c0f342ee92449069846cea1 (patch) | |
tree | af741f5657862c073731290fddc23be1860e8f2e /java/src | |
parent | 064b60c65938dbdb45b1377975cee8a104e6d62b (diff) | |
download | protobuf-06eed37ec6a0bc912c0f342ee92449069846cea1.tar.gz protobuf-06eed37ec6a0bc912c0f342ee92449069846cea1.tar.bz2 protobuf-06eed37ec6a0bc912c0f342ee92449069846cea1.zip |
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
Diffstat (limited to 'java/src')
-rw-r--r-- | java/src/test/java/com/google/protobuf/NanoTest.java | 45 |
1 files changed, 43 insertions, 2 deletions
diff --git a/java/src/test/java/com/google/protobuf/NanoTest.java b/java/src/test/java/com/google/protobuf/NanoTest.java index 80f091bf..b1251f84 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 |