summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBryan Duxbury <bryanduxbury@apache.org>2010-09-20 17:49:09 +0000
committerBryan Duxbury <bryanduxbury@apache.org>2010-09-20 17:49:09 +0000
commit1237dcb099a068f83f31cc51df428f5c828d0511 (patch)
treed79af03e648901967d1df27703c0c00e2e85c5f7
parentf38b2f10fb83284c9a449e9aec82fc2a10cba81e (diff)
downloadthrift-1237dcb099a068f83f31cc51df428f5c828d0511.tar.gz
THRIFT-880. javame: JavaME code generator and runtime library
This patch adds a new generator and library that produces code suitable for use in JavaME environments. Patch: David Engberg git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@999022 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--compiler/cpp/Makefile.am3
-rw-r--r--compiler/cpp/src/generate/t_javame_generator.cc3391
-rw-r--r--configure.ac2
-rw-r--r--lib/javame/src/org/apache/thrift/TApplicationException.java130
-rw-r--r--lib/javame/src/org/apache/thrift/TBase.java46
-rw-r--r--lib/javame/src/org/apache/thrift/TBaseHelper.java209
-rw-r--r--lib/javame/src/org/apache/thrift/TByteArrayOutputStream.java41
-rw-r--r--lib/javame/src/org/apache/thrift/TDeserializer.java95
-rw-r--r--lib/javame/src/org/apache/thrift/TEnum.java24
-rw-r--r--lib/javame/src/org/apache/thrift/TException.java45
-rw-r--r--lib/javame/src/org/apache/thrift/TFieldRequirementType.java48
-rw-r--r--lib/javame/src/org/apache/thrift/TProcessor.java32
-rw-r--r--lib/javame/src/org/apache/thrift/TProcessorFactory.java39
-rw-r--r--lib/javame/src/org/apache/thrift/TSerializer.java110
-rw-r--r--lib/javame/src/org/apache/thrift/TServiceClient.java39
-rw-r--r--lib/javame/src/org/apache/thrift/meta_data/FieldMetaData.java88
-rw-r--r--lib/javame/src/org/apache/thrift/meta_data/FieldValueMetaData.java61
-rw-r--r--lib/javame/src/org/apache/thrift/meta_data/ListMetaData.java49
-rw-r--r--lib/javame/src/org/apache/thrift/meta_data/MapMetaData.java51
-rw-r--r--lib/javame/src/org/apache/thrift/meta_data/SetMetaData.java47
-rw-r--r--lib/javame/src/org/apache/thrift/meta_data/StructMetaData.java51
-rw-r--r--lib/javame/src/org/apache/thrift/protocol/TBase64Utils.java128
-rw-r--r--lib/javame/src/org/apache/thrift/protocol/TBinaryProtocol.java350
-rw-r--r--lib/javame/src/org/apache/thrift/protocol/TField.java38
-rw-r--r--lib/javame/src/org/apache/thrift/protocol/TList.java36
-rw-r--r--lib/javame/src/org/apache/thrift/protocol/TMap.java38
-rw-r--r--lib/javame/src/org/apache/thrift/protocol/TMessage.java38
-rw-r--r--lib/javame/src/org/apache/thrift/protocol/TMessageType.java30
-rw-r--r--lib/javame/src/org/apache/thrift/protocol/TProtocol.java169
-rw-r--r--lib/javame/src/org/apache/thrift/protocol/TProtocolException.java81
-rw-r--r--lib/javame/src/org/apache/thrift/protocol/TProtocolFactory.java30
-rw-r--r--lib/javame/src/org/apache/thrift/protocol/TProtocolUtil.java158
-rw-r--r--lib/javame/src/org/apache/thrift/protocol/TSet.java36
-rw-r--r--lib/javame/src/org/apache/thrift/protocol/TStruct.java34
-rw-r--r--lib/javame/src/org/apache/thrift/protocol/TType.java40
-rw-r--r--lib/javame/src/org/apache/thrift/transport/TFramedTransport.java122
-rw-r--r--lib/javame/src/org/apache/thrift/transport/THttpClient.java167
-rw-r--r--lib/javame/src/org/apache/thrift/transport/TIOStreamTransport.java153
-rw-r--r--lib/javame/src/org/apache/thrift/transport/TTransport.java121
-rw-r--r--lib/javame/src/org/apache/thrift/transport/TTransportException.java80
-rw-r--r--lib/javame/src/org/apache/thrift/transport/TTransportFactory.java41
41 files changed, 6491 insertions, 0 deletions
diff --git a/compiler/cpp/Makefile.am b/compiler/cpp/Makefile.am
index 6d0d409dc..24687dc43 100644
--- a/compiler/cpp/Makefile.am
+++ b/compiler/cpp/Makefile.am
@@ -107,6 +107,9 @@ endif
if THRIFT_GEN_js
thrift_SOURCES += src/generate/t_js_generator.cc
endif
+if THRIFT_GEN_javame
+thrift_SOURCES += src/generate/t_javame_generator.cc
+endif
thrift_CXXFLAGS = -Wall -I$(srcdir)/src
thrift_LDFLAGS = -Wall
diff --git a/compiler/cpp/src/generate/t_javame_generator.cc b/compiler/cpp/src/generate/t_javame_generator.cc
new file mode 100644
index 000000000..1bd720a0c
--- /dev/null
+++ b/compiler/cpp/src/generate/t_javame_generator.cc
@@ -0,0 +1,3391 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <sstream>
+#include <string>
+#include <fstream>
+#include <iostream>
+#include <vector>
+#include <cctype>
+
+#include <sys/stat.h>
+#include <stdexcept>
+
+#include "platform.h"
+#include "t_oop_generator.h"
+using namespace std;
+
+
+/**
+ * Java code generator.
+ *
+ */
+class t_javame_generator : public t_oop_generator {
+ public:
+ t_javame_generator(
+ t_program* program,
+ const std::map<std::string, std::string>& parsed_options,
+ const std::string& option_string)
+ : t_oop_generator(program)
+ {
+ std::map<std::string, std::string>::const_iterator iter;
+ out_dir_base_ = "gen-javame";
+ }
+
+ /**
+ * Init and close methods
+ */
+
+ void init_generator();
+ void close_generator();
+
+ void generate_consts(std::vector<t_const*> consts);
+
+ /**
+ * Program-level generation functions
+ */
+
+ void generate_typedef (t_typedef* ttypedef);
+ void generate_enum (t_enum* tenum);
+ void generate_struct (t_struct* tstruct);
+ void generate_union (t_struct* tunion);
+ void generate_xception(t_struct* txception);
+ void generate_service (t_service* tservice);
+
+ void print_const_value(std::ofstream& out, std::string name, t_type* type, t_const_value* value, bool in_static, bool defval=false);
+ std::string render_const_value(std::ofstream& out, std::string name, t_type* type, t_const_value* value);
+
+ /**
+ * Service-level generation functions
+ */
+
+ void generate_java_struct(t_struct* tstruct, bool is_exception);
+
+ void generate_java_struct_definition(std::ofstream& out, t_struct* tstruct, bool is_xception=false, bool in_class=false, bool is_result=false);
+ void generate_java_struct_equality(std::ofstream& out, t_struct* tstruct);
+ void generate_java_struct_compare_to(std::ofstream& out, t_struct* tstruct);
+ void generate_java_struct_reader(std::ofstream& out, t_struct* tstruct);
+ void generate_java_validator(std::ofstream& out, t_struct* tstruct);
+ void generate_java_struct_result_writer(std::ofstream& out, t_struct* tstruct);
+ void generate_java_struct_writer(std::ofstream& out, t_struct* tstruct);
+ void generate_java_struct_tostring(std::ofstream& out, t_struct* tstruct);
+ void generate_java_struct_clear(std::ofstream& out, t_struct* tstruct);
+ void generate_field_value_meta_data(std::ofstream& out, t_type* type);
+ std::string get_java_type_string(t_type* type);
+ void generate_reflection_setters(std::ostringstream& out, t_type* type, std::string field_name, std::string cap_name);
+ void generate_reflection_getters(std::ostringstream& out, t_type* type, std::string field_name, std::string cap_name);
+ void generate_generic_field_getters_setters(std::ofstream& out, t_struct* tstruct);
+ void generate_java_bean_boilerplate(std::ofstream& out, t_struct* tstruct);
+
+ void generate_function_helpers(t_function* tfunction);
+ std::string get_cap_name(std::string name);
+ std::string generate_isset_check(t_field* field);
+ std::string generate_isset_check(std::string field);
+ void generate_isset_set(ofstream& out, t_field* field);
+ std::string isset_field_id(t_field* field);
+
+ void generate_primitive_service_interface (t_service* tservice);
+ void generate_service_interface (t_service* tservice);
+ void generate_service_helpers (t_service* tservice);
+ void generate_service_client (t_service* tservice);
+ void generate_service_server (t_service* tservice);
+ void generate_process_function (t_service* tservice, t_function* tfunction);
+
+ void generate_java_union(t_struct* tstruct);
+ void generate_union_constructor(ofstream& out, t_struct* tstruct);
+ void generate_union_getters_and_setters(ofstream& out, t_struct* tstruct);
+ void generate_union_abstract_methods(ofstream& out, t_struct* tstruct);
+ void generate_check_type(ofstream& out, t_struct* tstruct);
+ void generate_read_value(ofstream& out, t_struct* tstruct);
+ void generate_write_value(ofstream& out, t_struct* tstruct);
+ void generate_get_field_desc(ofstream& out, t_struct* tstruct);
+ void generate_get_struct_desc(ofstream& out, t_struct* tstruct);
+ void generate_get_field_name(ofstream& out, t_struct* tstruct);
+
+ void generate_union_comparisons(ofstream& out, t_struct* tstruct);
+ void generate_union_hashcode(ofstream& out, t_struct* tstruct);
+
+ /**
+ * Serialization constructs
+ */
+
+ void generate_deserialize_field (std::ofstream& out,
+ t_field* tfield,
+ std::string prefix="");
+
+ void generate_deserialize_struct (std::ofstream& out,
+ t_struct* tstruct,
+ std::string prefix="");
+
+ void generate_deserialize_container (std::ofstream& out,
+ t_type* ttype,
+ std::string prefix="");
+
+ void generate_deserialize_set_element (std::ofstream& out,
+ t_set* tset,
+ std::string prefix="");
+
+ void generate_deserialize_map_element (std::ofstream& out,
+ t_map* tmap,
+ std::string prefix="");
+
+ void generate_deserialize_list_element (std::ofstream& out,
+ t_list* tlist,
+ std::string prefix="");
+
+ void generate_serialize_field (std::ofstream& out,
+ t_field* tfield,
+ std::string prefix="");
+
+ void generate_serialize_struct (std::ofstream& out,
+ t_struct* tstruct,
+ std::string prefix="");
+
+ void generate_serialize_container (std::ofstream& out,
+ t_type* ttype,
+ std::string prefix="");
+
+ void generate_serialize_map_element (std::ofstream& out,
+ t_map* tmap,
+ std::string iter,
+ std::string map);
+
+ void generate_serialize_set_element (std::ofstream& out,
+ t_set* tmap,
+ std::string iter);
+
+ void generate_serialize_list_element (std::ofstream& out,
+ t_list* tlist,
+ std::string iter);
+
+ void generate_java_doc (std::ofstream& out,
+ t_field* field);
+
+ void generate_java_doc (std::ofstream& out,
+ t_doc* tdoc);
+
+ void generate_java_doc (std::ofstream& out,
+ t_function* tdoc);
+
+ void generate_java_docstring_comment (std::ofstream &out,
+ string contents);
+
+ void generate_deep_copy_container(std::ofstream& out, std::string source_name_p1, std::string source_name_p2, std::string result_name, t_type* type);
+ void generate_deep_copy_non_container(std::ofstream& out, std::string source_name, std::string dest_name, t_type* type);
+
+ bool has_bit_vector(t_struct* tstruct);
+
+ /**
+ * Helper rendering functions
+ */
+
+ std::string java_package();
+ std::string java_type_imports();
+ std::string java_thrift_imports();
+ std::string type_name(t_type* ttype, bool in_container=false, bool in_init=false, bool skip_generic=false);
+ std::string base_type_name(t_base_type* tbase, bool in_container=false);
+ std::string declare_field(t_field* tfield, bool init=false);
+ std::string function_signature(t_function* tfunction, std::string prefix="");
+ std::string argument_list(t_struct* tstruct, bool include_types = true);
+ std::string type_to_enum(t_type* ttype);
+ std::string get_enum_class_name(t_type* type);
+ void generate_struct_desc(ofstream& out, t_struct* tstruct);
+ void generate_field_descs(ofstream& out, t_struct* tstruct);
+ std::string box_type(t_type* type, string value);
+
+ bool type_can_be_null(t_type* ttype) {
+ ttype = get_true_type(ttype);
+
+ return
+ ttype->is_container() ||
+ ttype->is_struct() ||
+ ttype->is_xception() ||
+ ttype->is_string() ||
+ ttype->is_enum();
+ }
+
+ std::string constant_name(std::string name);
+
+ private:
+
+ /**
+ * File streams
+ */
+
+ std::string package_name_;
+ std::ofstream f_service_;
+ std::string package_dir_;
+
+};
+
+
+/**
+ * Prepares for file generation by opening up the necessary file output
+ * streams.
+ *
+ * @param tprogram The program to generate
+ */
+void t_javame_generator::init_generator() {
+ // Make output directory
+ MKDIR(get_out_dir().c_str());
+ package_name_ = program_->get_namespace("java");
+
+ string dir = package_name_;
+ string subdir = get_out_dir();
+ string::size_type loc;
+ while ((loc = dir.find(".")) != string::npos) {
+ subdir = subdir + "/" + dir.substr(0, loc);
+ MKDIR(subdir.c_str());
+ dir = dir.substr(loc+1);
+ }
+ if (dir.size() > 0) {
+ subdir = subdir + "/" + dir;
+ MKDIR(subdir.c_str());
+ }
+
+ package_dir_ = subdir;
+}
+
+/**
+ * Packages the generated file
+ *
+ * @return String of the package, i.e. "package org.apache.thriftdemo;"
+ */
+string t_javame_generator::java_package() {
+ if (!package_name_.empty()) {
+ return string("package ") + package_name_ + ";\n\n";
+ }
+ return "";
+}
+
+/**
+ * Prints standard java imports
+ *
+ * @return List of imports for Java types that are used in here
+ */
+string t_javame_generator::java_type_imports() {
+ return
+ string() +
+ "import java.util.Hashtable;\n" +
+ "import java.util.Vector;\n" +
+ "import java.util.Enumeration;\n\n";
+}
+
+/**
+ * Prints standard java imports
+ *
+ * @return List of imports necessary for thrift
+ */
+string t_javame_generator::java_thrift_imports() {
+ return
+ string() +
+ "import org.apache.thrift.*;\n" +
+ "import org.apache.thrift.meta_data.*;\n" +
+ "import org.apache.thrift.transport.*;\n" +
+ "import org.apache.thrift.protocol.*;\n\n";
+}
+
+/**
+ * Nothing in Java
+ */
+void t_javame_generator::close_generator() {}
+
+/**
+ * Generates a typedef. This is not done in Java, since it does
+ * not support arbitrary name replacements, and it'd be a wacky waste
+ * of overhead to make wrapper classes.
+ *
+ * @param ttypedef The type definition
+ */
+void t_javame_generator::generate_typedef(t_typedef* ttypedef) {}
+
+/**
+ * Enums are a class with a set of static constants.
+ *
+ * @param tenum The enumeration
+ */
+void t_javame_generator::generate_enum(t_enum* tenum) {
+ // Make output file
+ string f_enum_name = package_dir_+"/"+(tenum->get_name())+".java";
+ ofstream f_enum;
+ f_enum.open(f_enum_name.c_str());
+
+ // Comment and package it
+ f_enum <<
+ autogen_comment() <<
+ java_package();
+
+ generate_java_doc(f_enum, tenum);
+ indent(f_enum) <<
+ "public class " << tenum->get_name() << " implements org.apache.thrift.TEnum ";
+ scope_up(f_enum);
+ f_enum << endl;
+
+ vector<t_enum_value*> constants = tenum->get_constants();
+ vector<t_enum_value*>::iterator c_iter;
+ int value = -1;
+ for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
+ if ((*c_iter)->has_value()) {
+ value = (*c_iter)->get_value();
+ } else {
+ ++value;
+ }
+
+ generate_java_doc(f_enum, *c_iter);
+ indent(f_enum) << "public static final " << tenum->get_name() <<
+ " " << (*c_iter)->get_name() << " = new " << tenum->get_name() <<
+ "(" << value << ");" << endl;
+ }
+ f_enum << endl;
+
+ // Field for thriftCode
+ indent(f_enum) << "private final int value;" << endl << endl;
+
+ indent(f_enum) << "private " << tenum->get_name() << "(int value) {" << endl;
+ indent(f_enum) << " this.value = value;" <<endl;
+ indent(f_enum) << "}" << endl << endl;
+
+ indent(f_enum) << "/**" << endl;
+ indent(f_enum) << " * Get the integer value of this enum value, as defined in the Thrift IDL." << endl;
+ indent(f_enum) << " */" << endl;
+ indent(f_enum) << "public int getValue() {" << endl;
+ indent(f_enum) << " return value;" <<endl;
+ indent(f_enum) << "}" << endl << endl;
+
+ indent(f_enum) << "/**" << endl;
+ indent(f_enum) << " * Find a the enum type by its integer value, as defined in the Thrift IDL." << endl;
+ indent(f_enum) << " * @return null if the value is not found." << endl;
+ indent(f_enum) << " */" << endl;
+ indent(f_enum) << "public static "+ tenum->get_name() + " findByValue(int value) { " << endl;
+
+ indent_up();
+
+ indent(f_enum) << "switch (value) {" << endl;
+ indent_up();
+
+ value = -1;
+
+ for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
+ if ((*c_iter)->has_value()) {
+ value = (*c_iter)->get_value();
+ } else {
+ ++value;
+ }
+
+ indent(f_enum) << "case " << value << ":" << endl;
+ indent(f_enum) << " return " << (*c_iter)->get_name() << ";" << endl;
+ }
+
+ indent(f_enum) << "default:" << endl;
+ indent(f_enum) << " return null;" << endl;
+
+ indent_down();
+
+ indent(f_enum) << "}" << endl;
+
+ indent_down();
+
+ indent(f_enum) << "}" << endl;
+
+ scope_down(f_enum);
+
+ f_enum.close();
+}
+
+/**
+ * Generates a class that holds all the constants.
+ */
+void t_javame_generator::generate_consts(std::vector<t_const*> consts) {
+ if (consts.empty()) {
+ return;
+ }
+
+ string f_consts_name = package_dir_+"/Constants.java";
+ ofstream f_consts;
+ f_consts.open(f_consts_name.c_str());
+
+ // Print header
+ f_consts <<
+ autogen_comment() <<
+ java_package() <<
+ java_type_imports();
+
+ f_consts <<
+ "public class Constants {" << endl <<
+ endl;
+ indent_up();
+ vector<t_const*>::iterator c_iter;
+ for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
+ print_const_value(f_consts,
+ (*c_iter)->get_name(),
+ (*c_iter)->get_type(),
+ (*c_iter)->get_value(),
+ false);
+ }
+ indent_down();
+ indent(f_consts) <<
+ "}" << endl;
+ f_consts.close();
+}
+
+
+/**
+ * Prints the value of a constant with the given type. Note that type checking
+ * is NOT performed in this function as it is always run beforehand using the
+ * validate_types method in main.cc
+ */
+void t_javame_generator::print_const_value(std::ofstream& out, string name, t_type* type, t_const_value* value, bool in_static, bool defval) {
+ type = get_true_type(type);
+
+ indent(out);
+ if (!defval) {
+ out <<
+ (in_static ? "" : "public static final ") <<
+ type_name(type) << " ";
+ }
+ if (type->is_base_type()) {
+ string v2 = render_const_value(out, name, type, value);
+ out << name << " = " << v2 << ";" << endl << endl;
+ } else if (type->is_enum()) {
+ out << name << " = " << render_const_value(out, name, type, value) << ";" << endl << endl;
+ } else if (type->is_struct() || type->is_xception()) {
+ const vector<t_field*>& fields = ((t_struct*)type)->get_members();
+ vector<t_field*>::const_iterator f_iter;
+ const map<t_const_value*, t_const_value*>& val = value->get_map();
+ map<t_const_value*, t_const_value*>::const_iterator v_iter;
+ out << name << " = new " << type_name(type, false, true) << "();" << endl;
+ if (!in_static) {
+ indent(out) << "static {" << endl;
+ indent_up();
+ }
+ for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
+ t_type* field_type = NULL;
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ if ((*f_iter)->get_name() == v_iter->first->get_string()) {
+ field_type = (*f_iter)->get_type();
+ }
+ }
+ if (field_type == NULL) {
+ throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
+ }
+ string val = render_const_value(out, name, field_type, v_iter->second);
+ indent(out) << name << ".";
+ std::string cap_name = get_cap_name(v_iter->first->get_string());
+ out << "set" << cap_name << "(" << val << ");" << endl;
+ }
+ if (!in_static) {
+ indent_down();
+ indent(out) << "}" << endl;
+ }
+ out << endl;
+ } else if (type->is_map()) {
+ out << name << " = new " << type_name(type, false, true) << "();" << endl;
+ if (!in_static) {
+ indent(out) << "static {" << endl;
+ indent_up();
+ }
+ t_type* ktype = ((t_map*)type)->get_key_type();
+ t_type* vtype = ((t_map*)type)->get_val_type();
+ const map<t_const_value*, t_const_value*>& val = value->get_map();
+ map<t_const_value*, t_const_value*>::const_iterator v_iter;
+ for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
+ string key = render_const_value(out, name, ktype, v_iter->first);
+ string val = render_const_value(out, name, vtype, v_iter->second);
+ indent(out) << name << ".put(" << box_type(ktype, key) << ", " <<
+ box_type(vtype, val) << ");" << endl;
+ }
+ if (!in_static) {
+ indent_down();
+ indent(out) << "}" << endl;
+ }
+ out << endl;
+ } else if (type->is_list() || type->is_set()) {
+ out << name << " = new " << type_name(type, false, true) << "();" << endl;
+ if (!in_static) {
+ indent(out) << "static {" << endl;
+ indent_up();
+ }
+ t_type* etype;
+ if (type->is_list()) {
+ etype = ((t_list*)type)->get_elem_type();
+ } else {
+ etype = ((t_set*)type)->get_elem_type();
+ }
+ const vector<t_const_value*>& val = value->get_list();
+ vector<t_const_value*>::const_iterator v_iter;
+ for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
+ string val = render_const_value(out, name, etype, *v_iter);
+ if (type->is_list()) {
+ indent(out) << name << ".addElement(" << box_type(etype, val) << ");" << endl;
+ } else {
+ indent(out) << name << ".put(" <<
+ box_type(etype, val) << ", " <<
+ box_type(etype, val) << ");" << endl;
+ }
+ }
+ if (!in_static) {
+ indent_down();
+ indent(out) << "}" << endl;
+ }
+ out << endl;
+ } else {
+ throw "compiler error: no const of type " + type->get_name();
+ }
+}
+
+string t_javame_generator::render_const_value(ofstream& out, string name, t_type* type, t_const_value* value) {
+ type = get_true_type(type);
+ std::ostringstream render;
+
+ if (type->is_base_type()) {
+ t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
+ switch (tbase) {
+ case t_base_type::TYPE_STRING:
+ render << '"' << get_escaped_string(value) << '"';
+ break;
+ case t_base_type::TYPE_BOOL:
+ render << ((value->get_integer() > 0) ? "true" : "false");
+ break;
+ case t_base_type::TYPE_BYTE:
+ render << "(byte)" << value->get_integer();
+ break;
+ case t_base_type::TYPE_I16:
+ render << "(short)" << value->get_integer();
+ break;
+ case t_base_type::TYPE_I32:
+ render << value->get_integer();
+ break;
+ case t_base_type::TYPE_I64:
+ render << value->get_integer() << "L";
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ if (value->get_type() == t_const_value::CV_INTEGER) {
+ render << "(double)" << value->get_integer();
+ } else {
+ render << value->get_double();
+ }
+ break;
+ default:
+ throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase);
+ }
+ } else if (type->is_enum()) {
+ render << type_name(type, false, false) << "." << value->get_identifier();
+ } else {
+ string t = tmp("tmp");
+ print_const_value(out, t, type, value, true);
+ render << t;
+ }
+
+ return render.str();
+}
+
+string t_javame_generator::box_type(t_type* type, string value) {
+ if (type->is_base_type()) {
+ switch (((t_base_type*)type)->get_base()) {
+ case t_base_type::TYPE_BOOL:
+ return "new Boolean(" + value + ")";
+ case t_base_type::TYPE_BYTE:
+ return "new Byte(" + value + ")";
+ case t_base_type::TYPE_I16:
+ return "new Short(" + value + ")";
+ case t_base_type::TYPE_I32:
+ return "new Integer(" + value + ")";
+ case t_base_type::TYPE_I64:
+ return "new Long(" + value + ")";
+ case t_base_type::TYPE_DOUBLE:
+ return "new Double(" + value + ")";
+ default:
+ break;
+ }
+ }
+ return value;
+}
+
+/**
+ * Generates a struct definition for a thrift data type. This will be a TBase
+ * implementor.
+ *
+ * @param tstruct The struct definition
+ */
+void t_javame_generator::generate_struct(t_struct* tstruct) {
+ if (tstruct->is_union()) {
+ generate_java_union(tstruct);
+ } else {
+ generate_java_struct(tstruct, false);
+ }
+}
+
+/**
+ * Exceptions are structs, but they inherit from Exception
+ *
+ * @param tstruct The struct definition
+ */
+void t_javame_generator::generate_xception(t_struct* txception) {
+ generate_java_struct(txception, true);
+}
+
+
+/**
+ * Java struct definition.
+ *
+ * @param tstruct The struct definition
+ */
+void t_javame_generator::generate_java_struct(t_struct* tstruct,
+ bool is_exception) {
+ // Make output file
+ string f_struct_name = package_dir_+"/"+(tstruct->get_name())+".java";
+ ofstream f_struct;
+ f_struct.open(f_struct_name.c_str());
+
+ f_struct <<
+ autogen_comment() <<
+ java_package() <<
+ java_type_imports() <<
+ java_thrift_imports();
+
+ generate_java_struct_definition(f_struct,
+ tstruct,
+ is_exception);
+ f_struct.close();
+}
+
+/**
+ * Java union definition.
+ *
+ * @param tstruct The struct definition
+ */
+void t_javame_generator::generate_java_union(t_struct* tstruct) {
+ // Make output file
+ string f_struct_name = package_dir_+"/"+(tstruct->get_name())+".java";
+ ofstream f_struct;
+ f_struct.open(f_struct_name.c_str());
+
+ f_struct <<
+ autogen_comment() <<
+ java_package() <<
+ java_type_imports() <<
+ java_thrift_imports();
+
+ generate_java_doc(f_struct, tstruct);
+
+ bool is_final = (tstruct->annotations_.find("final") != tstruct->annotations_.end());
+
+ indent(f_struct) <<
+ "public " << (is_final ? "final " : "") << "class " << tstruct->get_name()
+ << " extends TUnion ";
+
+ scope_up(f_struct);
+
+ generate_struct_desc(f_struct, tstruct);
+ generate_field_descs(f_struct, tstruct);
+
+ f_struct << endl;
+
+ generate_union_constructor(f_struct, tstruct);
+
+ f_struct << endl;
+
+ generate_union_abstract_methods(f_struct, tstruct);
+
+ f_struct << endl;
+
+ generate_union_getters_and_setters(f_struct, tstruct);
+
+ f_struct << endl;
+
+ generate_union_comparisons(f_struct, tstruct);
+
+ f_struct << endl;
+
+ generate_union_hashcode(f_struct, tstruct);
+
+ f_struct << endl;
+
+ scope_down(f_struct);
+
+ f_struct.close();
+}
+
+void t_javame_generator::generate_union_constructor(ofstream& out, t_struct* tstruct) {
+ indent(out) << "public " << type_name(tstruct) << "() {" << endl;
+ indent(out) << " super();" << endl;
+ indent(out) << "}" << endl << endl;
+
+ indent(out) << "public " << type_name(tstruct) << "(_Fields setField, Object value) {" << endl;
+ indent(out) << " super(setField, value);" << endl;
+ indent(out) << "}" << endl << endl;
+
+ indent(out) << "public " << type_name(tstruct) << "(" << type_name(tstruct) << " other) {" << endl;
+ indent(out) << " super(other);" << endl;
+ indent(out) << "}" << endl;
+
+ indent(out) << "public " << tstruct->get_name() << " deepCopy() {" << endl;
+ indent(out) << " return new " << tstruct->get_name() << "(this);" << endl;
+ indent(out) << "}" << endl << endl;
+
+ // generate "constructors" for each field
+ const vector<t_field*>& members = tstruct->get_members();
+ vector<t_field*>::const_iterator m_iter;
+ for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
+ indent(out) << "public static " << type_name(tstruct) << " " << (*m_iter)->get_name() << "(" << type_name((*m_iter)->get_type()) << " value) {" << endl;
+ indent(out) << " " << type_name(tstruct) << " x = new " << type_name(tstruct) << "();" << endl;
+ indent(out) << " x.set" << get_cap_name((*m_iter)->get_name()) << "(value);" << endl;
+ indent(out) << " return x;" << endl;
+ indent(out) << "}" << endl << endl;
+ }
+}
+
+void t_javame_generator::generate_union_getters_and_setters(ofstream& out, t_struct* tstruct) {
+ const vector<t_field*>& members = tstruct->get_members();
+ vector<t_field*>::const_iterator m_iter;
+
+ bool first = true;
+ for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
+ if (first) {
+ first = false;
+ } else {
+ out << endl;
+ }
+
+ t_field* field = (*m_iter);
+
+ generate_java_doc(out, field);
+ indent(out) << "public " << type_name(field->get_type()) << " get" << get_cap_name(field->get_name()) << "() {" << endl;
+ indent(out) << " if (getSetField() == _Fields." << constant_name(field->get_name()) << ") {" << endl;
+ indent(out) << " return (" << type_name(field->get_type(), true) << ")getFieldValue();" << endl;
+ indent(out) << " } else {" << endl;
+ indent(out) << " throw new RuntimeException(\"Cannot get field '" << field->get_name()
+ << "' because union is currently set to \" + getFieldDesc(getSetField()).name);" << endl;
+ indent(out) << " }" << endl;
+ indent(out) << "}" << endl;
+
+ out << endl;
+
+ generate_java_doc(out, field);
+ indent(out) << "public void set" << get_cap_name(field->get_name()) << "(" << type_name(field->get_type()) << " value) {" << endl;
+ if (type_can_be_null(field->get_type())) {
+ indent(out) << " if (value == null) throw new NullPointerException();" << endl;
+ }
+ indent(out) << " setField_ = _Fields." << constant_name(field->get_name()) << ";" << endl;
+ indent(out) << " value_ = value;" << endl;
+ indent(out) << "}" << endl;
+ }
+}
+
+void t_javame_generator::generate_union_abstract_methods(ofstream& out, t_struct* tstruct) {
+ generate_check_type(out, tstruct);
+ out << endl;
+ generate_read_value(out, tstruct);
+ out << endl;
+ generate_write_value(out, tstruct);
+ out << endl;
+ generate_get_field_desc(out, tstruct);
+ out << endl;
+ generate_get_struct_desc(out, tstruct);
+ out << endl;
+}
+
+void t_javame_generator::generate_check_type(ofstream& out, t_struct* tstruct) {
+ indent(out) << "protected void checkType(_Fields setField, Object value) throws ClassCastException {" << endl;
+ indent_up();
+
+ indent(out) << "switch (setField) {" << endl;
+ indent_up();
+
+ const vector<t_field*>& members = tstruct->get_members();
+ vector<t_field*>::const_iterator m_iter;
+
+ for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
+ t_field* field = (*m_iter);
+
+ indent(out) << "case " << constant_name(field->get_name()) << ":" << endl;
+ indent(out) << " if (value instanceof " << type_name(field->get_type(), true, false, true) << ") {" << endl;
+ indent(out) << " break;" << endl;
+ indent(out) << " }" << endl;
+ indent(out) << " throw new ClassCastException(\"Was expecting value of type "
+ << type_name(field->get_type(), true, false) << " for field '" << field->get_name()
+ << "', but got \" + value.getClass().getSimpleName());" << endl;
+ // do the real check here
+ }
+
+ indent(out) << "default:" << endl;
+ indent(out) << " throw new IllegalArgumentException(\"Unknown field id \" + setField);" << endl;
+
+ indent_down();
+ indent(out) << "}" << endl;
+
+ indent_down();
+ indent(out) << "}" << endl;
+}
+
+void t_javame_generator::generate_read_value(ofstream& out, t_struct* tstruct) {
+ indent(out) << "protected Object readValue(TProtocol iprot, TField field) throws TException {" << endl;
+
+ indent_up();
+
+ indent(out) << "_Fields setField = _Fields.findByThriftId(field.id);" << endl;
+ indent(out) << "if (setField != null) {" << endl;
+ indent_up();
+ indent(out) << "switch (setField) {" << endl;
+ indent_up();
+
+ const vector<t_field*>& members = tstruct->get_members();
+ vector<t_field*>::const_iterator m_iter;
+
+ for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
+ t_field* field = (*m_iter);
+
+ indent(out) << "case " << constant_name(field->get_name()) << ":" << endl;
+ indent_up();
+ indent(out) << "if (field.type == " << constant_name(field->get_name()) << "_FIELD_DESC.type) {" << endl;
+ indent_up();
+ indent(out) << type_name(field->get_type(), true, false) << " " << field->get_name() << ";" << endl;
+ generate_deserialize_field(out, field, "");
+ indent(out) << "return " << field->get_name() << ";" << endl;
+ indent_down();
+ indent(out) << "} else {" << endl;
+ indent(out) << " TProtocolUtil.skip(iprot, field.type);" << endl;
+ indent(out) << " return null;" << endl;
+ indent(out) << "}" << endl;
+ indent_down();
+ }
+
+ indent(out) << "default:" << endl;
+ indent(out) << " throw new IllegalStateException(\"setField wasn't null, but didn't match any of the case statements!\");" << endl;
+
+ indent_down();
+ indent(out) << "}" << endl;
+
+ indent_down();
+ indent(out) << "} else {" << endl;
+ indent_up();
+ indent(out) << "TProtocolUtil.skip(iprot, field.type);" << endl;
+ indent(out) << "return null;" << endl;
+ indent_down();
+ indent(out) << "}" << endl;
+
+ indent_down();
+ indent(out) << "}" << endl;
+}
+
+void t_javame_generator::generate_write_value(ofstream& out, t_struct* tstruct) {
+ indent(out) << "protected void writeValue(TProtocol oprot) throws TException {" << endl;
+
+ indent_up();
+
+ indent(out) << "switch (setField_) {" << endl;
+ indent_up();
+
+ const vector<t_field*>& members = tstruct->get_members();
+ vector<t_field*>::const_iterator m_iter;
+
+ for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
+ t_field* field = (*m_iter);
+
+ indent(out) << "case " << constant_name(field->get_name()) << ":" << endl;
+ indent_up();
+ indent(out) << type_name(field->get_type(), true, false) << " " << field->get_name()
+ << " = (" << type_name(field->get_type(), true, false) << ")value_;" << endl;
+ generate_serialize_field(out, field, "");
+ indent(out) << "return;" << endl;
+ indent_down();
+ }
+
+ indent(out) << "default:" << endl;
+ indent(out) << " throw new IllegalStateException(\"Cannot write union with unknown field \" + setField_);" << endl;
+
+ indent_down();
+ indent(out) << "}" << endl;
+
+ indent_down();
+
+
+
+ indent(out) << "}" << endl;
+}
+
+void t_javame_generator::generate_get_field_desc(ofstream& out, t_struct* tstruct) {
+ indent(out) << "protected TField getFieldDesc(_Fields setField) {" << endl;
+ indent_up();
+
+ const vector<t_field*>& members = tstruct->get_members();
+ vector<t_field*>::const_iterator m_iter;
+
+ indent(out) << "switch (setField) {" << endl;
+ indent_up();
+
+ for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
+ t_field* field = (*m_iter);
+ indent(out) << "case " << constant_name(field->get_name()) << ":" << endl;
+ indent(out) << " return " << constant_name(field->get_name()) << "_FIELD_DESC;" << endl;
+ }
+
+ indent(out) << "default:" << endl;
+ indent(out) << " throw new IllegalArgumentException(\"Unknown field id \" + setField);" << endl;
+
+ indent_down();
+ indent(out) << "}" << endl;
+
+ indent_down();
+ indent(out) << "}" << endl;
+}
+
+void t_javame_generator::generate_get_struct_desc(ofstream& out, t_struct* tstruct) {
+ indent(out) << "protected TStruct getStructDesc() {" << endl;
+ indent(out) << " return STRUCT_DESC;" << endl;
+ indent(out) << "}" << endl;
+}
+
+void t_javame_generator::generate_union_comparisons(ofstream& out, t_struct* tstruct) {
+ // equality
+ indent(out) << "public boolean equals(Object other) {" << endl;
+ indent(out) << " if (other instanceof " << tstruct->get_name() << ") {" << endl;
+ indent(out) << " return equals((" << tstruct->get_name() << ")other);" << endl;
+ indent(out) << " } else {" << endl;
+ indent(out) << " return false;" << endl;
+ indent(out) << " }" << endl;
+ indent(out) << "}" << endl;
+
+ out << endl;
+
+ indent(out) << "public boolean equals(" << tstruct->get_name() << " other) {" << endl;
+ indent(out) << " return other != null && getSetField() == other.getSetField() && getFieldValue().equals(other.getFieldValue());" << endl;
+ indent(out) << "}" << endl;
+ out << endl;
+
+ indent(out) << "public int compareTo(" << type_name(tstruct) << " other) {" << endl;
+ indent(out) << " int lastComparison = TBaseHelper.compareTo(getSetField(), other.getSetField());" << endl;
+ indent(out) << " if (lastComparison == 0) {" << endl;
+ indent(out) << " return TBaseHelper.compareTo(getFieldValue(), other.getFieldValue());" << endl;
+ indent(out) << " }" << endl;
+ indent(out) << " return lastComparison;" << endl;
+ indent(out) << "}" << endl;
+ out << endl;
+}
+
+void t_javame_generator::generate_union_hashcode(ofstream& out, t_struct* tstruct) {
+ indent(out) << "/**" << endl;
+ indent(out) << " * If you'd like this to perform more respectably, use the hashcode generator option." << endl;
+ indent(out) << " */" << endl;
+ indent(out) << "public int hashCode() {" << endl;
+ indent(out) << " return 0;" << endl;
+ indent(out) << "}" << endl;
+}
+
+/**
+ * Java struct definition. This has various parameters, as it could be
+ * generated standalone or inside another class as a helper. If it
+ * is a helper than it is a static class.
+ *
+ * @param tstruct The struct definition
+ * @param is_exception Is this an exception?
+ * @param in_class If inside a class, needs to be static class
+ * @param is_result If this is a result it needs a different writer
+ */
+void t_javame_generator::generate_java_struct_definition(ofstream &out,
+ t_struct* tstruct,
+ bool is_exception,
+ bool in_class,
+ bool is_result) {
+ generate_java_doc(out, tstruct);
+
+ bool is_final = (tstruct->annotations_.find("final") != tstruct->annotations_.end());
+
+ indent(out) <<
+ "public " << (is_final ? "final " : "") <<
+ (in_class ? "static " : "") << "class " << tstruct->get_name() << " ";
+
+ if (is_exception) {
+ out << "extends Exception ";
+ }
+ out << "implements TBase ";
+
+ scope_up(out);
+
+ generate_struct_desc(out, tstruct);
+
+ // Members are public for -java, private for -javabean
+ const vector<t_field*>& members = tstruct->get_members();
+ vector<t_field*>::const_iterator m_iter;
+
+ out << endl;
+
+ generate_field_descs(out, tstruct);
+
+ out << endl;
+
+ for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
+ indent(out) << "private ";
+ out << declare_field(*m_iter, false) << endl;
+ }
+
+ // isset data
+ if (members.size() > 0) {
+ out << endl;
+
+ indent(out) << "// isset id assignments" << endl;
+
+ int i = 0;
+ for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
+ if (!type_can_be_null((*m_iter)->get_type())) {
+ indent(out) << "private static final int " << isset_field_id(*m_iter)
+ << " = " << i << ";" << endl;
+ i++;
+ }
+ }
+
+ if (i > 0) {
+ indent(out) << "private boolean[] __isset_vector = new boolean[" << i << "];" << endl;
+ }
+
+ out << endl;
+ }
+
+ bool all_optional_members = true;
+
+ // Default constructor
+ indent(out) <<
+ "public " << tstruct->get_name() << "() {" << endl;
+ indent_up();
+ for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
+ t_type* t = get_true_type((*m_iter)->get_type());
+ if ((*m_iter)->get_value() != NULL) {
+ print_const_value(out, "this." + (*m_iter)->get_name(), t, (*m_iter)->get_value(), true, true);
+ }
+ if ((*m_iter)->get_req() != t_field::T_OPTIONAL) {
+ all_optional_members = false;
+ }
+ }
+ indent_down();
+ indent(out) << "}" << endl << endl;
+
+ if (!members.empty() && !all_optional_members) {
+ // Full constructor for all fields
+ indent(out) <<
+ "public " << tstruct->get_name() << "(" << endl;
+ indent_up();
+ bool first = true;
+ for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
+ if ((*m_iter)->get_req() != t_field::T_OPTIONAL) {
+ if (!first) {
+ out << "," << endl;
+ }
+ first = false;
+ indent(out) << type_name((*m_iter)->get_type()) << " " <<
+ (*m_iter)->get_name();
+ }
+ }
+ out << ")" << endl;
+ indent_down();
+ indent(out) << "{" << endl;
+ indent_up();
+ indent(out) << "this();" << endl;
+ for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
+ if ((*m_iter)->get_req() != t_field::T_OPTIONAL) {
+ indent(out) << "this." << (*m_iter)->get_name() << " = " <<
+ (*m_iter)->get_name() << ";" << endl;
+ generate_isset_set(out, (*m_iter));
+ }
+ }
+ indent_down();
+ indent(out) << "}" << endl << endl;
+ }
+
+ // copy constructor
+ indent(out) << "/**" << endl;
+ indent(out) << " * Performs a deep copy on <i>other</i>." << endl;
+ indent(out) << " */" << endl;
+ indent(out) << "public " << tstruct->get_name() << "(" << tstruct->get_name() << " other) {" << endl;
+ indent_up();
+
+ if (has_bit_vector(tstruct)) {
+ indent(out) << "System.arraycopy(other.__isset_vector, 0, __isset_vector, 0, other.__isset_vector.length);" << endl;
+ }
+
+ for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
+ t_field* field = (*m_iter);
+ std::string field_name = field->get_name();
+ t_type* type = field->get_type();
+ bool can_be_null = type_can_be_null(type);
+
+ if (can_be_null) {
+ indent(out) << "if (other." << generate_isset_check(field) << ") {" << endl;
+ indent_up();
+ }
+
+ if (type->is_container()) {
+ generate_deep_copy_container(out, "other", field_name, "__this__" + field_name, type);
+ indent(out) << "this." << field_name << " = __this__" << field_name << ";" << endl;
+ } else {
+ indent(out) << "this." << field_name << " = ";
+ generate_deep_copy_non_container(out, "other." + field_name, field_name, type);
+ out << ";" << endl;
+ }
+
+ if (can_be_null) {
+ indent_down();
+ indent(out) << "}" << endl;
+ }
+ }
+
+ indent_down();
+ indent(out) << "}" << endl << endl;
+
+ // clone method, so that you can deep copy an object when you don't know its class.
+ indent(out) << "public " << tstruct->get_name() << " deepCopy() {" << endl;
+ indent(out) << " return new " << tstruct->get_name() << "(this);" << endl;
+ indent(out) << "}" << endl << endl;
+
+ indent(out) << "public " << tstruct->get_name() << " clone() {" << endl;
+ indent(out) << " return new " << tstruct->get_name() << "(this);" << endl;
+ indent(out) << "}" << endl << endl;
+
+ generate_java_struct_clear(out, tstruct);
+
+ generate_java_bean_boilerplate(out, tstruct);
+ generate_generic_field_getters_setters(out, tstruct);
+
+ generate_java_struct_equality(out, tstruct);
+ generate_java_struct_compare_to(out, tstruct);
+
+ generate_java_struct_reader(out, tstruct);
+ if (is_result) {
+ generate_java_struct_result_writer(out, tstruct);
+ } else {
+ generate_java_struct_writer(out, tstruct);
+ }
+ generate_java_struct_tostring(out, tstruct);
+ generate_java_validator(out, tstruct);
+ scope_down(out);
+ out << endl;
+}
+
+/**
+ * Generates equals methods and a hashCode method for a structure.
+ *
+ * @param tstruct The struct definition
+ */
+void t_javame_generator::generate_java_struct_equality(ofstream& out,
+ t_struct* tstruct) {
+ out << indent() << "public boolean equals(Object that) {" << endl;
+ indent_up();
+ out <<
+ indent() << "if (that == null)" << endl <<
+ indent() << " return false;" << endl <<
+ indent() << "if (that instanceof " << tstruct->get_name() << ")" << endl <<
+ indent() << " return this.equals((" << tstruct->get_name() << ")that);" << endl <<
+ indent() << "return false;" << endl;
+ scope_down(out);
+ out << endl;
+
+ out <<
+ indent() << "public boolean equals(" << tstruct->get_name() << " that) {" << endl;
+ indent_up();
+ out <<
+ indent() << "if (that == null)" << endl <<
+ indent() << " return false;" << endl;
+
+ const vector<t_field*>& members = tstruct->get_members();
+ vector<t_field*>::const_iterator m_iter;
+ for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
+ out << endl;
+
+ t_type* t = get_true_type((*m_iter)->get_type());
+ // Most existing Thrift code does not use isset or optional/required,
+ // so we treat "default" fields as required.
+ bool is_optional = (*m_iter)->get_req() == t_field::T_OPTIONAL;
+ bool can_be_null = type_can_be_null(t);
+ string name = (*m_iter)->get_name();
+
+ string this_present = "true";
+ string that_present = "true";
+ string unequal;
+
+ if (is_optional || can_be_null) {
+ this_present += " && this." + generate_isset_check(*m_iter);
+ that_present += " && that." + generate_isset_check(*m_iter);
+ }
+
+ out <<
+ indent() << "boolean this_present_" << name << " = "
+ << this_present << ";" << endl <<
+ indent() << "boolean that_present_" << name << " = "
+ << that_present << ";" << endl <<
+ indent() << "if (" << "this_present_" << name
+ << " || that_present_" << name << ") {" << endl;
+ indent_up();
+ out <<
+ indent() << "if (!(" << "this_present_" << name
+ << " && that_present_" << name << "))" << endl <<
+ indent() << " return false;" << endl;
+
+ if (t->is_base_type() && ((t_base_type*)t)->is_binary()) {
+ unequal = "TBaseHelper.compareTo(this." + name + ", that." +
+ name + ") != 0";
+ } else if (can_be_null) {
+ unequal = "!this." + name + ".equals(that." + name + ")";
+ } else {
+ unequal = "this." + name + " != that." + name;
+ }
+
+ out <<
+ indent() << "if (" << unequal << ")" << endl <<
+ indent() << " return false;" << endl;
+
+ scope_down(out);
+ }
+ out << endl;
+ indent(out) << "return true;" << endl;
+ scope_down(out);
+ out << endl;
+
+ out << indent() << "public int hashCode() {" << endl;
+ indent_up();
+ indent(out) << "return 0;" << endl;
+ indent_down();
+ indent(out) << "}" << endl << endl;
+}
+
+void t_javame_generator::generate_java_struct_compare_to(ofstream& out, t_struct* tstruct) {
+ indent(out) << "public int compareTo(Object otherObject) {" << endl;
+ // indent(out) << "public int compareTo(" << type_name(tstruct) << " other) {" << endl;
+ indent_up();
+
+ indent(out) << "if (!getClass().equals(otherObject.getClass())) {" << endl;
+ indent(out) << " return getClass().getName().compareTo(otherObject.getClass().getName());" << endl;
+ indent(out) << "}" << endl;
+ out << endl;
+ indent(out) << type_name(tstruct) << " other = (" << type_name(tstruct) << ")otherObject;";
+
+ indent(out) << "int lastComparison = 0;" << endl;
+ out << endl;
+
+ const vector<t_field*>& members = tstruct->get_members();
+ vector<t_field*>::const_iterator m_iter;
+ for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
+ t_field* field = *m_iter;
+ indent(out) << "lastComparison = TBaseHelper.compareTo(" << generate_isset_check(field) << ", other." << generate_isset_check(field) << ");" << endl;
+ indent(out) << "if (lastComparison != 0) {" << endl;
+ indent(out) << " return lastComparison;" << endl;
+ indent(out) << "}" << endl;
+
+ indent(out) << "if (" << generate_isset_check(field) << ") {" << endl;
+ if (field->get_type()->is_struct() || field->get_type()->is_xception()) {
+ indent(out) << " lastComparison = this." << field->get_name() << ".compareTo(other." << field->get_name() << ");" << endl;
+ } else {
+ indent(out) << " lastComparison = TBaseHelper.compareTo(this." << field->get_name() << ", other." << field->get_name() << ");" << endl;
+ }
+
+ indent(out) << " if (lastComparison != 0) {" << endl;
+ indent(out) << " return lastComparison;" << endl;
+ indent(out) << " }" << endl;
+ indent(out) << "}" << endl;
+ }
+
+ indent(out) << "return 0;" << endl;
+
+ indent_down();
+ indent(out) << "}" << endl << endl;
+}
+
+/**
+ * Generates a function to read all the fields of the struct.
+ *
+ * @param tstruct The struct definition
+ */
+void t_javame_generator::generate_java_struct_reader(ofstream& out,
+ t_struct* tstruct) {
+ out <<
+ indent() << "public void read(TProtocol iprot) throws TException {" << endl;
+ indent_up();
+
+ const vector<t_field*>& fields = tstruct->get_members();
+ vector<t_field*>::const_iterator f_iter;
+
+ // Declare stack tmp variables and read struct header
+ out <<
+ indent() << "TField field;" << endl <<
+ indent() << "iprot.readStructBegin();" << endl;
+
+ // Loop over reading in fields
+ indent(out) <<
+ "while (true)" << endl;
+ scope_up(out);
+
+ // Read beginning field marker
+ indent(out) <<
+ "field = iprot.readFieldBegin();" << endl;
+
+ // Check for field STOP marker and break
+ indent(out) <<
+ "if (field.type == TType.STOP) { " << endl;
+ indent_up();
+ indent(out) <<
+ "break;" << endl;
+ indent_down();
+ indent(out) <<
+ "}" << endl;
+
+ // Switch statement on the field we are reading
+ indent(out) << "switch (field.id) {" << endl;
+
+ indent_up();
+
+ // Generate deserialization code for known cases
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ indent(out) <<
+ "case " << (*f_iter)->get_key() << ": // " << constant_name((*f_iter)->get_name()) << endl;
+ indent_up();
+ indent(out) <<
+ "if (field.type == " << type_to_enum((*f_iter)->get_type()) << ") {" << endl;
+ indent_up();
+
+ generate_deserialize_field(out, *f_iter, "this.");
+ generate_isset_set(out, *f_iter);
+ indent_down();
+ out <<
+ indent() << "} else { " << endl <<
+ indent() << " TProtocolUtil.skip(iprot, field.type);" << endl <<
+ indent() << "}" << endl <<
+ indent() << "break;" << endl;
+ indent_down();
+ }
+
+ indent(out) << "default:" << endl;
+ indent(out) << " TProtocolUtil.skip(iprot, field.type);" << endl;
+
+ indent_down();
+ indent(out) << "}" << endl;
+
+ // Read field end marker
+ indent(out) <<
+ "iprot.readFieldEnd();" << endl;
+
+ indent_down();
+ indent(out) << "}" << endl;
+
+ out <<
+ indent() << "iprot.readStructEnd();" << endl;
+
+ // performs various checks (e.g. check that all required fields are set)
+ indent(out) << "validate();" << endl;
+
+ indent_down();
+ out <<
+ indent() << "}" << endl <<
+ endl;
+}
+
+// generates java method to perform various checks
+// (e.g. check that all required fields are set)
+void t_javame_generator::generate_java_validator(ofstream& out,
+ t_struct* tstruct){
+ indent(out) << "public void validate() throws TException {" << endl;
+ indent_up();
+
+ const vector<t_field*>& fields = tstruct->get_members();
+ vector<t_field*>::const_iterator f_iter;
+
+ out << indent() << "// check for required fields" << endl;
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ if ((*f_iter)->get_req() == t_field::T_REQUIRED) {
+ out <<
+ indent() << "if (!" << generate_isset_check(*f_iter) << ") {" << endl <<
+ indent() << " throw new TProtocolException(\"Required field '" << (*f_iter)->get_name() << "' is unset! Struct:\" + toString());" << endl <<
+ indent() << "}" << endl << endl;
+ }
+ }
+
+ indent_down();
+ indent(out) << "}" << endl << endl;
+}
+
+/**
+ * Generates a function to write all the fields of the struct
+ *
+ * @param tstruct The struct definition
+ */
+void t_javame_generator::generate_java_struct_writer(ofstream& out,
+ t_struct* tstruct) {
+ out <<
+ indent() << "public void write(TProtocol oprot) throws TException {" << endl;
+ indent_up();
+
+ string name = tstruct->get_name();
+ const vector<t_field*>& fields = tstruct->get_sorted_members();
+ vector<t_field*>::const_iterator f_iter;
+
+ // performs various checks (e.g. check that all required fields are set)
+ indent(out) << "validate();" << endl << endl;
+
+ indent(out) << "oprot.writeStructBegin(STRUCT_DESC);" << endl;
+
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ bool null_allowed = type_can_be_null((*f_iter)->get_type());
+ if (null_allowed) {
+ out <<
+ indent() << "if (this." << (*f_iter)->get_name() << " != null) {" << endl;
+ indent_up();
+ }
+ bool optional = (*f_iter)->get_req() == t_field::T_OPTIONAL;
+ if (optional) {
+ indent(out) << "if (" << generate_isset_check((*f_iter)) << ") {" << endl;
+ indent_up();
+ }
+
+ indent(out) << "oprot.writeFieldBegin(" << constant_name((*f_iter)->get_name()) << "_FIELD_DESC);" << endl;
+
+ // Write field contents
+ generate_serialize_field(out, *f_iter, "this.");
+
+ // Write field closer
+ indent(out) <<
+ "oprot.writeFieldEnd();" << endl;
+
+ if (optional) {
+ indent_down();
+ indent(out) << "}" << endl;
+ }
+ if (null_allowed) {
+ indent_down();
+ indent(out) << "}" << endl;
+ }
+ }
+ // Write the struct map
+ out <<
+ indent() << "oprot.writeFieldStop();" << endl <<
+ indent() << "oprot.writeStructEnd();" << endl;
+
+ indent_down();
+ out <<
+ indent() << "}" << endl <<
+ endl;
+}
+
+/**
+ * Generates a function to write all the fields of the struct,
+ * which is a function result. These fields are only written
+ * if they are set in the Isset array, and only one of them
+ * can be set at a time.
+ *
+ * @param tstruct The struct definition
+ */
+void t_javame_generator::generate_java_struct_result_writer(ofstream& out,
+ t_struct* tstruct) {
+ out <<
+ indent() << "public void write(TProtocol oprot) throws TException {" << endl;
+ indent_up();
+
+ string name = tstruct->get_name();
+ const vector<t_field*>& fields = tstruct->get_sorted_members();
+ vector<t_field*>::const_iterator f_iter;
+
+ indent(out) << "oprot.writeStructBegin(STRUCT_DESC);" << endl;
+
+ bool first = true;
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ if (first) {
+ first = false;
+ out <<
+ endl <<
+ indent() << "if ";
+ } else {
+ out << " else if ";
+ }
+
+ out << "(this." << generate_isset_check(*f_iter) << ") {" << endl;
+
+ indent_up();
+
+ indent(out) << "oprot.writeFieldBegin(" << constant_name((*f_iter)->get_name()) << "_FIELD_DESC);" << endl;
+
+ // Write field contents
+ generate_serialize_field(out, *f_iter, "this.");
+
+ // Write field closer
+ indent(out) <<
+ "oprot.writeFieldEnd();" << endl;
+
+ indent_down();
+ indent(out) << "}";
+ }
+ // Write the struct map
+ out <<
+ endl <<
+ indent() << "oprot.writeFieldStop();" << endl <<
+ indent() << "oprot.writeStructEnd();" << endl;
+
+ indent_down();
+ out <<
+ indent() << "}" << endl <<
+ endl;
+}
+
+void t_javame_generator::generate_reflection_getters(ostringstream& out, t_type* type, string field_name, string cap_name) {
+ indent(out) << "case " << constant_name(field_name) << ":" << endl;
+ indent_up();
+
+ if (type->is_base_type() && !type->is_string()) {
+ t_base_type* base_type = (t_base_type*)type;
+
+ indent(out) << "return new " << type_name(type, true, false) << "(" << (base_type->is_bool() ? "is" : "get") << cap_name << "());" << endl << endl;
+ } else {
+ indent(out) << "return get" << cap_name << "();" << endl << endl;
+ }
+
+ indent_down();
+}
+
+void t_javame_generator::generate_reflection_setters(ostringstream& out, t_type* type, string field_name, string cap_name) {
+ indent(out) << "case " << constant_name(field_name) << ":" << endl;
+ indent_up();
+ indent(out) << "if (value == null) {" << endl;
+ indent(out) << " unset" << get_cap_name(field_name) << "();" << endl;
+ indent(out) << "} else {" << endl;
+ indent(out) << " set" << cap_name << "((" << type_name(type, true, false) << ")value);" << endl;
+ indent(out) << "}" << endl;
+ indent(out) << "break;" << endl << endl;
+
+ indent_down();
+}
+
+void t_javame_generator::generate_generic_field_getters_setters(std::ofstream& out, t_struct* tstruct) {
+
+ std::ostringstream getter_stream;
+ std::ostringstream setter_stream;
+
+ // build up the bodies of both the getter and setter at once
+ const vector<t_field*>& fields = tstruct->get_members();
+ vector<t_field*>::const_iterator f_iter;
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ t_field* field = *f_iter;
+ t_type* type = get_true_type(field->get_type());
+ std::string field_name = field->get_name();
+ std::string cap_name = get_cap_name(field_name);
+
+ indent_up();
+ generate_reflection_setters(setter_stream, type, field_name, cap_name);
+ generate_reflection_getters(getter_stream, type, field_name, cap_name);
+ indent_down();
+ }
+
+}
+
+/**
+ * Generates a set of Java Bean boilerplate functions (setters, getters, etc.)
+ * for the given struct.
+ *
+ * @param tstruct The struct definition
+ */
+void t_javame_generator::generate_java_bean_boilerplate(ofstream& out,
+ t_struct* tstruct) {
+ const vector<t_field*>& fields = tstruct->get_members();
+ vector<t_field*>::const_iterator f_iter;
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ t_field* field = *f_iter;
+ t_type* type = get_true_type(field->get_type());
+ std::string field_name = field->get_name();
+ std::string cap_name = get_cap_name(field_name);
+
+ if (type->is_container()) {
+ // Method to return the size of the collection
+ indent(out) << "public int get" << cap_name;
+ out << get_cap_name("size() {") << endl;
+
+ indent_up();
+ indent(out) << "return (this." << field_name << " == null) ? 0 : " <<
+ "this." << field_name << ".size();" << endl;
+ indent_down();
+ indent(out) << "}" << endl << endl;
+ }
+
+ if (type->is_set() || type->is_list()) {
+
+ t_type* element_type;
+ if (type->is_set()) {
+ element_type = ((t_set*)type)->get_elem_type();
+ } else {
+ element_type = ((t_list*)type)->get_elem_type();
+ }
+
+ // Iterator getter for sets and lists
+ indent(out) << "public Enumeration get" << cap_name;
+ out << get_cap_name("Enumeration() {") << endl;
+
+ indent_up();
+ indent(out) << "return (this." << field_name << " == null) ? null : " <<
+ "this." << field_name << ".elements();" << endl;
+ indent_down();
+ indent(out) << "}" << endl << endl;
+
+ // Add to set or list, create if the set/list is null
+ indent(out);
+ out << "public void add" << get_cap_name("to");
+ out << cap_name << "(" << type_name(element_type) << " elem) {" << endl;
+
+ indent_up();
+ indent(out) << "if (this." << field_name << " == null) {" << endl;
+ indent_up();
+ indent(out) << "this." << field_name << " = new " << type_name(type, false, true) <<
+ "();" << endl;
+ indent_down();
+ indent(out) << "}" << endl;
+ if (type->is_set()) {
+ indent(out) << "this." << field_name << ".put(" <<
+ box_type(element_type, "elem") << ", " <<
+ box_type(element_type, "elem") << ");" << endl;
+ } else {
+ indent(out) << "this." << field_name << ".addElement(" <<
+ box_type(element_type, "elem") << ");" << endl;
+ }
+ indent_down();
+ indent(out) << "}" << endl << endl;
+
+ } else if (type->is_map()) {
+ // Put to map
+ t_type* key_type = ((t_map*)type)->get_key_type();
+ t_type* val_type = ((t_map*)type)->get_val_type();
+
+ indent(out);
+ out << "public void putTo"
+ << cap_name << "(" << type_name(key_type, true) << " key, "
+ << type_name(val_type, true) << " val) {" << endl;
+
+ indent_up();
+ indent(out) << "if (this." << field_name << " == null) {" << endl;
+ indent_up();
+ indent(out) << "this." << field_name << " = new " <<
+ type_name(type, false, true) << "();" << endl;
+ indent_down();
+ indent(out) << "}" << endl;
+ indent(out) << "this." << field_name << ".put(key, val);" << endl;
+ indent_down();
+ indent(out) << "}" << endl << endl;
+ }
+
+ // Simple getter
+ generate_java_doc(out, field);
+ indent(out) << "public " << type_name(type);
+ if (type->is_base_type() &&
+ ((t_base_type*)type)->get_base() == t_base_type::TYPE_BOOL) {
+ out << " is";
+ } else {
+ out << " get";
+ }
+ out << cap_name << "() {" << endl;
+ indent_up();
+ indent(out) << "return this." << field_name << ";" << endl;
+ indent_down();
+ indent(out) << "}" << endl << endl;
+
+ // Simple setter
+ generate_java_doc(out, field);
+ indent(out) << "public ";
+ out << "void";
+ out << " set" << cap_name << "(" << type_name(type) << " " << field_name << ") {" << endl;
+ indent_up();
+ indent(out) << "this." << field_name << " = " << field_name << ";" <<
+ endl;
+ generate_isset_set(out, field);
+
+ indent_down();
+ indent(out) << "}" << endl << endl;
+
+ // Unsetter
+ indent(out) << "public void unset" << cap_name << "() {" << endl;
+ indent_up();
+ if (type_can_be_null(type)) {
+ indent(out) << "this." << field_name << " = null;" << endl;
+ } else {
+ indent(out) << "__isset_vector[" << isset_field_id(field) << "] = false;" << endl;
+ }
+ indent_down();
+ indent(out) << "}" << endl << endl;
+
+ // isSet method
+ indent(out) << "/** Returns true if field " << field_name << " is set (has been asigned a value) and false otherwise */" << endl;
+ indent(out) << "public boolean is" << get_cap_name("set") << cap_name << "() {" << endl;
+ indent_up();
+ if (type_can_be_null(type)) {
+ indent(out) << "return this." << field_name << " != null;" << endl;
+ } else {
+ indent(out) << "return __isset_vector[" << isset_field_id(field) << "];" << endl;
+ }
+ indent_down();
+ indent(out) << "}" << endl << endl;
+
+ indent(out) << "public void set" << cap_name << get_cap_name("isSet") << "(boolean value) {" << endl;
+ indent_up();
+ if (type_can_be_null(type)) {
+ indent(out) << "if (!value) {" << endl;
+ indent(out) << " this." << field_name << " = null;" << endl;
+ indent(out) << "}" << endl;
+ } else {
+ indent(out) << "__isset_vector[" << isset_field_id(field) << "] = value;" << endl;
+ }
+ indent_down();
+ indent(out) << "}" << endl << endl;
+ }
+}
+
+/**
+ * Generates a toString() method for the given struct
+ *
+ * @param tstruct The struct definition
+ */
+void t_javame_generator::generate_java_struct_tostring(ofstream& out,
+ t_struct* tstruct) {
+ out << indent() << "public String toString() {" << endl;
+ indent_up();
+
+ out <<
+ indent() << "StringBuffer sb = new StringBuffer(\"" << tstruct->get_name() << "(\");" << endl;
+ out << indent() << "boolean first = true;" << endl << endl;
+
+ const vector<t_field*>& fields = tstruct->get_members();
+ vector<t_field*>::const_iterator f_iter;
+ bool first = true;
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ bool could_be_unset = (*f_iter)->get_req() == t_field::T_OPTIONAL;
+ if(could_be_unset) {
+ indent(out) << "if (" << generate_isset_check(*f_iter) << ") {" << endl;
+ indent_up();
+ }
+
+ t_field* field = (*f_iter);
+
+ if (!first) {
+ indent(out) << "if (!first) sb.append(\", \");" << endl;
+ }
+ indent(out) << "sb.append(\"" << (*f_iter)->get_name() << ":\");" << endl;
+ bool can_be_null = type_can_be_null(field->get_type());
+ if (can_be_null) {
+ indent(out) << "if (this." << (*f_iter)->get_name() << " == null) {" << endl;
+ indent(out) << " sb.append(\"null\");" << endl;
+ indent(out) << "} else {" << endl;
+ indent_up();
+ }
+
+ if (field->get_type()->is_base_type() && ((t_base_type*)(field->get_type()))->is_binary()) {
+ indent(out) << "TBaseHelper.toString(this." << field->get_name() << ", sb);" << endl;
+ } else {
+ indent(out) << "sb.append(this." << (*f_iter)->get_name() << ");" << endl;
+ }
+
+ if (can_be_null) {
+ indent_down();
+ indent(out) << "}" << endl;
+ }
+ indent(out) << "first = false;" << endl;
+
+ if(could_be_unset) {
+ indent_down();
+ indent(out) << "}" << endl;
+ }
+ first = false;
+ }
+ out <<
+ indent() << "sb.append(\")\");" << endl <<
+ indent() << "return sb.toString();" << endl;
+
+ indent_down();
+ indent(out) << "}" << endl <<
+ endl;
+}
+
+/**
+ * Returns a string with the java representation of the given thrift type
+ * (e.g. for the type struct it returns "TType.STRUCT")
+ */
+std::string t_javame_generator::get_java_type_string(t_type* type) {
+ if (type->is_list()){
+ return "TType.LIST";
+ } else if (type->is_map()) {
+ return "TType.MAP";
+ } else if (type->is_set()) {
+ return "TType.SET";
+ } else if (type->is_struct() || type->is_xception()) {
+ return "TType.STRUCT";
+ } else if (type->is_enum()) {
+ return "TType.ENUM";
+ } else if (type->is_typedef()) {
+ return get_java_type_string(((t_typedef*)type)->get_type());
+ } else if (type->is_base_type()) {
+ switch (((t_base_type*)type)->get_base()) {
+ case t_base_type::TYPE_VOID : return "TType.VOID"; break;
+ case t_base_type::TYPE_STRING : return "TType.STRING"; break;
+ case t_base_type::TYPE_BOOL : return "TType.BOOL"; break;
+ case t_base_type::TYPE_BYTE : return "TType.BYTE"; break;
+ case t_base_type::TYPE_I16 : return "TType.I16"; break;
+ case t_base_type::TYPE_I32 : return "TType.I32"; break;
+ case t_base_type::TYPE_I64 : return "TType.I64"; break;
+ case t_base_type::TYPE_DOUBLE : return "TType.DOUBLE"; break;
+ default : throw std::runtime_error("Unknown thrift type \"" + type->get_name() + "\" passed to t_javame_generator::get_java_type_string!"); break; // This should never happen!
+ }
+ } else {
+ throw std::runtime_error("Unknown thrift type \"" + type->get_name() + "\" passed to t_javame_generator::get_java_type_string!"); // This should never happen!
+ }
+}
+
+void t_javame_generator::generate_field_value_meta_data(std::ofstream& out, t_type* type){
+ out << endl;
+ indent_up();
+ indent_up();
+ if (type->is_struct()){
+ indent(out) << "new StructMetaData(TType.STRUCT, " << type_name(type) << ".class";
+ } else if (type->is_container()){
+ if (type->is_list()){
+ indent(out) << "new ListMetaData(TType.LIST, ";
+ t_type* elem_type = ((t_list*)type)->get_elem_type();
+ generate_field_value_meta_data(out, elem_type);
+ } else if (type->is_set()){
+ indent(out) << "new SetMetaData(TType.SET, ";
+ t_type* elem_type = ((t_list*)type)->get_elem_type();
+ generate_field_value_meta_data(out, elem_type);
+ } else{ // map
+ indent(out) << "new MapMetaData(TType.MAP, ";
+ t_type* key_type = ((t_map*)type)->get_key_type();
+ t_type* val_type = ((t_map*)type)->get_val_type();
+ generate_field_value_meta_data(out, key_type);
+ out << ", ";
+ generate_field_value_meta_data(out, val_type);
+ }
+ } else if (type->is_enum()) {
+ indent(out) << "new EnumMetaData(TType.ENUM, " << type_name(type) << ".class";
+ } else {
+ indent(out) << "new FieldValueMetaData(" << get_java_type_string(type);
+ if (type->is_typedef()) {
+ indent(out) << ", \"" << ((t_typedef*)type)->get_symbolic() << "\"";
+ }
+ }
+ out << ")";
+ indent_down();
+ indent_down();
+}
+
+
+/**
+ * Generates a thrift service. In C++, this comprises an entirely separate
+ * header and source file. The header file defines the methods and includes
+ * the data types defined in the main header file, and the implementation
+ * file contains implementations of the basic printer and default interfaces.
+ *
+ * @param tservice The service definition
+ */
+void t_javame_generator::generate_service(t_service* tservice) {
+ // Make output file
+ string f_service_name = package_dir_+"/"+service_name_+".java";
+ f_service_.open(f_service_name.c_str());
+
+ f_service_ <<
+ autogen_comment() <<
+ java_package() <<
+ java_type_imports() <<
+ java_thrift_imports();
+
+ f_service_ <<
+ "public class " << service_name_ << " {" << endl <<
+ endl;
+ indent_up();
+
+ // Generate the three main parts of the service
+ generate_service_interface(tservice);
+ generate_service_client(tservice);
+ generate_service_server(tservice);
+ generate_service_helpers(tservice);
+
+ indent_down();
+ f_service_ <<
+ "}" << endl;
+ f_service_.close();
+}
+
+/**
+ * Generates a service interface definition.
+ *
+ * @param tservice The service to generate a header definition for
+ */
+void t_javame_generator::generate_primitive_service_interface(t_service* tservice) {
+ f_service_ << indent() << "public interface Iface extends " <<
+ service_name_ << "Iface { }" << endl << endl;
+
+ string f_interface_name = package_dir_ + "/" + service_name_ + "Iface.java";
+ std::ofstream f_iface;
+ f_iface.open(f_interface_name.c_str());
+
+ string extends_iface = "";
+ if (tservice->get_extends() != NULL) {
+ extends_iface = " extends " + type_name(tservice->get_extends()) + "Iface";
+ }
+
+ f_iface <<
+ autogen_comment() <<
+ java_package() <<
+ java_type_imports() <<
+ java_thrift_imports();
+ generate_java_doc(f_iface, tservice);
+ f_iface <<
+ "public interface " << service_name_ << "Iface" << extends_iface << " {" << endl << endl;
+ vector<t_function*> functions = tservice->get_functions();
+ vector<t_function*>::iterator f_iter;
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ generate_java_doc(f_iface, *f_iter);
+ f_iface << " public " << function_signature(*f_iter) << ";" << endl << endl;
+ }
+ f_iface << "}" << endl << endl;
+}
+
+/**
+ * Generates a service interface definition.
+ *
+ * @param tservice The service to generate a header definition for
+ */
+void t_javame_generator::generate_service_interface(t_service* tservice) {
+ string extends = "";
+ string extends_iface = "";
+ if (tservice->get_extends() != NULL) {
+ extends = type_name(tservice->get_extends());
+ extends_iface = " extends " + extends + ".Iface";
+ }
+
+ generate_java_doc(f_service_, tservice);
+ f_service_ << indent() << "public interface Iface" << extends_iface <<
+ " {" << endl << endl;
+ indent_up();
+ vector<t_function*> functions = tservice->get_functions();
+ vector<t_function*>::iterator f_iter;
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ generate_java_doc(f_service_, *f_iter);
+ indent(f_service_) << "public " << function_signature(*f_iter) << ";" << endl << endl;
+ }
+ indent_down();
+ f_service_ << indent() << "}" << endl << endl;
+}
+
+/**
+ * Generates structs for all the service args and return types
+ *
+ * @param tservice The service
+ */
+void t_javame_generator::generate_service_helpers(t_service* tservice) {
+ vector<t_function*> functions = tservice->get_functions();
+ vector<t_function*>::iterator f_iter;
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ t_struct* ts = (*f_iter)->get_arglist();
+ generate_java_struct_definition(f_service_, ts, false, true);
+ generate_function_helpers(*f_iter);
+ }
+}
+
+/**
+ * Generates a service client definition.
+ *
+ * @param tservice The service to generate a server for.
+ */
+void t_javame_generator::generate_service_client(t_service* tservice) {
+ string extends = "";
+ string extends_client = "";
+ if (tservice->get_extends() != NULL) {
+ extends = type_name(tservice->get_extends());
+ extends_client = " extends " + extends + ".Client";
+ }
+
+ indent(f_service_) <<
+ "public static class Client" << extends_client << " implements TServiceClient, Iface {" << endl;
+ indent_up();
+
+ indent(f_service_) <<
+ "public Client(TProtocol prot)" << endl;
+ scope_up(f_service_);
+ indent(f_service_) <<
+ "this(prot, prot);" << endl;
+ scope_down(f_service_);
+ f_service_ << endl;
+
+ indent(f_service_) <<
+ "public Client(TProtocol iprot, TProtocol oprot)" << endl;
+ scope_up(f_service_);
+ if (extends.empty()) {
+ f_service_ <<
+ indent() << "iprot_ = iprot;" << endl <<
+ indent() << "oprot_ = oprot;" << endl;
+ } else {
+ f_service_ <<
+ indent() << "super(iprot, oprot);" << endl;
+ }
+ scope_down(f_service_);
+ f_service_ << endl;
+
+ if (extends.empty()) {
+ f_service_ <<
+ indent() << "protected TProtocol iprot_;" << endl <<
+ indent() << "protected TProtocol oprot_;" << endl <<
+ endl <<
+ indent() << "protected int seqid_;" << endl <<
+ endl;
+
+ indent(f_service_) <<
+ "public TProtocol getInputProtocol()" << endl;
+ scope_up(f_service_);
+ indent(f_service_) <<
+ "return this.iprot_;" << endl;
+ scope_down(f_service_);
+ f_service_ << endl;
+
+ indent(f_service_) <<
+ "public TProtocol getOutputProtocol()" << endl;
+ scope_up(f_service_);
+ indent(f_service_) <<
+ "return this.oprot_;" << endl;
+ scope_down(f_service_);
+ f_service_ << endl;
+
+ }
+
+ // Generate client method implementations
+ vector<t_function*> functions = tservice->get_functions();
+ vector<t_function*>::const_iterator f_iter;
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ string funname = (*f_iter)->get_name();
+
+ // Open function
+ indent(f_service_) <<
+ "public " << function_signature(*f_iter) << endl;
+ scope_up(f_service_);
+ indent(f_service_) <<
+ "send_" << funname << "(";
+
+ // Get the struct of function call params
+ t_struct* arg_struct = (*f_iter)->get_arglist();
+
+ // Declare the function arguments
+ const vector<t_field*>& fields = arg_struct->get_members();
+ vector<t_field*>::const_iterator fld_iter;
+ bool first = true;
+ for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
+ if (first) {
+ first = false;
+ } else {
+ f_service_ << ", ";
+ }
+ f_service_ << (*fld_iter)->get_name();
+ }
+ f_service_ << ");" << endl;
+
+ if (!(*f_iter)->is_oneway()) {
+ f_service_ << indent();
+ if (!(*f_iter)->get_returntype()->is_void()) {
+ f_service_ << "return ";
+ }
+ f_service_ <<
+ "recv_" << funname << "();" << endl;
+ }
+ scope_down(f_service_);
+ f_service_ << endl;
+
+ t_function send_function(g_type_void,
+ string("send_") + (*f_iter)->get_name(),
+ (*f_iter)->get_arglist());
+
+ string argsname = (*f_iter)->get_name() + "_args";
+
+ // Open function
+ indent(f_service_) <<
+ "public " << function_signature(&send_function) << endl;
+ scope_up(f_service_);
+
+ // Serialize the request
+ f_service_ <<
+ indent() << "oprot_.writeMessageBegin(new TMessage(\"" << funname << "\", TMessageType.CALL, ++seqid_));" << endl <<
+ indent() << argsname << " args = new " << argsname << "();" << endl;
+
+ for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
+ f_service_ <<
+ indent() << "args.set" << get_cap_name((*fld_iter)->get_name()) << "(" << (*fld_iter)->get_name() << ");" << endl;
+ }
+
+ f_service_ <<
+ indent() << "args.write(oprot_);" << endl <<
+ indent() << "oprot_.writeMessageEnd();" << endl <<
+ indent() << "oprot_.getTransport().flush();" << endl;
+
+ scope_down(f_service_);
+ f_service_ << endl;
+
+ if (!(*f_iter)->is_oneway()) {
+ string resultname = (*f_iter)->get_name() + "_result";
+
+ t_struct noargs(program_);
+ t_function recv_function((*f_iter)->get_returntype(),
+ string("recv_") + (*f_iter)->get_name(),
+ &noargs,
+ (*f_iter)->get_xceptions());
+ // Open function
+ indent(f_service_) <<
+ "public " << function_signature(&recv_function) << endl;
+ scope_up(f_service_);
+
+ f_service_ <<
+ indent() << "TMessage msg = iprot_.readMessageBegin();" << endl <<
+ indent() << "if (msg.type == TMessageType.EXCEPTION) {" << endl <<
+ indent() << " TApplicationException x = TApplicationException.read(iprot_);" << endl <<
+ indent() << " iprot_.readMessageEnd();" << endl <<
+ indent() << " throw x;" << endl <<
+ indent() << "}" << endl <<
+ indent() << "if (msg.seqid != seqid_) {" << endl <<
+ indent() << " throw new TApplicationException(TApplicationException.BAD_SEQUENCE_ID, \"" << (*f_iter)->get_name() << " failed: out of sequence response\");" << endl <<
+ indent() << "}" << endl <<
+ indent() << resultname << " result = new " << resultname << "();" << endl <<
+ indent() << "result.read(iprot_);" << endl <<
+ indent() << "iprot_.readMessageEnd();" << endl;
+
+ // Careful, only return _result if not a void function
+ if (!(*f_iter)->get_returntype()->is_void()) {
+ f_service_ <<
+ indent() << "if (result." << generate_isset_check("success") << ") {" << endl <<
+ indent() << " return result.success;" << endl <<
+ indent() << "}" << endl;
+ }
+
+ t_struct* xs = (*f_iter)->get_xceptions();
+ const std::vector<t_field*>& xceptions = xs->get_members();
+ vector<t_field*>::const_iterator x_iter;
+ for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
+ f_service_ <<
+ indent() << "if (result." << (*x_iter)->get_name() << " != null) {" << endl <<
+ indent() << " throw result." << (*x_iter)->get_name() << ";" << endl <<
+ indent() << "}" << endl;
+ }
+
+ // If you get here it's an exception, unless a void function
+ if ((*f_iter)->get_returntype()->is_void()) {
+ indent(f_service_) <<
+ "return;" << endl;
+ } else {
+ f_service_ <<
+ indent() << "throw new TApplicationException(TApplicationException.MISSING_RESULT, \"" << (*f_iter)->get_name() << " failed: unknown result\");" << endl;
+ }
+
+ // Close function
+ scope_down(f_service_);
+ f_service_ << endl;
+ }
+ }
+
+ indent_down();
+ indent(f_service_) <<
+ "}" << endl;
+}
+
+/**
+ * Generates a service server definition.
+ *
+ * @param tservice The service to generate a server for.
+ */
+void t_javame_generator::generate_service_server(t_service* tservice) {
+ // Generate the dispatch methods
+ vector<t_function*> functions = tservice->get_functions();
+ vector<t_function*>::iterator f_iter;
+
+ // Extends stuff
+ string extends = "";
+ string extends_processor = "";
+ if (tservice->get_extends() != NULL) {
+ extends = type_name(tservice->get_extends());
+ extends_processor = " extends " + extends + ".Processor";
+ }
+
+ // Generate the header portion
+ indent(f_service_) <<
+ "public static class Processor" << extends_processor << " implements TProcessor {" << endl;
+ indent_up();
+
+ indent(f_service_) <<
+ "public Processor(Iface iface)" << endl;
+ scope_up(f_service_);
+ if (!extends.empty()) {
+ f_service_ <<
+ indent() << "super(iface);" << endl;
+ }
+ f_service_ <<
+ indent() << "iface_ = iface;" << endl;
+
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ f_service_ <<
+ indent() << "processMap_.put(\"" << (*f_iter)->get_name() << "\", new " << (*f_iter)->get_name() << "());" << endl;
+ }
+
+ scope_down(f_service_);
+ f_service_ << endl;
+
+ if (extends.empty()) {
+ f_service_ <<
+ indent() << "protected static interface ProcessFunction {" << endl <<
+ indent() << " public void process(int seqid, TProtocol iprot, TProtocol oprot) throws TException;" << endl <<
+ indent() << "}" << endl <<
+ endl;
+ }
+
+ f_service_ <<
+ indent() << "private Iface iface_;" << endl;
+
+ if (extends.empty()) {
+ f_service_ <<
+ indent() << "protected final Hashtable processMap_ = new Hashtable();" << endl;
+ }
+
+ f_service_ << endl;
+
+ // Generate the server implementation
+ indent(f_service_) <<
+ "public boolean process(TProtocol iprot, TProtocol oprot) throws TException" << endl;
+ scope_up(f_service_);
+
+ f_service_ <<
+ indent() << "TMessage msg = iprot.readMessageBegin();" << endl;
+
+ // TODO(mcslee): validate message, was the seqid etc. legit?
+
+ f_service_ <<
+ indent() << "ProcessFunction fn = (ProcessFunction)processMap_.get(msg.name);" << endl <<
+ indent() << "if (fn == null) {" << endl <<
+ indent() << " TProtocolUtil.skip(iprot, TType.STRUCT);" << endl <<
+ indent() << " iprot.readMessageEnd();" << endl <<
+ indent() << " TApplicationException x = new TApplicationException(TApplicationException.UNKNOWN_METHOD, \"Invalid method name: '\"+msg.name+\"'\");" << endl <<
+ indent() << " oprot.writeMessageBegin(new TMessage(msg.name, TMessageType.EXCEPTION, msg.seqid));" << endl <<
+ indent() << " x.write(oprot);" << endl <<
+ indent() << " oprot.writeMessageEnd();" << endl <<
+ indent() << " oprot.getTransport().flush();" << endl <<
+ indent() << " return true;" << endl <<
+ indent() << "}" << endl <<
+ indent() << "fn.process(msg.seqid, iprot, oprot);" << endl;
+
+ f_service_ <<
+ indent() << "return true;" << endl;
+
+ scope_down(f_service_);
+ f_service_ << endl;
+
+ // Generate the process subfunctions
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ generate_process_function(tservice, *f_iter);
+ }
+
+ indent_down();
+ indent(f_service_) <<
+ "}" << endl <<
+ endl;
+}
+
+/**
+ * Generates a struct and helpers for a function.
+ *
+ * @param tfunction The function
+ */
+void t_javame_generator::generate_function_helpers(t_function* tfunction) {
+ if (tfunction->is_oneway()) {
+ return;
+ }
+
+ t_struct result(program_, tfunction->get_name() + "_result");
+ t_field success(tfunction->get_returntype(), "success", 0);
+ if (!tfunction->get_returntype()->is_void()) {
+ result.append(&success);
+ }
+
+ t_struct* xs = tfunction->get_xceptions();
+ const vector<t_field*>& fields = xs->get_members();
+ vector<t_field*>::const_iterator f_iter;
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ result.append(*f_iter);
+ }
+
+ generate_java_struct_definition(f_service_, &result, false, true, true);
+}
+
+/**
+ * Generates a process function definition.
+ *
+ * @param tfunction The function to write a dispatcher for
+ */
+void t_javame_generator::generate_process_function(t_service* tservice,
+ t_function* tfunction) {
+ // Open class
+ indent(f_service_) <<
+ "private class " << tfunction->get_name() << " implements ProcessFunction {" << endl;
+ indent_up();
+
+ // Open function
+ indent(f_service_) <<
+ "public void process(int seqid, TProtocol iprot, TProtocol oprot) throws TException" << endl;
+ scope_up(f_service_);
+
+ string argsname = tfunction->get_name() + "_args";
+ string resultname = tfunction->get_name() + "_result";
+
+ f_service_ <<
+ indent() << argsname << " args = new " << argsname << "();" << endl <<
+ indent() << "try {" << endl;
+ indent_up();
+ f_service_ <<
+ indent() << "args.read(iprot);" << endl;
+ indent_down();
+ f_service_ <<
+ indent() << "} catch (TProtocolException e) {" << endl;
+ indent_up();
+ f_service_ <<
+ indent() << "iprot.readMessageEnd();" << endl <<
+ indent() << "TApplicationException x = new TApplicationException(TApplicationException.PROTOCOL_ERROR, e.getMessage());" << endl <<
+ indent() << "oprot.writeMessageBegin(new TMessage(\"" << tfunction->get_name() << "\", TMessageType.EXCEPTION, seqid));" << endl <<
+ indent() << "x.write(oprot);" << endl <<
+ indent() << "oprot.writeMessageEnd();" << endl <<
+ indent() << "oprot.getTransport().flush();" << endl <<
+ indent() << "return;" << endl;
+ indent_down();
+ f_service_ << indent() << "}" << endl;
+ f_service_ <<
+ indent() << "iprot.readMessageEnd();" << endl;
+
+ t_struct* xs = tfunction->get_xceptions();
+ const std::vector<t_field*>& xceptions = xs->get_members();
+ vector<t_field*>::const_iterator x_iter;
+
+ // Declare result for non oneway function
+ if (!tfunction->is_oneway()) {
+ f_service_ <<
+ indent() << resultname << " result = new " << resultname << "();" << endl;
+ }
+
+ // Try block for a function with exceptions
+ if (xceptions.size() > 0) {
+ f_service_ <<
+ indent() << "try {" << endl;
+ indent_up();
+ }
+
+ // Generate the function call
+ t_struct* arg_struct = tfunction->get_arglist();
+ const std::vector<t_field*>& fields = arg_struct->get_members();
+ vector<t_field*>::const_iterator f_iter;
+
+ f_service_ << indent();
+ if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void()) {
+ f_service_ << "result.success = ";
+ }
+ f_service_ <<
+ "iface_." << tfunction->get_name() << "(";
+ bool first = true;
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ if (first) {
+ first = false;
+ } else {
+ f_service_ << ", ";
+ }
+ f_service_ << "args." << (*f_iter)->get_name();
+ }
+ f_service_ << ");" << endl;
+
+ // Set isset on success field
+ if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void() && !type_can_be_null(tfunction->get_returntype())) {
+ f_service_ <<
+ indent() << "result.set" << get_cap_name("success") << get_cap_name("isSet") << "(true);" << endl;
+ }
+
+ if (!tfunction->is_oneway() && xceptions.size() > 0) {
+ indent_down();
+ f_service_ << indent() << "}";
+ for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
+ f_service_ << " catch (" << type_name((*x_iter)->get_type(), false, false) << " " << (*x_iter)->get_name() << ") {" << endl;
+ if (!tfunction->is_oneway()) {
+ indent_up();
+ f_service_ <<
+ indent() << "result." << (*x_iter)->get_name() << " = " << (*x_iter)->get_name() << ";" << endl;
+ indent_down();
+ f_service_ << indent() << "}";
+ } else {
+ f_service_ << "}";
+ }
+ }
+ f_service_ << " catch (Throwable th) {" << endl;
+ indent_up();
+ f_service_ <<
+ indent() << "TApplicationException x = new TApplicationException(TApplicationException.INTERNAL_ERROR, \"Internal error processing " << tfunction->get_name() << "\");" << endl <<
+ indent() << "oprot.writeMessageBegin(new TMessage(\"" << tfunction->get_name() << "\", TMessageType.EXCEPTION, seqid));" << endl <<
+ indent() << "x.write(oprot);" << endl <<
+ indent() << "oprot.writeMessageEnd();" << endl <<
+ indent() << "oprot.getTransport().flush();" << endl <<
+ indent() << "return;" << endl;
+ indent_down();
+ f_service_ << indent() << "}" << endl;
+ }
+
+ // Shortcut out here for oneway functions
+ if (tfunction->is_oneway()) {
+ f_service_ <<
+ indent() << "return;" << endl;
+ scope_down(f_service_);
+
+ // Close class
+ indent_down();
+ f_service_ <<
+ indent() << "}" << endl <<
+ endl;
+ return;
+ }
+
+ f_service_ <<
+ indent() << "oprot.writeMessageBegin(new TMessage(\"" << tfunction->get_name() << "\", TMessageType.REPLY, seqid));" << endl <<
+ indent() << "result.write(oprot);" << endl <<
+ indent() << "oprot.writeMessageEnd();" << endl <<
+ indent() << "oprot.getTransport().flush();" << endl;
+
+ // Close function
+ scope_down(f_service_);
+ f_service_ << endl;
+
+ // Close class
+ indent_down();
+ f_service_ <<
+ indent() << "}" << endl <<
+ endl;
+}
+
+/**
+ * Deserializes a field of any type.
+ *
+ * @param tfield The field
+ * @param prefix The variable name or container for this field
+ */
+void t_javame_generator::generate_deserialize_field(ofstream& out,
+ t_field* tfield,
+ string prefix) {
+ t_type* type = get_true_type(tfield->get_type());
+
+ if (type->is_void()) {
+ throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " +
+ prefix + tfield->get_name();
+ }
+
+ string name = prefix + tfield->get_name();
+
+ if (type->is_struct() || type->is_xception()) {
+ generate_deserialize_struct(out,
+ (t_struct*)type,
+ name);
+ } else if (type->is_container()) {
+ generate_deserialize_container(out, type, name);
+ } else if (type->is_base_type()) {
+ indent(out) << name << " = iprot.";
+
+ t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
+ switch (tbase) {
+ case t_base_type::TYPE_VOID:
+ throw "compiler error: cannot serialize void field in a struct: " +
+ name;
+ break;
+ case t_base_type::TYPE_STRING:
+ if (!((t_base_type*)type)->is_binary()) {
+ out << "readString();";
+ } else {
+ out << "readBinary();";
+ }
+ break;
+ case t_base_type::TYPE_BOOL:
+ out << "readBool();";
+ break;
+ case t_base_type::TYPE_BYTE:
+ out << "readByte();";
+ break;
+ case t_base_type::TYPE_I16:
+ out << "readI16();";
+ break;
+ case t_base_type::TYPE_I32:
+ out << "readI32();";
+ break;
+ case t_base_type::TYPE_I64:
+ out << "readI64();";
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ out << "readDouble();";
+ break;
+ default:
+ throw "compiler error: no Java name for base type " + t_base_type::t_base_name(tbase);
+ }
+ out << endl;
+ } else if (type->is_enum()) {
+ indent(out) << name << " = " << type_name(tfield->get_type(), true, false) + ".findByValue(iprot.readI32());" << endl;
+ } else {
+ printf("DO NOT KNOW HOW TO DESERIALIZE FIELD '%s' TYPE '%s'\n",
+ tfield->get_name().c_str(), type_name(type).c_str());
+ }
+}
+
+/**
+ * Generates an unserializer for a struct, invokes read()
+ */
+void t_javame_generator::generate_deserialize_struct(ofstream& out,
+ t_struct* tstruct,
+ string prefix) {
+ out <<
+ indent() << prefix << " = new " << type_name(tstruct) << "();" << endl <<
+ indent() << prefix << ".read(iprot);" << endl;
+}
+
+/**
+ * Deserializes a container by reading its size and then iterating
+ */
+void t_javame_generator::generate_deserialize_container(ofstream& out,
+ t_type* ttype,
+ string prefix) {
+ scope_up(out);
+
+ string obj;
+
+ if (ttype->is_map()) {
+ obj = tmp("_map");
+ } else if (ttype->is_set()) {
+ obj = tmp("_set");
+ } else if (ttype->is_list()) {
+ obj = tmp("_list");
+ }
+
+ // Declare variables, read header
+ if (ttype->is_map()) {
+ indent(out) << "TMap " << obj << " = iprot.readMapBegin();" << endl;
+ } else if (ttype->is_set()) {
+ indent(out) << "TSet " << obj << " = iprot.readSetBegin();" << endl;
+ } else if (ttype->is_list()) {
+ indent(out) << "TList " << obj << " = iprot.readListBegin();" << endl;
+ }
+
+ indent(out)
+ << prefix << " = new " << type_name(ttype, false, true)
+ // size the collection correctly
+ << "("
+ << (ttype->is_list() ? "" : "2*" )
+ << obj << ".size"
+ << ");" << endl;
+
+ // For loop iterates over elements
+ string i = tmp("_i");
+ indent(out) <<
+ "for (int " << i << " = 0; " <<
+ i << " < " << obj << ".size" << "; " <<
+ "++" << i << ")" << endl;
+
+ scope_up(out);
+
+ if (ttype->is_map()) {
+ generate_deserialize_map_element(out, (t_map*)ttype, prefix);
+ } else if (ttype->is_set()) {
+ generate_deserialize_set_element(out, (t_set*)ttype, prefix);
+ } else if (ttype->is_list()) {
+ generate_deserialize_list_element(out, (t_list*)ttype, prefix);
+ }
+
+ scope_down(out);
+
+ // Read container end
+ if (ttype->is_map()) {
+ indent(out) << "iprot.readMapEnd();" << endl;
+ } else if (ttype->is_set()) {
+ indent(out) << "iprot.readSetEnd();" << endl;
+ } else if (ttype->is_list()) {
+ indent(out) << "iprot.readListEnd();" << endl;
+ }
+
+ scope_down(out);
+}
+
+
+/**
+ * Generates code to deserialize a map
+ */
+void t_javame_generator::generate_deserialize_map_element(ofstream& out,
+ t_map* tmap,
+ string prefix) {
+ string key = tmp("_key");
+ string val = tmp("_val");
+ t_field fkey(tmap->get_key_type(), key);
+ t_field fval(tmap->get_val_type(), val);
+
+ indent(out) <<
+ declare_field(&fkey) << endl;
+ indent(out) <<
+ declare_field(&fval) << endl;
+
+ generate_deserialize_field(out, &fkey);
+ generate_deserialize_field(out, &fval);
+
+ indent(out) <<
+ prefix << ".put(" <<
+ box_type(tmap->get_key_type(), key) << ", " <<
+ box_type(tmap->get_val_type(), val) << ");" << endl;
+}
+
+/**
+ * Deserializes a set element
+ */
+void t_javame_generator::generate_deserialize_set_element(ofstream& out,
+ t_set* tset,
+ string prefix) {
+ string elem = tmp("_elem");
+ t_field felem(tset->get_elem_type(), elem);
+
+ indent(out) <<
+ declare_field(&felem) << endl;
+
+ generate_deserialize_field(out, &felem);
+
+ indent(out) <<
+ prefix << ".put(" <<
+ box_type(tset->get_elem_type(), elem) << ", " <<
+ box_type(tset->get_elem_type(), elem) << ");" << endl;
+}
+
+/**
+ * Deserializes a list element
+ */
+void t_javame_generator::generate_deserialize_list_element(ofstream& out,
+ t_list* tlist,
+ string prefix) {
+ string elem = tmp("_elem");
+ t_field felem(tlist->get_elem_type(), elem);
+
+ indent(out) <<
+ declare_field(&felem) << endl;
+
+ generate_deserialize_field(out, &felem);
+
+ indent(out) <<
+ prefix << ".addElement(" <<
+ box_type(tlist->get_elem_type(), elem) << ");" << endl;
+}
+
+
+/**
+ * Serializes a field of any type.
+ *
+ * @param tfield The field to serialize
+ * @param prefix Name to prepend to field name
+ */
+void t_javame_generator::generate_serialize_field(ofstream& out,
+ t_field* tfield,
+ string prefix) {
+ t_type* type = get_true_type(tfield->get_type());
+
+ // Do nothing for void types
+ if (type->is_void()) {
+ throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " +
+ prefix + tfield->get_name();
+ }
+
+ if (type->is_struct() || type->is_xception()) {
+ generate_serialize_struct(out,
+ (t_struct*)type,
+ prefix + tfield->get_name());
+ } else if (type->is_container()) {
+ generate_serialize_container(out,
+ type,
+ prefix + tfield->get_name());
+ } else if (type->is_enum()){
+ indent(out) << "oprot.writeI32(" << prefix + tfield->get_name() << ".getValue());" << endl;
+ } else if (type->is_base_type()) {
+ string name = prefix + tfield->get_name();
+ indent(out) <<
+ "oprot.";
+
+ if (type->is_base_type()) {
+ t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
+ switch (tbase) {
+ case t_base_type::TYPE_VOID:
+ throw
+ "compiler error: cannot serialize void field in a struct: " + name;
+ break;
+ case t_base_type::TYPE_STRING:
+ if (((t_base_type*)type)->is_binary()) {
+ out << "writeBinary(" << name << ");";
+ } else {
+ out << "writeString(" << name << ");";
+ }
+ break;
+ case t_base_type::TYPE_BOOL:
+ out << "writeBool(" << name << ");";
+ break;
+ case t_base_type::TYPE_BYTE:
+ out << "writeByte(" << name << ");";
+ break;
+ case t_base_type::TYPE_I16:
+ out << "writeI16(" << name << ");";
+ break;
+ case t_base_type::TYPE_I32:
+ out << "writeI32(" << name << ");";
+ break;
+ case t_base_type::TYPE_I64:
+ out << "writeI64(" << name << ");";
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ out << "writeDouble(" << name << ");";
+ break;
+ default:
+ throw "compiler error: no Java name for base type " + t_base_type::t_base_name(tbase);
+ }
+ } else if (type->is_enum()) {
+ out << "writeI32(" << name << ");";
+ }
+ out << endl;
+ } else {
+ printf("DO NOT KNOW HOW TO SERIALIZE FIELD '%s%s' TYPE '%s'\n",
+ prefix.c_str(),
+ tfield->get_name().c_str(),
+ type_name(type).c_str());
+ }
+}
+
+/**
+ * Serializes all the members of a struct.
+ *
+ * @param tstruct The struct to serialize
+ * @param prefix String prefix to attach to all fields
+ */
+void t_javame_generator::generate_serialize_struct(ofstream& out,
+ t_struct* tstruct,
+ string prefix) {
+ out <<
+ indent() << prefix << ".write(oprot);" << endl;
+}
+
+/**
+ * Serializes a container by writing its size then the elements.
+ *
+ * @param ttype The type of container
+ * @param prefix String prefix for fields
+ */
+void t_javame_generator::generate_serialize_container(ofstream& out,
+ t_type* ttype,
+ string prefix) {
+ scope_up(out);
+
+ if (ttype->is_map()) {
+ indent(out) <<
+ "oprot.writeMapBegin(new TMap(" <<
+ type_to_enum(((t_map*)ttype)->get_key_type()) << ", " <<
+ type_to_enum(((t_map*)ttype)->get_val_type()) << ", " <<
+ prefix << ".size()));" << endl;
+ } else if (ttype->is_set()) {
+ indent(out) <<
+ "oprot.writeSetBegin(new TSet(" <<
+ type_to_enum(((t_set*)ttype)->get_elem_type()) << ", " <<
+ prefix << ".size()));" << endl;
+ } else if (ttype->is_list()) {
+ indent(out) <<
+ "oprot.writeListBegin(new TList(" <<
+ type_to_enum(((t_list*)ttype)->get_elem_type()) << ", " <<
+ prefix << ".size()));" << endl;
+ }
+
+ string iter = tmp("_iter");
+ if (ttype->is_map()) {
+ string enumer = iter + "_enum";
+ string key_type = type_name(((t_map*)ttype)->get_key_type(), true, false);
+ indent(out) <<
+ "for (Enumeration " << enumer << " = " << prefix << ".keys(); " << enumer << ".hasMoreElements(); ) ";
+ scope_up(out);
+ indent(out)
+ << key_type << " " << iter << " = (" << key_type << ")" << enumer << ".nextElement();" << endl;
+ } else if (ttype->is_set()) {
+ string enumer = iter + "_enum";
+ string ele_type = type_name(((t_list*)ttype)->get_elem_type(), true);
+ indent(out) <<
+ "for (Enumeration " << enumer << " = " << prefix << ".keys(); " << enumer << ".hasMoreElements(); ) ";
+ scope_up(out);
+ indent(out)
+ << ele_type << " " << iter << " = ("
+ << ele_type << ")" << enumer << ".nextElement();" << endl;
+ } else if (ttype->is_list()) {
+ string enumer = iter + "_enum";
+ indent(out) <<
+ "for (Enumeration " << enumer << " = " << prefix << ".elements(); " << enumer << ".hasMoreElements(); ) ";
+ scope_up(out);
+ string ele_type = type_name(((t_list*)ttype)->get_elem_type(), true);
+ indent(out)
+ << ele_type << " " << iter << " = ("
+ << ele_type << ")" << enumer << ".nextElement();" << endl;
+ }
+
+ if (ttype->is_map()) {
+ generate_serialize_map_element(out, (t_map*)ttype, iter, prefix);
+ } else if (ttype->is_set()) {
+ generate_serialize_set_element(out, (t_set*)ttype, iter);
+ } else if (ttype->is_list()) {
+ generate_serialize_list_element(out, (t_list*)ttype, iter);
+ }
+ scope_down(out);
+
+ if (ttype->is_map()) {
+ indent(out) <<
+ "oprot.writeMapEnd();" << endl;
+ } else if (ttype->is_set()) {
+ indent(out) <<
+ "oprot.writeSetEnd();" << endl;
+ } else if (ttype->is_list()) {
+ indent(out) <<
+ "oprot.writeListEnd();" << endl;
+ }
+
+ scope_down(out);
+}
+
+/**
+ * Serializes the members of a map.
+ */
+void t_javame_generator::generate_serialize_map_element(ofstream& out,
+ t_map* tmap,
+ string iter,
+ string map) {
+ t_field kfield(tmap->get_key_type(), iter);
+ generate_serialize_field(out, &kfield, "");
+ string val_type = type_name(tmap->get_val_type(), true, false);
+ t_field vfield(tmap->get_val_type(), "(" + val_type + ")" + map + ".get(" + iter + ")");
+ generate_serialize_field(out, &vfield, "");
+}
+
+/**
+ * Serializes the members of a set.
+ */
+void t_javame_generator::generate_serialize_set_element(ofstream& out,
+ t_set* tset,
+ string iter) {
+ t_field efield(tset->get_elem_type(), iter);
+ generate_serialize_field(out, &efield, "");
+}
+
+/**
+ * Serializes the members of a list.
+ */
+void t_javame_generator::generate_serialize_list_element(ofstream& out,
+ t_list* tlist,
+ string iter) {
+ t_field efield(tlist->get_elem_type(), iter);
+ generate_serialize_field(out, &efield, "");
+}
+
+/**
+ * Returns a Java type name
+ *
+ * @param ttype The type
+ * @param container Is the type going inside a container?
+ * @return Java type name, i.e. Vector
+ */
+string t_javame_generator::type_name(t_type* ttype, bool in_container, bool in_init, bool skip_generic) {
+ // In Java typedefs are just resolved to their real type
+ ttype = get_true_type(ttype);
+ string prefix;
+
+ if (ttype->is_base_type()) {
+ return base_type_name((t_base_type*)ttype, in_container);
+ } else if (ttype->is_map()) {
+ return "Hashtable";
+ } else if (ttype->is_set()) {
+ return "Hashtable";
+ } else if (ttype->is_list()) {
+ return "Vector";
+ }
+
+ // Check for namespacing
+ t_program* program = ttype->get_program();
+ if (program != NULL && program != program_) {
+ string package = program->get_namespace("java");
+ if (!package.empty()) {
+ return package + "." + ttype->get_name();
+ }
+ }
+
+ return ttype->get_name();
+}
+
+/**
+ * Returns the C++ type that corresponds to the thrift type.
+ *
+ * @param tbase The base type
+ * @param container Is it going in a Java container?
+ */
+string t_javame_generator::base_type_name(t_base_type* type,
+ bool in_container) {
+ t_base_type::t_base tbase = type->get_base();
+
+ switch (tbase) {
+ case t_base_type::TYPE_VOID:
+ return "void";
+ case t_base_type::TYPE_STRING:
+ if (!type->is_binary()) {
+ return "String";
+ } else {
+ return "byte[]";
+ }
+ case t_base_type::TYPE_BOOL:
+ return (in_container ? "Boolean" : "boolean");
+ case t_base_type::TYPE_BYTE:
+ return (in_container ? "Byte" : "byte");
+ case t_base_type::TYPE_I16:
+ return (in_container ? "Short" : "short");
+ case t_base_type::TYPE_I32:
+ return (in_container ? "Integer" : "int");
+ case t_base_type::TYPE_I64:
+ return (in_container ? "Long" : "long");
+ case t_base_type::TYPE_DOUBLE:
+ return (in_container ? "Double" : "double");
+ default:
+ throw "compiler error: no C++ name for base type " + t_base_type::t_base_name(tbase);
+ }
+}
+
+/**
+ * Declares a field, which may include initialization as necessary.
+ *
+ * @param ttype The type
+ */
+string t_javame_generator::declare_field(t_field* tfield, bool init) {
+ // TODO(mcslee): do we ever need to initialize the field?
+ string result = type_name(tfield->get_type()) + " " + tfield->get_name();
+ if (init) {
+ t_type* ttype = get_true_type(tfield->get_type());
+ if (ttype->is_base_type() && tfield->get_value() != NULL) {
+ ofstream dummy;
+ result += " = " + render_const_value(dummy, tfield->get_name(), ttype, tfield->get_value());
+ } else if (ttype->is_base_type()) {
+ t_base_type::t_base tbase = ((t_base_type*)ttype)->get_base();
+ switch (tbase) {
+ case t_base_type::TYPE_VOID:
+ throw "NO T_VOID CONSTRUCT";
+ case t_base_type::TYPE_STRING:
+ result += " = null";
+ break;
+ case t_base_type::TYPE_BOOL:
+ result += " = false";
+ break;
+ case t_base_type::TYPE_BYTE:
+ case t_base_type::TYPE_I16:
+ case t_base_type::TYPE_I32:
+ case t_base_type::TYPE_I64:
+ result += " = 0";
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ result += " = (double)0";
+ break;
+ }
+
+ } else if (ttype->is_enum()) {
+ result += " = 0";
+ } else if (ttype->is_container()) {
+ result += " = new " + type_name(ttype, false, true) + "()";
+ } else {
+ result += " = new " + type_name(ttype, false, true) + "()";;
+ }
+ }
+ return result + ";";
+}
+
+/**
+ * Renders a function signature of the form 'type name(args)'
+ *
+ * @param tfunction Function definition
+ * @return String of rendered function definition
+ */
+string t_javame_generator::function_signature(t_function* tfunction,
+ string prefix) {
+ t_type* ttype = tfunction->get_returntype();
+ std::string result =
+ type_name(ttype) + " " + prefix + tfunction->get_name() + "(" + argument_list(tfunction->get_arglist()) + ") throws ";
+ t_struct* xs = tfunction->get_xceptions();
+ const std::vector<t_field*>& xceptions = xs->get_members();
+ vector<t_field*>::const_iterator x_iter;
+ for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
+ result += type_name((*x_iter)->get_type(), false, false) + ", ";
+ }
+ result += "TException";
+ return result;
+}
+
+/**
+ * Renders a comma separated field list, with type names
+ */
+string t_javame_generator::argument_list(t_struct* tstruct, bool include_types) {
+ string result = "";
+
+ const vector<t_field*>& fields = tstruct->get_members();
+ vector<t_field*>::const_iterator f_iter;
+ bool first = true;
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ if (first) {
+ first = false;
+ } else {
+ result += ", ";
+ }
+ if (include_types) {
+ result += type_name((*f_iter)->get_type()) + " ";
+ }
+ result += (*f_iter)->get_name();
+ }
+ return result;
+}
+
+/**
+ * Converts the parse type to a C++ enum string for the given type.
+ */
+string t_javame_generator::type_to_enum(t_type* type) {
+ type = get_true_type(type);
+
+ if (type->is_base_type()) {
+ t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
+ switch (tbase) {
+ case t_base_type::TYPE_VOID:
+ throw "NO T_VOID CONSTRUCT";
+ case t_base_type::TYPE_STRING:
+ return "TType.STRING";
+ case t_base_type::TYPE_BOOL:
+ return "TType.BOOL";
+ case t_base_type::TYPE_BYTE:
+ return "TType.BYTE";
+ case t_base_type::TYPE_I16:
+ return "TType.I16";
+ case t_base_type::TYPE_I32:
+ return "TType.I32";
+ case t_base_type::TYPE_I64:
+ return "TType.I64";
+ case t_base_type::TYPE_DOUBLE:
+ return "TType.DOUBLE";
+ }
+ } else if (type->is_enum()) {
+ return "TType.I32";
+ } else if (type->is_struct() || type->is_xception()) {
+ return "TType.STRUCT";
+ } else if (type->is_map()) {
+ return "TType.MAP";
+ } else if (type->is_set()) {
+ return "TType.SET";
+ } else if (type->is_list()) {
+ return "TType.LIST";
+ }
+
+ throw "INVALID TYPE IN type_to_enum: " + type->get_name();
+}
+
+/**
+ * Applies the correct style to a string based on the value of nocamel_style_
+ */
+std::string t_javame_generator::get_cap_name(std::string name){
+ name[0] = toupper(name[0]);
+ return name;
+}
+
+string t_javame_generator::constant_name(string name) {
+ string constant_name;
+
+ bool is_first = true;
+ bool was_previous_char_upper = false;
+ for (string::iterator iter = name.begin(); iter != name.end(); ++iter) {
+ string::value_type character = (*iter);
+
+ bool is_upper = isupper(character);
+
+ if (is_upper && !is_first && !was_previous_char_upper) {
+ constant_name += '_';
+ }
+ constant_name += toupper(character);
+
+ is_first = false;
+ was_previous_char_upper = is_upper;
+ }
+
+ return constant_name;
+}
+
+void t_javame_generator::generate_java_docstring_comment(ofstream &out, string contents) {
+ generate_docstring_comment(out,
+ "/**\n",
+ " * ", contents,
+ " */\n");
+}
+
+void t_javame_generator::generate_java_doc(ofstream &out,
+ t_field* field) {
+ if (field->get_type()->is_enum()) {
+ string combined_message = field->get_doc() + "\n@see " + get_enum_class_name(field->get_type());
+ generate_java_docstring_comment(out, combined_message);
+ } else {
+ generate_java_doc(out, (t_doc*)field);
+ }
+}
+
+/**
+ * Emits a JavaDoc comment if the provided object has a doc in Thrift
+ */
+void t_javame_generator::generate_java_doc(ofstream &out,
+ t_doc* tdoc) {
+ if (tdoc->has_doc()) {
+ generate_java_docstring_comment(out, tdoc->get_doc());
+ }
+}
+
+/**
+ * Emits a JavaDoc comment if the provided function object has a doc in Thrift
+ */
+void t_javame_generator::generate_java_doc(ofstream &out,
+ t_function* tfunction) {
+ if (tfunction->has_doc()) {
+ stringstream ss;
+ ss << tfunction->get_doc();
+ const vector<t_field*>& fields = tfunction->get_arglist()->get_members();
+ vector<t_field*>::const_iterator p_iter;
+ for (p_iter = fields.begin(); p_iter != fields.end(); ++p_iter) {
+ t_field* p = *p_iter;
+ ss << "\n@param " << p->get_name();
+ if (p->has_doc()) {
+ ss << " " << p->get_doc();
+ }
+ }
+ generate_docstring_comment(out,
+ "/**\n",
+ " * ", ss.str(),
+ " */\n");
+ }
+}
+
+void t_javame_generator::generate_deep_copy_container(ofstream &out, std::string source_name_p1, std::string source_name_p2,
+ std::string result_name, t_type* type) {
+
+ t_container* container = (t_container*)type;
+ std::string source_name;
+ if (source_name_p2 == "")
+ source_name = source_name_p1;
+ else
+ source_name = source_name_p1 + "." + source_name_p2;
+
+ indent(out) << type_name(type, true, false) << " " << result_name << " = new " << type_name(container, false, true) << "();" << endl;
+
+ std::string iterator_element_name = source_name_p1 + "_element";
+ std::string enumeration_name = source_name_p1 + "_enum";
+ std::string result_element_name = result_name + "_copy";
+
+ if(container->is_map()) {
+ t_type* key_type = ((t_map*)container)->get_key_type();
+ t_type* val_type = ((t_map*)container)->get_val_type();
+
+ indent(out) <<
+ "for (Enumeration " << enumeration_name << " = " << source_name << ".keys(); " << enumeration_name << ".hasMoreElements(); ) {" << endl;
+ indent_up();
+
+ out << endl;
+
+ indent(out) << type_name(key_type, true, false) << " " << iterator_element_name << "_key = (" << type_name(key_type, true, false) << ")" << enumeration_name << ".nextElement();" << endl;
+ indent(out) << type_name(val_type, true, false) << " " << iterator_element_name << "_value = ("
+ << type_name(val_type, true, false) << ")" << source_name << ".get(" << iterator_element_name << "_key);" << endl;
+
+ out << endl;
+
+ if (key_type->is_container()) {
+ generate_deep_copy_container(out, iterator_element_name + "_key", "", result_element_name + "_key", key_type);
+ } else {
+ indent(out) << type_name(key_type, true, false) << " " << result_element_name << "_key = ";
+ generate_deep_copy_non_container(out, iterator_element_name + "_key", result_element_name + "_key", key_type);
+ out << ";" << endl;
+ }
+
+ out << endl;
+
+ if (val_type->is_container()) {
+ generate_deep_copy_container(out, iterator_element_name + "_value", "", result_element_name + "_value", val_type);
+ } else {
+ indent(out) << type_name(val_type, true, false) << " " << result_element_name << "_value = ";
+ generate_deep_copy_non_container(out, iterator_element_name + "_value", result_element_name + "_value", val_type);
+ out << ";" << endl;
+ }
+
+ out << endl;
+
+ indent(out) << result_name << ".put(" << result_element_name << "_key, " << result_element_name << "_value);" << endl;
+
+ indent_down();
+ indent(out) << "}" << endl;
+
+ } else {
+ t_type* elem_type;
+
+ if (container->is_set()) {
+ elem_type = ((t_set*)container)->get_elem_type();
+ } else {
+ elem_type = ((t_list*)container)->get_elem_type();
+ }
+
+ indent(out)
+ << "for (Enumeration " << enumeration_name << " = " << source_name << ".elements(); "
+ << enumeration_name << ".hasMoreElements(); ) {" << endl;
+ indent_up();
+ indent(out)
+ << type_name(elem_type, true, false) << " " << iterator_element_name << " = ("
+ << type_name(elem_type, true, false) << ")" << enumeration_name << ".nextElement();" << endl;
+ if (elem_type->is_container()) {
+ // recursive deep copy
+ generate_deep_copy_container(out, iterator_element_name, "", result_element_name, elem_type);
+ if (elem_type->is_list()) {
+ indent(out) << result_name << ".addElement(" << result_element_name << ");" << endl;
+ } else {
+ indent(out) << result_name << ".put(" << result_element_name << ", " << result_element_name << ");" << endl;
+ }
+ } else {
+ // iterative copy
+ if(((t_base_type*)elem_type)->is_binary()){
+ indent(out) << type_name(elem_type, true, false) << " temp_binary_element = ";
+ generate_deep_copy_non_container(out, iterator_element_name, "temp_binary_element", elem_type);
+ out << ";" << endl;
+ if (elem_type->is_list()) {
+ indent(out) << result_name << ".addElement(temp_binary_element);" << endl;
+ } else {
+ indent(out) << result_name << ".put(temp_binary_element, temp_binary_element);" << endl;
+ }
+ } else{
+ indent(out) << result_name << ".addElement(";
+ generate_deep_copy_non_container(out, iterator_element_name, result_name, elem_type);
+ out << ");" << endl;
+ }
+ }
+
+ indent_down();
+
+ indent(out) << "}" << endl;
+
+ }
+}
+
+void t_javame_generator::generate_deep_copy_non_container(ofstream& out, std::string source_name, std::string dest_name, t_type* type) {
+ if (type->is_base_type() || type->is_enum() || type->is_typedef()) {
+ // binary fields need to be copied with System.arraycopy
+ if (((t_base_type*)type)->is_binary()){
+ out << "new byte[" << source_name << ".length];" << endl;
+ indent(out) << "System.arraycopy(" << source_name << ", 0, " << dest_name << ", 0, " << source_name << ".length)";
+ }
+ // everything else can be copied directly
+ else
+ out << source_name;
+ } else {
+ out << "new " << type_name(type, true, true) << "(" << source_name << ")";
+ }
+}
+
+std::string t_javame_generator::generate_isset_check(t_field* field) {
+ return generate_isset_check(field->get_name());
+}
+
+std::string t_javame_generator::isset_field_id(t_field* field) {
+ return "__" + upcase_string(field->get_name() + "_isset_id");
+}
+
+std::string t_javame_generator::generate_isset_check(std::string field_name) {
+ return "is" + get_cap_name("set") + get_cap_name(field_name) + "()";
+}
+
+void t_javame_generator::generate_isset_set(ofstream& out, t_field* field) {
+ if (!type_can_be_null(field->get_type())) {
+ indent(out) << "set" << get_cap_name(field->get_name()) << get_cap_name("isSet") << "(true);" << endl;
+ }
+}
+
+std::string t_javame_generator::get_enum_class_name(t_type* type) {
+ string package = "";
+ t_program* program = type->get_program();
+ if (program != NULL && program != program_) {
+ package = program->get_namespace("java") + ".";
+ }
+ return package + type->get_name();
+}
+
+void t_javame_generator::generate_struct_desc(ofstream& out, t_struct* tstruct) {
+ indent(out) <<
+ "private static final TStruct STRUCT_DESC = new TStruct(\"" << tstruct->get_name() << "\");" << endl;
+}
+
+void t_javame_generator::generate_field_descs(ofstream& out, t_struct* tstruct) {
+ const vector<t_field*>& members = tstruct->get_members();
+ vector<t_field*>::const_iterator m_iter;
+
+ for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
+ indent(out) <<
+ "private static final TField " << constant_name((*m_iter)->get_name()) <<
+ "_FIELD_DESC = new TField(\"" << (*m_iter)->get_name() << "\", " <<
+ type_to_enum((*m_iter)->get_type()) << ", " <<
+ "(short)" << (*m_iter)->get_key() << ");" << endl;
+ }
+}
+
+bool t_javame_generator::has_bit_vector(t_struct* tstruct) {
+ const vector<t_field*>& members = tstruct->get_members();
+ vector<t_field*>::const_iterator m_iter;
+
+ for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
+ if (!type_can_be_null(get_true_type((*m_iter)->get_type()))) {
+ return true;
+ }
+ }
+ return false;
+}
+
+void t_javame_generator::generate_java_struct_clear(std::ofstream& out, t_struct* tstruct) {
+ indent(out) << "public void clear() {" << endl;
+
+ const vector<t_field*>& members = tstruct->get_members();
+ vector<t_field*>::const_iterator m_iter;
+
+ indent_up();
+ for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
+ t_type* t = get_true_type((*m_iter)->get_type());
+ if ((*m_iter)->get_value() != NULL) {
+ print_const_value(out, "this." + (*m_iter)->get_name(), t, (*m_iter)->get_value(), true, true);
+ } else {
+ if (type_can_be_null(t)) {
+ indent(out) << "this." << (*m_iter)->get_name() << " = null;" << endl;
+ } else {
+ // must be a base type
+ // means it also needs to be explicitly unset
+ indent(out) << "set" << get_cap_name((*m_iter)->get_name()) << get_cap_name("isSet") << "(false);" << endl;
+ switch (((t_base_type*)t)->get_base()) {
+ case t_base_type::TYPE_BYTE:
+ case t_base_type::TYPE_I16:
+ case t_base_type::TYPE_I32:
+ case t_base_type::TYPE_I64:
+ indent(out) << "this." << (*m_iter)->get_name() << " = 0;" << endl;
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ indent(out) << "this." << (*m_iter)->get_name() << " = 0.0;" << endl;
+ break;
+ case t_base_type::TYPE_BOOL:
+ indent(out) << "this." << (*m_iter)->get_name() << " = false;" << endl;
+ break;
+ default: // prevent gcc compiler warning
+ break;
+ }
+ }
+ }
+ }
+ indent_down();
+
+ indent(out) << "}" << endl << endl;
+}
+
+THRIFT_REGISTER_GENERATOR(javame, "Java ME", "");
diff --git a/configure.ac b/configure.ac
index 53fff48d8..656193b31 100644
--- a/configure.ac
+++ b/configure.ac
@@ -317,6 +317,8 @@ AX_THRIFT_GEN(html, [HTML], yes)
AM_CONDITIONAL([THRIFT_GEN_html], [test "$ax_thrift_gen_html" = "yes"])
AX_THRIFT_GEN(js, [JavaScript], yes)
AM_CONDITIONAL([THRIFT_GEN_js], [test "$ax_thrift_gen_js" = "yes"])
+AX_THRIFT_GEN(javame, [JavaME], yes)
+AM_CONDITIONAL([THRIFT_GEN_javame], [test "$ax_thrift_gen_javame" = "yes"])
AC_CONFIG_HEADERS(config.h:config.hin)
diff --git a/lib/javame/src/org/apache/thrift/TApplicationException.java b/lib/javame/src/org/apache/thrift/TApplicationException.java
new file mode 100644
index 000000000..de9a162f9
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/TApplicationException.java
@@ -0,0 +1,130 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift;
+
+import org.apache.thrift.protocol.TField;
+import org.apache.thrift.protocol.TProtocol;
+import org.apache.thrift.protocol.TProtocolUtil;
+import org.apache.thrift.protocol.TStruct;
+import org.apache.thrift.protocol.TType;
+
+/**
+ * Application level exception
+ *
+ */
+public class TApplicationException extends TException {
+
+ private static final long serialVersionUID = 1L;
+
+ public static final int UNKNOWN = 0;
+ public static final int UNKNOWN_METHOD = 1;
+ public static final int INVALID_MESSAGE_TYPE = 2;
+ public static final int WRONG_METHOD_NAME = 3;
+ public static final int BAD_SEQUENCE_ID = 4;
+ public static final int MISSING_RESULT = 5;
+ public static final int INTERNAL_ERROR = 6;
+ public static final int PROTOCOL_ERROR = 7;
+
+
+ protected int type_ = UNKNOWN;
+
+ public TApplicationException() {
+ super();
+ }
+
+ public TApplicationException(int type) {
+ super();
+ type_ = type;
+ }
+
+ public TApplicationException(int type, String message) {
+ super(message);
+ type_ = type;
+ }
+
+ public TApplicationException(String message) {
+ super(message);
+ }
+
+ public int getType() {
+ return type_;
+ }
+
+ public static TApplicationException read(TProtocol iprot) throws TException {
+ TField field;
+ iprot.readStructBegin();
+
+ String message = null;
+ int type = UNKNOWN;
+
+ while (true) {
+ field = iprot.readFieldBegin();
+ if (field.type == TType.STOP) {
+ break;
+ }
+ switch (field.id) {
+ case 1:
+ if (field.type == TType.STRING) {
+ message = iprot.readString();
+ } else {
+ TProtocolUtil.skip(iprot, field.type);
+ }
+ break;
+ case 2:
+ if (field.type == TType.I32) {
+ type = iprot.readI32();
+ } else {
+ TProtocolUtil.skip(iprot, field.type);
+ }
+ break;
+ default:
+ TProtocolUtil.skip(iprot, field.type);
+ break;
+ }
+ iprot.readFieldEnd();
+ }
+ iprot.readStructEnd();
+
+ return new TApplicationException(type, message);
+ }
+
+ public void write(TProtocol oprot) throws TException {
+ TStruct struct = new TStruct("TApplicationException");
+ TField field = new TField();
+ oprot.writeStructBegin(struct);
+ if (getMessage() != null) {
+ field.name = "message";
+ field.type = TType.STRING;
+ field.id = 1;
+ oprot.writeFieldBegin(field);
+ oprot.writeString(getMessage());
+ oprot.writeFieldEnd();
+ }
+ field.name = "type";
+ field.type = TType.I32;
+ field.id = 2;
+ oprot.writeFieldBegin(field);
+ oprot.writeI32(type_);
+ oprot.writeFieldEnd();
+ oprot.writeFieldStop();
+ oprot.writeStructEnd();
+
+ }
+}
diff --git a/lib/javame/src/org/apache/thrift/TBase.java b/lib/javame/src/org/apache/thrift/TBase.java
new file mode 100644
index 000000000..13ea024e2
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/TBase.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift;
+
+import org.apache.thrift.protocol.TProtocol;
+
+/**
+ * Generic base interface for generated Thrift objects.
+ *
+ */
+public interface TBase {
+
+ /**
+ * Reads the TObject from the given input protocol.
+ *
+ * @param iprot Input protocol
+ */
+ public void read(TProtocol iprot) throws TException;
+
+ /**
+ * Writes the objects out to the protocol
+ *
+ * @param oprot Output protocol
+ */
+ public void write(TProtocol oprot) throws TException;
+
+ public int compareTo(Object other);
+
+}
diff --git a/lib/javame/src/org/apache/thrift/TBaseHelper.java b/lib/javame/src/org/apache/thrift/TBaseHelper.java
new file mode 100644
index 000000000..5ec899206
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/TBaseHelper.java
@@ -0,0 +1,209 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.thrift;
+
+import java.util.Vector;
+import java.util.Hashtable;
+import java.util.Enumeration;
+
+public class TBaseHelper {
+
+ public static int compareTo(boolean a, boolean b) {
+ return (a == b) ? 0 : (a ? 1 : -1);
+
+
+ }
+ public static int compareTo(Boolean a, Boolean b) {
+ return (a.booleanValue() == b.booleanValue()) ? 0 : (a.booleanValue() ? 1 : -1);
+
+
+ }
+ public static int compareTo(Boolean a, boolean b) {
+ return (a.booleanValue() == b) ? 0 : (a.booleanValue() ? 1 : -1);
+
+
+ }
+
+ public static Boolean booleanValueOf(boolean b) {
+ return (b ? Boolean.TRUE : Boolean.FALSE);
+ }
+
+ public static int compareTo(byte a, byte b) {
+ if (a < b) {
+ return -1;
+ } else if (b < a) {
+ return 1;
+ } else {
+ return 0;
+ }
+ }
+
+ public static int compareTo(short a, short b) {
+ if (a < b) {
+ return -1;
+ } else if (b < a) {
+ return 1;
+ } else {
+ return 0;
+ }
+ }
+
+ public static int compareTo(int a, int b) {
+ if (a < b) {
+ return -1;
+ } else if (b < a) {
+ return 1;
+ } else {
+ return 0;
+ }
+ }
+
+ public static int compareTo(long a, long b) {
+ if (a < b) {
+ return -1;
+ } else if (b < a) {
+ return 1;
+ } else {
+ return 0;
+ }
+ }
+
+ public static int compareTo(double a, double b) {
+ if (a < b) {
+ return -1;
+ } else if (b < a) {
+ return 1;
+ } else {
+ return 0;
+ }
+ }
+
+ public static int compareTo(String a, String b) {
+ return a.compareTo(b);
+ }
+
+ public static int compareTo(byte[] a, byte[] b) {
+ int sizeCompare = compareTo(a.length, b.length);
+ if (sizeCompare != 0) {
+ return sizeCompare;
+ }
+ for (int i = 0; i < a.length; i++) {
+ int byteCompare = compareTo(a, b);
+ if (byteCompare != 0) {
+ return byteCompare;
+ }
+ }
+ return 0;
+ }
+
+ public static int compareObjects(Object a, Object b) {
+ if (a instanceof Vector) {
+ return compareTo((Vector)a, (Vector)b);
+ } if (a instanceof Hashtable) {
+ return compareTo((Hashtable)a, (Hashtable)b);
+ } else {
+ return ((TBase)a).compareTo(b);
+ }
+ }
+
+ public static int compareTo(Vector a, Vector b) {
+ int lastComparison = compareTo(a.size(), b.size());
+ if (lastComparison != 0) {
+ return lastComparison;
+ }
+ for (int i = 0; i < a.size(); i++) {
+ Object oA = a.elementAt(i);
+ Object oB = b.elementAt(i);
+ lastComparison = compareObjects(oA, oB);
+ if (lastComparison != 0) {
+ return lastComparison;
+ }
+
+ }
+ return 0;
+ }
+
+ public static int compareTo(Hashtable a, Hashtable b) {
+ int lastComparison = compareTo(a.size(), b.size());
+ if (lastComparison != 0) {
+ return lastComparison;
+ }
+ Enumeration enumA = a.keys();
+ Enumeration enumB = b.keys();
+ while (lastComparison == 0 && enumA.hasMoreElements()) {
+ Object keyA = enumA.nextElement();
+ Object keyB = enumB.nextElement();
+ lastComparison = compareObjects(keyA, keyB);
+ if (lastComparison == 0) {
+ lastComparison = compareObjects(a.get(keyA), b.get(keyB));
+ }
+ }
+ return lastComparison;
+ }
+
+ public static int compareTo(TEnum a, TEnum b) {
+ return compareTo(a.getValue(), b.getValue());
+ }
+
+ /*
+ public static int compareTo(List a, List b) {
+ int lastComparison = compareTo(a.size(), b.size());
+ if (lastComparison != 0) {
+ return lastComparison;
+ }
+ for (int i = 0; i < a.size(); i++) {
+ Object oA = a.get(i);
+ Object oB = b.get(i);
+ if (oA instanceof List) {
+ lastComparison = compareTo((List) oA, (List) oB);
+ } else {
+ lastComparison = compareTo((Comparable) oA, (Comparable) oB);
+ }
+ if (lastComparison != 0) {
+ return lastComparison;
+ }
+ }
+ return 0;
+ }
+ */
+
+ public static void toString(byte[] bytes, StringBuffer sb) {
+ toString(bytes, 0, bytes.length, sb);
+ }
+
+ public static void toString(byte[] buf, int arrayOffset, int origLimit, StringBuffer sb) {
+ int limit = (origLimit - arrayOffset > 128) ? arrayOffset + 128 : origLimit;
+
+ for (int i = arrayOffset; i < limit; i++) {
+ if (i > arrayOffset) {
+ sb.append(" ");
+ }
+ sb.append(paddedByteString(buf[i]));
+ }
+ if (origLimit != limit) {
+ sb.append("...");
+ }
+ }
+
+ public static String paddedByteString(byte b) {
+ int extended = (b | 0x100) & 0x1ff;
+ return Integer.toHexString(extended).toUpperCase().substring(1);
+ }
+
+}
diff --git a/lib/javame/src/org/apache/thrift/TByteArrayOutputStream.java b/lib/javame/src/org/apache/thrift/TByteArrayOutputStream.java
new file mode 100644
index 000000000..077c2e6f7
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/TByteArrayOutputStream.java
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift;
+
+import java.io.ByteArrayOutputStream;
+
+/**
+ * Class that allows access to the underlying buf without doing deep
+ * copies on it.
+ *
+ */
+public class TByteArrayOutputStream extends ByteArrayOutputStream {
+ public TByteArrayOutputStream(int size) {
+ super(size);
+ }
+
+ public byte[] get() {
+ return buf;
+ }
+
+ public int len() {
+ return count;
+ }
+}
diff --git a/lib/javame/src/org/apache/thrift/TDeserializer.java b/lib/javame/src/org/apache/thrift/TDeserializer.java
new file mode 100644
index 000000000..8eceae717
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/TDeserializer.java
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift;
+
+import java.io.ByteArrayInputStream;
+import java.io.UnsupportedEncodingException;
+
+import org.apache.thrift.protocol.TBinaryProtocol;
+import org.apache.thrift.protocol.TProtocolFactory;
+import org.apache.thrift.transport.TIOStreamTransport;
+
+/**
+ * Generic utility for easily deserializing objects from a byte array or Java
+ * String.
+ *
+ */
+public class TDeserializer {
+ private final TProtocolFactory protocolFactory_;
+
+ /**
+ * Create a new TDeserializer that uses the TBinaryProtocol by default.
+ */
+ public TDeserializer() {
+ this(new TBinaryProtocol.Factory());
+ }
+
+ /**
+ * Create a new TDeserializer. It will use the TProtocol specified by the
+ * factory that is passed in.
+ *
+ * @param protocolFactory Factory to create a protocol
+ */
+ public TDeserializer(TProtocolFactory protocolFactory) {
+ protocolFactory_ = protocolFactory;
+ }
+
+ /**
+ * Deserialize the Thrift object from a byte array.
+ *
+ * @param base The object to read into
+ * @param bytes The array to read from
+ */
+ public void deserialize(TBase base, byte[] bytes) throws TException {
+ base.read(
+ protocolFactory_.getProtocol(
+ new TIOStreamTransport(
+ new ByteArrayInputStream(bytes))));
+ }
+
+ /**
+ * Deserialize the Thrift object from a Java string, using a specified
+ * character set for decoding.
+ *
+ * @param base The object to read into
+ * @param data The string to read from
+ * @param charset Valid JVM charset
+ */
+ public void deserialize(TBase base, String data, String charset) throws TException {
+ try {
+ deserialize(base, data.getBytes(charset));
+ } catch (UnsupportedEncodingException uex) {
+ throw new TException("JVM DOES NOT SUPPORT ENCODING: " + charset);
+ }
+ }
+
+ /**
+ * Deerialize the Thrift object from a Java string, using the default JVM
+ * charset encoding.
+ *
+ * @param base The object to read into
+ * @param data The string to read from
+ * @return Serialized object as a String
+ */
+ public void toString(TBase base, String data) throws TException {
+ deserialize(base, data.getBytes());
+ }
+}
+
diff --git a/lib/javame/src/org/apache/thrift/TEnum.java b/lib/javame/src/org/apache/thrift/TEnum.java
new file mode 100644
index 000000000..325fdece7
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/TEnum.java
@@ -0,0 +1,24 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift;
+
+public interface TEnum {
+ public int getValue();
+}
diff --git a/lib/javame/src/org/apache/thrift/TException.java b/lib/javame/src/org/apache/thrift/TException.java
new file mode 100644
index 000000000..7f375b813
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/TException.java
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift;
+
+/**
+ * Generic exception class for Thrift.
+ *
+ */
+public class TException extends Exception {
+
+ private static final long serialVersionUID = 1L;
+
+ public TException() {
+ super();
+ }
+
+ public TException(String message) {
+ super(message);
+ }
+
+ public TException(Throwable cause) {
+ super(cause.getMessage());
+ }
+
+ public TException(String message, Throwable cause) {
+ super(message);
+ }
+}
diff --git a/lib/javame/src/org/apache/thrift/TFieldRequirementType.java b/lib/javame/src/org/apache/thrift/TFieldRequirementType.java
new file mode 100644
index 000000000..104a391d8
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/TFieldRequirementType.java
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift;
+
+/**
+ * Requirement type constants.
+ *
+ */
+public final class TFieldRequirementType {
+ public static final byte REQUIRED = 1;
+ public static final byte OPTIONAL = 2;
+ public static final byte DEFAULT = 3;
+}
diff --git a/lib/javame/src/org/apache/thrift/TProcessor.java b/lib/javame/src/org/apache/thrift/TProcessor.java
new file mode 100644
index 000000000..d79522c3e
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/TProcessor.java
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift;
+
+import org.apache.thrift.protocol.TProtocol;
+
+/**
+ * A processor is a generic object which operates upon an input stream and
+ * writes to some output stream.
+ *
+ */
+public interface TProcessor {
+ public boolean process(TProtocol in, TProtocol out)
+ throws TException;
+}
diff --git a/lib/javame/src/org/apache/thrift/TProcessorFactory.java b/lib/javame/src/org/apache/thrift/TProcessorFactory.java
new file mode 100644
index 000000000..bcd8a38fd
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/TProcessorFactory.java
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift;
+
+import org.apache.thrift.transport.TTransport;
+
+/**
+ * The default processor factory just returns a singleton
+ * instance.
+ */
+public class TProcessorFactory {
+
+ private final TProcessor processor_;
+
+ public TProcessorFactory(TProcessor processor) {
+ processor_ = processor;
+ }
+
+ public TProcessor getProcessor(TTransport trans) {
+ return processor_;
+ }
+}
diff --git a/lib/javame/src/org/apache/thrift/TSerializer.java b/lib/javame/src/org/apache/thrift/TSerializer.java
new file mode 100644
index 000000000..4e1ce6129
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/TSerializer.java
@@ -0,0 +1,110 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift;
+
+import java.io.ByteArrayOutputStream;
+import java.io.UnsupportedEncodingException;
+
+import org.apache.thrift.protocol.TBinaryProtocol;
+import org.apache.thrift.protocol.TProtocol;
+import org.apache.thrift.protocol.TProtocolFactory;
+import org.apache.thrift.transport.TIOStreamTransport;
+
+/**
+ * Generic utility for easily serializing objects into a byte array or Java
+ * String.
+ *
+ */
+public class TSerializer {
+
+ /**
+ * This is the byte array that data is actually serialized into
+ */
+ private final ByteArrayOutputStream baos_ = new ByteArrayOutputStream();
+
+ /**
+ * This transport wraps that byte array
+ */
+ private final TIOStreamTransport transport_ = new TIOStreamTransport(baos_);
+
+ /**
+ * Internal protocol used for serializing objects.
+ */
+ private TProtocol protocol_;
+
+ /**
+ * Create a new TSerializer that uses the TBinaryProtocol by default.
+ */
+ public TSerializer() {
+ this(new TBinaryProtocol.Factory());
+ }
+
+ /**
+ * Create a new TSerializer. It will use the TProtocol specified by the
+ * factory that is passed in.
+ *
+ * @param protocolFactory Factory to create a protocol
+ */
+ public TSerializer(TProtocolFactory protocolFactory) {
+ protocol_ = protocolFactory.getProtocol(transport_);
+ }
+
+ /**
+ * Serialize the Thrift object into a byte array. The process is simple,
+ * just clear the byte array output, write the object into it, and grab the
+ * raw bytes.
+ *
+ * @param base The object to serialize
+ * @return Serialized object in byte[] format
+ */
+ public byte[] serialize(TBase base) throws TException {
+ baos_.reset();
+ base.write(protocol_);
+ return baos_.toByteArray();
+ }
+
+ /**
+ * Serialize the Thrift object into a Java string, using a specified
+ * character set for encoding.
+ *
+ * @param base The object to serialize
+ * @param charset Valid JVM charset
+ * @return Serialized object as a String
+ */
+ public String toString(TBase base, String charset) throws TException {
+ try {
+ return new String(serialize(base), charset);
+ } catch (UnsupportedEncodingException uex) {
+ throw new TException("JVM DOES NOT SUPPORT ENCODING: " + charset);
+ }
+ }
+
+ /**
+ * Serialize the Thrift object into a Java string, using the default JVM
+ * charset encoding.
+ *
+ * @param base The object to serialize
+ * @return Serialized object as a String
+ */
+ public String toString(TBase base) throws TException {
+ return new String(serialize(base));
+ }
+}
+
diff --git a/lib/javame/src/org/apache/thrift/TServiceClient.java b/lib/javame/src/org/apache/thrift/TServiceClient.java
new file mode 100644
index 000000000..ee07b7821
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/TServiceClient.java
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift;
+
+import org.apache.thrift.protocol.TProtocol;
+
+/**
+ * A TServiceClient is used to communicate with a TService implementation
+ * across protocols and transports.
+ */
+public interface TServiceClient {
+ /**
+ * Get the TProtocol being used as the input (read) protocol.
+ * @return
+ */
+ public TProtocol getInputProtocol();
+ /**
+ * Get the TProtocol being used as the output (write) protocol.
+ * @return
+ */
+ public TProtocol getOutputProtocol();
+}
diff --git a/lib/javame/src/org/apache/thrift/meta_data/FieldMetaData.java b/lib/javame/src/org/apache/thrift/meta_data/FieldMetaData.java
new file mode 100644
index 000000000..11a5a08b1
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/meta_data/FieldMetaData.java
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift.meta_data;
+
+import org.apache.thrift.TBase;
+
+import java.util.Hashtable;
+
+
+/**
+ * This class is used to store meta data about thrift fields. Every field in a
+ * a struct should have a corresponding instance of this class describing it.
+ *
+ */
+public class FieldMetaData {
+ public final String fieldName;
+ public final byte requirementType;
+ public final FieldValueMetaData valueMetaData;
+ private static Hashtable structMap;
+
+ static {
+ structMap = new Hashtable();
+ }
+
+ public FieldMetaData(String name, byte req, FieldValueMetaData vMetaData){
+ this.fieldName = name;
+ this.requirementType = req;
+ this.valueMetaData = vMetaData;
+ }
+
+ public static void addStructMetaDataMap(Class sClass, Hashtable map){
+ structMap.put(sClass, map);
+ }
+
+ /**
+ * Returns a map with metadata (i.e. instances of FieldMetaData) that
+ * describe the fields of the given class.
+ *
+ * @param sClass The TBase class for which the metadata map is requested
+ */
+ public static Hashtable getStructMetaDataMap(Class sClass){
+ if (!structMap.containsKey(sClass)){ // Load class if it hasn't been loaded
+ try{
+ sClass.newInstance();
+ } catch (InstantiationException e){
+ throw new RuntimeException("InstantiationException for TBase class: " + sClass.getName() + ", message: " + e.getMessage());
+ } catch (IllegalAccessException e){
+ throw new RuntimeException("IllegalAccessException for TBase class: " + sClass.getName() + ", message: " + e.getMessage());
+ }
+ }
+ return (Hashtable) structMap.get(sClass);
+ }
+}
diff --git a/lib/javame/src/org/apache/thrift/meta_data/FieldValueMetaData.java b/lib/javame/src/org/apache/thrift/meta_data/FieldValueMetaData.java
new file mode 100644
index 000000000..4c017b6bf
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/meta_data/FieldValueMetaData.java
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift.meta_data;
+
+import org.apache.thrift.protocol.TType;
+
+
+/**
+ * FieldValueMetaData and collection of subclasses to store metadata about
+ * the value(s) of a field
+ */
+public class FieldValueMetaData {
+ public final byte type;
+
+ public FieldValueMetaData(byte type){
+ this.type = type;
+ }
+
+ public boolean isStruct() {
+ return type == TType.STRUCT;
+ }
+
+ public boolean isContainer() {
+ return type == TType.LIST || type == TType.MAP || type == TType.SET;
+ }
+}
diff --git a/lib/javame/src/org/apache/thrift/meta_data/ListMetaData.java b/lib/javame/src/org/apache/thrift/meta_data/ListMetaData.java
new file mode 100644
index 000000000..5cd719423
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/meta_data/ListMetaData.java
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift.meta_data;
+
+import org.apache.thrift.meta_data.FieldValueMetaData;
+
+public class ListMetaData extends FieldValueMetaData {
+ public final FieldValueMetaData elemMetaData;
+
+ public ListMetaData(byte type, FieldValueMetaData eMetaData){
+ super(type);
+ this.elemMetaData = eMetaData;
+ }
+}
diff --git a/lib/javame/src/org/apache/thrift/meta_data/MapMetaData.java b/lib/javame/src/org/apache/thrift/meta_data/MapMetaData.java
new file mode 100644
index 000000000..0d1d6f659
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/meta_data/MapMetaData.java
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift.meta_data;
+
+import org.apache.thrift.meta_data.FieldValueMetaData;
+
+public class MapMetaData extends FieldValueMetaData {
+ public final FieldValueMetaData keyMetaData;
+ public final FieldValueMetaData valueMetaData;
+
+ public MapMetaData(byte type, FieldValueMetaData kMetaData, FieldValueMetaData vMetaData){
+ super(type);
+ this.keyMetaData = kMetaData;
+ this.valueMetaData = vMetaData;
+ }
+}
diff --git a/lib/javame/src/org/apache/thrift/meta_data/SetMetaData.java b/lib/javame/src/org/apache/thrift/meta_data/SetMetaData.java
new file mode 100644
index 000000000..3ec6868a1
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/meta_data/SetMetaData.java
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift.meta_data;
+
+public class SetMetaData extends FieldValueMetaData {
+ public final FieldValueMetaData elemMetaData;
+
+ public SetMetaData(byte type, FieldValueMetaData eMetaData){
+ super(type);
+ this.elemMetaData = eMetaData;
+ }
+}
diff --git a/lib/javame/src/org/apache/thrift/meta_data/StructMetaData.java b/lib/javame/src/org/apache/thrift/meta_data/StructMetaData.java
new file mode 100644
index 000000000..e30d90814
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/meta_data/StructMetaData.java
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift.meta_data;
+
+import org.apache.thrift.TBase;
+import org.apache.thrift.meta_data.FieldValueMetaData;
+
+
+public class StructMetaData extends FieldValueMetaData {
+ public final Class structClass;
+
+ public StructMetaData(byte type, Class sClass){
+ super(type);
+ this.structClass = sClass;
+ }
+}
diff --git a/lib/javame/src/org/apache/thrift/protocol/TBase64Utils.java b/lib/javame/src/org/apache/thrift/protocol/TBase64Utils.java
new file mode 100644
index 000000000..37a9fd9f9
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/protocol/TBase64Utils.java
@@ -0,0 +1,128 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift.protocol;
+
+/**
+ * Class for encoding and decoding Base64 data.
+ *
+ * This class is kept at package level because the interface does no input
+ * validation and is therefore too low-level for generalized reuse.
+ *
+ * Note also that the encoding does not pad with equal signs , as discussed in
+ * section 2.2 of the RFC (http://www.faqs.org/rfcs/rfc3548.html). Furthermore,
+ * bad data encountered when decoding is neither rejected or ignored but simply
+ * results in bad decoded data -- this is not in compliance with the RFC but is
+ * done in the interest of performance.
+ *
+ */
+class TBase64Utils {
+
+ private static final String ENCODE_TABLE =
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+
+ /**
+ * Encode len bytes of data in src at offset srcOff, storing the result into
+ * dst at offset dstOff. len must be 1, 2, or 3. dst must have at least len+1
+ * bytes of space at dstOff. src and dst should not be the same object. This
+ * method does no validation of the input values in the interest of
+ * performance.
+ *
+ * @param src the source of bytes to encode
+ * @param srcOff the offset into the source to read the unencoded bytes
+ * @param len the number of bytes to encode (must be 1, 2, or 3).
+ * @param dst the destination for the encoding
+ * @param dstOff the offset into the destination to place the encoded bytes
+ */
+ static final void encode(byte[] src, int srcOff, int len, byte[] dst,
+ int dstOff) {
+ dst[dstOff] = (byte)ENCODE_TABLE.charAt((src[srcOff] >> 2) & 0x3F);
+ if (len == 3) {
+ dst[dstOff + 1] =
+ (byte)ENCODE_TABLE.charAt(
+ ((src[srcOff] << 4) + (src[srcOff+1] >> 4)) & 0x3F);
+ dst[dstOff + 2] =
+ (byte)ENCODE_TABLE.charAt(
+ ((src[srcOff+1] << 2) + (src[srcOff+2] >> 6)) & 0x3F);
+ dst[dstOff + 3] =
+ (byte)ENCODE_TABLE.charAt(src[srcOff+2] & 0x3F);
+ }
+ else if (len == 2) {
+ dst[dstOff+1] =
+ (byte)ENCODE_TABLE.charAt(
+ ((src[srcOff] << 4) + (src[srcOff+1] >> 4)) & 0x3F);
+ dst[dstOff + 2] =
+ (byte)ENCODE_TABLE.charAt((src[srcOff+1] << 2) & 0x3F);
+
+ }
+ else { // len == 1) {
+ dst[dstOff + 1] =
+ (byte)ENCODE_TABLE.charAt((src[srcOff] << 4) & 0x3F);
+ }
+ }
+
+ private static final byte[] DECODE_TABLE = {
+ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
+ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
+ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,
+ 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,
+ -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,
+ 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,
+ -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
+ 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1,
+ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
+ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
+ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
+ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
+ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
+ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
+ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
+ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
+ };
+
+ /**
+ * Decode len bytes of data in src at offset srcOff, storing the result into
+ * dst at offset dstOff. len must be 2, 3, or 4. dst must have at least len-1
+ * bytes of space at dstOff. src and dst may be the same object as long as
+ * dstoff <= srcOff. This method does no validation of the input values in
+ * the interest of performance.
+ *
+ * @param src the source of bytes to decode
+ * @param srcOff the offset into the source to read the encoded bytes
+ * @param len the number of bytes to decode (must be 2, 3, or 4)
+ * @param dst the destination for the decoding
+ * @param dstOff the offset into the destination to place the decoded bytes
+ */
+ static final void decode(byte[] src, int srcOff, int len, byte[] dst,
+ int dstOff) {
+ dst[dstOff] = (byte)
+ ((DECODE_TABLE[src[srcOff] & 0x0FF] << 2) |
+ (DECODE_TABLE[src[srcOff+1] & 0x0FF] >> 4));
+ if (len > 2) {
+ dst[dstOff+1] = (byte)
+ (((DECODE_TABLE[src[srcOff+1] & 0x0FF] << 4) & 0xF0) |
+ (DECODE_TABLE[src[srcOff+2] & 0x0FF] >> 2));
+ if (len > 3) {
+ dst[dstOff+2] = (byte)
+ (((DECODE_TABLE[src[srcOff+2] & 0x0FF] << 6) & 0xC0) |
+ DECODE_TABLE[src[srcOff+3] & 0x0FF]);
+ }
+ }
+ }
+}
diff --git a/lib/javame/src/org/apache/thrift/protocol/TBinaryProtocol.java b/lib/javame/src/org/apache/thrift/protocol/TBinaryProtocol.java
new file mode 100644
index 000000000..1d16889e7
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/protocol/TBinaryProtocol.java
@@ -0,0 +1,350 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift.protocol;
+
+import java.io.UnsupportedEncodingException;
+
+import org.apache.thrift.TException;
+import org.apache.thrift.transport.TTransport;
+
+/**
+ * Binary protocol implementation for thrift.
+ *
+ */
+public class TBinaryProtocol extends TProtocol {
+
+ protected static final int VERSION_MASK = 0xffff0000;
+ protected static final int VERSION_1 = 0x80010000;
+
+ protected boolean strictRead_ = false;
+ protected boolean strictWrite_ = true;
+
+ protected int readLength_;
+ protected boolean checkReadLength_ = false;
+
+ /**
+ * Factory
+ */
+ public static class Factory implements TProtocolFactory {
+ protected boolean strictRead_ = false;
+ protected boolean strictWrite_ = true;
+
+ public Factory() {
+ this(false, true);
+ }
+
+ public Factory(boolean strictRead, boolean strictWrite) {
+ strictRead_ = strictRead;
+ strictWrite_ = strictWrite;
+ }
+
+ public TProtocol getProtocol(TTransport trans) {
+ return new TBinaryProtocol(trans, strictRead_, strictWrite_);
+ }
+ }
+
+ /**
+ * Constructor
+ */
+ public TBinaryProtocol(TTransport trans) {
+ this(trans, false, true);
+ }
+
+ public TBinaryProtocol(TTransport trans, boolean strictRead, boolean strictWrite) {
+ super(trans);
+ strictRead_ = strictRead;
+ strictWrite_ = strictWrite;
+ }
+
+ public void writeMessageBegin(TMessage message) throws TException {
+ if (strictWrite_) {
+ int version = VERSION_1 | message.type;
+ writeI32(version);
+ writeString(message.name);
+ writeI32(message.seqid);
+ } else {
+ writeString(message.name);
+ writeByte(message.type);
+ writeI32(message.seqid);
+ }
+ }
+
+ public void writeMessageEnd() {}
+
+ public void writeStructBegin(TStruct struct) {}
+
+ public void writeStructEnd() {}
+
+ public void writeFieldBegin(TField field) throws TException {
+ writeByte(field.type);
+ writeI16(field.id);
+ }
+
+ public void writeFieldEnd() {}
+
+ public void writeFieldStop() throws TException {
+ writeByte(TType.STOP);
+ }
+
+ public void writeMapBegin(TMap map) throws TException {
+ writeByte(map.keyType);
+ writeByte(map.valueType);
+ writeI32(map.size);
+ }
+
+ public void writeMapEnd() {}
+
+ public void writeListBegin(TList list) throws TException {
+ writeByte(list.elemType);
+ writeI32(list.size);
+ }
+
+ public void writeListEnd() {}
+
+ public void writeSetBegin(TSet set) throws TException {
+ writeByte(set.elemType);
+ writeI32(set.size);
+ }
+
+ public void writeSetEnd() {}
+
+ public void writeBool(boolean b) throws TException {
+ writeByte(b ? (byte)1 : (byte)0);
+ }
+
+ private byte [] bout = new byte[1];
+ public void writeByte(byte b) throws TException {
+ bout[0] = b;
+ trans_.write(bout, 0, 1);
+ }
+
+ private byte[] i16out = new byte[2];
+ public void writeI16(short i16) throws TException {
+ i16out[0] = (byte)(0xff & (i16 >> 8));
+ i16out[1] = (byte)(0xff & (i16));
+ trans_.write(i16out, 0, 2);
+ }
+
+ private byte[] i32out = new byte[4];
+ public void writeI32(int i32) throws TException {
+ i32out[0] = (byte)(0xff & (i32 >> 24));
+ i32out[1] = (byte)(0xff & (i32 >> 16));
+ i32out[2] = (byte)(0xff & (i32 >> 8));
+ i32out[3] = (byte)(0xff & (i32));
+ trans_.write(i32out, 0, 4);
+ }
+
+ private byte[] i64out = new byte[8];
+ public void writeI64(long i64) throws TException {
+ i64out[0] = (byte)(0xff & (i64 >> 56));
+ i64out[1] = (byte)(0xff & (i64 >> 48));
+ i64out[2] = (byte)(0xff & (i64 >> 40));
+ i64out[3] = (byte)(0xff & (i64 >> 32));
+ i64out[4] = (byte)(0xff & (i64 >> 24));
+ i64out[5] = (byte)(0xff & (i64 >> 16));
+ i64out[6] = (byte)(0xff & (i64 >> 8));
+ i64out[7] = (byte)(0xff & (i64));
+ trans_.write(i64out, 0, 8);
+ }
+
+ public void writeDouble(double dub) throws TException {
+ writeI64(Double.doubleToLongBits(dub));
+ }
+
+ public void writeString(String str) throws TException {
+ try {
+ byte[] dat = str.getBytes("UTF-8");
+ writeI32(dat.length);
+ trans_.write(dat, 0, dat.length);
+ } catch (UnsupportedEncodingException uex) {
+ throw new TException("JVM DOES NOT SUPPORT UTF-8");
+ }
+ }
+
+ public void writeBinary(byte[] bin) throws TException {
+ writeI32(bin.length);
+ trans_.write(bin, 0, bin.length);
+ }
+
+ /**
+ * Reading methods.
+ */
+
+ public TMessage readMessageBegin() throws TException {
+ TMessage message = new TMessage();
+
+ int size = readI32();
+ if (size < 0) {
+ int version = size & VERSION_MASK;
+ if (version != VERSION_1) {
+ throw new TProtocolException(TProtocolException.BAD_VERSION, "Bad version in readMessageBegin");
+ }
+ message.type = (byte)(size & 0x000000ff);
+ message.name = readString();
+ message.seqid = readI32();
+ } else {
+ if (strictRead_) {
+ throw new TProtocolException(TProtocolException.BAD_VERSION, "Missing version in readMessageBegin, old client?");
+ }
+ message.name = readStringBody(size);
+ message.type = readByte();
+ message.seqid = readI32();
+ }
+ return message;
+ }
+
+ public void readMessageEnd() {}
+
+ public TStruct readStructBegin() {
+ return new TStruct();
+ }
+
+ public void readStructEnd() {}
+
+ public TField readFieldBegin() throws TException {
+ TField field = new TField();
+ field.type = readByte();
+ if (field.type != TType.STOP) {
+ field.id = readI16();
+ }
+ return field;
+ }
+
+ public void readFieldEnd() {}
+
+ public TMap readMapBegin() throws TException {
+ TMap map = new TMap();
+ map.keyType = readByte();
+ map.valueType = readByte();
+ map.size = readI32();
+ return map;
+ }
+
+ public void readMapEnd() {}
+
+ public TList readListBegin() throws TException {
+ TList list = new TList();
+ list.elemType = readByte();
+ list.size = readI32();
+ return list;
+ }
+
+ public void readListEnd() {}
+
+ public TSet readSetBegin() throws TException {
+ TSet set = new TSet();
+ set.elemType = readByte();
+ set.size = readI32();
+ return set;
+ }
+
+ public void readSetEnd() {}
+
+ public boolean readBool() throws TException {
+ return (readByte() == 1);
+ }
+
+ private byte[] bin = new byte[1];
+ public byte readByte() throws TException {
+ readAll(bin, 0, 1);
+ return bin[0];
+ }
+
+ private byte[] i16rd = new byte[2];
+ public short readI16() throws TException {
+ readAll(i16rd, 0, 2);
+ return
+ (short)
+ (((i16rd[0] & 0xff) << 8) |
+ ((i16rd[1] & 0xff)));
+ }
+
+ private byte[] i32rd = new byte[4];
+ public int readI32() throws TException {
+ readAll(i32rd, 0, 4);
+ return
+ ((i32rd[0] & 0xff) << 24) |
+ ((i32rd[1] & 0xff) << 16) |
+ ((i32rd[2] & 0xff) << 8) |
+ ((i32rd[3] & 0xff));
+ }
+
+ private byte[] i64rd = new byte[8];
+ public long readI64() throws TException {
+ readAll(i64rd, 0, 8);
+ return
+ ((long)(i64rd[0] & 0xff) << 56) |
+ ((long)(i64rd[1] & 0xff) << 48) |
+ ((long)(i64rd[2] & 0xff) << 40) |
+ ((long)(i64rd[3] & 0xff) << 32) |
+ ((long)(i64rd[4] & 0xff) << 24) |
+ ((long)(i64rd[5] & 0xff) << 16) |
+ ((long)(i64rd[6] & 0xff) << 8) |
+ ((long)(i64rd[7] & 0xff));
+ }
+
+ public double readDouble() throws TException {
+ return Double.longBitsToDouble(readI64());
+ }
+
+ public String readString() throws TException {
+ int size = readI32();
+ return readStringBody(size);
+ }
+
+ public String readStringBody(int size) throws TException {
+ try {
+ checkReadLength(size);
+ byte[] buf = new byte[size];
+ trans_.readAll(buf, 0, size);
+ return new String(buf, "UTF-8");
+ } catch (UnsupportedEncodingException uex) {
+ throw new TException("JVM DOES NOT SUPPORT UTF-8");
+ }
+ }
+
+ public byte[] readBinary() throws TException {
+ int size = readI32();
+ checkReadLength(size);
+ byte[] buf = new byte[size];
+ trans_.readAll(buf, 0, size);
+ return buf;
+ }
+
+ private int readAll(byte[] buf, int off, int len) throws TException {
+ checkReadLength(len);
+ return trans_.readAll(buf, off, len);
+ }
+
+ public void setReadLength(int readLength) {
+ readLength_ = readLength;
+ checkReadLength_ = true;
+ }
+
+ protected void checkReadLength(int length) throws TException {
+ if (checkReadLength_) {
+ readLength_ -= length;
+ if (readLength_ < 0) {
+ throw new TException("Message length exceeded: " + length);
+ }
+ }
+ }
+
+}
diff --git a/lib/javame/src/org/apache/thrift/protocol/TField.java b/lib/javame/src/org/apache/thrift/protocol/TField.java
new file mode 100644
index 000000000..061140ff8
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/protocol/TField.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift.protocol;
+
+/**
+ * Helper class that encapsulates field metadata.
+ *
+ */
+public class TField {
+ public TField() {}
+
+ public TField(String n, byte t, short i) {
+ name = n;
+ type = t;
+ id = i;
+ }
+
+ public String name = "";
+ public byte type = TType.STOP;
+ public short id = 0;
+}
diff --git a/lib/javame/src/org/apache/thrift/protocol/TList.java b/lib/javame/src/org/apache/thrift/protocol/TList.java
new file mode 100644
index 000000000..88f678561
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/protocol/TList.java
@@ -0,0 +1,36 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift.protocol;
+
+/**
+ * Helper class that encapsulates list metadata.
+ *
+ */
+public class TList {
+ public TList() {}
+
+ public TList(byte t, int s) {
+ elemType = t;
+ size = s;
+ }
+
+ public byte elemType = TType.STOP;
+ public int size = 0;
+}
diff --git a/lib/javame/src/org/apache/thrift/protocol/TMap.java b/lib/javame/src/org/apache/thrift/protocol/TMap.java
new file mode 100644
index 000000000..718a1193e
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/protocol/TMap.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift.protocol;
+
+/**
+ * Helper class that encapsulates map metadata.
+ *
+ */
+public class TMap {
+ public TMap() {}
+
+ public TMap(byte k, byte v, int s) {
+ keyType = k;
+ valueType = v;
+ size = s;
+ }
+
+ public byte keyType = TType.STOP;
+ public byte valueType = TType.STOP;
+ public int size = 0;
+}
diff --git a/lib/javame/src/org/apache/thrift/protocol/TMessage.java b/lib/javame/src/org/apache/thrift/protocol/TMessage.java
new file mode 100644
index 000000000..408b05a9a
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/protocol/TMessage.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift.protocol;
+
+/**
+ * Helper class that encapsulates struct metadata.
+ *
+ */
+public class TMessage {
+ public TMessage() {}
+
+ public TMessage(String n, byte t, int s) {
+ name = n;
+ type = t;
+ seqid = s;
+ }
+
+ public String name = "";
+ public byte type;
+ public int seqid;
+}
diff --git a/lib/javame/src/org/apache/thrift/protocol/TMessageType.java b/lib/javame/src/org/apache/thrift/protocol/TMessageType.java
new file mode 100644
index 000000000..714ea7091
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/protocol/TMessageType.java
@@ -0,0 +1,30 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift.protocol;
+
+/**
+ * Message type constants in the Thrift protocol.
+ *
+ */
+public final class TMessageType {
+ public static final byte CALL = 1;
+ public static final byte REPLY = 2;
+ public static final byte EXCEPTION = 3;
+}
diff --git a/lib/javame/src/org/apache/thrift/protocol/TProtocol.java b/lib/javame/src/org/apache/thrift/protocol/TProtocol.java
new file mode 100644
index 000000000..710e6d4c1
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/protocol/TProtocol.java
@@ -0,0 +1,169 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift.protocol;
+
+import org.apache.thrift.TException;
+import org.apache.thrift.transport.TTransport;
+
+/**
+ * Protocol interface definition.
+ *
+ */
+public abstract class TProtocol {
+
+ /**
+ * Prevent direct instantiation
+ */
+ private TProtocol() {}
+
+ /**
+ * Transport
+ */
+ protected TTransport trans_;
+
+ /**
+ * Constructor
+ */
+ protected TProtocol(TTransport trans) {
+ trans_ = trans;
+ }
+
+ /**
+ * Transport accessor
+ */
+ public TTransport getTransport() {
+ return trans_;
+ }
+
+ /**
+ * Writing methods.
+ */
+
+ public abstract void writeMessageBegin(TMessage message) throws TException;
+
+ public abstract void writeMessageEnd() throws TException;
+
+ public abstract void writeStructBegin(TStruct struct) throws TException;
+
+ public abstract void writeStructEnd() throws TException;
+
+ public abstract void writeFieldBegin(TField field) throws TException;
+
+ public abstract void writeFieldEnd() throws TException;
+
+ public abstract void writeFieldStop() throws TException;
+
+ public abstract void writeMapBegin(TMap map) throws TException;
+
+ public abstract void writeMapEnd() throws TException;
+
+ public abstract void writeListBegin(TList list) throws TException;
+
+ public abstract void writeListEnd() throws TException;
+
+ public abstract void writeSetBegin(TSet set) throws TException;
+
+ public abstract void writeSetEnd() throws TException;
+
+ public abstract void writeBool(boolean b) throws TException;
+
+ public void writeBool(Boolean b) throws TException {
+ writeBool(b.booleanValue());
+ }
+
+ public abstract void writeByte(byte b) throws TException;
+
+ public void writeByte(Byte b) throws TException {
+ writeByte(b.byteValue());
+ }
+
+ public abstract void writeI16(short i16) throws TException;
+
+ public void writeI16(Short i16) throws TException {
+ writeI16(i16.shortValue());
+ }
+
+ public abstract void writeI32(int i32) throws TException;
+
+ public void writeI32(Integer i32) throws TException {
+ writeI32(i32.intValue());
+ }
+
+ public abstract void writeI64(long i64) throws TException;
+
+ public void writeI64(Long i64) throws TException {
+ writeI64(i64.longValue());
+ }
+
+ public abstract void writeDouble(double dub) throws TException;
+
+ public void writeDouble(Double d) throws TException {
+ writeDouble(d.doubleValue());
+ }
+
+ public abstract void writeString(String str) throws TException;
+
+ public abstract void writeBinary(byte[] bin) throws TException;
+
+ /**
+ * Reading methods.
+ */
+
+ public abstract TMessage readMessageBegin() throws TException;
+
+ public abstract void readMessageEnd() throws TException;
+
+ public abstract TStruct readStructBegin() throws TException;
+
+ public abstract void readStructEnd() throws TException;
+
+ public abstract TField readFieldBegin() throws TException;
+
+ public abstract void readFieldEnd() throws TException;
+
+ public abstract TMap readMapBegin() throws TException;
+
+ public abstract void readMapEnd() throws TException;
+
+ public abstract TList readListBegin() throws TException;
+
+ public abstract void readListEnd() throws TException;
+
+ public abstract TSet readSetBegin() throws TException;
+
+ public abstract void readSetEnd() throws TException;
+
+ public abstract boolean readBool() throws TException;
+
+ public abstract byte readByte() throws TException;
+
+ public abstract short readI16() throws TException;
+
+ public abstract int readI32() throws TException;
+
+ public abstract long readI64() throws TException;
+
+ public abstract double readDouble() throws TException;
+
+ public abstract String readString() throws TException;
+
+ public abstract byte[] readBinary() throws TException;
+
+}
diff --git a/lib/javame/src/org/apache/thrift/protocol/TProtocolException.java b/lib/javame/src/org/apache/thrift/protocol/TProtocolException.java
new file mode 100644
index 000000000..248815bec
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/protocol/TProtocolException.java
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift.protocol;
+
+import org.apache.thrift.TException;
+
+/**
+ * Protocol exceptions.
+ *
+ */
+public class TProtocolException extends TException {
+
+
+ private static final long serialVersionUID = 1L;
+ public static final int UNKNOWN = 0;
+ public static final int INVALID_DATA = 1;
+ public static final int NEGATIVE_SIZE = 2;
+ public static final int SIZE_LIMIT = 3;
+ public static final int BAD_VERSION = 4;
+ public static final int NOT_IMPLEMENTED = 5;
+
+ protected int type_ = UNKNOWN;
+
+ public TProtocolException() {
+ super();
+ }
+
+ public TProtocolException(int type) {
+ super();
+ type_ = type;
+ }
+
+ public TProtocolException(int type, String message) {
+ super(message);
+ type_ = type;
+ }
+
+ public TProtocolException(String message) {
+ super(message);
+ }
+
+ public TProtocolException(int type, Throwable cause) {
+ super(cause);
+ type_ = type;
+ }
+
+ public TProtocolException(Throwable cause) {
+ super(cause);
+ }
+
+ public TProtocolException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public TProtocolException(int type, String message, Throwable cause) {
+ super(message, cause);
+ type_ = type;
+ }
+
+ public int getType() {
+ return type_;
+ }
+
+}
diff --git a/lib/javame/src/org/apache/thrift/protocol/TProtocolFactory.java b/lib/javame/src/org/apache/thrift/protocol/TProtocolFactory.java
new file mode 100644
index 000000000..afa502b70
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/protocol/TProtocolFactory.java
@@ -0,0 +1,30 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift.protocol;
+
+import org.apache.thrift.transport.TTransport;
+
+/**
+ * Factory interface for constructing protocol instances.
+ *
+ */
+public interface TProtocolFactory {
+ public TProtocol getProtocol(TTransport trans);
+}
diff --git a/lib/javame/src/org/apache/thrift/protocol/TProtocolUtil.java b/lib/javame/src/org/apache/thrift/protocol/TProtocolUtil.java
new file mode 100644
index 000000000..9bf10f67e
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/protocol/TProtocolUtil.java
@@ -0,0 +1,158 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift.protocol;
+
+import org.apache.thrift.TException;
+
+/**
+ * Utility class with static methods for interacting with protocol data
+ * streams.
+ *
+ */
+public class TProtocolUtil {
+
+ /**
+ * The maximum recursive depth the skip() function will traverse before
+ * throwing a TException.
+ */
+ private static int maxSkipDepth = Integer.MAX_VALUE;
+
+ /**
+ * Specifies the maximum recursive depth that the skip function will
+ * traverse before throwing a TException. This is a global setting, so
+ * any call to skip in this JVM will enforce this value.
+ *
+ * @param depth the maximum recursive depth. A value of 2 would allow
+ * the skip function to skip a structure or collection with basic children,
+ * but it would not permit skipping a struct that had a field containing
+ * a child struct. A value of 1 would only allow skipping of simple
+ * types and empty structs/collections.
+ */
+ public static void setMaxSkipDepth(int depth) {
+ maxSkipDepth = depth;
+ }
+
+ /**
+ * Skips over the next data element from the provided input TProtocol object.
+ *
+ * @param prot the protocol object to read from
+ * @param type the next value will be intepreted as this TType value.
+ */
+ public static void skip(TProtocol prot, byte type)
+ throws TException {
+ skip(prot, type, maxSkipDepth);
+ }
+
+ /**
+ * Skips over the next data element from the provided input TProtocol object.
+ *
+ * @param prot the protocol object to read from
+ * @param type the next value will be intepreted as this TType value.
+ * @param maxDepth this function will only skip complex objects to this
+ * recursive depth, to prevent Java stack overflow.
+ */
+ public static void skip(TProtocol prot, byte type, int maxDepth)
+ throws TException {
+ if (maxDepth <= 0) {
+ throw new TException("Maximum skip depth exceeded");
+ }
+ switch (type) {
+ case TType.BOOL:
+ {
+ prot.readBool();
+ break;
+ }
+ case TType.BYTE:
+ {
+ prot.readByte();
+ break;
+ }
+ case TType.I16:
+ {
+ prot.readI16();
+ break;
+ }
+ case TType.I32:
+ {
+ prot.readI32();
+ break;
+ }
+ case TType.I64:
+ {
+ prot.readI64();
+ break;
+ }
+ case TType.DOUBLE:
+ {
+ prot.readDouble();
+ break;
+ }
+ case TType.STRING:
+ {
+ prot.readBinary();
+ break;
+ }
+ case TType.STRUCT:
+ {
+ prot.readStructBegin();
+ while (true) {
+ TField field = prot.readFieldBegin();
+ if (field.type == TType.STOP) {
+ break;
+ }
+ skip(prot, field.type, maxDepth - 1);
+ prot.readFieldEnd();
+ }
+ prot.readStructEnd();
+ break;
+ }
+ case TType.MAP:
+ {
+ TMap map = prot.readMapBegin();
+ for (int i = 0; i < map.size; i++) {
+ skip(prot, map.keyType, maxDepth - 1);
+ skip(prot, map.valueType, maxDepth - 1);
+ }
+ prot.readMapEnd();
+ break;
+ }
+ case TType.SET:
+ {
+ TSet set = prot.readSetBegin();
+ for (int i = 0; i < set.size; i++) {
+ skip(prot, set.elemType, maxDepth - 1);
+ }
+ prot.readSetEnd();
+ break;
+ }
+ case TType.LIST:
+ {
+ TList list = prot.readListBegin();
+ for (int i = 0; i < list.size; i++) {
+ skip(prot, list.elemType, maxDepth - 1);
+ }
+ prot.readListEnd();
+ break;
+ }
+ default:
+ break;
+ }
+ }
+}
diff --git a/lib/javame/src/org/apache/thrift/protocol/TSet.java b/lib/javame/src/org/apache/thrift/protocol/TSet.java
new file mode 100644
index 000000000..f4e9b1d34
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/protocol/TSet.java
@@ -0,0 +1,36 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift.protocol;
+
+/**
+ * Helper class that encapsulates set metadata.
+ *
+ */
+public class TSet {
+ public TSet() {}
+
+ public TSet(byte t, int s) {
+ elemType = t;
+ size = s;
+ }
+
+ public byte elemType = TType.STOP;
+ public int size = 0;
+}
diff --git a/lib/javame/src/org/apache/thrift/protocol/TStruct.java b/lib/javame/src/org/apache/thrift/protocol/TStruct.java
new file mode 100644
index 000000000..cecbbcfce
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/protocol/TStruct.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift.protocol;
+
+/**
+ * Helper class that encapsulates struct metadata.
+ *
+ */
+public class TStruct {
+ public TStruct() {}
+
+ public TStruct(String n) {
+ name = n;
+ }
+
+ public String name = "";
+}
diff --git a/lib/javame/src/org/apache/thrift/protocol/TType.java b/lib/javame/src/org/apache/thrift/protocol/TType.java
new file mode 100644
index 000000000..dbdc3caa8
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/protocol/TType.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift.protocol;
+
+/**
+ * Type constants in the Thrift protocol.
+ *
+ */
+public final class TType {
+ public static final byte STOP = 0;
+ public static final byte VOID = 1;
+ public static final byte BOOL = 2;
+ public static final byte BYTE = 3;
+ public static final byte DOUBLE = 4;
+ public static final byte I16 = 6;
+ public static final byte I32 = 8;
+ public static final byte I64 = 10;
+ public static final byte STRING = 11;
+ public static final byte STRUCT = 12;
+ public static final byte MAP = 13;
+ public static final byte SET = 14;
+ public static final byte LIST = 15;
+}
diff --git a/lib/javame/src/org/apache/thrift/transport/TFramedTransport.java b/lib/javame/src/org/apache/thrift/transport/TFramedTransport.java
new file mode 100644
index 000000000..c83748ad2
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/transport/TFramedTransport.java
@@ -0,0 +1,122 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift.transport;
+
+import java.io.ByteArrayInputStream;
+
+import org.apache.thrift.TByteArrayOutputStream;
+
+/**
+ * Socket implementation of the TTransport interface. To be commented soon!
+ *
+ */
+public class TFramedTransport extends TTransport {
+
+ /**
+ * Underlying transport
+ */
+ private TTransport transport_ = null;
+
+ /**
+ * Buffer for output
+ */
+ private final TByteArrayOutputStream writeBuffer_ =
+ new TByteArrayOutputStream(1024);
+
+ /**
+ * Buffer for input
+ */
+ private ByteArrayInputStream readBuffer_ = null;
+
+ public static class Factory extends TTransportFactory {
+ public Factory() {
+ }
+
+ public TTransport getTransport(TTransport base) {
+ return new TFramedTransport(base);
+ }
+ }
+
+ /**
+ * Constructor wraps around another tranpsort
+ */
+ public TFramedTransport(TTransport transport) {
+ transport_ = transport;
+ }
+
+ public void open() throws TTransportException {
+ transport_.open();
+ }
+
+ public boolean isOpen() {
+ return transport_.isOpen();
+ }
+
+ public void close() {
+ transport_.close();
+ }
+
+ public int read(byte[] buf, int off, int len) throws TTransportException {
+ if (readBuffer_ != null) {
+ int got = readBuffer_.read(buf, off, len);
+ if (got > 0) {
+ return got;
+ }
+ }
+
+ // Read another frame of data
+ readFrame();
+
+ return readBuffer_.read(buf, off, len);
+ }
+
+ private void readFrame() throws TTransportException {
+ byte[] i32rd = new byte[4];
+ transport_.readAll(i32rd, 0, 4);
+ int size =
+ ((i32rd[0] & 0xff) << 24) |
+ ((i32rd[1] & 0xff) << 16) |
+ ((i32rd[2] & 0xff) << 8) |
+ ((i32rd[3] & 0xff));
+
+ byte[] buff = new byte[size];
+ transport_.readAll(buff, 0, size);
+ readBuffer_ = new ByteArrayInputStream(buff);
+ }
+
+ public void write(byte[] buf, int off, int len) throws TTransportException {
+ writeBuffer_.write(buf, off, len);
+ }
+
+ public void flush() throws TTransportException {
+ byte[] buf = writeBuffer_.get();
+ int len = writeBuffer_.len();
+ writeBuffer_.reset();
+
+ byte[] i32out = new byte[4];
+ i32out[0] = (byte)(0xff & (len >> 24));
+ i32out[1] = (byte)(0xff & (len >> 16));
+ i32out[2] = (byte)(0xff & (len >> 8));
+ i32out[3] = (byte)(0xff & (len));
+ transport_.write(i32out, 0, 4);
+ transport_.write(buf, 0, len);
+ transport_.flush();
+ }
+}
diff --git a/lib/javame/src/org/apache/thrift/transport/THttpClient.java b/lib/javame/src/org/apache/thrift/transport/THttpClient.java
new file mode 100644
index 000000000..451a2e52b
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/transport/THttpClient.java
@@ -0,0 +1,167 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift.transport;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Enumeration;
+import java.util.Hashtable;
+
+import javax.microedition.io.Connector;
+import javax.microedition.io.HttpConnection;
+
+/**
+ * HTTP implementation of the TTransport interface. Used for working with a
+ * Thrift web services implementation.
+ *
+ */
+public class THttpClient extends TTransport {
+
+ private String url_ = null;
+
+ private final ByteArrayOutputStream requestBuffer_ = new ByteArrayOutputStream();
+
+ private InputStream inputStream_ = null;
+
+ private int connectTimeout_ = 0;
+
+ private int readTimeout_ = 0;
+
+ private Hashtable customHeaders_ = null;
+
+ public THttpClient(String url) throws TTransportException {
+ url_ = url;
+ }
+
+ public void setConnectTimeout(int timeout) {
+ connectTimeout_ = timeout;
+ }
+
+ public void setReadTimeout(int timeout) {
+ readTimeout_ = timeout;
+ }
+
+ public void setCustomHeaders(Hashtable headers) {
+ customHeaders_ = headers;
+ }
+
+ public void setCustomHeader(String key, String value) {
+ if (customHeaders_ == null) {
+ customHeaders_ = new Hashtable();
+ }
+ customHeaders_.put(key, value);
+ }
+
+ public void open() {}
+
+ public void close() {
+ if (null != inputStream_) {
+ try {
+ inputStream_.close();
+ } catch (IOException ioe) {
+ }
+ inputStream_ = null;
+ }
+ }
+
+ public boolean isOpen() {
+ return true;
+ }
+
+ public int read(byte[] buf, int off, int len) throws TTransportException {
+ if (inputStream_ == null) {
+ throw new TTransportException("Response buffer is empty, no request.");
+ }
+ try {
+ int ret = inputStream_.read(buf, off, len);
+ if (ret == -1) {
+ throw new TTransportException("No more data available.");
+ }
+ return ret;
+ } catch (IOException iox) {
+ throw new TTransportException(iox);
+ }
+ }
+
+ public void write(byte[] buf, int off, int len) {
+ requestBuffer_.write(buf, off, len);
+ }
+
+ public void flush() throws TTransportException {
+ // Extract request and reset buffer
+ byte[] data = requestBuffer_.toByteArray();
+ requestBuffer_.reset();
+
+ try {
+ // Create connection object
+ HttpConnection connection = (HttpConnection)Connector.open(url_);
+
+ // Timeouts, only if explicitly set
+ if (connectTimeout_ > 0) {
+ // XXX: not available
+ // connection.setConnectTimeout(connectTimeout_);
+ }
+ if (readTimeout_ > 0) {
+ // XXX: not available
+ // connection.setReadTimeout(readTimeout_);
+ }
+
+ // Make the request
+ connection.setRequestMethod("POST");
+ connection.setRequestProperty("Content-Type", "application/x-thrift");
+ connection.setRequestProperty("Accept", "application/x-thrift");
+ connection.setRequestProperty("User-Agent", "JavaME/THttpClient");
+
+ connection.setRequestProperty("Connection", "Keep-Alive");
+ connection.setRequestProperty("Keep-Alive", "5000");
+ connection.setRequestProperty("Http-version", "HTTP/1.1");
+ connection.setRequestProperty("Cache-Control", "no-transform");
+
+
+ if (customHeaders_ != null) {
+ for (Enumeration e = customHeaders_.keys() ; e.hasMoreElements() ;) {
+ String key = (String)e.nextElement();
+ String value = (String)customHeaders_.get(key);
+ connection.setRequestProperty(key, value);
+ }
+ }
+ // connection.setDoOutput(true);
+ // connection.connect();
+
+ OutputStream os = connection.openOutputStream();
+ os.write(data);
+ os.close();
+
+ int responseCode = connection.getResponseCode();
+ if (responseCode != HttpConnection.HTTP_OK) {
+ throw new TTransportException("HTTP Response code: " + responseCode);
+ }
+
+ // Read the responses
+ inputStream_ = connection.openInputStream();
+
+ } catch (IOException iox) {
+ System.out.println(iox.toString());
+ throw new TTransportException(iox);
+ }
+ }
+}
diff --git a/lib/javame/src/org/apache/thrift/transport/TIOStreamTransport.java b/lib/javame/src/org/apache/thrift/transport/TIOStreamTransport.java
new file mode 100644
index 000000000..ccf706461
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/transport/TIOStreamTransport.java
@@ -0,0 +1,153 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift.transport;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+/**
+ * This is the most commonly used base transport. It takes an InputStream
+ * and an OutputStream and uses those to perform all transport operations.
+ * This allows for compatibility with all the nice constructs Java already
+ * has to provide a variety of types of streams.
+ *
+ */
+public class TIOStreamTransport extends TTransport {
+
+
+ /** Underlying inputStream */
+ protected InputStream inputStream_ = null;
+
+ /** Underlying outputStream */
+ protected OutputStream outputStream_ = null;
+
+ /**
+ * Subclasses can invoke the default constructor and then assign the input
+ * streams in the open method.
+ */
+ protected TIOStreamTransport() {}
+
+ /**
+ * Input stream constructor.
+ *
+ * @param is Input stream to read from
+ */
+ public TIOStreamTransport(InputStream is) {
+ inputStream_ = is;
+ }
+
+ /**
+ * Output stream constructor.
+ *
+ * @param os Output stream to read from
+ */
+ public TIOStreamTransport(OutputStream os) {
+ outputStream_ = os;
+ }
+
+ /**
+ * Two-way stream constructor.
+ *
+ * @param is Input stream to read from
+ * @param os Output stream to read from
+ */
+ public TIOStreamTransport(InputStream is, OutputStream os) {
+ inputStream_ = is;
+ outputStream_ = os;
+ }
+
+ /**
+ * The streams must already be open at construction time, so this should
+ * always return true.
+ *
+ * @return true
+ */
+ public boolean isOpen() {
+ return true;
+ }
+
+ /**
+ * The streams must already be open. This method does nothing.
+ */
+ public void open() throws TTransportException {}
+
+ /**
+ * Closes both the input and output streams.
+ */
+ public void close() {
+ if (inputStream_ != null) {
+ try {
+ inputStream_.close();
+ } catch (IOException iox) {
+ }
+ inputStream_ = null;
+ }
+ if (outputStream_ != null) {
+ try {
+ outputStream_.close();
+ } catch (IOException iox) {
+ }
+ outputStream_ = null;
+ }
+ }
+
+ /**
+ * Reads from the underlying input stream if not null.
+ */
+ public int read(byte[] buf, int off, int len) throws TTransportException {
+ if (inputStream_ == null) {
+ throw new TTransportException(TTransportException.NOT_OPEN, "Cannot read from null inputStream");
+ }
+ try {
+ return inputStream_.read(buf, off, len);
+ } catch (IOException iox) {
+ throw new TTransportException(TTransportException.UNKNOWN, iox);
+ }
+ }
+
+ /**
+ * Writes to the underlying output stream if not null.
+ */
+ public void write(byte[] buf, int off, int len) throws TTransportException {
+ if (outputStream_ == null) {
+ throw new TTransportException(TTransportException.NOT_OPEN, "Cannot write to null outputStream");
+ }
+ try {
+ outputStream_.write(buf, off, len);
+ } catch (IOException iox) {
+ throw new TTransportException(TTransportException.UNKNOWN, iox);
+ }
+ }
+
+ /**
+ * Flushes the underlying output stream if not null.
+ */
+ public void flush() throws TTransportException {
+ if (outputStream_ == null) {
+ throw new TTransportException(TTransportException.NOT_OPEN, "Cannot flush null outputStream");
+ }
+ try {
+ outputStream_.flush();
+ } catch (IOException iox) {
+ throw new TTransportException(TTransportException.UNKNOWN, iox);
+ }
+ }
+}
diff --git a/lib/javame/src/org/apache/thrift/transport/TTransport.java b/lib/javame/src/org/apache/thrift/transport/TTransport.java
new file mode 100644
index 000000000..a6c047bb5
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/transport/TTransport.java
@@ -0,0 +1,121 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift.transport;
+
+/**
+ * Generic class that encapsulates the I/O layer. This is basically a thin
+ * wrapper around the combined functionality of Java input/output streams.
+ *
+ */
+public abstract class TTransport {
+
+ /**
+ * Queries whether the transport is open.
+ *
+ * @return True if the transport is open.
+ */
+ public abstract boolean isOpen();
+
+ /**
+ * Is there more data to be read?
+ *
+ * @return True if the remote side is still alive and feeding us
+ */
+ public boolean peek() {
+ return isOpen();
+ }
+
+ /**
+ * Opens the transport for reading/writing.
+ *
+ * @throws TTransportException if the transport could not be opened
+ */
+ public abstract void open()
+ throws TTransportException;
+
+ /**
+ * Closes the transport.
+ */
+ public abstract void close();
+
+ /**
+ * Reads up to len bytes into buffer buf, starting att offset off.
+ *
+ * @param buf Array to read into
+ * @param off Index to start reading at
+ * @param len Maximum number of bytes to read
+ * @return The number of bytes actually read
+ * @throws TTransportException if there was an error reading data
+ */
+ public abstract int read(byte[] buf, int off, int len)
+ throws TTransportException;
+
+ /**
+ * Guarantees that all of len bytes are actually read off the transport.
+ *
+ * @param buf Array to read into
+ * @param off Index to start reading at
+ * @param len Maximum number of bytes to read
+ * @return The number of bytes actually read, which must be equal to len
+ * @throws TTransportException if there was an error reading data
+ */
+ public int readAll(byte[] buf, int off, int len)
+ throws TTransportException {
+ int got = 0;
+ int ret = 0;
+ while (got < len) {
+ ret = read(buf, off+got, len-got);
+ if (ret <= 0) {
+ throw new TTransportException("Cannot read. Remote side has closed. Tried to read " + len + " bytes, but only got " + got + " bytes.");
+ }
+ got += ret;
+ }
+ return got;
+ }
+
+ /**
+ * Writes the buffer to the output
+ *
+ * @param buf The output data buffer
+ * @throws TTransportException if an error occurs writing data
+ */
+ public void write(byte[] buf) throws TTransportException {
+ write(buf, 0, buf.length);
+ }
+
+ /**
+ * Writes up to len bytes from the buffer.
+ *
+ * @param buf The output data buffer
+ * @param off The offset to start writing from
+ * @param len The number of bytes to write
+ * @throws TTransportException if there was an error writing data
+ */
+ public abstract void write(byte[] buf, int off, int len)
+ throws TTransportException;
+
+ /**
+ * Flush any pending data out of a transport buffer.
+ *
+ * @throws TTransportException if there was an error writing out data.
+ */
+ public void flush()
+ throws TTransportException {}
+}
diff --git a/lib/javame/src/org/apache/thrift/transport/TTransportException.java b/lib/javame/src/org/apache/thrift/transport/TTransportException.java
new file mode 100644
index 000000000..d08f3b02b
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/transport/TTransportException.java
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift.transport;
+
+import org.apache.thrift.TException;
+
+/**
+ * Transport exceptions.
+ *
+ */
+public class TTransportException extends TException {
+
+ private static final long serialVersionUID = 1L;
+
+ public static final int UNKNOWN = 0;
+ public static final int NOT_OPEN = 1;
+ public static final int ALREADY_OPEN = 2;
+ public static final int TIMED_OUT = 3;
+ public static final int END_OF_FILE = 4;
+
+ protected int type_ = UNKNOWN;
+
+ public TTransportException() {
+ super();
+ }
+
+ public TTransportException(int type) {
+ super();
+ type_ = type;
+ }
+
+ public TTransportException(int type, String message) {
+ super(message);
+ type_ = type;
+ }
+
+ public TTransportException(String message) {
+ super(message);
+ }
+
+ public TTransportException(int type, Throwable cause) {
+ super(cause);
+ type_ = type;
+ }
+
+ public TTransportException(Throwable cause) {
+ super(cause);
+ }
+
+ public TTransportException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public TTransportException(int type, String message, Throwable cause) {
+ super(message, cause);
+ type_ = type;
+ }
+
+ public int getType() {
+ return type_;
+ }
+
+}
diff --git a/lib/javame/src/org/apache/thrift/transport/TTransportFactory.java b/lib/javame/src/org/apache/thrift/transport/TTransportFactory.java
new file mode 100644
index 000000000..45b607f3e
--- /dev/null
+++ b/lib/javame/src/org/apache/thrift/transport/TTransportFactory.java
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift.transport;
+
+/**
+ * Factory class used to create wrapped instance of Transports.
+ * This is used primarily in servers, which get Transports from
+ * a ServerTransport and then may want to mutate them (i.e. create
+ * a BufferedTransport from the underlying base transport)
+ *
+ */
+public class TTransportFactory {
+
+ /**
+ * Return a wrapped instance of the base Transport.
+ *
+ * @param in The base transport
+ * @returns Wrapped Transport
+ */
+ public TTransport getTransport(TTransport trans) {
+ return trans;
+ }
+
+}