diff options
26 files changed, 1016 insertions, 1287 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; diff --git a/java-compat/src/Ice/src/main/java/Ice/InputStream.java b/java-compat/src/Ice/src/main/java/Ice/InputStream.java index d3f1c597205..0a37c82e08d 100644 --- a/java-compat/src/Ice/src/main/java/Ice/InputStream.java +++ b/java-compat/src/Ice/src/main/java/Ice/InputStream.java @@ -650,7 +650,7 @@ public class InputStream throw new Ice.UnmarshalOutOfBoundsException(); } - EncodingVersion encoding = EncodingVersion.ice_read(this, null); + EncodingVersion encoding = EncodingVersion.ice_read(this); IceInternal.Protocol.checkSupportedEncoding(encoding); // Make sure the encoding is supported. if(encoding.equals(Ice.Util.Encoding_1_0)) diff --git a/java-compat/test/src/main/java/test/Ice/optional/AllTests.java b/java-compat/test/src/main/java/test/Ice/optional/AllTests.java index 0e66fb1c8fb..ed5c23495a3 100644 --- a/java-compat/test/src/main/java/test/Ice/optional/AllTests.java +++ b/java-compat/test/src/main/java/test/Ice/optional/AllTests.java @@ -1061,11 +1061,11 @@ public class AllTests in.startEncapsulation(); test(in.readOptional(1, Ice.OptionalFormat.VSize)); in.skipSize(); - SmallStruct f = SmallStruct.ice_read(in, null); + SmallStruct f = SmallStruct.ice_read(in); test(f.m == (byte)56); test(in.readOptional(3, Ice.OptionalFormat.VSize)); in.skipSize(); - SmallStruct.ice_read(in, f); + f = SmallStruct.ice_read(in); test(f.m == (byte)56); in.endEncapsulation(); @@ -1110,11 +1110,11 @@ public class AllTests in.startEncapsulation(); test(in.readOptional(1, Ice.OptionalFormat.VSize)); in.skipSize(); - FixedStruct f = FixedStruct.ice_read(in, null); + FixedStruct f = FixedStruct.ice_read(in); test(f.m == 56); test(in.readOptional(3, Ice.OptionalFormat.VSize)); in.skipSize(); - FixedStruct.ice_read(in, f); + f = FixedStruct.ice_read(in); test(f.m == 56); in.endEncapsulation(); @@ -1160,11 +1160,11 @@ public class AllTests in.startEncapsulation(); test(in.readOptional(1, Ice.OptionalFormat.FSize)); in.skip(4); - VarStruct v = VarStruct.ice_read(in, null); + VarStruct v = VarStruct.ice_read(in); test(v.m.equals("test")); test(in.readOptional(3, Ice.OptionalFormat.FSize)); in.skip(4); - VarStruct.ice_read(in, v); + v = VarStruct.ice_read(in); test(v.m.equals("test")); in.endEncapsulation(); diff --git a/java-compat/test/src/main/java/test/Ice/stream/Client.java b/java-compat/test/src/main/java/test/Ice/stream/Client.java index 56ab0cb8d57..3f1ebd43b9a 100644 --- a/java-compat/test/src/main/java/test/Ice/stream/Client.java +++ b/java-compat/test/src/main/java/test/Ice/stream/Client.java @@ -273,7 +273,7 @@ public class Client extends test.Util.Application SmallStruct.ice_write(out, s); byte[] data = out.finished(); in = new Ice.InputStream(comm, data); - SmallStruct s2 = SmallStruct.ice_read(in, null); + SmallStruct s2 = SmallStruct.ice_read(in); test(s2.equals(s)); } diff --git a/java/src/Ice/src/main/java/com/zeroc/Ice/InputStream.java b/java/src/Ice/src/main/java/com/zeroc/Ice/InputStream.java index 7ae9b830ac8..8ec41d1f9eb 100644 --- a/java/src/Ice/src/main/java/com/zeroc/Ice/InputStream.java +++ b/java/src/Ice/src/main/java/com/zeroc/Ice/InputStream.java @@ -14,7 +14,6 @@ import java.io.IOException; import com.zeroc.IceInternal.Buffer; import com.zeroc.IceInternal.Instance; import com.zeroc.IceInternal.Protocol; -import com.zeroc.IceInternal.SequencePatcher; /** * Interface for input streams used to extract Slice types from a sequence @@ -585,7 +584,7 @@ public class InputStream } _encapsStack.sz = sz; - EncodingVersion encoding = EncodingVersion.ice_read(this, null); + EncodingVersion encoding = EncodingVersion.ice_read(this); Protocol.checkSupportedEncoding(encoding); // Make sure the encoding is supported. _encapsStack.setEncoding(encoding); @@ -654,7 +653,7 @@ public class InputStream throw new UnmarshalOutOfBoundsException(); } - EncodingVersion encoding = EncodingVersion.ice_read(this, null); + EncodingVersion encoding = EncodingVersion.ice_read(this); Protocol.checkSupportedEncoding(encoding); // Make sure the encoding is supported. if(encoding.equals(Util.Encoding_1_0)) @@ -750,7 +749,7 @@ public class InputStream { throw new UnmarshalOutOfBoundsException(); } - EncodingVersion encoding = EncodingVersion.ice_read(this, null); + EncodingVersion encoding = EncodingVersion.ice_read(this); try { _buf.b.position(_buf.b.position() + sz - 6); @@ -794,7 +793,7 @@ public class InputStream /** * Indicates that unmarshaling is complete, except for any class instances. The application must call this method * only if the stream actually contains class instances. Calling <code>readPendingValues</code> triggers the - * calls to {@link ReadValueCallback#valueReady} that inform the application that unmarshaling of an instance + * calls to consumers provided with {@link #readValue} to inform the application that unmarshaling of an instance * is complete. **/ public void readPendingValues() @@ -1046,7 +1045,7 @@ public class InputStream * * @return The deserialized Java object. **/ - public java.io.Serializable readSerializable() + public <T extends java.io.Serializable> T readSerializable(Class<T> cl) { int sz = readAndCheckSeqSize(1); if (sz == 0) @@ -1058,7 +1057,7 @@ public class InputStream { com.zeroc.IceInternal.InputStreamWrapper w = new com.zeroc.IceInternal.InputStreamWrapper(sz, _buf.b); in = new com.zeroc.IceInternal.ObjectInputStream(_instance, w); - return (java.io.Serializable)in.readObject(); + return cl.cast(in.readObject()); } catch(LocalException ex) { @@ -1085,6 +1084,24 @@ public class InputStream } /** + * Extracts a optional serializable Java object from the stream. + * + * @param tag The numeric tag associated with the value. + * @return The optional value (if any). + **/ + public <T extends java.io.Serializable> java.util.Optional<T> readSerializable(int tag, Class<T> cl) + { + if(readOptional(tag, OptionalFormat.VSize)) + { + return java.util.Optional.of(readSerializable(cl)); + } + else + { + return java.util.Optional.empty(); + } + } + + /** * Extracts a boolean value from the stream. * * @return The extracted boolean. @@ -1803,6 +1820,16 @@ public class InputStream return _instance.proxyFactory().streamToProxy(this); } + public <T extends ObjectPrx> T readProxy(java.util.function.Function<ObjectPrx, T> cast) + { + if(_instance == null) + { + throw new MarshalException("cannot unmarshal a proxy without a communicator"); + } + + return cast.apply(_instance.proxyFactory().streamToProxy(this)); + } + /** * Extracts an optional proxy from the stream. The stream must have been initialized with a communicator. * @@ -1823,6 +1850,26 @@ public class InputStream } /** + * Extracts an optional proxy from the stream. The stream must have been initialized with a communicator. + * + * @param tag The numeric tag associated with the value. + * @param cast The uncheckedCast function to call on the unmarshaled proxy to obtain the correct proxy type. + * @return The optional value (if any). + **/ + public <T extends ObjectPrx> java.util.Optional<T> readProxy(int tag, java.util.function.Function<ObjectPrx, T> cast) + { + if(readOptional(tag, OptionalFormat.FSize)) + { + skip(4); + return java.util.Optional.of(readProxy(cast)); + } + else + { + return java.util.Optional.empty(); + } + } + + /** * Read an enumerated value. * * @param maxValue The maximum enumerator value in the definition. @@ -1852,43 +1899,96 @@ public class InputStream } /** - * Extracts the index of a Slice value from the stream. + * Extracts a Slice value from the stream. * - * @param cb The callback to notify the application when the extracted instance is available. - * The stream extracts Slice values in stages. The Ice run time calls {@link ReadValueCallback#valueReady} - * when the corresponding instance has been fully unmarshaled. + * @param cb The consumer to notify when the extracted instance is available. The stream + * extracts Slice values in stages. The Ice run time calls accept on the consumer when + * the corresponding instance has been fully unmarshaled. * - * @see ReadValueCallback + * @param cls The type of the Ice.Value to unmarshal. **/ - public void readValue(ReadValueCallback cb) + public <T extends Value> void readValue(java.util.function.Consumer<T> cb, Class<T> cls) { initEncaps(); - _encapsStack.decoder.readValue(cb); + if(cb == null) + { + _encapsStack.decoder.readValue(null); + } + else + { + _encapsStack.decoder.readValue(v -> { + if(v == null || cls.isInstance(v)) + { + cb.accept(cls.cast(v)); + } + else + { + com.zeroc.IceInternal.Ex.throwUOE(cls, v); + } + }); + } + } + + /** + * Extracts a Slice value from the stream. + * + * @param cb The consumer to notify when the extracted instance is available. The stream + * extracts Slice values in stages. The Ice run time calls accept on the consumer when + * the corresponding instance has been fully unmarshaled. + **/ + public void readValue(java.util.function.Consumer<Value> cb) + { + readValue(cb, Value.class); } /** - * Extracts the index of an optional Slice value from the stream. + * Extracts an optional Slice value from the stream. * * @param tag The numeric tag associated with the value. - * @param cb The callback to notify the application when the extracted instance is available. - * The stream extracts Slice values in stages. The Ice run time calls {@link ReadValueCallback#valueReady} - * when the corresponding instance has been fully unmarshaled. * - * @see ReadValueCallback + * @param cb The consumer to notify when the extracted instance is available. The stream + * extracts Slice values in stages. The Ice run time calls accept on the consumer when + * the corresponding instance has been fully unmarshaled. + * + * @param cls The type of the Ice.Value to unmarshal. **/ - public void readValue(int tag, ReadValueCallback cb) + public <T extends Value> void readValue(int tag, java.util.function.Consumer<java.util.Optional<T>> cb, Class<T> cls) { if(readOptional(tag, OptionalFormat.Class)) { - readValue(cb); + if(cb != null) + { + readValue(v -> cb.accept(java.util.Optional.ofNullable(v)), cls); + } + else + { + readValue(null); + } } else { - cb.valueReady(null); + if(cb != null) + { + cb.accept(java.util.Optional.empty()); + } } } /** + * Extracts an optional Slice value from the stream. + * + * @param tag The numeric tag associated with the value. + * + * @param cb The consumer to notify when the extracted instance is available. The stream + * extracts Slice values in stages. The Ice run time calls accept on the consumer when + * the corresponding instance has been fully unmarshaled. + **/ + public void readValue(int tag, java.util.function.Consumer<java.util.Optional<Value>> cb) + { + readValue(tag, cb, Value.class); + } + + /** * Extracts a user exception from the stream and throws it. * * @throws UserException The user exception that was unmarshaled. @@ -2005,7 +2105,7 @@ public class InputStream } case Class: { - readValue(null); + readValue(null, null); break; } } @@ -2148,7 +2248,7 @@ public class InputStream _unmarshaledMap = new java.util.TreeMap<>(); } - abstract void readValue(ReadValueCallback cb); + abstract void readValue(java.util.function.Consumer<Value> cb); abstract void throwException(UserExceptionFactory factory) throws UserException; @@ -2275,7 +2375,7 @@ public class InputStream return v; } - protected void addPatchEntry(int index, ReadValueCallback cb) + protected void addPatchEntry(int index, java.util.function.Consumer<Value> cb) { assert(index > 0); @@ -2286,7 +2386,7 @@ public class InputStream Value obj = _unmarshaledMap.get(index); if(obj != null) { - cb.valueReady(obj); + cb.accept(obj); return; } @@ -2300,7 +2400,7 @@ public class InputStream // the callback will be called when the instance is // unmarshaled. // - java.util.LinkedList<ReadValueCallback> l = _patchMap.get(index); + java.util.LinkedList<java.util.function.Consumer<Value>> l = _patchMap.get(index); if(l == null) { // @@ -2335,7 +2435,7 @@ public class InputStream // // Patch all instances now that the instance is unmarshaled. // - java.util.LinkedList<ReadValueCallback> l = _patchMap.get(index); + java.util.LinkedList<java.util.function.Consumer<Value>> l = _patchMap.get(index); if(l != null) { assert(l.size() > 0); @@ -2343,9 +2443,9 @@ public class InputStream // // Patch all pointers that refer to the instance. // - for(ReadValueCallback cb : l) + for(java.util.function.Consumer<Value> cb : l) { - cb.valueReady(v); + cb.accept(v); } // @@ -2410,7 +2510,7 @@ public class InputStream // // Encapsulation attributes for value unmarshaling. // - protected java.util.TreeMap<Integer, java.util.LinkedList<ReadValueCallback> > _patchMap; + protected java.util.TreeMap<Integer, java.util.LinkedList<java.util.function.Consumer<Value>> > _patchMap; private java.util.TreeMap<Integer, Value> _unmarshaledMap; private java.util.TreeMap<Integer, String> _typeIdMap; private int _typeIdIndex; @@ -2427,7 +2527,7 @@ public class InputStream } @Override - void readValue(ReadValueCallback cb) + void readValue(java.util.function.Consumer<Value> cb) { assert(cb != null); @@ -2443,7 +2543,7 @@ public class InputStream if(index == 0) { - cb.valueReady(null); + cb.accept(null); } else { @@ -2721,7 +2821,7 @@ public class InputStream } @Override - void readValue(ReadValueCallback cb) + void readValue(java.util.function.Consumer<Value> cb) { int index = _stream.readSize(); if(index < 0) @@ -2732,7 +2832,7 @@ public class InputStream { if(cb != null) { - cb.valueReady(null); + cb.accept(null); } } else if(_current != null && (_current.sliceFlags & Protocol.FLAG_HAS_INDIRECTION_TABLE) != 0) @@ -3088,7 +3188,7 @@ public class InputStream return false; } - private int readInstance(int index, ReadValueCallback cb) + private int readInstance(int index, java.util.function.Consumer<Value> cb) { assert(index > 0); @@ -3255,7 +3355,7 @@ public class InputStream if(cb != null) { - cb.valueReady(v); + cb.accept(v); } return index; @@ -3286,8 +3386,8 @@ public class InputStream info.instances = new Value[table != null ? table.length : 0]; for(int j = 0; j < info.instances.length; ++j) { - addPatchEntry(table[j], - new SequencePatcher<Value>(info.instances, Value.class, Value.ice_staticId(), j)); + final int k = j; + addPatchEntry(table[j], v -> info.instances[k] = v); } } @@ -3313,7 +3413,7 @@ public class InputStream private static final class IndirectPatchEntry { int index; - ReadValueCallback cb; + java.util.function.Consumer<Value> cb; } private static final class InstanceData diff --git a/java/src/Ice/src/main/java/com/zeroc/Ice/OutputStream.java b/java/src/Ice/src/main/java/com/zeroc/Ice/OutputStream.java index c48c3a2ac6c..99cc006d538 100644 --- a/java/src/Ice/src/main/java/com/zeroc/Ice/OutputStream.java +++ b/java/src/Ice/src/main/java/com/zeroc/Ice/OutputStream.java @@ -738,6 +738,34 @@ public class OutputStream } /** + * Writes an optional serializable Java object to the stream. + * + * @param tag The optional tag. + * @param v The optional serializable object to write. + **/ + public <T extends java.io.Serializable> void writeSerializable(int tag, java.util.Optional<T> v) + { + if(v != null && v.isPresent()) + { + writeSerializable(tag, v.get()); + } + } + + /** + * Writes an optional serializable Java object to the stream. + * + * @param tag The optional tag. + * @param v The serializable object to write. + **/ + public void writeSerializable(int tag, java.io.Serializable v) + { + if(writeOptional(tag, OptionalFormat.VSize)) + { + writeSerializable(v); + } + } + + /** * Writes a boolean to the stream. * * @param v The boolean to write to the stream. diff --git a/java/src/Ice/src/main/java/com/zeroc/Ice/ReadValueCallback.java b/java/src/Ice/src/main/java/com/zeroc/Ice/ReadValueCallback.java deleted file mode 100644 index 41e9ffba464..00000000000 --- a/java/src/Ice/src/main/java/com/zeroc/Ice/ReadValueCallback.java +++ /dev/null @@ -1,28 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package com.zeroc.Ice; - -/** - * Callback class to inform an application when an instance of a Slice class has been - * unmarshaled from an input stream. - * - * @see InputStream#readValue - **/ -@FunctionalInterface -public interface ReadValueCallback -{ - /** - * The Ice run time calls this method when it has fully unmarshaled the state - * of a Slice class instance. - * - * @param v The unmarshaled Slice class instance. - **/ - void valueReady(Value v); -} diff --git a/java/src/Ice/src/main/java/com/zeroc/Ice/_ObjectPrxI.java b/java/src/Ice/src/main/java/com/zeroc/Ice/_ObjectPrxI.java index 426c3b24e3b..f361b379f96 100644 --- a/java/src/Ice/src/main/java/com/zeroc/Ice/_ObjectPrxI.java +++ b/java/src/Ice/src/main/java/com/zeroc/Ice/_ObjectPrxI.java @@ -119,9 +119,7 @@ public class _ObjectPrxI implements ObjectPrx, java.io.Serializable OutgoingAsync<String[]> f = new OutgoingAsync<>(this, "ice_ids", OperationMode.Nonmutating, sync, null); f.invoke(true, context, null, null, istr -> { - String[] ret; - ret = StringSeqHelper.read(istr); - return ret; + return istr.readStringSeq(); }); return f; } diff --git a/java/src/Ice/src/main/java/com/zeroc/IceInternal/DictionaryPatcher.java b/java/src/Ice/src/main/java/com/zeroc/IceInternal/DictionaryPatcher.java deleted file mode 100644 index f77f47eb84d..00000000000 --- a/java/src/Ice/src/main/java/com/zeroc/IceInternal/DictionaryPatcher.java +++ /dev/null @@ -1,38 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package com.zeroc.IceInternal; - -public class DictionaryPatcher<K, V> implements com.zeroc.Ice.ReadValueCallback -{ - public DictionaryPatcher(java.util.Map<K, V> dict, Class<V> cls, String type, K key) - { - _dict = dict; - _cls = cls; - _type = type; - _key = key; - } - - public void valueReady(com.zeroc.Ice.Value v) - { - if(v == null || _cls.isInstance(v)) - { - _dict.put(_key, _cls.cast(v)); - } - else - { - Ex.throwUOE(_type, v); - } - } - - private java.util.Map<K, V> _dict; - private Class<V> _cls; - private String _type; - private K _key; -} diff --git a/java/src/Ice/src/main/java/com/zeroc/IceInternal/Ex.java b/java/src/Ice/src/main/java/com/zeroc/IceInternal/Ex.java index 0880a697bae..347a954a501 100644 --- a/java/src/Ice/src/main/java/com/zeroc/IceInternal/Ex.java +++ b/java/src/Ice/src/main/java/com/zeroc/IceInternal/Ex.java @@ -11,6 +11,33 @@ package com.zeroc.IceInternal; public class Ex { + public static <T extends com.zeroc.Ice.Value> void throwUOE(Class<T> expectedType, com.zeroc.Ice.Value v) + { + // + // If the object is an unknown sliced object, we didn't find an + // value factory, in this case raise a NoValueFactoryException + // instead. + // + if(v instanceof com.zeroc.Ice.UnknownSlicedValue) + { + com.zeroc.Ice.UnknownSlicedValue usv = (com.zeroc.Ice.UnknownSlicedValue)v; + throw new com.zeroc.Ice.NoValueFactoryException("", usv.getUnknownTypeId()); + } + + String type = v.ice_id(); + String expected; + try + { + expected = (String)expectedType.getMethod("ice_staticId").invoke(null); + } + catch(Exception ex) + { + expected = ""; + assert(false); + } + throw new com.zeroc.Ice.UnexpectedObjectException( + "expected element of type `" + expected + "' but received '" + type, type, expected); + } public static void throwUOE(String expectedType, com.zeroc.Ice.Value v) { // diff --git a/java/src/Ice/src/main/java/com/zeroc/IceInternal/ListPatcher.java b/java/src/Ice/src/main/java/com/zeroc/IceInternal/ListPatcher.java deleted file mode 100644 index b3772713014..00000000000 --- a/java/src/Ice/src/main/java/com/zeroc/IceInternal/ListPatcher.java +++ /dev/null @@ -1,43 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package com.zeroc.IceInternal; - -public class ListPatcher<T> implements com.zeroc.Ice.ReadValueCallback -{ - public ListPatcher(java.util.List<T> list, Class<T> cls, String type, int index) - { - _list = list; - _cls = cls; - _type = type; - _index = index; - } - - public void valueReady(com.zeroc.Ice.Value v) - { - if(v == null || _cls.isInstance(v)) - { - // - // This isn't very efficient for sequentially-accessed lists, but there - // isn't much we can do about it as long as a new patcher instance is - // created for each element. - // - _list.set(_index, _cls.cast(v)); - } - else - { - Ex.throwUOE(_type, v); - } - } - - private java.util.List<T> _list; - private Class<T> _cls; - private String _type; - private int _index; -} diff --git a/java/src/Ice/src/main/java/com/zeroc/IceInternal/Patcher.java b/java/src/Ice/src/main/java/com/zeroc/IceInternal/Patcher.java deleted file mode 100644 index 43263641dd0..00000000000 --- a/java/src/Ice/src/main/java/com/zeroc/IceInternal/Patcher.java +++ /dev/null @@ -1,52 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package com.zeroc.IceInternal; - -public class Patcher<T> implements com.zeroc.Ice.ReadValueCallback -{ - @FunctionalInterface - public interface Callback<T> - { - void valueReady(T v); - } - - public Patcher(Class<T> cls, String type) - { - this(cls, type, null); - } - - public Patcher(Class<T> cls, String type, Callback<T> cb) - { - _cls = cls; - _type = type; - _cb = cb; - } - - public void valueReady(com.zeroc.Ice.Value v) - { - if(v == null || _cls.isInstance(v)) - { - value = _cls.cast(v); - if(_cb != null) - { - _cb.valueReady(value); - } - } - else - { - Ex.throwUOE(_type, v); - } - } - - private Class<T> _cls; - private String _type; - private Callback<T> _cb; - public T value; -} diff --git a/java/src/Ice/src/main/java/com/zeroc/IceInternal/ProxyFactory.java b/java/src/Ice/src/main/java/com/zeroc/IceInternal/ProxyFactory.java index 321ac80e0b8..4475a642d66 100644 --- a/java/src/Ice/src/main/java/com/zeroc/IceInternal/ProxyFactory.java +++ b/java/src/Ice/src/main/java/com/zeroc/IceInternal/ProxyFactory.java @@ -59,8 +59,7 @@ public final class ProxyFactory public com.zeroc.Ice.ObjectPrx streamToProxy(com.zeroc.Ice.InputStream s) { - com.zeroc.Ice.Identity ident = com.zeroc.Ice.Identity.ice_read(s, null); - + com.zeroc.Ice.Identity ident = com.zeroc.Ice.Identity.ice_read(s); Reference ref = _instance.referenceFactory().create(ident, s); return referenceToProxy(ref); } diff --git a/java/src/Ice/src/main/java/com/zeroc/IceInternal/ProxyOutgoingAsyncBaseI.java b/java/src/Ice/src/main/java/com/zeroc/IceInternal/ProxyOutgoingAsyncBaseI.java index d717ef07a49..f17b524caf7 100644 --- a/java/src/Ice/src/main/java/com/zeroc/IceInternal/ProxyOutgoingAsyncBaseI.java +++ b/java/src/Ice/src/main/java/com/zeroc/IceInternal/ProxyOutgoingAsyncBaseI.java @@ -71,7 +71,7 @@ public abstract class ProxyOutgoingAsyncBaseI<T> extends OutgoingAsyncBaseI<T> i case ReplyStatus.replyFacetNotExist: case ReplyStatus.replyOperationNotExist: { - com.zeroc.Ice.Identity id = com.zeroc.Ice.Identity.ice_read(is, null); + com.zeroc.Ice.Identity id = com.zeroc.Ice.Identity.ice_read(is); // // For compatibility with the old FacetPath. diff --git a/java/src/Ice/src/main/java/com/zeroc/IceInternal/ReferenceFactory.java b/java/src/Ice/src/main/java/com/zeroc/IceInternal/ReferenceFactory.java index 1543263bfa0..9c53ce16157 100644 --- a/java/src/Ice/src/main/java/com/zeroc/IceInternal/ReferenceFactory.java +++ b/java/src/Ice/src/main/java/com/zeroc/IceInternal/ReferenceFactory.java @@ -589,8 +589,8 @@ public final class ReferenceFactory com.zeroc.Ice.EncodingVersion encoding; if(!s.getEncoding().equals(com.zeroc.Ice.Util.Encoding_1_0)) { - protocol = com.zeroc.Ice.ProtocolVersion.ice_read(s, null); - encoding = com.zeroc.Ice.EncodingVersion.ice_read(s, null); + protocol = com.zeroc.Ice.ProtocolVersion.ice_read(s); + encoding = com.zeroc.Ice.EncodingVersion.ice_read(s); } else { diff --git a/java/src/Ice/src/main/java/com/zeroc/IceInternal/SequencePatcher.java b/java/src/Ice/src/main/java/com/zeroc/IceInternal/SequencePatcher.java deleted file mode 100644 index 22309d162f0..00000000000 --- a/java/src/Ice/src/main/java/com/zeroc/IceInternal/SequencePatcher.java +++ /dev/null @@ -1,39 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package com.zeroc.IceInternal; - -public class SequencePatcher<T> implements com.zeroc.Ice.ReadValueCallback -{ - public SequencePatcher(T[] seq, Class<T> cls, String type, int index) - { - _seq = seq; - _cls = cls; - _type = type; - _index = index; - } - - public void valueReady(com.zeroc.Ice.Value v) - { - if(v == null || _cls.isInstance(v)) - { - _seq[_index] = _cls.cast(v); - } - else - { - Ex.throwUOE(_type, v); - } - - } - - private T[] _seq; - private Class<T> _cls; - private String _type; - private int _index; -} diff --git a/java/src/Ice/src/main/java/com/zeroc/IceInternal/TraceUtil.java b/java/src/Ice/src/main/java/com/zeroc/IceInternal/TraceUtil.java index 852c190b4ff..b8d1a2a273d 100644 --- a/java/src/Ice/src/main/java/com/zeroc/IceInternal/TraceUtil.java +++ b/java/src/Ice/src/main/java/com/zeroc/IceInternal/TraceUtil.java @@ -174,7 +174,7 @@ public final class TraceUtil toStringMode = stream.instance().toStringMode(); } - com.zeroc.Ice.Identity identity = com.zeroc.Ice.Identity.ice_read(stream, null); + com.zeroc.Ice.Identity identity = com.zeroc.Ice.Identity.ice_read(stream); out.write("\nidentity = " + com.zeroc.Ice.Util.identityToString(identity, toStringMode)); String[] facet = stream.readStringSeq(); diff --git a/java/test/src/main/java/test/Ice/custom/Test.ice b/java/test/src/main/java/test/Ice/custom/Test.ice index 42d7faae656..1320a119948 100644 --- a/java/test/src/main/java/test/Ice/custom/Test.ice +++ b/java/test/src/main/java/test/Ice/custom/Test.ice @@ -76,6 +76,29 @@ class TestIntf FloatBuffer opFloatBufferSeq(FloatBuffer inSeq, out FloatBuffer outSeq); DoubleBuffer opDoubleBufferSeq(DoubleBuffer inSeq, out DoubleBuffer outSeq); + optional(1) CSeq opOptCSeq(optional(2) CSeq inSeq, out optional(3) CSeq outSeq); + optional(1) CArray opOptCArray(optional(2) CArray inSeq, out optional(3) CArray outSeq); + optional(1) CList opOptCList(optional(2) CList inSeq, out optional(3) CList outSeq); + optional(1) BoolSeq opOptBoolSeq(optional(2) BoolSeq inSeq, out optional(3) BoolSeq outSeq); + optional(1) ByteSeq opOptByteSeq(optional(2) ByteSeq inSeq, out optional(3) ByteSeq outSeq); + optional(1) ShortSeq opOptShortSeq(optional(2) ShortSeq inSeq, out optional(3) ShortSeq outSeq); + optional(1) IntSeq opOptIntSeq(optional(2) IntSeq inSeq, out optional(3) IntSeq outSeq); + optional(1) LongSeq opOptLongSeq(optional(2) LongSeq inSeq, out optional(3) LongSeq outSeq); + optional(1) FloatSeq opOptFloatSeq(optional(2) FloatSeq inSeq, out optional(3) FloatSeq outSeq); + optional(1) DoubleSeq opOptDoubleSeq(optional(2) DoubleSeq inSeq, out optional(3) DoubleSeq outSeq); + optional(1) StringSeq opOptStringSeq(optional(2) StringSeq inSeq, out optional(3) StringSeq outSeq); + optional(1) ESeq opOptESeq(optional(2) ESeq inSeq, out optional(3) ESeq outSeq); + optional(1) SSeq opOptSSeq(optional(2) SSeq inSeq, out optional(3) SSeq outSeq); + optional(1) DSeq opOptDSeq(optional(2) DSeq inSeq, out optional(3) DSeq outSeq); + optional(1) StringSeqSeq opOptStringSeqSeq(optional(2) StringSeqSeq inSeq, out optional(3) StringSeqSeq outSeq); + + optional(1) ByteBuffer opOptByteBufferSeq(optional(2) ByteBuffer inSeq, out optional(3) ByteBuffer outSeq); + optional(1) ShortBuffer opOptShortBufferSeq(optional(2) ShortBuffer inSeq, out optional(3) ShortBuffer outSeq); + optional(1) IntBuffer opOptIntBufferSeq(optional(2) IntBuffer inSeq, out optional(3) IntBuffer outSeq); + optional(1) LongBuffer opOptLongBufferSeq(optional(2) LongBuffer inSeq, out optional(3) LongBuffer outSeq); + optional(1) FloatBuffer opOptFloatBufferSeq(optional(2) FloatBuffer inSeq, out optional(3) FloatBuffer outSeq); + optional(1) DoubleBuffer opOptDoubleBufferSeq(optional(2) DoubleBuffer inSeq, out optional(3) DoubleBuffer outSeq); + void shutdown(); ["java:type:java.util.ArrayList<C>"] CSeq seq; diff --git a/java/test/src/main/java/test/Ice/custom/TestI.java b/java/test/src/main/java/test/Ice/custom/TestI.java index 2393f30932a..33129d6e8f6 100644 --- a/java/test/src/main/java/test/Ice/custom/TestI.java +++ b/java/test/src/main/java/test/Ice/custom/TestI.java @@ -195,6 +195,132 @@ public final class TestI implements _TestIntfDisp } @Override + public TestIntf.OpOptCArrayResult opOptCArray(java.util.Optional<List<C>> inSeq, com.zeroc.Ice.Current current) + { + return new TestIntf.OpOptCArrayResult(inSeq, inSeq); + } + + @Override + public TestIntf.OpOptCListResult opOptCList(java.util.Optional<List<C>> inSeq, com.zeroc.Ice.Current current) + { + return new TestIntf.OpOptCListResult(inSeq, inSeq); + } + + @Override + public TestIntf.OpOptCSeqResult opOptCSeq(java.util.Optional<C[]> inSeq, com.zeroc.Ice.Current current) + { + return new TestIntf.OpOptCSeqResult(inSeq, inSeq); + } + + @Override + public TestIntf.OpOptBoolSeqResult opOptBoolSeq(java.util.Optional<List<Boolean>> inSeq, com.zeroc.Ice.Current current) + { + return new TestIntf.OpOptBoolSeqResult(inSeq, inSeq); + } + + @Override + public TestIntf.OpOptByteSeqResult opOptByteSeq(java.util.Optional<List<Byte>> inSeq, com.zeroc.Ice.Current current) + { + return new TestIntf.OpOptByteSeqResult(inSeq, inSeq); + } + + @Override + public TestIntf.OpOptDSeqResult opOptDSeq(java.util.Optional<List<Map<Integer,String>>> inSeq, com.zeroc.Ice.Current current) + { + return new TestIntf.OpOptDSeqResult(inSeq, inSeq); + } + + @Override + public TestIntf.OpOptDoubleSeqResult opOptDoubleSeq(java.util.Optional<List<Double>> inSeq, com.zeroc.Ice.Current current) + { + return new TestIntf.OpOptDoubleSeqResult(inSeq, inSeq); + } + + @Override + public TestIntf.OpOptESeqResult opOptESeq(java.util.Optional<List<E>> inSeq, com.zeroc.Ice.Current current) + { + return new TestIntf.OpOptESeqResult(inSeq, inSeq); + } + + @Override + public TestIntf.OpOptFloatSeqResult opOptFloatSeq(java.util.Optional<List<Float>> inSeq, com.zeroc.Ice.Current current) + { + return new TestIntf.OpOptFloatSeqResult(inSeq, inSeq); + } + + @Override + public TestIntf.OpOptIntSeqResult opOptIntSeq(java.util.Optional<List<Integer>> inSeq, com.zeroc.Ice.Current current) + { + return new TestIntf.OpOptIntSeqResult(inSeq, inSeq); + } + + @Override + public TestIntf.OpOptLongSeqResult opOptLongSeq(java.util.Optional<List<Long>> inSeq, com.zeroc.Ice.Current current) + { + return new TestIntf.OpOptLongSeqResult(inSeq, inSeq); + } + + @Override + public TestIntf.OpOptSSeqResult opOptSSeq(java.util.Optional<List<S>> inSeq, com.zeroc.Ice.Current current) + { + return new TestIntf.OpOptSSeqResult(inSeq, inSeq); + } + + @Override + public TestIntf.OpOptShortSeqResult opOptShortSeq(java.util.Optional<List<Short>> inSeq, com.zeroc.Ice.Current current) + { + return new TestIntf.OpOptShortSeqResult(inSeq, inSeq); + } + + @Override + public TestIntf.OpOptStringSeqResult opOptStringSeq(java.util.Optional<List<String>> inSeq, com.zeroc.Ice.Current current) + { + return new TestIntf.OpOptStringSeqResult(inSeq, inSeq); + } + + @Override + public TestIntf.OpOptStringSeqSeqResult opOptStringSeqSeq(java.util.Optional<List<List<String>>> inSeq, com.zeroc.Ice.Current current) + { + return new TestIntf.OpOptStringSeqSeqResult(inSeq, inSeq); + } + + @Override + public TestIntf.OpOptByteBufferSeqResult opOptByteBufferSeq(java.util.Optional<ByteBuffer> inSeq, com.zeroc.Ice.Current current) + { + return new TestIntf.OpOptByteBufferSeqResult(inSeq, inSeq); + } + + @Override + public TestIntf.OpOptShortBufferSeqResult opOptShortBufferSeq(java.util.Optional<ShortBuffer> inSeq, com.zeroc.Ice.Current current) + { + return new TestIntf.OpOptShortBufferSeqResult(inSeq, inSeq); + } + + @Override + public TestIntf.OpOptIntBufferSeqResult opOptIntBufferSeq(java.util.Optional<IntBuffer> inSeq, com.zeroc.Ice.Current current) + { + return new TestIntf.OpOptIntBufferSeqResult(inSeq, inSeq); + } + + @Override + public TestIntf.OpOptLongBufferSeqResult opOptLongBufferSeq(java.util.Optional<LongBuffer> inSeq, com.zeroc.Ice.Current current) + { + return new TestIntf.OpOptLongBufferSeqResult(inSeq, inSeq); + } + + @Override + public TestIntf.OpOptFloatBufferSeqResult opOptFloatBufferSeq(java.util.Optional<FloatBuffer> inSeq, com.zeroc.Ice.Current current) + { + return new TestIntf.OpOptFloatBufferSeqResult(inSeq, inSeq); + } + + @Override + public TestIntf.OpOptDoubleBufferSeqResult opOptDoubleBufferSeq(java.util.Optional<DoubleBuffer> inSeq, com.zeroc.Ice.Current current) + { + return new TestIntf.OpOptDoubleBufferSeqResult(inSeq, inSeq); + } + + @Override public void shutdown(com.zeroc.Ice.Current current) { _communicator.shutdown(); diff --git a/java/test/src/main/java/test/Ice/objects/Test.ice b/java/test/src/main/java/test/Ice/objects/Test.ice index 6172f7f3323..a06d4ac3391 100644 --- a/java/test/src/main/java/test/Ice/objects/Test.ice +++ b/java/test/src/main/java/test/Ice/objects/Test.ice @@ -174,7 +174,7 @@ class Initial ["marshaled-result"] B getMB(); ["amd", "marshaled-result"] B getAMDMB(); - void getAll(out B b1, out B b2, out C theC, out D theD); + void getAll(out B b1, out B b2, out C theC, out D theD, out D istr); I getI(); I getJ(); diff --git a/java/test/src/main/java/test/Ice/optional/AllTests.java b/java/test/src/main/java/test/Ice/optional/AllTests.java index ce80d92f6ad..79eaa579207 100644 --- a/java/test/src/main/java/test/Ice/optional/AllTests.java +++ b/java/test/src/main/java/test/Ice/optional/AllTests.java @@ -379,10 +379,10 @@ public class AllTests test(inv.returnValue); InputStream in = new InputStream(communicator, inv.outParams); in.startEncapsulation(); - ReadValueCallbackI cb = new ReadValueCallbackI(); - in.readValue(cb); + final Wrapper<TestObjectReader> cb = new Wrapper<>(); + in.readValue(v -> cb.value = v, TestObjectReader.class); in.endEncapsulation(); - test(cb.obj != null && cb.obj instanceof TestObjectReader); + test(cb.value != null); os = new OutputStream(communicator); os.startEncapsulation(); @@ -393,9 +393,9 @@ public class AllTests test(inv.returnValue); in = new InputStream(communicator, inv.outParams); in.startEncapsulation(); - in.readValue(cb); + in.readValue(v -> cb.value = v, TestObjectReader.class); in.endEncapsulation(); - test(cb.obj != null && cb.obj instanceof TestObjectReader); + test(cb.value != null); factory.setEnabled(false); // @@ -481,9 +481,9 @@ public class AllTests test(inv.returnValue); in = new InputStream(communicator, inv.outParams); in.startEncapsulation(); - in.readValue(cb); + in.readValue(v -> cb.value = v, TestObjectReader.class); in.endEncapsulation(); - test(cb.obj != null && cb.obj instanceof TestObjectReader); + test(cb.value != null); factory.setEnabled(false); out.println("ok"); @@ -518,9 +518,10 @@ public class AllTests test(inv.returnValue); in = new InputStream(communicator, inv.outParams); in.startEncapsulation(); - in.readValue(cb); + Wrapper<com.zeroc.Ice.Value> cbv = new Wrapper<>(); + in.readValue(v -> cbv.value = v); in.endEncapsulation(); - test(cb.obj != null); + test(cbv.value != null); factory.setEnabled(false); } out.println("ok"); @@ -545,7 +546,7 @@ public class AllTests in = new InputStream(communicator, inEncaps); in.startEncapsulation(); final Wrapper<F> w = new Wrapper<>(); - in.readValue(v -> w.value = ((FObjectReader)v).getF()); + in.readValue(v -> w.value = v.getF(), FObjectReader.class); in.endEncapsulation(); factory.setEnabled(false); test(w.value.ae != null && !w.value.hasAf()); @@ -584,9 +585,10 @@ public class AllTests test(inv.returnValue); in = new InputStream(communicator, inv.outParams); in.startEncapsulation(); - in.readValue(cb); + Wrapper<CObjectReader> ccb = new Wrapper<>(); + in.readValue(v -> ccb.value = v, CObjectReader.class); in.endEncapsulation(); - test(cb.obj instanceof CObjectReader); + test(ccb.value != null); factory.setEnabled(false); factory.setEnabled(true); @@ -600,10 +602,11 @@ public class AllTests test(inv.returnValue); in = new InputStream(communicator, inv.outParams); in.startEncapsulation(); - in.readValue(cb); + Wrapper<DObjectReader> dcb = new Wrapper<>(); + in.readValue(v -> dcb.value = v, DObjectReader.class); in.endEncapsulation(); - test(cb.obj != null && cb.obj instanceof DObjectReader); - ((DObjectReader)cb.obj).check(); + test(dcb.value != null); + dcb.value.check(); factory.setEnabled(false); } out.println("ok"); @@ -661,10 +664,8 @@ public class AllTests inv = initial.ice_invoke("opByteReq", OperationMode.Normal, inEncaps); in = new InputStream(communicator, inv.outParams); in.startEncapsulation(); - test(in.readOptional(1, OptionalFormat.F1)); - test(in.readByte() == (byte)56); - test(in.readOptional(3, OptionalFormat.F1)); - test(in.readByte() == (byte)56); + test(in.readByte(1).get() == (byte)56); + test(in.readByte(3).get() == (byte)56); in.endEncapsulation(); in = new InputStream(communicator, inv.outParams); @@ -700,10 +701,8 @@ public class AllTests inv = initial.ice_invoke("opBoolReq", OperationMode.Normal, inEncaps); in = new InputStream(communicator, inv.outParams); in.startEncapsulation(); - test(in.readOptional(1, OptionalFormat.F1)); - test(in.readBool() == true); - test(in.readOptional(3, OptionalFormat.F1)); - test(in.readBool() == true); + test(in.readBool(1).get() == true); + test(in.readBool(3).get() == true); in.endEncapsulation(); in = new InputStream(communicator, inv.outParams); @@ -739,10 +738,8 @@ public class AllTests inv = initial.ice_invoke("opShortReq", OperationMode.Normal, inEncaps); in = new InputStream(communicator, inv.outParams); in.startEncapsulation(); - test(in.readOptional(1, OptionalFormat.F2)); - test(in.readShort() == 56); - test(in.readOptional(3, OptionalFormat.F2)); - test(in.readShort() == 56); + test(in.readShort(1).get() == 56); + test(in.readShort(3).get() == 56); in.endEncapsulation(); in = new InputStream(communicator, inv.outParams); @@ -778,10 +775,8 @@ public class AllTests inv = initial.ice_invoke("opIntReq", OperationMode.Normal, inEncaps); in = new InputStream(communicator, inv.outParams); in.startEncapsulation(); - test(in.readOptional(1, OptionalFormat.F4)); - test(in.readInt() == 56); - test(in.readOptional(3, OptionalFormat.F4)); - test(in.readInt() == 56); + test(in.readInt(1).getAsInt() == 56); + test(in.readInt(3).getAsInt() == 56); in.endEncapsulation(); in = new InputStream(communicator, inv.outParams); @@ -817,10 +812,8 @@ public class AllTests inv = initial.ice_invoke("opLongReq", OperationMode.Normal, inEncaps); in = new InputStream(communicator, inv.outParams); in.startEncapsulation(); - test(in.readOptional(2, OptionalFormat.F8)); - test(in.readLong() == 56); - test(in.readOptional(3, OptionalFormat.F8)); - test(in.readLong() == 56); + test(in.readLong(2).getAsLong() == 56); + test(in.readLong(3).getAsLong() == 56); in.endEncapsulation(); in = new InputStream(communicator, inv.outParams); @@ -856,10 +849,8 @@ public class AllTests inv = initial.ice_invoke("opFloatReq", OperationMode.Normal, inEncaps); in = new InputStream(communicator, inv.outParams); in.startEncapsulation(); - test(in.readOptional(1, OptionalFormat.F4)); - test(in.readFloat() == 1.0); - test(in.readOptional(3, OptionalFormat.F4)); - test(in.readFloat() == 1.0); + test(in.readFloat(1).get() == 1.0); + test(in.readFloat(3).get() == 1.0); in.endEncapsulation(); in = new InputStream(communicator, inv.outParams); @@ -895,10 +886,8 @@ public class AllTests inv = initial.ice_invoke("opDoubleReq", OperationMode.Normal, inEncaps); in = new InputStream(communicator, inv.outParams); in.startEncapsulation(); - test(in.readOptional(1, OptionalFormat.F8)); - test(in.readDouble() == 1.0); - test(in.readOptional(3, OptionalFormat.F8)); - test(in.readDouble() == 1.0); + test(in.readDouble(1).getAsDouble() == 1.0); + test(in.readDouble(3).getAsDouble() == 1.0); in.endEncapsulation(); in = new InputStream(communicator, inv.outParams); @@ -934,10 +923,8 @@ public class AllTests inv = initial.ice_invoke("opStringReq", OperationMode.Normal, inEncaps); in = new InputStream(communicator, inv.outParams); in.startEncapsulation(); - test(in.readOptional(1, OptionalFormat.VSize)); - test(in.readString().equals("test")); - test(in.readOptional(3, OptionalFormat.VSize)); - test(in.readString().equals("test")); + test(in.readString(1).get().equals("test")); + test(in.readString(3).get().equals("test")); in.endEncapsulation(); in = new InputStream(communicator, inv.outParams); @@ -1017,11 +1004,11 @@ public class AllTests in.startEncapsulation(); test(in.readOptional(1, OptionalFormat.VSize)); in.skipSize(); - SmallStruct f = SmallStruct.ice_read(in, null); + SmallStruct f = SmallStruct.ice_read(in); test(f.m == (byte)56); test(in.readOptional(3, OptionalFormat.VSize)); in.skipSize(); - SmallStruct.ice_read(in, f); + f = SmallStruct.ice_read(in); test(f.m == (byte)56); in.endEncapsulation(); @@ -1061,11 +1048,11 @@ public class AllTests in.startEncapsulation(); test(in.readOptional(1, OptionalFormat.VSize)); in.skipSize(); - FixedStruct f = FixedStruct.ice_read(in, null); + FixedStruct f = FixedStruct.ice_read(in); test(f.m == 56); test(in.readOptional(3, OptionalFormat.VSize)); in.skipSize(); - FixedStruct.ice_read(in, f); + f = FixedStruct.ice_read(in); test(f.m == 56); in.endEncapsulation(); @@ -1106,11 +1093,11 @@ public class AllTests in.startEncapsulation(); test(in.readOptional(1, OptionalFormat.FSize)); in.skip(4); - VarStruct v = VarStruct.ice_read(in, null); + VarStruct v = VarStruct.ice_read(in); test(v.m.equals("test")); test(in.readOptional(3, OptionalFormat.FSize)); in.skip(4); - VarStruct.ice_read(in, v); + v = VarStruct.ice_read(in); test(v.m.equals("test")); in.endEncapsulation(); @@ -1147,14 +1134,12 @@ public class AllTests inv = initial.ice_invoke("opOneOptionalReq", OperationMode.Normal, inEncaps); in = new InputStream(communicator, inv.outParams); in.startEncapsulation(); - test(in.readOptional(1, OptionalFormat.Class)); - ReadValueCallbackI p2cb = new ReadValueCallbackI(); - in.readValue(p2cb); - test(in.readOptional(3, OptionalFormat.Class)); - ReadValueCallbackI p3cb = new ReadValueCallbackI(); - in.readValue(p3cb); + Wrapper<java.util.Optional<OneOptional>> p2cb = new Wrapper<>(); + in.readValue(1, v -> p2cb.value = v, OneOptional.class); + Wrapper<java.util.Optional<OneOptional>> p3cb = new Wrapper<>(); + in.readValue(3, v -> p3cb.value = v, OneOptional.class); in.endEncapsulation(); - test(((OneOptional)p2cb.obj).getA() == 58 && ((OneOptional)p3cb.obj).getA() == 58); + test(p2cb.value.get().getA() == 58 && p3cb.value.get().getA() == 58); in = new InputStream(communicator, inv.outParams); in.startEncapsulation(); @@ -1191,12 +1176,8 @@ public class AllTests inv = initial.ice_invoke("opOneOptionalProxyReq", OperationMode.Normal, inEncaps); in = new InputStream(communicator, inv.outParams); in.startEncapsulation(); - test(in.readOptional(1, OptionalFormat.FSize)); - in.skip(4); - test(in.readProxy().equals(p1.get())); - test(in.readOptional(3, OptionalFormat.FSize)); - in.skip(4); - test(in.readProxy().equals(p1.get())); + test(in.readProxy(1).get().equals(p1.get())); + test(in.readProxy(3).get().equals(p1.get())); in.endEncapsulation(); in = new InputStream(communicator, inv.outParams); @@ -1240,10 +1221,8 @@ public class AllTests inv = initial.ice_invoke("opByteSeqReq", OperationMode.Normal, inEncaps); in = new InputStream(communicator, inv.outParams); in.startEncapsulation(); - test(in.readOptional(1, OptionalFormat.VSize)); - test(java.util.Arrays.equals(in.readByteSeq(), p1.get())); - test(in.readOptional(3, OptionalFormat.VSize)); - test(java.util.Arrays.equals(in.readByteSeq(), p1.get())); + test(java.util.Arrays.equals(in.readByteSeq(1).get(), p1.get())); + test(java.util.Arrays.equals(in.readByteSeq(3).get(), p1.get())); in.endEncapsulation(); in = new InputStream(communicator, inv.outParams); @@ -1283,10 +1262,8 @@ public class AllTests inv = initial.ice_invoke("opBoolSeqReq", OperationMode.Normal, inEncaps); in = new InputStream(communicator, inv.outParams); in.startEncapsulation(); - test(in.readOptional(1, OptionalFormat.VSize)); - test(java.util.Arrays.equals(in.readBoolSeq(), p1.get())); - test(in.readOptional(3, OptionalFormat.VSize)); - test(java.util.Arrays.equals(in.readBoolSeq(), p1.get())); + test(java.util.Arrays.equals(in.readBoolSeq(1).get(), p1.get())); + test(java.util.Arrays.equals(in.readBoolSeq(3).get(), p1.get())); in.endEncapsulation(); in = new InputStream(communicator, inv.outParams); @@ -1328,12 +1305,8 @@ public class AllTests inv = initial.ice_invoke("opShortSeqReq", OperationMode.Normal, inEncaps); in = new InputStream(communicator, inv.outParams); in.startEncapsulation(); - test(in.readOptional(1, OptionalFormat.VSize)); - in.skipSize(); - test(java.util.Arrays.equals(in.readShortSeq(), p1.get())); - test(in.readOptional(3, OptionalFormat.VSize)); - in.skipSize(); - test(java.util.Arrays.equals(in.readShortSeq(), p1.get())); + test(java.util.Arrays.equals(in.readShortSeq(1).get(), p1.get())); + test(java.util.Arrays.equals(in.readShortSeq(3).get(), p1.get())); in.endEncapsulation(); in = new InputStream(communicator, inv.outParams); @@ -1375,12 +1348,8 @@ public class AllTests inv = initial.ice_invoke("opIntSeqReq", OperationMode.Normal, inEncaps); in = new InputStream(communicator, inv.outParams); in.startEncapsulation(); - test(in.readOptional(1, OptionalFormat.VSize)); - in.skipSize(); - test(java.util.Arrays.equals(in.readIntSeq(), p1.get())); - test(in.readOptional(3, OptionalFormat.VSize)); - in.skipSize(); - test(java.util.Arrays.equals(in.readIntSeq(), p1.get())); + test(java.util.Arrays.equals(in.readIntSeq(1).get(), p1.get())); + test(java.util.Arrays.equals(in.readIntSeq(3).get(), p1.get())); in.endEncapsulation(); in = new InputStream(communicator, inv.outParams); @@ -1422,12 +1391,8 @@ public class AllTests inv = initial.ice_invoke("opLongSeqReq", OperationMode.Normal, inEncaps); in = new InputStream(communicator, inv.outParams); in.startEncapsulation(); - test(in.readOptional(1, OptionalFormat.VSize)); - in.skipSize(); - test(java.util.Arrays.equals(in.readLongSeq(), p1.get())); - test(in.readOptional(3, OptionalFormat.VSize)); - in.skipSize(); - test(java.util.Arrays.equals(in.readLongSeq(), p1.get())); + test(java.util.Arrays.equals(in.readLongSeq(1).get(), p1.get())); + test(java.util.Arrays.equals(in.readLongSeq(3).get(), p1.get())); in.endEncapsulation(); in = new InputStream(communicator, inv.outParams); @@ -1469,12 +1434,8 @@ public class AllTests inv = initial.ice_invoke("opFloatSeqReq", OperationMode.Normal, inEncaps); in = new InputStream(communicator, inv.outParams); in.startEncapsulation(); - test(in.readOptional(1, OptionalFormat.VSize)); - in.skipSize(); - test(java.util.Arrays.equals(in.readFloatSeq(), p1.get())); - test(in.readOptional(3, OptionalFormat.VSize)); - in.skipSize(); - test(java.util.Arrays.equals(in.readFloatSeq(), p1.get())); + test(java.util.Arrays.equals(in.readFloatSeq(1).get(), p1.get())); + test(java.util.Arrays.equals(in.readFloatSeq(3).get(), p1.get())); in.endEncapsulation(); in = new InputStream(communicator, inv.outParams); @@ -1516,12 +1477,8 @@ public class AllTests inv = initial.ice_invoke("opDoubleSeqReq", OperationMode.Normal, inEncaps); in = new InputStream(communicator, inv.outParams); in.startEncapsulation(); - test(in.readOptional(1, OptionalFormat.VSize)); - in.skipSize(); - test(java.util.Arrays.equals(in.readDoubleSeq(), p1.get())); - test(in.readOptional(3, OptionalFormat.VSize)); - in.skipSize(); - test(java.util.Arrays.equals(in.readDoubleSeq(), p1.get())); + test(java.util.Arrays.equals(in.readDoubleSeq(1).get(), p1.get())); + test(java.util.Arrays.equals(in.readDoubleSeq(3).get(), p1.get())); in.endEncapsulation(); in = new InputStream(communicator, inv.outParams); @@ -1564,12 +1521,8 @@ public class AllTests inv = initial.ice_invoke("opStringSeqReq", OperationMode.Normal, inEncaps); in = new InputStream(communicator, inv.outParams); in.startEncapsulation(); - test(in.readOptional(1, OptionalFormat.FSize)); - in.skip(4); - test(java.util.Arrays.equals(in.readStringSeq(), p1.get())); - test(in.readOptional(3, OptionalFormat.FSize)); - in.skip(4); - test(java.util.Arrays.equals(in.readStringSeq(), p1.get())); + test(java.util.Arrays.equals(in.readStringSeq(1).get(), p1.get())); + test(java.util.Arrays.equals(in.readStringSeq(3).get(), p1.get())); in.endEncapsulation(); in = new InputStream(communicator, inv.outParams); @@ -1902,10 +1855,10 @@ public class AllTests in = new InputStream(communicator, inv.outParams); in.startEncapsulation(); test(in.readOptional(1, OptionalFormat.VSize)); - SerializableClass sc = SerializableHelper.read(in); + SerializableClass sc = in.readSerializable(SerializableClass.class); test(sc.equals(p1.get())); test(in.readOptional(3, OptionalFormat.VSize)); - sc = SerializableHelper.read(in); + sc = in.readSerializable(SerializableClass.class); test(sc.equals(p1.get())); in.endEncapsulation(); @@ -2080,11 +2033,10 @@ public class AllTests in = new InputStream(communicator, inEncaps); in.startEncapsulation(); - test(in.readOptional(2, OptionalFormat.Class)); - final Wrapper<A> w = new Wrapper<>(); - in.readValue(v -> w.value = (A)v); + final Wrapper<java.util.Optional<A>> w = new Wrapper<>(); + in.readValue(2, v -> w.value = v, A.class); in.endEncapsulation(); - test(w.value != null && w.value.requiredA == 56); + test(w.value.get() != null && w.value.get().requiredA == 56); } out.println("ok"); @@ -2359,13 +2311,10 @@ public class AllTests in.startSlice(); String s = in.readString(); test(s.equals("test")); - test(in.readOptional(1, OptionalFormat.FSize)); - in.skip(4); - String[] o = in.readStringSeq(); + String[] o = in.readStringSeq(1).get(); test(o.length == 4 && o[0].equals("test1") && o[1].equals("test2") && o[2].equals("test3") && o[3].equals("test4")); - test(in.readOptional(1000, OptionalFormat.Class)); - in.readValue(a); + in.readValue(1000, v -> a.value = v, A.class); in.endSlice(); // ::Test::B in.startSlice(); @@ -2380,10 +2329,10 @@ public class AllTests void check() { - test(((A)a.obj).getMc() == 18); + test(a.value.get().getMc() == 18); } - private ReadValueCallbackI a = new ReadValueCallbackI(); + private Wrapper<java.util.Optional<A>> a = new Wrapper<>(); } private static class FObjectReader extends com.zeroc.Ice.ValueReader @@ -2398,7 +2347,7 @@ public class AllTests //in.read(1, _f.af); in.endSlice(); in.startSlice(); - in.readValue(v -> _f.ae = (A)v); + in.readValue(v -> _f.ae = v, A.class); in.endSlice(); in.endValue(false); } @@ -2456,15 +2405,4 @@ public class AllTests private boolean _enabled; } - - private static class ReadValueCallbackI implements com.zeroc.Ice.ReadValueCallback - { - @Override - public void valueReady(com.zeroc.Ice.Value obj) - { - this.obj = obj; - } - - com.zeroc.Ice.Value obj; - } } diff --git a/java/test/src/main/java/test/Ice/stream/Client.java b/java/test/src/main/java/test/Ice/stream/Client.java index 9a78b1784bc..987b3a5d405 100644 --- a/java/test/src/main/java/test/Ice/stream/Client.java +++ b/java/test/src/main/java/test/Ice/stream/Client.java @@ -11,14 +11,6 @@ package test.Ice.stream; import java.io.PrintWriter; -import com.zeroc.Ice.BoolSeqHelper; -import com.zeroc.Ice.ByteSeqHelper; -import com.zeroc.Ice.DoubleSeqHelper; -import com.zeroc.Ice.FloatSeqHelper; -import com.zeroc.Ice.ShortSeqHelper; -import com.zeroc.Ice.IntSeqHelper; -import com.zeroc.Ice.LongSeqHelper; -import com.zeroc.Ice.StringSeqHelper; import com.zeroc.Ice.InputStream; import com.zeroc.Ice.OutputStream; import test.Ice.stream.Test.*; @@ -75,15 +67,9 @@ public class Client extends test.Util.Application } } - private static class TestReadValueCallback implements com.zeroc.Ice.ReadValueCallback + private static class Wrapper<T extends com.zeroc.Ice.Value> { - @Override - public void valueReady(com.zeroc.Ice.Value obj) - { - this.obj = obj; - } - - com.zeroc.Ice.Value obj; + T obj; } private static class MyClassFactoryWrapper implements com.zeroc.Ice.ValueFactory @@ -252,7 +238,7 @@ public class Client extends test.Util.Application SmallStruct.ice_write(out, s); byte[] data = out.finished(); in = new InputStream(comm, data); - SmallStruct s2 = SmallStruct.ice_read(in, null); + SmallStruct s2 = SmallStruct.ice_read(in); test(s2.equals(s)); } @@ -267,10 +253,10 @@ public class Client extends test.Util.Application out.writePendingValues(); byte[] data = out.finished(); in = new InputStream(comm, data); - TestReadValueCallback cb = new TestReadValueCallback(); - in.readValue(cb); + final Wrapper<OptionalClass> cb = new Wrapper<>(); + in.readValue(value -> cb.obj = value, OptionalClass.class); in.readPendingValues(); - OptionalClass o2 = (OptionalClass)cb.obj; + OptionalClass o2 = cb.obj; test(o2.bo == o.bo); test(o2.by == o.by); if(comm.getProperties().getProperty("Ice.Default.EncodingVersion").equals("1.0")) @@ -296,10 +282,10 @@ public class Client extends test.Util.Application out.writePendingValues(); byte[] data = out.finished(); in = new InputStream(comm, com.zeroc.Ice.Util.Encoding_1_0, data); - TestReadValueCallback cb = new TestReadValueCallback(); - in.readValue(cb); + final Wrapper<OptionalClass> cb = new Wrapper<>(); + in.readValue(value -> cb.obj = value, OptionalClass.class); in.readPendingValues(); - OptionalClass o2 = (OptionalClass)cb.obj; + OptionalClass o2 = cb.obj; test(o2.bo == o.bo); test(o2.by == o.by); test(!o2.hasSh()); @@ -315,10 +301,10 @@ public class Client extends test.Util.Application false }; out = new OutputStream(comm); - BoolSeqHelper.write(out, arr); + out.writeBoolSeq(arr); byte[] data = out.finished(); in = new InputStream(comm, data); - boolean[] arr2 = BoolSeqHelper.read(in); + boolean[] arr2 = in.readBoolSeq(); test(java.util.Arrays.equals(arr2, arr)); final boolean[][] arrS = @@ -344,10 +330,10 @@ public class Client extends test.Util.Application (byte)0x22 }; out = new OutputStream(comm); - ByteSeqHelper.write(out, arr); + out.writeByteSeq(arr); byte[] data = out.finished(); in = new InputStream(comm, data); - byte[] arr2 = ByteSeqHelper.read(in); + byte[] arr2 = in.readByteSeq(); test(java.util.Arrays.equals(arr2, arr)); final byte[][] arrS = @@ -371,7 +357,7 @@ public class Client extends test.Util.Application out.writeSerializable(small); byte[] data = out.finished(); in = new InputStream(comm, data); - test.Ice.stream.Serialize.Small small2 = (test.Ice.stream.Serialize.Small)in.readSerializable(); + test.Ice.stream.Serialize.Small small2 = in.readSerializable(test.Ice.stream.Serialize.Small.class); test(small2.i == 99); } @@ -384,10 +370,10 @@ public class Client extends test.Util.Application (short)0x22 }; out = new OutputStream(comm); - ShortSeqHelper.write(out, arr); + out.writeShortSeq(arr); byte[] data = out.finished(); in = new InputStream(comm, data); - short[] arr2 = ShortSeqHelper.read(in); + short[] arr2 = in.readShortSeq(); test(java.util.Arrays.equals(arr2, arr)); final short[][] arrS = @@ -413,10 +399,10 @@ public class Client extends test.Util.Application 0x22 }; out = new OutputStream(comm); - IntSeqHelper.write(out, arr); + out.writeIntSeq(arr); byte[] data = out.finished(); in = new InputStream(comm, data); - int[] arr2 = IntSeqHelper.read(in); + int[] arr2 = in.readIntSeq(); test(java.util.Arrays.equals(arr2, arr)); final int[][] arrS = @@ -442,10 +428,10 @@ public class Client extends test.Util.Application 0x22 }; out = new OutputStream(comm); - LongSeqHelper.write(out, arr); + out.writeLongSeq(arr); byte[] data = out.finished(); in = new InputStream(comm, data); - long[] arr2 = LongSeqHelper.read(in); + long[] arr2 = in.readLongSeq(); test(java.util.Arrays.equals(arr2, arr)); final long[][] arrS = @@ -471,10 +457,10 @@ public class Client extends test.Util.Application 4 }; out = new OutputStream(comm); - FloatSeqHelper.write(out, arr); + out.writeFloatSeq(arr); byte[] data = out.finished(); in = new InputStream(comm, data); - float[] arr2 = FloatSeqHelper.read(in); + float[] arr2 = in.readFloatSeq(); test(java.util.Arrays.equals(arr2, arr)); final float[][] arrS = @@ -500,10 +486,10 @@ public class Client extends test.Util.Application 4 }; out = new OutputStream(comm); - DoubleSeqHelper.write(out, arr); + out.writeDoubleSeq(arr); byte[] data = out.finished(); in = new InputStream(comm, data); - double[] arr2 = DoubleSeqHelper.read(in); + double[] arr2 = in.readDoubleSeq(); test(java.util.Arrays.equals(arr2, arr)); final double[][] arrS = @@ -529,10 +515,10 @@ public class Client extends test.Util.Application "string4" }; out = new OutputStream(comm); - StringSeqHelper.write(out, arr); + out.writeStringSeq(arr); byte[] data = out.finished(); in = new InputStream(comm, data); - String[] arr2 = StringSeqHelper.read(in); + String[] arr2 = in.readStringSeq(); test(java.util.Arrays.equals(arr2, arr)); final String[][] arrS = @@ -667,12 +653,11 @@ public class Client extends test.Util.Application test(writer.called); factoryWrapper.setFactory(new TestValueFactory()); in = new InputStream(comm, data); - TestReadValueCallback cb = new TestReadValueCallback(); - in.readValue(cb); + final Wrapper<TestObjectReader> cb = new Wrapper<>(); + in.readValue(value -> cb.obj = value, TestObjectReader.class); in.readPendingValues(); test(cb.obj != null); - test(cb.obj instanceof TestObjectReader); - TestObjectReader reader = (TestObjectReader)cb.obj; + TestObjectReader reader = cb.obj; test(reader.called); test(reader.obj != null); test(reader.obj.s.e == MyEnum.enum2); |