diff options
Diffstat (limited to 'cpp/src')
-rw-r--r-- | cpp/src/Slice/JavaUtil.cpp | 998 | ||||
-rw-r--r-- | cpp/src/Slice/JavaUtil.h | 3 | ||||
-rw-r--r-- | cpp/src/slice2java/Gen.cpp | 393 | ||||
-rw-r--r-- | cpp/src/slice2java/Gen.h | 4 | ||||
-rw-r--r-- | cpp/src/slice2java/GenCompat.cpp | 7 |
5 files changed, 555 insertions, 850 deletions
diff --git a/cpp/src/Slice/JavaUtil.cpp b/cpp/src/Slice/JavaUtil.cpp index 5387f7bab41..6dbb2fb7be5 100644 --- a/cpp/src/Slice/JavaUtil.cpp +++ b/cpp/src/Slice/JavaUtil.cpp @@ -960,9 +960,9 @@ Slice::JavaCompatGenerator::getPackage(const ContainedPtr& cont) const string Slice::JavaCompatGenerator::getAbsolute(const ContainedPtr& cont, - const string& package, - const string& prefix, - const string& suffix) const + const string& package, + const string& prefix, + const string& suffix) const { string name = cont->name(); if(prefix == "" && suffix == "") @@ -1886,11 +1886,11 @@ Slice::JavaCompatGenerator::writeMarshalUnmarshalCode(Output& out, { out << nl << stream << ".skipSize();"; } - out << nl << v << " = " << typeS << ".ice_read(" << stream << ", " << v << ");"; + out << nl << v << " = " << typeS << ".ice_read(" << stream << ");"; } else { - out << nl << v << " = " << typeS << ".ice_read(" << stream << ", " << v << ");"; + out << nl << v << " = " << typeS << ".ice_read(" << stream << ");"; } } return; @@ -2275,14 +2275,14 @@ Slice::JavaCompatGenerator::writeMarshalUnmarshalCode(Output& out, ConstructedPtr constructed = ConstructedPtr::dynamicCast(type); assert(constructed); - string typeS = getAbsolute(constructed, package); + string typeS = getAbsolute(constructed, package, "", "Helper"); if(marshal) { - out << nl << typeS << "Helper.write(" << stream << ", " << v << ");"; + out << nl << typeS << ".write(" << stream << ", " << v << ");"; } else { - out << nl << v << " = " << typeS << "Helper.read(" << stream << ");"; + out << nl << v << " = " << typeS << ".read(" << stream << ");"; } } @@ -2335,14 +2335,14 @@ Slice::JavaCompatGenerator::writeDictionaryMarshalUnmarshalCode(Output& out, // if(useHelper) { - string typeS = getAbsolute(dict, package); + string typeS = getAbsolute(dict, package, "", "Helper"); if(marshal) { - out << nl << typeS << "Helper.write(" << stream << ", " << v << ");"; + out << nl << typeS << ".write(" << stream << ", " << v << ");"; } else { - out << nl << v << " = " << typeS << "Helper.read(" << stream << ");"; + out << nl << v << " = " << typeS << ".read(" << stream << ");"; } return; } @@ -2643,14 +2643,14 @@ Slice::JavaCompatGenerator::writeSequenceMarshalUnmarshalCode(Output& out, // if(useHelper) { - string typeS = getAbsolute(seq, package); + string typeS = getAbsolute(seq, package, "", "Helper"); if(marshal) { - out << nl << typeS << "Helper.write(" << stream << ", " << v << ");"; + out << nl << typeS << ".write(" << stream << ", " << v << ");"; } else { - out << nl << v << " = " << typeS << "Helper.read(" << stream << ");"; + out << nl << v << " = " << typeS << ".read(" << stream << ");"; } return; } @@ -3067,10 +3067,10 @@ Slice::JavaCompatGenerator::hasTypeMetaData(const TypePtr& type, const StringLis bool Slice::JavaCompatGenerator::getDictionaryTypes(const DictionaryPtr& dict, - const string& package, - const StringList& metaData, - string& instanceType, - string& formalType) const + const string& package, + const StringList& metaData, + string& instanceType, + string& formalType) const { // // Get the types of the key and value. @@ -3102,10 +3102,10 @@ Slice::JavaCompatGenerator::getDictionaryTypes(const DictionaryPtr& dict, bool Slice::JavaCompatGenerator::getSequenceTypes(const SequencePtr& seq, - const string& package, - const StringList& metaData, - string& instanceType, - string& formalType) const + const string& package, + const StringList& metaData, + string& instanceType, + string& formalType) const { BuiltinPtr builtin = BuiltinPtr::dynamicCast(seq->type()); if(builtin) @@ -3767,7 +3767,26 @@ Slice::JavaGenerator::writeMarshalUnmarshalCode(Output& out, { stream = marshal ? "ostr" : "istr"; } + const bool optionalParam = mode == OptionalInParam || mode == OptionalOutParam || mode == OptionalReturnParam; + string typeS = typeToString(type, TypeModeIn, package, metaData); + + assert(!marshal || mode != OptionalMember); // Only support OptionalMember for un-marshaling + + static const char* builtinTable[] = + { + "Byte", + "Bool", + "Short", + "Int", + "Long", + "Float", + "Double", + "String", + "???", + "???", + "???" + }; const BuiltinPtr builtin = BuiltinPtr::dynamicCast(type); if(builtin) @@ -3775,209 +3794,35 @@ Slice::JavaGenerator::writeMarshalUnmarshalCode(Output& out, switch(builtin->kind()) { case Builtin::KindByte: - { - if(marshal) - { - if(optionalParam) - { - out << nl << stream << ".writeByte(" << tag << ", " << param << ");"; - } - else - { - out << nl << stream << ".writeByte(" << param << ");"; - } - } - else - { - if(optionalParam) - { - out << nl << param << " = " << stream << ".readByte(" << tag << ");"; - } - else - { - out << nl << param << " = " << stream << ".readByte();"; - } - } - break; - } case Builtin::KindBool: - { - if(marshal) - { - if(optionalParam) - { - out << nl << stream << ".writeBool(" << tag << ", " << param << ");"; - } - else - { - out << nl << stream << ".writeBool(" << param << ");"; - } - } - else - { - if(optionalParam) - { - out << nl << param << " = " << stream << ".readBool(" << tag << ");"; - } - else - { - out << nl << param << " = " << stream << ".readBool();"; - } - } - break; - } case Builtin::KindShort: - { - if(marshal) - { - if(optionalParam) - { - out << nl << stream << ".writeShort(" << tag << ", " << param << ");"; - } - else - { - out << nl << stream << ".writeShort(" << param << ");"; - } - } - else - { - if(optionalParam) - { - out << nl << param << " = " << stream << ".readShort(" << tag << ");"; - } - else - { - out << nl << param << " = " << stream << ".readShort();"; - } - } - break; - } case Builtin::KindInt: - { - if(marshal) - { - if(optionalParam) - { - out << nl << stream << ".writeInt(" << tag << ", " << param << ");"; - } - else - { - out << nl << stream << ".writeInt(" << param << ");"; - } - } - else - { - if(optionalParam) - { - out << nl << param << " = " << stream << ".readInt(" << tag << ");"; - } - else - { - out << nl << param << " = " << stream << ".readInt();"; - } - } - break; - } case Builtin::KindLong: - { - if(marshal) - { - if(optionalParam) - { - out << nl << stream << ".writeLong(" << tag << ", " << param << ");"; - } - else - { - out << nl << stream << ".writeLong(" << param << ");"; - } - } - else - { - if(optionalParam) - { - out << nl << param << " = " << stream << ".readLong(" << tag << ");"; - } - else - { - out << nl << param << " = " << stream << ".readLong();"; - } - } - break; - } case Builtin::KindFloat: - { - if(marshal) - { - if(optionalParam) - { - out << nl << stream << ".writeFloat(" << tag << ", " << param << ");"; - } - else - { - out << nl << stream << ".writeFloat(" << param << ");"; - } - } - else - { - if(optionalParam) - { - out << nl << param << " = " << stream << ".readFloat(" << tag << ");"; - } - else - { - out << nl << param << " = " << stream << ".readFloat();"; - } - } - break; - } case Builtin::KindDouble: - { - if(marshal) - { - if(optionalParam) - { - out << nl << stream << ".writeDouble(" << tag << ", " << param << ");"; - } - else - { - out << nl << stream << ".writeDouble(" << param << ");"; - } - } - else - { - if(optionalParam) - { - out << nl << param << " = " << stream << ".readDouble(" << tag << ");"; - } - else - { - out << nl << param << " = " << stream << ".readDouble();"; - } - } - break; - } case Builtin::KindString: { + string s = builtinTable[builtin->kind()]; if(marshal) { if(optionalParam) { - out << nl << stream << ".writeString(" << tag << ", " << param << ");"; + out << nl << stream << ".write" << s << "(" << tag << ", " << param << ");"; } else { - out << nl << stream << ".writeString(" << param << ");"; + out << nl << stream << ".write" << s << "(" << param << ");"; } } else { if(optionalParam) { - out << nl << param << " = " << stream << ".readString(" << tag << ");"; + out << nl << param << " = " << stream << ".read" << s << "(" << tag << ");"; } else { - out << nl << param << " = " << stream << ".readString();"; + out << nl << param << " = " << stream << ".read" << s << "();"; } } break; @@ -4018,12 +3863,6 @@ Slice::JavaGenerator::writeMarshalUnmarshalCode(Output& out, { out << nl << stream << ".writeProxy(" << tag << ", " << param << ");"; } - else if(mode == OptionalMember) - { - out << nl << "int pos = " << stream << ".startSize();"; - out << nl << stream << ".writeProxy(" << param << ");"; - out << nl << stream << ".endSize(pos);"; - } else { out << nl << stream << ".writeProxy(" << param << ");"; @@ -4059,36 +3898,11 @@ Slice::JavaGenerator::writeMarshalUnmarshalCode(Output& out, ProxyPtr prx = ProxyPtr::dynamicCast(type); if(prx) { - string typeS = typeToString(type, TypeModeIn, package); if(marshal) { if(optionalParam) { - if(optionalMapping) - { - out << nl << "if(" << param << " != null && " << param << ".isPresent() && " << stream - << ".writeOptional(" << tag << ", " << getOptionalFormat(type) << "))"; - out << sb; - out << nl << "int pos = " << stream << ".startSize();"; - out << nl << stream << ".writeProxy(" << param << ".get());"; - out << nl << stream << ".endSize(pos);"; - out << eb; - } - else - { - out << nl << "if(" << stream << ".writeOptional(" << tag << ", " << getOptionalFormat(type) << "))"; - out << sb; - out << nl << "int pos = " << stream << ".startSize();"; - out << nl << stream << ".writeProxy(" << param << ");"; - out << nl << stream << ".endSize(pos);"; - out << eb; - } - } - else if(mode == OptionalMember) - { - out << nl << "int pos = " << stream << ".startSize();"; - out << nl << stream << ".writeProxy(" << param << ");"; - out << nl << stream << ".endSize(pos);"; + out << nl << stream << ".writeProxy(" << tag << ", " << param << ");"; } else { @@ -4099,16 +3913,7 @@ Slice::JavaGenerator::writeMarshalUnmarshalCode(Output& out, { if(optionalParam) { - out << nl << "if(" << stream << ".readOptional(" << tag << ", " << getOptionalFormat(type) << "))"; - out << sb; - out << nl << stream << ".skip(4);"; - out << nl << param << " = java.util.Optional.ofNullable(" << typeS << ".uncheckedCast(" << stream - << ".readProxy()));"; - out << eb; - out << nl << "else"; - out << sb; - out << nl << param << " = java.util.Optional.empty();"; - out << eb; + out << nl << param << " = " << stream << ".readProxy(" << tag << ", " << typeS << "::uncheckedCast);"; } else if(mode == OptionalMember) { @@ -4152,177 +3957,49 @@ Slice::JavaGenerator::writeMarshalUnmarshalCode(Output& out, return; } - StructPtr st = StructPtr::dynamicCast(type); - if(st) - { - string typeS = typeToString(type, TypeModeIn, package, metaData); - if(marshal) - { - if(optionalParam || mode == OptionalMember) - { - string val; - if(optionalParam) - { - if(optionalMapping) - { - out << nl << "if(" << param << " != null && " << param << ".isPresent() && " << stream - << ".writeOptional(" << tag << ", " << getOptionalFormat(type) << "))"; - val = param + ".get()"; - } - else - { - out << nl << "if(" << stream << ".writeOptional(" << tag << ", " << getOptionalFormat(type) - << "))"; - val = param; - } - out << sb; - } - else - { - val = param; - } - - if(st->isVariableLength()) - { - out << nl << "int pos = " << stream << ".startSize();"; - out << nl << typeS << ".ice_write(" << stream << ", " << val << ");"; - out << nl << stream << ".endSize(pos);"; - } - else - { - out << nl << stream << ".writeSize(" << st->minWireSize() << ");"; - out << nl << typeS << ".ice_write(" << stream << ", " << val << ");"; - } - if(optionalParam) - { - out << eb; - } - } - else - { - out << nl << typeS << ".ice_write(" << stream << ", " << param << ");"; - } - } - else - { - if(optionalParam) - { - out << nl << "if(" << stream << ".readOptional(" << tag << ", " << getOptionalFormat(type) << "))"; - out << sb; - - if(st->isVariableLength()) - { - out << nl << stream << ".skip(4);"; - } - else - { - out << nl << stream << ".skipSize();"; - } - - out << nl << param << " = java.util.Optional.of(" << typeS << ".ice_read(" << stream - << ", null));"; - - out << eb; - out << nl << "else"; - out << sb; - out << nl << param << " = java.util.Optional.empty();"; - out << eb; - } - else if(mode == OptionalMember) - { - if(st->isVariableLength()) - { - out << nl << stream << ".skip(4);"; - } - else - { - out << nl << stream << ".skipSize();"; - } - out << nl << param << " = " << typeS << ".ice_read(" << stream << ", " << param << ");"; - } - else - { - out << nl << param << " = " << typeS << ".ice_read(" << stream << ", " << param << ");"; - } - } - return; - } - - EnumPtr en = EnumPtr::dynamicCast(type); - if(en) + DictionaryPtr dict = DictionaryPtr::dynamicCast(type); + if(dict) { - string typeS = typeToString(type, TypeModeIn, package, metaData); - if(marshal) + if(optionalParam || mode == OptionalMember) { - if(optionalParam) + string instanceType, formalType, origInstanceType, origFormalType; + getDictionaryTypes(dict, "", metaData, instanceType, formalType, false); + getDictionaryTypes(dict, "", StringList(), origInstanceType, origFormalType, false); + if(formalType == origFormalType && (marshal || instanceType == origInstanceType)) { - if(optionalMapping) + // + // If we can use the helper, it's easy. + // + string helper = getAbsolute(dict, package, "", "Helper"); + if(marshal) { - out << nl << "if(" << param << " != null && " << param << ".isPresent() && " << stream - << ".writeOptional(" << tag << ", " << getOptionalFormat(type) << "))"; - out << sb; - out << nl << typeS << ".ice_write(" << stream << ", " << param << ".get());"; - out << eb; + out << nl << helper << ".write" << spar << stream << tag << param << epar << ";"; + return; } - else + else if(mode != OptionalMember) { - out << nl << "if(" << stream << ".writeOptional(" << tag << ", " << getOptionalFormat(type) << "))"; - out << sb; - out << nl << typeS << ".ice_write(" << stream << ", " << param << ");"; - out << eb; + out << nl << param << " = " << helper << ".read" << spar << stream << tag << epar << ";"; + return; } } - else - { - out << nl << typeS << ".ice_write(" << stream << ", " << param << ");"; - } - } - else - { - if(optionalParam) - { - out << nl << "if(" << stream << ".readOptional(" << tag << ", " << getOptionalFormat(type) << "))"; - out << sb; - out << nl << param << " = java.util.Optional.of(" << typeS << ".ice_read(" << stream << "));"; - out << eb; - out << nl << "else"; - out << sb; - out << nl << param << " = java.util.Optional.empty();"; - out << eb; - } - else - { - out << nl << param << " = " << typeS << ".ice_read(" << stream << ");"; - } - } - return; - } - DictionaryPtr dict = DictionaryPtr::dynamicCast(type); - if(dict) - { - if(optionalParam || mode == OptionalMember) - { - string typeS = typeToString(type, TypeModeIn, package, metaData); TypePtr keyType = dict->keyType(); TypePtr valueType = dict->valueType(); - if(marshal) { if(optionalParam) { + out << nl; if(optionalMapping) { - out << nl << "if(" << param << " != null && " << param << ".isPresent() && " << stream + out << "if(" << param << " != null && " << param << ".isPresent() && " << stream << ".writeOptional(" << tag << ", " << getOptionalFormat(type) << "))"; - out << sb; } else { - out << nl << "if(" << stream << ".writeOptional(" << tag << ", " << getOptionalFormat(type) - << "))"; - out << sb; + out << "if(" << stream << ".writeOptional(" << tag << ", " << getOptionalFormat(type) << "))"; } + out << sb; } if(keyType->isVariableLength() || valueType->isVariableLength()) @@ -4334,21 +4011,12 @@ Slice::JavaGenerator::writeMarshalUnmarshalCode(Output& out, } else { - const size_t wireSize = keyType->minWireSize() + valueType->minWireSize(); - string tmpName; - if(optionalParam && optionalMapping) - { - tmpName = "optDict"; - out << nl << "final " << typeS << ' ' << tmpName << " = " << param << ".get();"; - } - else - { - tmpName = param; - } - out << nl << "final int optSize = " << tmpName << " == null ? 0 : " << tmpName << ".size();"; - out << nl << stream << ".writeSize(optSize > 254 ? optSize * " << wireSize - << " + 5 : optSize * " << wireSize << " + 1);"; - writeDictionaryMarshalUnmarshalCode(out, package, dict, tmpName, marshal, iter, true, customStream, metaData); + const size_t sz = keyType->minWireSize() + valueType->minWireSize(); + string d = optionalParam && optionalMapping ? param + ".get()" : param; + out << nl << "final int optSize = " << d << " == null ? 0 : " << d << ".size();"; + out << nl << stream + << ".writeSize(optSize > 254 ? optSize * " << sz << " + 5 : optSize * " << sz << " + 1);"; + writeDictionaryMarshalUnmarshalCode(out, package, dict, d, marshal, iter, true, customStream, metaData); } if(optionalParam) @@ -4358,20 +4026,13 @@ Slice::JavaGenerator::writeMarshalUnmarshalCode(Output& out, } else { - string tmpName; - + string d = optionalParam ? "optDict" : param; if(optionalParam) { - tmpName = "optDict"; out << nl << "if(" << stream << ".readOptional(" << tag << ", " << getOptionalFormat(type) << "))"; out << sb; - out << nl << typeS << ' ' << tmpName << ';'; + out << nl << typeS << ' ' << d << ';'; } - else - { - tmpName = param; - } - if(keyType->isVariableLength() || valueType->isVariableLength()) { out << nl << stream << ".skip(4);"; @@ -4380,12 +4041,10 @@ Slice::JavaGenerator::writeMarshalUnmarshalCode(Output& out, { out << nl << stream << ".skipSize();"; } - - writeDictionaryMarshalUnmarshalCode(out, package, dict, tmpName, marshal, iter, true, customStream, metaData); - + writeDictionaryMarshalUnmarshalCode(out, package, dict, d, marshal, iter, true, customStream, metaData); if(optionalParam) { - out << nl << param << " = java.util.Optional.of(" << tmpName << ");"; + out << nl << param << " = java.util.Optional.of(" << d << ");"; out << eb; out << nl << "else"; out << sb; @@ -4406,81 +4065,73 @@ Slice::JavaGenerator::writeMarshalUnmarshalCode(Output& out, { if(optionalParam || mode == OptionalMember) { - string typeS = typeToString(type, TypeModeIn, package, metaData); + string ignore; TypePtr elemType = seq->type(); - BuiltinPtr elemBuiltin = BuiltinPtr::dynamicCast(elemType); - - if(optionalParam && elemBuiltin && elemBuiltin->kind() != Builtin::KindObject && - elemBuiltin->kind() != Builtin::KindObjectProxy && elemBuiltin->kind() != Builtin::KindValue && - !hasTypeMetaData(seq, metaData)) + BuiltinPtr eltBltin = BuiltinPtr::dynamicCast(elemType); + if(!hasTypeMetaData(seq, metaData) && eltBltin && eltBltin->kind() < Builtin::KindObject) { - static const char* builtinTable[] = + string bs = builtinTable[eltBltin->kind()]; + if(marshal) { - "Byte", - "Bool", - "Short", - "Int", - "Long", - "Float", - "Double", - "String", - "???", - "???", - "???" - }; - - switch(elemBuiltin->kind()) + out << nl << stream << ".write" << bs << "Seq(" << tag << ", " << param << ");"; + return; + } + else if(mode != OptionalMember) { - case Builtin::KindByte: - case Builtin::KindBool: - case Builtin::KindShort: - case Builtin::KindInt: - case Builtin::KindLong: - case Builtin::KindFloat: - case Builtin::KindDouble: - case Builtin::KindString: + out << nl << param << " = " << stream << ".read" << bs << "Seq(" << tag << ");"; + return; + } + } + else if(findMetaData("java:serializable", seq->getMetaData(), ignore)) + { + if(marshal) + { + out << nl << stream << ".writeSerializable" << spar << tag << param << epar << ";"; + return; + } + else if(mode != OptionalMember) + { + out << nl << param << " = " << stream << ".readSerializable" << spar << tag << typeS + ".class" << epar << ";"; + return; + } + } + else if(!hasTypeMetaData(seq, metaData) || + findMetaData("java:type", seq->getMetaData(), ignore) || + findMetaData("java:type", metaData, ignore)) + { + string instanceType, formalType, origInstanceType, origFormalType; + getSequenceTypes(seq, "", metaData, instanceType, formalType, false); + getSequenceTypes(seq, "", StringList(), origInstanceType, origFormalType, false); + if(formalType == origFormalType && (marshal || instanceType == origInstanceType)) + { + string helper = getAbsolute(seq, package, "", "Helper"); + if(marshal) { - string bs = builtinTable[elemBuiltin->kind()]; - - if(marshal) - { - out << nl << stream << ".write" << bs << "Seq(" << tag << ", " << param << ");"; - } - else - { - out << nl << param << " = " << stream << ".read" << bs << "Seq(" << tag << ");"; - } + out << nl << helper << ".write" << spar << stream << tag << param << epar << ";"; return; } - case Builtin::KindValue: - case Builtin::KindObject: - case Builtin::KindObjectProxy: - case Builtin::KindLocalObject: + else if(mode != OptionalMember) { - assert(false); - break; + out << nl << param << " = " << helper << ".read" << spar << stream << tag << epar << ";"; + return; } } } - string ignore; - const size_t wireSize = elemType->minWireSize(); - if(marshal) { if(optionalParam) { + out << nl; if(optionalMapping) { - out << nl << "if(" << param << " != null && " << param << ".isPresent() && " << stream + out << "if(" << param << " != null && " << param << ".isPresent() && " << stream << ".writeOptional(" << tag << ", " << getOptionalFormat(type) << "))"; } else { - out << nl << "if(" << stream << ".writeOptional(" << tag << ", " << getOptionalFormat(type) - << "))"; + out << "if(" << stream << ".writeOptional(" << tag << ", " << getOptionalFormat(type) << "))"; } - out << sb; } @@ -4488,70 +4139,34 @@ Slice::JavaGenerator::writeMarshalUnmarshalCode(Output& out, { string s = optionalParam && optionalMapping ? param + ".get()" : param; out << nl << "int pos = " << stream << ".startSize();"; - writeSequenceMarshalUnmarshalCode(out, package, seq, s, marshal, iter, true, customStream, metaData); + writeSequenceMarshalUnmarshalCode(out, package, seq, s, true, iter, true, customStream, metaData); out << nl << stream << ".endSize(pos);"; } - else if(findMetaData("java:type:", metaData, ignore) || - findMetaData("java:type:", seq->getMetaData(), ignore)) - { - // - // The sequence is an instance of java.util.List<E>, where E is a fixed-size type. - // If the element type is bool or byte, we do NOT write an extra size. - // - - string tmpName; - if(optionalParam && optionalMapping) - { - tmpName = "optSeq"; - out << nl << "final " << typeS << ' ' << tmpName << " = " << param << ".get();"; - } - else - { - tmpName = param; - } - - if(wireSize > 1) - { - out << nl << "final int optSize = " << tmpName << " == null ? 0 : " << tmpName << ".size();"; - out << nl << stream << ".writeSize(optSize > 254 ? optSize * " << wireSize - << " + 5 : optSize * " << wireSize << " + 1);"; - } - writeSequenceMarshalUnmarshalCode(out, package, seq, tmpName, marshal, iter, true, customStream, metaData); - } - else if(findMetaData("java:protobuf:", seq->getMetaData(), ignore) || - findMetaData("java:serializable:", seq->getMetaData(), ignore)) - { - // - // This just writes a byte sequence. - // - string s = optionalParam && optionalMapping ? param + ".get()" : param; - writeSequenceMarshalUnmarshalCode(out, package, seq, s, marshal, iter, true, customStream, metaData); - } else { - // - // At this point we have a regular Java array of a fixed-size type. - // - - string tmpName; - if(optionalParam && optionalMapping) - { - tmpName = "optSeq"; - out << nl << "final " << typeS << ' ' << tmpName << " = " << param << ".get();"; - } - else - { - tmpName = param; - } - - if(wireSize > 1) + const size_t sz = elemType->minWireSize(); + string s = optionalParam && optionalMapping ? param + ".get()" : param; + if(sz > 1) { - out << nl << "final int optSize = " << tmpName << " == null ? 0 : " << tmpName << ".length;"; - out << nl << stream << ".writeSize(optSize > 254 ? optSize * " << wireSize - << " + 5 : optSize * " << wireSize << " + 1);"; + string ignore; + out << nl << "final int optSize = " << s << " == null ? 0 : "; + if(findMetaData("java:buffer", seq->getMetaData(), ignore) || + findMetaData("java:buffer", metaData, ignore)) + { + out << s << ".remaining() / " << sz << ";"; + } + else if(hasTypeMetaData(seq, metaData)) + { + out << s << ".size();"; + } + else + { + out << s << ".length;"; + } + out << nl << stream << ".writeSize(optSize > 254 ? optSize * " << sz + << " + 5 : optSize * " << sz << " + 1);"; } - - writeSequenceMarshalUnmarshalCode(out, package, seq, tmpName, marshal, iter, true, customStream, metaData); + writeSequenceMarshalUnmarshalCode(out, package, seq, s, true, iter, true, customStream, metaData); } if(optionalParam) @@ -4561,46 +4176,26 @@ Slice::JavaGenerator::writeMarshalUnmarshalCode(Output& out, } else { - string tmpName; + const size_t sz = elemType->minWireSize(); + string s = optionalParam ? "optSeq" : param; if(optionalParam) { - tmpName = "optSeq"; out << nl << "if(" << stream << ".readOptional(" << tag << ", " << getOptionalFormat(type) << "))"; out << sb; - out << nl << typeS << ' ' << tmpName << ';'; + out << nl << typeS << ' ' << s << ';'; } - else - { - tmpName = param; - } - if(elemType->isVariableLength()) { out << nl << stream << ".skip(4);"; } - else if(wireSize > 1) + else if(sz > 1) { - if(findMetaData("java:type:", metaData, ignore) || - findMetaData("java:type:", seq->getMetaData(), ignore)) - { - // - // The sequence is an instance of java.util.List<E>, where E is a fixed-size type. - // - - out << nl << stream << ".skipSize();"; - } - else if(!findMetaData("java:protobuf:", seq->getMetaData(), ignore) && - !findMetaData("java:serializable:", seq->getMetaData(), ignore)) - { - out << nl << stream << ".skipSize();"; - } + out << nl << stream << ".skipSize();"; } - - writeSequenceMarshalUnmarshalCode(out, package, seq, tmpName, marshal, iter, true, customStream, metaData); - + writeSequenceMarshalUnmarshalCode(out, package, seq, s, false, iter, true, customStream, metaData); if(optionalParam) { - out << nl << param << " = java.util.Optional.of(" << tmpName << ");"; + out << nl << param << " = java.util.Optional.of(" << s << ");"; out << eb; out << nl << "else"; out << sb; @@ -4617,15 +4212,34 @@ Slice::JavaGenerator::writeMarshalUnmarshalCode(Output& out, } ConstructedPtr constructed = ConstructedPtr::dynamicCast(type); + StructPtr st = StructPtr::dynamicCast(type); assert(constructed); - string typeS = getAbsolute(constructed, package); if(marshal) { - out << nl << typeS << ".write(" << stream << ", " << param << ");"; + if(optionalParam) + { + out << nl << typeS << ".ice_write(" << stream << ", " << tag << ", " << param << ");"; + } + else + { + out << nl << typeS << ".ice_write(" << stream << ", " << param << ");"; + } } else { - out << nl << param << " = " << typeS << ".read(" << stream << ");"; + if(optionalParam) + { + out << nl << param << " = " << typeS << ".ice_read(" << stream << ", " << tag << ");"; + } + else if(mode == OptionalMember && st) + { + out << nl << stream << (st->isVariableLength() ? ".skip(4);" : ".skipSize();"); + out << nl << param << " = " << typeS << ".ice_read(" << stream << ");"; + } + else + { + out << nl << param << " = " << typeS << ".ice_read(" << stream << ");"; + } } } @@ -4648,8 +4262,6 @@ Slice::JavaGenerator::writeDictionaryMarshalUnmarshalCode(Output& out, string v = param; - string instanceType; - // // We have to determine whether it's possible to use the // type's generated helper class for this marshal/unmarshal @@ -4664,28 +4276,22 @@ Slice::JavaGenerator::writeDictionaryMarshalUnmarshalCode(Output& out, // without local metadata to determine whether we can use // the helper. // - string formalType; + string instanceType, formalType, origInstanceType, origFormalType; getDictionaryTypes(dict, "", metaData, instanceType, formalType, false); - string origInstanceType, origFormalType; getDictionaryTypes(dict, "", StringList(), origInstanceType, origFormalType, false); - if((formalType != origFormalType) || (!marshal && instanceType != origInstanceType)) - { - useHelper = false; - } - - // - // If we can use the helper, it's easy. - // - if(useHelper) + if(useHelper && formalType == origFormalType && (marshal || instanceType == origInstanceType)) { - string typeS = getAbsolute(dict, package); + // + // If we can use the helper, it's easy. + // + string helper = getAbsolute(dict, package, "", "Helper"); if(marshal) { - out << nl << typeS << "Helper.write(" << stream << ", " << v << ");"; + out << nl << helper << ".write" << spar << stream << v << epar << ";"; } else { - out << nl << v << " = " << typeS << "Helper.read(" << stream << ");"; + out << nl << v << " = " << helper << ".read" << spar << stream << epar << ";"; } return; } @@ -4712,8 +4318,8 @@ Slice::JavaGenerator::writeDictionaryMarshalUnmarshalCode(Output& out, out << nl << "ostr.writeSize(" << v << ".size());"; string keyObjectS = typeToObjectString(key, TypeModeIn, package); string valueObjectS = typeToObjectString(value, TypeModeIn, package); - out << nl << "for(java.util.Map.Entry<" << keyObjectS << ", " << valueObjectS << "> e : " << v - << ".entrySet())"; + out << nl; + out << "for(java.util.Map.Entry<" << keyObjectS << ", " << valueObjectS << "> e : " << v << ".entrySet())"; out << sb; for(int i = 0; i < 2; i++) { @@ -4740,51 +4346,32 @@ Slice::JavaGenerator::writeDictionaryMarshalUnmarshalCode(Output& out, out << nl << "int sz" << iterS << " = " << stream << ".readSize();"; out << nl << "for(int i" << iterS << " = 0; i" << iterS << " < sz" << iterS << "; i" << iterS << "++)"; out << sb; - for(int i = 0; i < 2; i++) + + BuiltinPtr b = BuiltinPtr::dynamicCast(value); + if(ClassDeclPtr::dynamicCast(value) || (b && b->usesClasses())) { - string arg; - TypePtr type; - string typeS; - if(i == 0) - { - arg = "key"; - type = key; - typeS = keyS; - } - else - { - arg = "value"; - type = value; - typeS = valueS; - } + out << nl << "final " << keyS << " key;"; + writeMarshalUnmarshalCode(out, package, key, OptionalNone, false, 0, "key", false, iter, customStream); - BuiltinPtr b = BuiltinPtr::dynamicCast(type); - if(ClassDeclPtr::dynamicCast(type) || (b && b->usesClasses())) - { - string keyTypeStr = typeToObjectString(key, TypeModeIn, package); - string valueTypeStr = typeToObjectString(value, TypeModeIn, package); - writeMarshalUnmarshalCode(out, package, type, OptionalNone, false, 0, arg, false, iter, customStream, StringList(), - "new com.zeroc.IceInternal.DictionaryPatcher<" + keyTypeStr + ", " + - valueTypeStr + ">(" + v + ", " + typeS + ".class, " + typeS + - ".ice_staticId(), key)"); - } - else - { - if(StructPtr::dynamicCast(type)) - { - out << nl << typeS << ' ' << arg << " = null;"; - } - else - { - out << nl << typeS << ' ' << arg << ';'; - } - writeMarshalUnmarshalCode(out, package, type, OptionalNone, false, 0, arg, false, iter, customStream); - } + string valueS = typeToObjectString(value, TypeModeIn, package); + ostringstream patchParams; + patchParams << "value -> " << v << ".put(key, value), " << valueS << ".class"; + writeMarshalUnmarshalCode(out, package, value, OptionalNone, false, 0, "value", false, iter, customStream, + StringList(), patchParams.str()); } - BuiltinPtr builtin = BuiltinPtr::dynamicCast(value); - if(!(builtin && builtin->usesClasses()) && !ClassDeclPtr::dynamicCast(value)) + else { - out << nl << "" << v << ".put(key, value);"; + out << nl << keyS << " key;"; + writeMarshalUnmarshalCode(out, package, key, OptionalNone, false, 0, "key", false, iter, customStream); + + out << nl << valueS << " value;"; + writeMarshalUnmarshalCode(out, package, value, OptionalNone, false, 0, "value", false, iter, customStream); + + BuiltinPtr builtin = BuiltinPtr::dynamicCast(value); + if(!(builtin && builtin->usesClasses()) && !ClassDeclPtr::dynamicCast(value)) + { + out << nl << "" << v << ".put(key, value);"; + } } out << eb; } @@ -4807,6 +4394,7 @@ Slice::JavaGenerator::writeSequenceMarshalUnmarshalCode(Output& out, stream = marshal ? "ostr" : "istr"; } + string typeS = typeToString(seq, TypeModeIn, package); string v = param; // @@ -4827,8 +4415,7 @@ Slice::JavaGenerator::writeSequenceMarshalUnmarshalCode(Output& out, } else { - string type = typeToString(seq, TypeModeIn, package); - out << nl << v << " = (" << type << ")" << stream << ".readSerializable();"; + out << nl << v << " = " << stream << ".readSerializable(" << typeS << ".class);"; } return; } @@ -4847,7 +4434,7 @@ Slice::JavaGenerator::writeSequenceMarshalUnmarshalCode(Output& out, string type = typeToString(seq, TypeModeIn, package); out << nl << "try"; out << sb; - out << nl << v << " = " << type << ".parseFrom(" << stream << ".readByteSeq());"; + out << nl << v << " = " << typeS << ".parseFrom(" << stream << ".readByteSeq());"; out << eb; out << nl << "catch(com.google.protobuf.InvalidProtocolBufferException ex)"; out << sb; @@ -4858,6 +4445,18 @@ Slice::JavaGenerator::writeSequenceMarshalUnmarshalCode(Output& out, } } + static const char* builtinTable[] = + { + "Byte", + "Bool", + "Short", + "Int", + "Long", + "Float", + "Double", + "String" + }; + if(builtin && (builtin->kind() == Builtin::KindByte || builtin->kind() == Builtin::KindShort || builtin->kind() == Builtin::KindInt || builtin->kind() == Builtin::KindLong || @@ -4867,97 +4466,30 @@ Slice::JavaGenerator::writeSequenceMarshalUnmarshalCode(Output& out, static const string bytebuffer = "java:buffer"; if(seq->findMetaData(bytebuffer, meta) || findMetaData(bytebuffer, metaData, meta)) { - switch(builtin->kind()) + if(marshal) { - case Builtin::KindByte: - { - if(marshal) - { - out << nl << stream << ".writeByteBuffer(" << v << ");"; - } - else - { - out << nl << v << " = " << stream << ".readByteBuffer();"; - } - break; - } - case Builtin::KindShort: - { - if(marshal) - { - out << nl << stream << ".writeShortBuffer(" << v << ");"; - } - else - { - out << nl << v << " = " << stream << ".readShortBuffer();"; - } - break; - } - case Builtin::KindInt: - { - if(marshal) - { - out << nl << stream << ".writeIntBuffer(" << v << ");"; - } - else - { - out << nl << v << " = " << stream << ".readIntBuffer();"; - } - break; - } - case Builtin::KindLong: - { - if(marshal) - { - out << nl << stream << ".writeLongBuffer(" << v << ");"; - } - else - { - out << nl << v << " = " << stream << ".readLongBuffer();"; - } - break; - } - case Builtin::KindFloat: - { - if(marshal) - { - out << nl << stream << ".writeFloatBuffer(" << v << ");"; - } - else - { - out << nl << v << " = " << stream << ".readFloatBuffer();"; - } - break; - } - case Builtin::KindDouble: - { - if(marshal) - { - out << nl << stream << ".writeDoubleBuffer(" << v << ");"; - } - else - { - out << nl << v << " = " << stream << ".readDoubleBuffer();"; - } - break; - } - case Builtin::KindBool: - case Builtin::KindString: - case Builtin::KindObject: - case Builtin::KindObjectProxy: - case Builtin::KindLocalObject: - case Builtin::KindValue: - { - assert(false); - break; - } + out << nl << stream << ".write" << builtinTable[builtin->kind()] << "Buffer(" << v << ");"; + } + else + { + out << nl << v << " = " << stream << ".read" << builtinTable[builtin->kind()] << "Buffer();"; } return; } } - bool customType = false; - string instanceType; + if(!hasTypeMetaData(seq, metaData) && builtin && builtin->kind() <= Builtin::KindString) + { + if(marshal) + { + out << nl << stream << ".write" << builtinTable[builtin->kind()] << "Seq(" << v << ");"; + } + else + { + out << nl << v << " = " << stream << ".read" << builtinTable[builtin->kind()] << "Seq();"; + } + return; + } // // We have to determine whether it's possible to use the @@ -4973,28 +4505,22 @@ Slice::JavaGenerator::writeSequenceMarshalUnmarshalCode(Output& out, // without local metadata to determine whether we can use // the helper. // - string formalType; - customType = getSequenceTypes(seq, "", metaData, instanceType, formalType, false); - string origInstanceType, origFormalType; + string instanceType, formalType, origInstanceType, origFormalType; + bool customType = getSequenceTypes(seq, "", metaData, instanceType, formalType, false); getSequenceTypes(seq, "", StringList(), origInstanceType, origFormalType, false); - if((formalType != origFormalType) || (!marshal && instanceType != origInstanceType)) + if(useHelper && formalType == origFormalType && (marshal || instanceType == origInstanceType)) { - useHelper = false; - } - - // - // If we can use the helper, it's easy. - // - if(useHelper) - { - string typeS = getAbsolute(seq, package); + // + // If we can use the helper, it's easy. + // + string helper = getAbsolute(seq, package, "", "Helper"); if(marshal) { - out << nl << typeS << "Helper.write(" << stream << ", " << v << ");"; + out << nl << helper << ".write" << spar << stream << v << epar << ";"; } else { - out << nl << v << " = " << typeS << "Helper.read(" << stream << ");"; + out << nl << v << " = " << helper << ".read" << spar << stream << epar << ";"; } return; } @@ -5064,8 +4590,7 @@ Slice::JavaGenerator::writeSequenceMarshalUnmarshalCode(Output& out, out << nl << v << " = new " << instanceType << "();"; out << nl << "final int len" << iter << " = " << stream << ".readAndCheckSeqSize(" << type->minWireSize() << ");"; - out << nl << "for(int i" << iter << " = 0; i" << iter << " < len" << iter << "; i" << iter - << "++)"; + out << nl << "for(int i" << iter << " = 0; i" << iter << " < len" << iter << "; i" << iter << "++)"; out << sb; if(isObject) { @@ -5074,25 +4599,16 @@ Slice::JavaGenerator::writeSequenceMarshalUnmarshalCode(Output& out, // out << nl << v << ".add(null);"; ostringstream patchParams; - patchParams << "new com.zeroc.IceInternal.ListPatcher<" << origContentS << ">(" << v << ", " - << origContentS << ".class, " << origContentS << ".ice_staticId(), i" << iter << ')'; + out << nl << "final int fi" << iter << " = i" << iter << ";"; + patchParams << "value -> " << v << ".set(fi" << iter << ", value), " << origContentS << ".class"; + writeMarshalUnmarshalCode(out, package, type, OptionalNone, false, 0, "elem", false, iter, customStream, StringList(), patchParams.str()); } else { - if(StructPtr::dynamicCast(type)) - { - out << nl << cont << " elem = null;"; - } - else - { - out << nl << cont << " elem;"; - } + out << nl << cont << " elem;"; writeMarshalUnmarshalCode(out, package, type, OptionalNone, false, 0, "elem", false, iter, customStream); - } - if(!isObject) - { out << nl << v << ".add(elem);"; } out << eb; @@ -5294,9 +4810,8 @@ Slice::JavaGenerator::writeSequenceMarshalUnmarshalCode(Output& out, if(isObject) { ostringstream patchParams; - patchParams << "new com.zeroc.IceInternal.SequencePatcher<" << origContentS << ">(" << v << ", " - << origContentS << ".class, " << origContentS << ".ice_staticId(), i" << iter - << ')'; + out << nl << "final int fi" << iter << " = i" << iter << ";"; + patchParams << "value -> " << v << "[fi" << iter << "] = value, " << origContentS << ".class"; writeMarshalUnmarshalCode(out, package, type, OptionalNone, false, 0, o.str(), false, iter, customStream, StringList(), patchParams.str()); } @@ -5389,7 +4904,8 @@ Slice::JavaGenerator::hasTypeMetaData(const TypePtr& type, const StringList& loc } } - if(findMetaData("java:buffer", localMetaData, directive)) + if(findMetaData("java:buffer", metaData, directive) || + findMetaData("java:buffer", localMetaData, directive)) { SequencePtr seq = SequencePtr::dynamicCast(cont); if(seq) diff --git a/cpp/src/Slice/JavaUtil.h b/cpp/src/Slice/JavaUtil.h index fcc77d30f63..776c8169321 100644 --- a/cpp/src/Slice/JavaUtil.h +++ b/cpp/src/Slice/JavaUtil.h @@ -334,6 +334,9 @@ protected: OptionalMember }; + std::string getWriteFunction(const std::string&, const TypePtr&); + std::string getReadFunction(const std::string&, const TypePtr&); + void writeMarshalUnmarshalCode(::IceUtilInternal::Output&, const std::string&, const TypePtr&, OptionalMode, bool, int, const std::string&, bool, int&, const std::string& = "", const StringList& = StringList(), const std::string& = ""); diff --git a/cpp/src/slice2java/Gen.cpp b/cpp/src/slice2java/Gen.cpp index 34237ddc394..304ea15e175 100644 --- a/cpp/src/slice2java/Gen.cpp +++ b/cpp/src/slice2java/Gen.cpp @@ -445,14 +445,14 @@ Slice::JavaVisitor::writeResultType(Output& out, const OperationPtr& op, const s for(ParamDeclList::const_iterator pli = required.begin(); pli != required.end(); ++pli) { const string paramName = fixKwd((*pli)->name()); - const string patchParams = getPatcher((*pli)->type(), package, paramName, false); + const string patchParams = getPatcher((*pli)->type(), package, "this." + paramName); writeMarshalUnmarshalCode(out, package, (*pli)->type(), OptionalNone, false, 0, "this." + paramName, false, iter, "", (*pli)->getMetaData(), patchParams); } if(ret && !op->returnIsOptional()) { - const string patchParams = getPatcher(ret, package, retval, false); + const string patchParams = getPatcher(ret, package, retval); writeMarshalUnmarshalCode(out, package, ret, OptionalNone, false, 0, retval, false, iter, "", op->getMetaData(), patchParams); } @@ -466,21 +466,21 @@ Slice::JavaVisitor::writeResultType(Output& out, const OperationPtr& op, const s { if(checkReturnType && op->returnTag() < (*pli)->tag()) { - const string patchParams = getPatcher(ret, package, retval, true); + const string patchParams = getPatcher(ret, package, retval); writeMarshalUnmarshalCode(out, package, ret, OptionalReturnParam, true, op->returnTag(), retval, false, iter, "", op->getMetaData(), patchParams); checkReturnType = false; } const string paramName = fixKwd((*pli)->name()); - const string patchParams = getPatcher((*pli)->type(), package, paramName, true); + const string patchParams = getPatcher((*pli)->type(), package, paramName); writeMarshalUnmarshalCode(out, package, (*pli)->type(), OptionalOutParam, true, (*pli)->tag(), "this." + paramName, false, iter, "", (*pli)->getMetaData(), patchParams); } if(checkReturnType) { - const string patchParams = getPatcher(ret, package, retval, true); + const string patchParams = getPatcher(ret, package, retval); writeMarshalUnmarshalCode(out, package, ret, OptionalReturnParam, true, op->returnTag(), retval, false, iter, "", op->getMetaData(), patchParams); } @@ -695,7 +695,8 @@ Slice::JavaVisitor::writeMarshaledResultType(Output& out, const OperationPtr& op } void -Slice::JavaVisitor::allocatePatcher(Output& out, const TypePtr& type, const string& package, const string& name) +Slice::JavaVisitor::allocatePatcher(Output& out, const TypePtr& type, const string& package, const string& name, + bool optionalMapping) { BuiltinPtr b = BuiltinPtr::dynamicCast(type); ClassDeclPtr cl = ClassDeclPtr::dynamicCast(type); @@ -710,13 +711,16 @@ Slice::JavaVisitor::allocatePatcher(Output& out, const TypePtr& type, const stri { clsName = getAbsolute(cl, package); } - - out << nl << "com.zeroc.IceInternal.Patcher<" << clsName << "> " << name << " = new com.zeroc.IceInternal.Patcher<" - << clsName << ">(" << clsName << ".class, " << clsName << ".ice_staticId());"; + if(optionalMapping) + { + clsName = "java.util.Optional<" + clsName + ">"; + } + out << nl << "final com.zeroc.IceInternal.Holder<" << clsName << "> " + << name << " = new com.zeroc.IceInternal.Holder<>();"; } string -Slice::JavaVisitor::getPatcher(const TypePtr& type, const string& package, const string& dest, bool optionalMapping) +Slice::JavaVisitor::getPatcher(const TypePtr& type, const string& package, const string& dest) { BuiltinPtr b = BuiltinPtr::dynamicCast(type); ClassDeclPtr cl = ClassDeclPtr::dynamicCast(type); @@ -732,18 +736,7 @@ Slice::JavaVisitor::getPatcher(const TypePtr& type, const string& package, const { clsName = getAbsolute(cl, package); } - - ostr << "new com.zeroc.IceInternal.Patcher<" << clsName << ">(" << clsName << ".class, " - << clsName << ".ice_staticId(), " << "v -> "; - if(optionalMapping) - { - ostr << dest << " = java.util.Optional.ofNullable(v)"; - } - else - { - ostr << dest << " = v"; - } - ostr << ')'; + ostr << "v -> " << dest << " = v, " << clsName << ".class"; } return ostr.str(); } @@ -913,34 +906,24 @@ Slice::JavaVisitor::writeUnmarshalProxyResults(Output& out, const string& packag int iter = 0; + if(val) + { + allocatePatcher(out, type, package, name, optional); + } + else + { + out << nl << resultType << ' ' << name << ';'; + } + string patchParams = getPatcher(type, package, name + ".value"); if(optional) { - if(val) - { - allocatePatcher(out, type, package, name); - } - else - { - out << nl << resultType << ' ' << name << ';'; - } writeMarshalUnmarshalCode(out, package, type, ret ? OptionalReturnParam : OptionalOutParam, true, - tag, name, false, iter, "", metaData, name); + tag, name, false, iter, "", metaData, patchParams); } else { - if(val) - { - allocatePatcher(out, type, package, name); - } - else if(StructPtr::dynamicCast(type)) - { - out << nl << resultType << ' ' << name << " = null;"; - } - else - { - out << nl << resultType << ' ' << name << ';'; - } - writeMarshalUnmarshalCode(out, package, type, OptionalNone, false, 0, name, false, iter, "", metaData, name); + writeMarshalUnmarshalCode(out, package, type, OptionalNone, false, 0, name, false, iter, "", metaData, + patchParams); } if(op->returnsClasses(false)) @@ -948,11 +931,7 @@ Slice::JavaVisitor::writeUnmarshalProxyResults(Output& out, const string& packag out << nl << "istr.readPendingValues();"; } - if(optional && val) - { - out << nl << "return java.util.Optional.ofNullable(" << name << ".value);"; - } - else if(val) + if(val) { out << nl << "return " << name << ".value;"; } @@ -1039,16 +1018,16 @@ Slice::JavaVisitor::writeThrowsClause(const string& package, const ExceptionList } void -Slice::JavaVisitor::writeMarshalDataMember(Output& out, const string& package, const DataMemberPtr& member, int& iter, bool forStruct) +Slice::JavaVisitor::writeMarshalDataMember(Output& out, const string& package, const DataMemberPtr& member, int& iter, + bool forStruct) { if(member->optional()) { assert(!forStruct); - out << nl << "if(_" << member->name() << " && ostr_.writeOptional(" << member->tag() << ", " - << getOptionalFormat(member->type()) << "))"; + out << nl << "if(_" << member->name() << ")"; out << sb; - writeMarshalUnmarshalCode(out, package, member->type(), OptionalMember, false, 0, fixKwd(member->name()), true, - iter, "ostr_", member->getMetaData()); + writeMarshalUnmarshalCode(out, package, member->type(), OptionalInParam, false, member->tag(), + fixKwd(member->name()), true, iter, "ostr_", member->getMetaData()); out << eb; } else @@ -1066,11 +1045,12 @@ Slice::JavaVisitor::writeMarshalDataMember(Output& out, const string& package, c } void -Slice::JavaVisitor::writeUnmarshalDataMember(Output& out, const string& package, const DataMemberPtr& member, int& iter, bool forStruct) +Slice::JavaVisitor::writeUnmarshalDataMember(Output& out, const string& package, const DataMemberPtr& member, int& iter, + bool forStruct) { // TBD: Handle passing interface-by-value - const string patchParams = getPatcher(member->type(), package, fixKwd(member->name()), false); + const string patchParams = getPatcher(member->type(), package, fixKwd(member->name())); if(member->optional()) { @@ -1264,7 +1244,7 @@ Slice::JavaVisitor::writeDispatch(Output& out, const ClassDefPtr& p) const TypePtr paramType = (*pli)->type(); if(isValue(paramType)) { - allocatePatcher(out, paramType, package, "icePP_" + (*pli)->name()); + allocatePatcher(out, paramType, package, "icePP_" + (*pli)->name(), (*pli)->optional()); values.push_back(*pli); } else @@ -1272,21 +1252,7 @@ Slice::JavaVisitor::writeDispatch(Output& out, const ClassDefPtr& p) const string paramName = "iceP_" + (*pli)->name(); const string typeS = typeToString(paramType, TypeModeIn, package, (*pli)->getMetaData(), true, (*pli)->optional()); - if((*pli)->optional()) - { - out << nl << typeS << ' ' << paramName << ';'; - } - else - { - if(StructPtr::dynamicCast(paramType)) - { - out << nl << typeS << ' ' << paramName << " = null;"; - } - else - { - out << nl << typeS << ' ' << paramName << ';'; - } - } + out << nl << typeS << ' ' << paramName << ';'; } } @@ -1299,14 +1265,16 @@ Slice::JavaVisitor::writeDispatch(Output& out, const ClassDefPtr& p) for(ParamDeclList::const_iterator pli = required.begin(); pli != required.end(); ++pli) { const string paramName = isValue((*pli)->type()) ? ("icePP_" + (*pli)->name()) : "iceP_" + (*pli)->name(); + const string patchParams = getPatcher((*pli)->type(), package, paramName + ".value"); writeMarshalUnmarshalCode(out, package, (*pli)->type(), OptionalNone, false, 0, paramName, false, - iter, "", (*pli)->getMetaData(), paramName); + iter, "", (*pli)->getMetaData(), patchParams); } for(ParamDeclList::const_iterator pli = optional.begin(); pli != optional.end(); ++pli) { const string paramName = isValue((*pli)->type()) ? ("icePP_" + (*pli)->name()) : "iceP_" + (*pli)->name(); + const string patchParams = getPatcher((*pli)->type(), package, paramName + ".value"); writeMarshalUnmarshalCode(out, package, (*pli)->type(), OptionalInParam, true, (*pli)->tag(), - paramName, false, iter, "", (*pli)->getMetaData(), paramName); + paramName, false, iter, "", (*pli)->getMetaData(), patchParams); } if(op->sendsClasses(false)) { @@ -1316,19 +1284,9 @@ Slice::JavaVisitor::writeDispatch(Output& out, const ClassDefPtr& p) for(ParamDeclList::const_iterator pli = values.begin(); pli != values.end(); ++pli) { - const TypePtr paramType = (*pli)->type(); - const string paramName = "iceP_" + (*pli)->name(); - const string typeS = typeToString(paramType, TypeModeIn, package, (*pli)->getMetaData(), true, + const string typeS = typeToString((*pli)->type(), TypeModeIn, package, (*pli)->getMetaData(), true, (*pli)->optional()); - if((*pli)->optional()) - { - out << nl << typeS << ' ' << paramName << " = java.util.Optional.ofNullable(icePP_" << (*pli)->name() - << ".value);"; - } - else - { - out << nl << typeS << ' ' << paramName << " = icePP_" << (*pli)->name() << ".value;"; - } + out << nl << typeS << ' ' << "iceP_" << (*pli)->name() << " = icePP_" << (*pli)->name() << ".value;"; } } else @@ -3365,6 +3323,7 @@ Slice::Gen::TypesVisitor::visitStructStart(const StructPtr& p) { out << "," << nl << *q; } + out.restoreIndent(); out << sb; return true; @@ -3581,16 +3540,63 @@ Slice::Gen::TypesVisitor::visitStructEnd(const StructPtr& p) out << eb; out << eb; - out << sp << nl << "static public " << name << " ice_read(com.zeroc.Ice.InputStream istr, " << name << " v)"; - out << sb; - out << nl << "if(v == null)"; + out << sp << nl << "static public " << name << " ice_read(com.zeroc.Ice.InputStream istr)"; out << sb; - out << nl << " v = new " << name << "();"; - out << eb; + out << nl << name << " v = new " << name << "();"; out << nl << "v.ice_readMembers(istr);"; out << nl << "return v;"; out << eb; + string optName = "java.util.Optional<" + name + ">"; + out << sp; + out << nl << "static public void ice_write(com.zeroc.Ice.OutputStream ostr, int tag, " << optName << " v)"; + out << sb; + out << nl << "if(v != null && v.isPresent())"; + out << sb; + out << nl << "ice_write(ostr, tag, v.get());"; + out << eb; + out << eb; + + out << sp; + out << nl << "static public void ice_write(com.zeroc.Ice.OutputStream ostr, int tag, " << name << " v)"; + out << sb; + out << nl << "if(ostr.writeOptional(tag, " << getOptionalFormat(p) << "))"; + out << sb; + if(p->isVariableLength()) + { + out << nl << "int pos = ostr.startSize();"; + out << nl << "ice_write(ostr, v);"; + out << nl << "ostr.endSize(pos);"; + } + else + { + out << nl << "ostr.writeSize(" << p->minWireSize() << ");"; + out << nl << "ice_write(ostr, v);"; + } + out << eb; + out << eb; + + out << sp; + out << nl << "static public " << optName << " ice_read(com.zeroc.Ice.InputStream istr, int tag)"; + out << sb; + out << nl << "if(istr.readOptional(tag, " << getOptionalFormat(p) << "))"; + out << sb; + if(p->isVariableLength()) + { + out << nl << "istr.skip(4);"; + } + else + { + out << nl << "istr.skipSize();"; + } + out << nl << "return java.util.Optional.of(" << typeS << ".ice_read(istr));"; + out << eb; + out << nl << "else"; + out << sb; + out << nl << "return java.util.Optional.empty();"; + out << eb; + out << eb; + out << nl << nl << "private static final " << name << " _nullMarshalValue = new " << name << "();"; } @@ -4077,6 +4083,38 @@ Slice::Gen::TypesVisitor::visitEnum(const EnumPtr& p) out << nl << "return validate(v);"; out << eb; + string optName = "java.util.Optional<" + name + ">"; + out << sp; + out << nl << "public static void ice_write(com.zeroc.Ice.OutputStream ostr, int tag, " << optName << " v)"; + out << sb; + out << nl << "if(v != null && v.isPresent())"; + out << sb; + out << nl << "ice_write(ostr, tag, v.get());"; + out << eb; + out << eb; + + out << sp; + out << nl << "public static void ice_write(com.zeroc.Ice.OutputStream ostr, int tag, " << name << " v)"; + out << sb; + out << nl << "if(ostr.writeOptional(tag, " << getOptionalFormat(p) << "))"; + out << sb; + out << nl << "ice_write(ostr, v);"; + out << eb; + out << eb; + + out << sp; + out << nl << "public static " << optName << " ice_read(com.zeroc.Ice.InputStream istr, int tag)"; + out << sb; + out << nl << "if(istr.readOptional(tag, " << getOptionalFormat(p) << "))"; + out << sb; + out << nl << "return java.util.Optional.of(ice_read(istr));"; + out << eb; + out << nl << "else"; + out << sb; + out << nl << "return java.util.Optional.empty();"; + out << eb; + out << eb; + out << sp << nl << "private static " << name << " validate(int v)"; out << sb; out << nl << "final " << name << " e = valueOf(v);"; @@ -4191,18 +4229,15 @@ Slice::Gen::HelperVisitor::visitSequence(const SequencePtr& p) return; } + string meta; BuiltinPtr builtin = BuiltinPtr::dynamicCast(p->type()); - if(builtin && - (builtin->kind() == Builtin::KindByte || builtin->kind() == Builtin::KindShort || - builtin->kind() == Builtin::KindInt || builtin->kind() == Builtin::KindLong || - builtin->kind() == Builtin::KindFloat || builtin->kind() == Builtin::KindDouble)) + if(!hasTypeMetaData(p) && builtin && builtin->kind() <= Builtin::KindString) { - string prefix = "java:buffer"; - string meta; - if(p->findMetaData(prefix, meta)) - { - return; // No holders for buffer types. - } + return; // No helpers for sequence of primitive types + } + else if(hasTypeMetaData(p) && !p->findMetaData("java:type", meta)) + { + return; // No helpers for custom metadata other than java:type } string name = p->name(); @@ -4283,12 +4318,110 @@ Slice::Gen::HelperVisitor::visitSequence(const SequencePtr& p) } out << nl << "public static " << typeS << " read(com.zeroc.Ice.InputStream istr)"; out << sb; - out << nl << typeS << " v;"; + out << nl << "final " << typeS << " v;"; iter = 0; writeSequenceMarshalUnmarshalCode(out, package, p, "v", false, iter, false); out << nl << "return v;"; out << eb; + + static const char* builtinTable[] = { "Byte", "Bool", "Short", "Int", "Long", "Float", "Double", "String" }; + + string optTypeS = "java.util.Optional<" + typeS + ">"; + out << sp; + out << nl << "public static void write(com.zeroc.Ice.OutputStream ostr, int tag, " << optTypeS << " v)"; + out << sb; + if(!hasTypeMetaData(p) && builtin && builtin->kind() < Builtin::KindObject) + { + out << nl << "ostr.write" << builtinTable[builtin->kind()] << "Seq(tag, v);"; + } + else + { + out << nl << "if(v != null && v.isPresent())"; + out << sb; + out << nl << "write(ostr, tag, v.get());"; + out << eb; + } + out << eb; + + out << sp; + out << nl << "public static void write(com.zeroc.Ice.OutputStream ostr, int tag, " << typeS << " v)"; + out << sb; + if(!hasTypeMetaData(p) && builtin && builtin->kind() < Builtin::KindObject) + { + out << nl << "ostr.write" << builtinTable[builtin->kind()] << "Seq(tag, v);"; + } + else + { + out << nl << "if(ostr.writeOptional(tag, " << getOptionalFormat(p) << "))"; + out << sb; + if(p->type()->isVariableLength()) + { + out << nl << "int pos = ostr.startSize();"; + writeSequenceMarshalUnmarshalCode(out, package, p, "v", true, iter, true); + out << nl << "ostr.endSize(pos);"; + } + else + { + // + // The sequence is an instance of java.util.List<E>, where E is a fixed-size type. + // If the element type is bool or byte, we do NOT write an extra size. + // + const size_t sz = p->type()->minWireSize(); + if(sz > 1) + { + string metaData; + out << nl << "final int optSize = v == null ? 0 : "; + if(findMetaData("java:buffer", p->getMetaData(), metaData)) + { + out << "v.remaining() / " << sz << ";"; + } + else if(hasTypeMetaData(p)) + { + out << "v.size();"; + } + else + { + out << "v.length;"; + } + out << nl << "ostr.writeSize(optSize > 254 ? optSize * " << sz << " + 5 : optSize * " << sz << " + 1);"; + } + writeSequenceMarshalUnmarshalCode(out, package, p, "v", true, iter, true); + } + out << eb; + } + out << eb; + + out << sp; + out << nl << "public static " << optTypeS << " read(com.zeroc.Ice.InputStream istr, int tag)"; + out << sb; + if(!hasTypeMetaData(p) && builtin && builtin->kind() < Builtin::KindObject) + { + out << nl << "return istr.read" << builtinTable[builtin->kind()] << "Seq(tag);"; + } + else + { + out << nl << "if(istr.readOptional(tag, " << getOptionalFormat(p) << "))"; + out << sb; + if(p->type()->isVariableLength()) + { + out << nl << "istr.skip(4);"; + } + else if(p->type()->minWireSize() > 1) + { + out << nl << "istr.skipSize();"; + } + out << nl << typeS << " v;"; + writeSequenceMarshalUnmarshalCode(out, package, p, "v", false, iter, true); + out << nl << "return java.util.Optional.of(v);"; + out << eb; + out << nl << "else"; + out << sb; + out << nl << "return java.util.Optional.empty();"; + out << eb; + } + out << eb; + out << eb; close(); } @@ -4336,6 +4469,62 @@ Slice::Gen::HelperVisitor::visitDictionary(const DictionaryPtr& p) out << nl << "return v;"; out << eb; + string optTypeS = "java.util.Optional<" + formalType + ">"; + out << sp; + out << nl << "public static void write(com.zeroc.Ice.OutputStream ostr, int tag, " << optTypeS << " v)"; + out << sb; + out << nl << "if(v != null && v.isPresent())"; + out << sb; + out << nl << "write(ostr, tag, v.get());"; + out << eb; + out << eb; + + out << sp; + out << nl << "public static void write(com.zeroc.Ice.OutputStream ostr, int tag, " << formalType << " v)"; + out << sb; + out << nl << "if(ostr.writeOptional(tag, " << getOptionalFormat(p) << "))"; + out << sb; + TypePtr keyType = p->keyType(); + TypePtr valueType = p->valueType(); + if(keyType->isVariableLength() || valueType->isVariableLength()) + { + out << nl << "int pos = ostr.startSize();"; + writeDictionaryMarshalUnmarshalCode(out, package, p, "v", true, iter, true); + out << nl << "ostr.endSize(pos);"; + } + else + { + const size_t sz = keyType->minWireSize() + valueType->minWireSize(); + out << nl << "final int optSize = v == null ? 0 : v.size();"; + out << nl << "ostr.writeSize(optSize > 254 ? optSize * " << sz << " + 5 : optSize * " << sz << " + 1);"; + writeDictionaryMarshalUnmarshalCode(out, package, p, "v", true, iter, true); + } + out << eb; + out << eb; + + out << sp; + out << nl << "public static " << optTypeS << " read(com.zeroc.Ice.InputStream istr, int tag)"; + out << sb; + out << nl << "if(istr.readOptional(tag, " << getOptionalFormat(p) << "))"; + out << sb; + if(keyType->isVariableLength() || valueType->isVariableLength()) + { + out << nl << "istr.skip(4);"; + } + else + { + out << nl << "istr.skipSize();"; + } + out << nl << formalType << " v;"; + writeDictionaryMarshalUnmarshalCode(out, package, p, "v", false, iter, true); + out << nl << "return java.util.Optional.of(v);"; + out << eb; + out << nl << "else"; + out << sb; + out << nl << "return java.util.Optional.empty();"; + out << eb; + out << eb; + out << eb; close(); } diff --git a/cpp/src/slice2java/Gen.h b/cpp/src/slice2java/Gen.h index 11c7d0f7e45..df117f30ab1 100644 --- a/cpp/src/slice2java/Gen.h +++ b/cpp/src/slice2java/Gen.h @@ -46,8 +46,8 @@ protected: void writeMarshaledResultType(::IceUtilInternal::Output&, const OperationPtr&, const std::string&, const DocCommentPtr&); - void allocatePatcher(::IceUtilInternal::Output&, const TypePtr&, const std::string&, const std::string&); - std::string getPatcher(const TypePtr&, const std::string&, const std::string&, bool); + void allocatePatcher(::IceUtilInternal::Output&, const TypePtr&, const std::string&, const std::string&, bool); + std::string getPatcher(const TypePtr&, const std::string&, const std::string&); std::string getFutureType(const OperationPtr&, const std::string&); std::string getFutureImplType(const OperationPtr&, const std::string&); diff --git a/cpp/src/slice2java/GenCompat.cpp b/cpp/src/slice2java/GenCompat.cpp index 4b5dacaf2e4..245cdceb5ab 100644 --- a/cpp/src/slice2java/GenCompat.cpp +++ b/cpp/src/slice2java/GenCompat.cpp @@ -3732,12 +3732,9 @@ Slice::GenCompat::TypesVisitor::visitStructEnd(const StructPtr& p) out << eb; out << eb; - out << sp << nl << "static public " << name << nl << "ice_read(Ice.InputStream istr, " << name << " v)"; + out << sp << nl << "static public " << name << nl << "ice_read(Ice.InputStream istr)"; out << sb; - out << nl << "if(v == null)"; - out << sb; - out << nl << " v = new " << name << "();"; - out << eb; + out << nl << name << " v = new " << name << "();"; out << nl << "v.ice_readMembers(istr);"; out << nl << "return v;"; out << eb; |