| 1 | /***************************************************************************** |
| 2 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | you may not use this file except in compliance with the License. |
| 4 | You may obtain a copy of the License at |
| 5 | |
| 6 | http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | |
| 8 | Unless required by applicable law or agreed to in writing, software |
| 9 | distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | See the License for the specific language governing permissions and |
| 12 | limitations under the License. |
| 13 | |
| 14 | See NOTICE file for details. |
| 15 | *****************************************************************************/ |
| 16 | #include "jpype.h" |
| 17 | #include "pyjp.h" |
| 18 | #include "jp_stringtype.h" |
| 19 | |
| 20 | JPStringType::JPStringType(JPJavaFrame& frame, |
| 21 | jclass clss, |
| 22 | const string& name, |
| 23 | JPClass* super, |
| 24 | JPClassList& interfaces, |
| 25 | jint modifiers) |
| 26 | : JPClass(frame, clss, name, super, interfaces, modifiers) |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | JPStringType::~JPStringType() |
| 31 | = default; |
| 32 | |
| 33 | JPPyObject JPStringType::convertToPythonObject(JPJavaFrame& frame, jvalue val, bool cast) |
| 34 | { |
| 35 | JP_TRACE_IN("JPStringType::asHostObject"); |
| 36 | JPContext *context = frame.getContext(); |
| 37 | |
| 38 | if (!cast) |
| 39 | { |
| 40 | // This loses type |
| 41 | if (val.l == nullptr) |
| 42 | { |
| 43 | return JPPyObject::getNone(); |
| 44 | } |
| 45 | |
| 46 | if (context->getConvertStrings()) |
| 47 | { |
| 48 | string str = frame.toStringUTF8((jstring) (val.l)); |
| 49 | return JPPyObject::call(PyUnicode_FromStringAndSize(str.c_str(), str.length())); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | return JPClass::convertToPythonObject(frame, val, cast); |
| 54 | JP_TRACE_OUT; // GCOV_EXCL_LINE |
| 55 | } |
| 56 | |
| 57 | JPMatch::Type JPStringType::findJavaConversion(JPMatch& match) |
| 58 | { |
| 59 | JP_TRACE_IN("JPStringType::findJavaConversion"); |
| 60 | if (nullConversion->matches(this, match) |
| 61 | || objectConversion->matches(this, match) |
| 62 | || stringConversion->matches(this, match) |
| 63 | || hintsConversion->matches(this, match) |
| 64 | ) |
| 65 | return match.type; |
| 66 | return match.type = JPMatch::_none; |
| 67 | JP_TRACE_OUT; // GCOV_EXCL_LINE |
| 68 | } |
| 69 | |
| 70 | void JPStringType::getConversionInfo(JPConversionInfo &info) |
| 71 | { |
| 72 | JPJavaFrame frame = JPJavaFrame::outer(); |
| 73 | JPContext* context = frame.getContext(); |
| 74 | objectConversion->getInfo(this, info); |
| 75 | stringConversion->getInfo(this, info); |
| 76 | hintsConversion->getInfo(this, info); |
| 77 | if (context->getConvertStrings()) |
| 78 | PyList_Append(info.ret, (PyObject*) & PyUnicode_Type); // GCOVR_EXCL_LINE |
| 79 | else |
| 80 | PyList_Append(info.ret, (PyObject*) getHost()); |
| 81 | } |
| 82 | |
| 83 | JPValue JPStringType::newInstance(JPJavaFrame& frame, JPPyObjectVector& args) |
| 84 | { |
| 85 | JP_TRACE_IN("JPStringType::newInstance"); |
| 86 | if (args.size() == 1 && JPPyString::check(args[0])) |
| 87 | { |
| 88 | // JNI has a short cut for constructing java.lang.String |
| 89 | JP_TRACE("Direct"); |
| 90 | string str = JPPyString::asStringUTF8(args[0]); |
| 91 | return JPValue(this, frame.fromStringUTF8(str)); |
| 92 | } |
| 93 | return JPClass::newInstance(frame, args); |
| 94 | JP_TRACE_OUT; // GCOV_EXCL_LINE |
| 95 | } |