blob: 016fdd5d748d628c0438694e5e7527a7e375c5e3 [file] [log] [blame]
temporal40ee5512008-07-10 02:12:20 +00001// Protocol Buffers - Google's data interchange format
kenton@google.com24bf56f2008-09-24 20:31:01 +00002// Copyright 2008 Google Inc. All rights reserved.
Feng Xiaoe4288622014-10-01 16:26:23 -07003// https://developers.google.com/protocol-buffers/
temporal40ee5512008-07-10 02:12:20 +00004//
kenton@google.com24bf56f2008-09-24 20:31:01 +00005// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
temporal40ee5512008-07-10 02:12:20 +00008//
kenton@google.com24bf56f2008-09-24 20:31:01 +00009// * 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.
temporal40ee5512008-07-10 02:12:20 +000018//
kenton@google.com24bf56f2008-09-24 20:31:01 +000019// 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.
temporal40ee5512008-07-10 02:12:20 +000030
31// Author: kenton@google.com (Kenton Varda)
32// Based on original Protocol Buffers design by
33// Sanjay Ghemawat, Jeff Dean, and others.
34
35#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_H__
36#define GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_H__
37
38#include <string>
jieluo@google.com4de8f552014-07-18 00:47:59 +000039#include <map>
temporal40ee5512008-07-10 02:12:20 +000040#include <google/protobuf/compiler/java/java_field.h>
41
42namespace google {
43namespace protobuf {
jieluo@google.com4de8f552014-07-18 00:47:59 +000044 namespace compiler {
45 namespace java {
46 class Context; // context.h
47 class ClassNameResolver; // name_resolver.h
48 }
49 }
temporal40ee5512008-07-10 02:12:20 +000050 namespace io {
51 class Printer; // printer.h
52 }
53}
54
55namespace protobuf {
56namespace compiler {
57namespace java {
58
59class MessageGenerator {
60 public:
61 explicit MessageGenerator(const Descriptor* descriptor);
jieluo@google.com4de8f552014-07-18 00:47:59 +000062 virtual ~MessageGenerator();
temporal40ee5512008-07-10 02:12:20 +000063
64 // All static variables have to be declared at the top-level of the file
65 // so that we can control initialization order, which is important for
66 // DescriptorProto bootstrapping to work.
jieluo@google.com4de8f552014-07-18 00:47:59 +000067 virtual void GenerateStaticVariables(io::Printer* printer) = 0;
temporal40ee5512008-07-10 02:12:20 +000068
kenton@google.com24bf56f2008-09-24 20:31:01 +000069 // Output code which initializes the static variables generated by
70 // GenerateStaticVariables().
jieluo@google.com4de8f552014-07-18 00:47:59 +000071 virtual void GenerateStaticVariableInitializers(io::Printer* printer) = 0;
kenton@google.com24bf56f2008-09-24 20:31:01 +000072
temporal40ee5512008-07-10 02:12:20 +000073 // Generate the class itself.
jieluo@google.com4de8f552014-07-18 00:47:59 +000074 virtual void Generate(io::Printer* printer) = 0;
temporal40ee5512008-07-10 02:12:20 +000075
liujisi@google.com33165fe2010-11-02 13:14:58 +000076 // Generates the base interface that both the class and its builder implement
jieluo@google.com4de8f552014-07-18 00:47:59 +000077 virtual void GenerateInterface(io::Printer* printer) = 0;
liujisi@google.com33165fe2010-11-02 13:14:58 +000078
kenton@google.com24bf56f2008-09-24 20:31:01 +000079 // Generate code to register all contained extensions with an
80 // ExtensionRegistry.
jieluo@google.com4de8f552014-07-18 00:47:59 +000081 virtual void GenerateExtensionRegistrationCode(io::Printer* printer) = 0;
82
83 protected:
84 const Descriptor* descriptor_;
85
86 private:
87 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageGenerator);
88};
89
90class ImmutableMessageGenerator : public MessageGenerator {
91 public:
92 explicit ImmutableMessageGenerator(const Descriptor* descriptor,
93 Context* context);
94 virtual ~ImmutableMessageGenerator();
95
96 virtual void Generate(io::Printer* printer);
97 virtual void GenerateInterface(io::Printer* printer);
98 virtual void GenerateExtensionRegistrationCode(io::Printer* printer);
99 virtual void GenerateStaticVariables(io::Printer* printer);
100 virtual void GenerateStaticVariableInitializers(io::Printer* printer);
kenton@google.com24bf56f2008-09-24 20:31:01 +0000101
temporal40ee5512008-07-10 02:12:20 +0000102 private:
liujisi@google.com33165fe2010-11-02 13:14:58 +0000103 enum UseMemoization {
104 MEMOIZE,
105 DONT_MEMOIZE
106 };
107
jieluo@google.com4de8f552014-07-18 00:47:59 +0000108 void GenerateFieldAccessorTable(io::Printer* printer);
109 void GenerateFieldAccessorTableInitializer(io::Printer* printer);
110
temporal40ee5512008-07-10 02:12:20 +0000111 void GenerateMessageSerializationMethods(io::Printer* printer);
112 void GenerateParseFromMethods(io::Printer* printer);
113 void GenerateSerializeOneField(io::Printer* printer,
114 const FieldDescriptor* field);
115 void GenerateSerializeOneExtensionRange(
116 io::Printer* printer, const Descriptor::ExtensionRange* range);
117
118 void GenerateBuilder(io::Printer* printer);
119 void GenerateCommonBuilderMethods(io::Printer* printer);
Jisi Liu885b6122015-02-28 14:51:22 -0800120 void GenerateDescriptorMethods(io::Printer* printer, bool is_builder);
Feng Xiao99aa0f92014-11-20 16:18:53 -0800121 void GenerateBuilderParsingMethods(io::Printer* printer);
liujisi@google.com33165fe2010-11-02 13:14:58 +0000122 void GenerateIsInitialized(io::Printer* printer,
123 UseMemoization useMemoization);
Feng Xiao99aa0f92014-11-20 16:18:53 -0800124 void GenerateInitializers(io::Printer* printer);
liujisi@google.com33165fe2010-11-02 13:14:58 +0000125 void GenerateEqualsAndHashCode(io::Printer* printer);
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000126 void GenerateParser(io::Printer* printer);
127 void GenerateParsingConstructor(io::Printer* printer);
128
jieluo@google.com4de8f552014-07-18 00:47:59 +0000129 Context* context_;
130 ClassNameResolver* name_resolver_;
131 FieldGeneratorMap<ImmutableFieldGenerator> field_generators_;
temporal40ee5512008-07-10 02:12:20 +0000132
jieluo@google.com4de8f552014-07-18 00:47:59 +0000133 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableMessageGenerator);
temporal40ee5512008-07-10 02:12:20 +0000134};
135
136} // namespace java
137} // namespace compiler
138} // namespace protobuf
139
140} // namespace google
141#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_H__