1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // http://code.google.com/p/protobuf/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 // Author: kenton@google.com (Kenton Varda)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
34
35 #include <google/protobuf/compiler/java/java_field.h>
36 #include <google/protobuf/compiler/java/java_helpers.h>
37 #include <google/protobuf/compiler/java/java_primitive_field.h>
38 #include <google/protobuf/compiler/java/java_enum_field.h>
39 #include <google/protobuf/compiler/java/java_message_field.h>
40 #include <google/protobuf/stubs/common.h>
41
42 namespace google {
43 namespace protobuf {
44 namespace compiler {
45 namespace java {
46
~FieldGenerator()47 FieldGenerator::~FieldGenerator() {}
48
GenerateParsingCodeFromPacked(io::Printer * printer) const49 void FieldGenerator::GenerateParsingCodeFromPacked(io::Printer* printer) const {
50 // Reaching here indicates a bug. Cases are:
51 // - This FieldGenerator should support packing, but this method should be
52 // overridden.
53 // - This FieldGenerator doesn't support packing, and this method should
54 // never have been called.
55 GOOGLE_LOG(FATAL) << "GenerateParsingCodeFromPacked() "
56 << "called on field generator that does not support packing.";
57 }
58
FieldGeneratorMap(const Descriptor * descriptor)59 FieldGeneratorMap::FieldGeneratorMap(const Descriptor* descriptor)
60 : descriptor_(descriptor),
61 field_generators_(
62 new scoped_ptr<FieldGenerator>[descriptor->field_count()]),
63 extension_generators_(
64 new scoped_ptr<FieldGenerator>[descriptor->extension_count()]) {
65
66 // Construct all the FieldGenerators.
67 for (int i = 0; i < descriptor->field_count(); i++) {
68 field_generators_[i].reset(MakeGenerator(descriptor->field(i)));
69 }
70 for (int i = 0; i < descriptor->extension_count(); i++) {
71 extension_generators_[i].reset(MakeGenerator(descriptor->extension(i)));
72 }
73 }
74
MakeGenerator(const FieldDescriptor * field)75 FieldGenerator* FieldGeneratorMap::MakeGenerator(const FieldDescriptor* field) {
76 if (field->is_repeated()) {
77 switch (GetJavaType(field)) {
78 case JAVATYPE_MESSAGE:
79 return new RepeatedMessageFieldGenerator(field);
80 case JAVATYPE_ENUM:
81 return new RepeatedEnumFieldGenerator(field);
82 default:
83 return new RepeatedPrimitiveFieldGenerator(field);
84 }
85 } else {
86 switch (GetJavaType(field)) {
87 case JAVATYPE_MESSAGE:
88 return new MessageFieldGenerator(field);
89 case JAVATYPE_ENUM:
90 return new EnumFieldGenerator(field);
91 default:
92 return new PrimitiveFieldGenerator(field);
93 }
94 }
95 }
96
~FieldGeneratorMap()97 FieldGeneratorMap::~FieldGeneratorMap() {}
98
get(const FieldDescriptor * field) const99 const FieldGenerator& FieldGeneratorMap::get(
100 const FieldDescriptor* field) const {
101 GOOGLE_CHECK_EQ(field->containing_type(), descriptor_);
102 return *field_generators_[field->index()];
103 }
104
get_extension(int index) const105 const FieldGenerator& FieldGeneratorMap::get_extension(int index) const {
106 return *extension_generators_[index];
107 }
108
109 } // namespace java
110 } // namespace compiler
111 } // namespace protobuf
112 } // namespace google
113