| 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_arrayclass.h" |
| 19 | #include "jp_context.h" |
| 20 | #include "jp_stringtype.h" |
| 21 | |
| 22 | JPArrayClass::JPArrayClass(JPJavaFrame& frame, |
| 23 | jclass cls, |
| 24 | const string& name, |
| 25 | JPClass* superClass, |
| 26 | JPClass* componentType, |
| 27 | jint modifiers) |
| 28 | : JPClass(frame, cls, name, superClass, JPClassList(), modifiers) |
| 29 | { |
| 30 | m_ComponentType = componentType; |
| 31 | } |
| 32 | |
| 33 | JPArrayClass::~JPArrayClass() |
| 34 | = default; |
| 35 | |
| 36 | JPMatch::Type JPArrayClass::findJavaConversion(JPMatch &match) |
| 37 | { |
| 38 | JP_TRACE_IN("JPArrayClass::findJavaConversion"); |
| 39 | if (nullConversion->matches(this, match) |
| 40 | || objectConversion->matches(this, match) |
| 41 | || bufferConversion->matches(this, match) |
| 42 | || charArrayConversion->matches(this, match) |
| 43 | || byteArrayConversion->matches(this, match) |
| 44 | || sequenceConversion->matches(this, match) |
| 45 | || hintsConversion->matches(this, match) |
| 46 | ) |
| 47 | return match.type; |
| 48 | JP_TRACE("None"); |
| 49 | return match.type = JPMatch::_none; |
| 50 | JP_TRACE_OUT; |
| 51 | } |
| 52 | |
| 53 | void JPArrayClass::getConversionInfo(JPConversionInfo &info) |
| 54 | { |
| 55 | JPJavaFrame frame = JPJavaFrame::outer(); |
| 56 | objectConversion->getInfo(this, info); |
| 57 | charArrayConversion->getInfo(this, info); |
| 58 | byteArrayConversion->getInfo(this, info); |
| 59 | sequenceConversion->getInfo(this, info); |
| 60 | hintsConversion->getInfo(this, info); |
| 61 | PyList_Append(info.ret, PyJPClass_create(frame, this).get()); |
| 62 | } |
| 63 | |
| 64 | JPPyObject JPArrayClass::convertToPythonObject(JPJavaFrame& frame, jvalue value, bool cast) |
| 65 | { |
| 66 | JP_TRACE_IN("JPArrayClass::convertToPythonObject"); |
| 67 | if (!cast) |
| 68 | { |
| 69 | if (value.l == nullptr) |
| 70 | return JPPyObject::getNone(); |
| 71 | } |
| 72 | JPPyObject wrapper = PyJPClass_create(frame, this); |
| 73 | JPPyObject obj = PyJPArray_create(frame, (PyTypeObject*) wrapper.get(), JPValue(this, value)); |
| 74 | return obj; |
| 75 | JP_TRACE_OUT; |
| 76 | } |
| 77 | |
| 78 | jvalue JPArrayClass::convertToJavaVector(JPJavaFrame& frame, JPPyObjectVector& refs, jsize start, jsize end) |
| 79 | { |
| 80 | JP_TRACE_IN("JPArrayClass::convertToJavaVector"); |
| 81 | auto length = (jsize) (end - start); |
| 82 | |
| 83 | jarray array = m_ComponentType->newArrayOf(frame, length); |
| 84 | jvalue res; |
| 85 | for (jsize i = start; i < end; i++) |
| 86 | { |
| 87 | m_ComponentType->setArrayItem(frame, array, i - start, refs[i]); |
| 88 | } |
| 89 | res.l = array; |
| 90 | return res; |
| 91 | JP_TRACE_OUT; |
| 92 | } |
| 93 | |
| 94 | JPValue JPArrayClass::newArray(JPJavaFrame& frame, int length) |
| 95 | { |
| 96 | jvalue v; |
| 97 | v.l = m_ComponentType->newArrayOf(frame, length); |
| 98 | return JPValue(this, v); |
| 99 | } |