blob: be5bfb07546e908930a2907346c8a1c901ed4fc4 [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
Daniel Martin0f1393d2015-04-09 12:20:55 -040070 // GenerateStaticVariables(). Returns an estimate of bytecode size.
Daniel Martine2416ca2014-11-25 10:37:57 -050071 virtual int 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);
Daniel Martin0f1393d2015-04-09 12:20:55 -0400100
101 // Returns an estimate of the number of bytes the printed code will compile to
Daniel Martine2416ca2014-11-25 10:37:57 -0500102 virtual int GenerateStaticVariableInitializers(io::Printer* printer);
kenton@google.com24bf56f2008-09-24 20:31:01 +0000103
temporal40ee5512008-07-10 02:12:20 +0000104 private:
liujisi@google.com33165fe2010-11-02 13:14:58 +0000105
jieluo@google.com4de8f552014-07-18 00:47:59 +0000106 void GenerateFieldAccessorTable(io::Printer* printer);
Daniel Martin0f1393d2015-04-09 12:20:55 -0400107
108 // Returns an estimate of the number of bytes the printed code will compile to
Daniel Martine2416ca2014-11-25 10:37:57 -0500109 int GenerateFieldAccessorTableInitializer(io::Printer* printer);
jieluo@google.com4de8f552014-07-18 00:47:59 +0000110
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);
Bo Yang5db21732015-05-21 14:28:59 -0700119 void GenerateIsInitialized(io::Printer* printer);
120 void GenerateDescriptorMethods(io::Printer* printer);
Feng Xiao99aa0f92014-11-20 16:18:53 -0800121 void GenerateInitializers(io::Printer* printer);
liujisi@google.com33165fe2010-11-02 13:14:58 +0000122 void GenerateEqualsAndHashCode(io::Printer* printer);
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000123 void GenerateParser(io::Printer* printer);
124 void GenerateParsingConstructor(io::Printer* printer);
Feng Xiaoeee38b02015-08-22 18:25:48 -0700125 void GenerateAnyMethods(io::Printer* printer);
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000126
jieluo@google.com4de8f552014-07-18 00:47:59 +0000127 Context* context_;
128 ClassNameResolver* name_resolver_;
129 FieldGeneratorMap<ImmutableFieldGenerator> field_generators_;
temporal40ee5512008-07-10 02:12:20 +0000130
jieluo@google.com4de8f552014-07-18 00:47:59 +0000131 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableMessageGenerator);
temporal40ee5512008-07-10 02:12:20 +0000132};
133
134} // namespace java
135} // namespace compiler
136} // namespace protobuf
137
138} // namespace google
139#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_H__