Debian Codegraph @

native/common/jp_classhints.cpp

raw symbols SCIP
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 <utility>
17
18 #include <Python.h>
19 #include "jpype.h"
20 #include "jp_arrayclass.h"
21 #include "jp_classhints.h"
22 #include "jp_proxy.h"
23 #include "jp_stringtype.h"
24
25 #include "pyjp.h"
26
27 JPMatch::JPMatch()
28 {
29 conversion = nullptr;
30 frame = nullptr;
31 object = nullptr;
32 type = JPMatch::_none;
33 slot = (JPValue*) - 1;
34 closure = nullptr;
35 }
36
37 JPMatch::JPMatch(JPJavaFrame *fr, PyObject *obj)
38 {
39 conversion = nullptr;
40 frame = fr;
41 object = obj;
42 type = JPMatch::_none;
43 slot = (JPValue*) - 1;
44 closure = nullptr;
45 }
46
47 JPValue *JPMatch::getJavaSlot()
48 {
49 if (slot == (JPValue*) - 1)
50 return slot = PyJPValue_getJavaSlot(object);
51 return slot;
52 }
53
54 jvalue JPMatch::convert()
55 {
56 // Sanity check, this should not happen
57 if (conversion == nullptr)
58 JP_RAISE(PyExc_SystemError, "Fail in conversion"); // GCOVR_EXCL_LINE
59 return conversion->convert(*this);
60 }
61
62 JPMethodMatch::JPMethodMatch(JPJavaFrame &frame, JPPyObjectVector& args, bool callInstance)
63 : m_Arguments(args.size())
64 {
65 m_Type = JPMatch::_none;
66 m_IsVarIndirect = false;
67 m_Overload = nullptr;
68 m_Offset = 0;
69 m_Skip = 0;
70 m_Hash = callInstance ? 0 : 1000;
71 for (size_t i = 0; i < args.size(); ++i)
72 {
73 PyObject *arg = args[i];
74 m_Arguments[i] = JPMatch(&frame, arg);
75
76 // This is an LCG used to compute a hash code for the incoming
77 // arguments using (A*X+A_i) mod2^64 where A_i is the address of each
78 // type the argument list. The hash will be checked to avoid needing
79 // to resolve the method if the same overload is called twice. There
80 // is only a speed cost if there is a collision, so we don't need to
81 // prove this is a perfect hash function.
82 m_Hash *= 0x10523C01;
83 m_Hash += (long) (Py_TYPE(arg));
84 }
85 }
86
87 JPConversion::~JPConversion() = default;
88 JPClassHints::JPClassHints()
89 {
90 m_ConvertJava = false;
91 }
92
93 JPClassHints::~JPClassHints()
94 {
95 for (auto & conversion : conversions)
96 {
97 delete conversion;
98 }
99 conversions.clear();
100 }
101
102 JPMatch::Type JPClassHints::getConversion(JPMatch& match, JPClass *cls)
103 {
104 JPConversion *best = nullptr;
105 for (auto & conversion : conversions)
106 {
107 JPMatch::Type quality = conversion->matches(cls, match);
108 if (quality > JPMatch::_explicit)
109 return match.type;
110 if (quality != JPMatch::_none)
111 best = conversion;
112 }
113 match.conversion = best;
114 if (best == nullptr)
115 return match.type = JPMatch::_none;
116 return match.type = JPMatch::_explicit;
117 }
118
119 void JPIndexConversion::getInfo(JPClass *cls, JPConversionInfo &info)
120 {
121 PyObject *typing = PyImport_AddModule("jpype.protocol");
122 JPPyObject proto = JPPyObject::call(PyObject_GetAttrString(typing, "SupportsIndex"));
123 PyList_Append(info.implicit, proto.get());
124 }
125
126 void JPNumberConversion::getInfo(JPClass *cls, JPConversionInfo &info)
127 {
128 JPIndexConversion::getInfo(cls, info);
129 PyObject *typing = PyImport_AddModule("jpype.protocol");
130 JPPyObject proto = JPPyObject::call(PyObject_GetAttrString(typing, "SupportsFloat"));
131 PyList_Append(info.implicit, proto.get());
132 }
133
134 /**
135 * Conversion for all user specified conversions.
136 */
137 class JPPythonConversion : public JPConversion
138 {
139 public:
140
141 explicit JPPythonConversion(PyObject *method)
142 {
143 method_ = JPPyObject::use(method);
144 }
145
146 ~JPPythonConversion() override = default;
147
148 jvalue convert(JPMatch &match) override
149 {
150 JP_TRACE_IN("JPPythonConversion::convert");
151 JPClass *cls = ((JPClass*) match.closure);
152 JPPyObject args = JPPyTuple_Pack(cls->getHost(), match.object);
153 JPPyObject ret = JPPyObject::call(PyObject_Call(method_.get(), args.get(), nullptr));
154 JPValue *value = PyJPValue_getJavaSlot(ret.get());
155 if (value != nullptr)
156 {
157 jvalue v = value->getValue();
158 JP_TRACE("Value", v.l);
159 v.l = match.frame->NewLocalRef(v.l);
160 return v;
161 }
162 JPProxy *proxy = PyJPProxy_getJPProxy(ret.get());
163 if (proxy != nullptr)
164 {
165 jvalue v = proxy->getProxy();
166 JP_TRACE("Proxy", v.l);
167 v.l = match.frame->NewLocalRef(v.l);
168 return v;
169 }
170 JP_RAISE(PyExc_TypeError, "Bad type conversion");
171 JP_TRACE_OUT;
172 }
173 private:
174
175 JPPyObject method_;
176 } ;
177
178 //<editor-fold desc="attribute conversion" defaultstate="collapsed">
179
180 class JPAttributeConversion : public JPPythonConversion
181 {
182 public:
183
184 JPAttributeConversion(string attribute, PyObject *method)
185 : JPPythonConversion(method), attribute_(std::move(attribute))
186 {
187 }
188
189 ~JPAttributeConversion() override = default;
190
191 JPMatch::Type matches(JPClass *cls, JPMatch &match) override
192 {
193 JP_TRACE_IN("JPAttributeConversion::matches");
194 JPPyObject attr = JPPyObject::accept(PyObject_GetAttrString(match.object, attribute_.c_str()));
195 if (attr.isNull())
196 return JPMatch::_none;
197 match.conversion = this;
198 match.closure = cls;
199 return match.type = JPMatch::_implicit;
200 JP_TRACE_OUT;
201 }
202
203 void getInfo(JPClass *cls, JPConversionInfo &info) override
204 {
205 PyList_Append(info.attributes, JPPyString::fromStringUTF8(attribute_).get());
206 }
207
208
209 private:
210 std::string attribute_;
211
212 } ;
213
214 void JPClassHints::addAttributeConversion(const string &attribute, PyObject *conversion)
215 {
216 JP_TRACE_IN("JPClassHints::addAttributeConversion", this);
217 JP_TRACE(attribute);
218 conversions.push_back(new JPAttributeConversion(attribute, conversion));
219 JP_TRACE_OUT;
220 }
221
222 //</editor-fold>
223 //<editor-fold desc="type conversion" defaultstate="collapsed">
224
225 class JPNoneConversion : public JPConversion
226 {
227 public:
228
229 explicit JPNoneConversion(PyObject *type)
230 {
231 type_ = JPPyObject::use(type);
232 }
233
234 ~JPNoneConversion() override
235 = default;
236
237 JPMatch::Type matches(JPClass *cls, JPMatch &match) override
238 {
239 JP_TRACE_IN("JPTypeConversion::matches");
240 if (!PyObject_IsInstance(match.object, type_.get()))
241 return JPMatch::_none;
242 match.closure = cls;
243 match.conversion = this;
244 match.type = JPMatch::_none;
245 return JPMatch::_implicit; // Prevent further searching
246 JP_TRACE_OUT;
247 }
248
249 void getInfo(JPClass *cls, JPConversionInfo &info) override
250 {
251 PyList_Append(info.none, type_.get());
252 }
253
254 jvalue convert(JPMatch &match) override
255 {
256 return jvalue();
257 }
258
259 private:
260 JPPyObject type_;
261 } ;
262
263 class JPTypeConversion : public JPPythonConversion
264 {
265 public:
266
267 JPTypeConversion(PyObject *type, PyObject *method, bool exact)
268 : JPPythonConversion(method), exact_(exact)
269 {
270 type_ = JPPyObject::use(type);
271 }
272
273 ~JPTypeConversion() override
274 = default;
275
276 JPMatch::Type matches(JPClass *cls, JPMatch &match) override
277 {
278 JP_TRACE_IN("JPTypeConversion::matches");
279 if ((exact_ && ((PyObject*) Py_TYPE(match.object)) == type_.get())
280 || PyObject_IsInstance(match.object, type_.get()))
281 {
282 match.closure = cls;
283 match.conversion = this;
284 return match.type = JPMatch::_implicit;
285 }
286 return JPMatch::_none;
287 JP_TRACE_OUT;
288 }
289
290 void getInfo(JPClass *cls, JPConversionInfo &info) override
291 {
292 PyList_Append(info.implicit, type_.get());
293 }
294
295 private:
296 JPPyObject type_;
297 bool exact_;
298 } ;
299
300 void JPClassHints::addTypeConversion(PyObject *type, PyObject *method, bool exact)
301 {
302 JP_TRACE_IN("JPClassHints::addTypeConversion", this);
303 if (PyJPClass_Check(type))
304 m_ConvertJava = true;
305 conversions.push_back(new JPTypeConversion(type, method, exact));
306 JP_TRACE_OUT;
307 }
308
309 void JPClassHints::excludeConversion(PyObject *type)
310 {
311 JP_TRACE_IN("JPClassHints::addTypeConversion", this);
312 conversions.push_front(new JPNoneConversion(type));
313 JP_TRACE_OUT;
314 }
315
316 void JPClassHints::getInfo(JPClass *cls, JPConversionInfo &info)
317 {
318 for (auto iter = conversions.begin();
319 iter != conversions.end(); ++iter)
320 {
321 (*iter)->getInfo(cls, info);
322 }
323 }
324
325 class JPHintsConversion : public JPConversion
326 {
327 public:
328
329 JPMatch::Type matches(JPClass *cls, JPMatch &match) override
330 {
331 auto *pyhints = (PyJPClassHints*) cls->getHints();
332 JPClassHints *hints = pyhints->m_Hints;
333 hints->getConversion(match, cls);
334 return match.type;
335 }
336
337 void getInfo(JPClass *cls, JPConversionInfo &info) override
338 {
339 auto *pyhints = (PyJPClassHints*) cls->getHints();
340 JPClassHints *hints = pyhints->m_Hints;
341 hints->getInfo(cls, info);
342 }
343
344 jvalue convert(JPMatch &match) override
345 {
346 return jvalue();
347 }
348 } _hintsConversion;
349
350 //</editor-fold>
351
352 class JPConversionCharArray : public JPConversion
353 {
354 public:
355
356 JPMatch::Type matches(JPClass *cls, JPMatch &match) override
357 {
358 JP_TRACE_IN("JPConversionCharArray::matches");
359 auto* acls = dynamic_cast<JPArrayClass*>( cls);
360 if (match.frame == nullptr || !JPPyString::check(match.object) ||
361 acls->getComponentType() != JPContext_global->_char)
362 return match.type = JPMatch::_none;
363 match.conversion = this;
364 return match.type = JPMatch::_implicit;
365 JP_TRACE_OUT;
366 }
367
368 void getInfo(JPClass *cls, JPConversionInfo &info) override
369 {
370 auto* acls = dynamic_cast<JPArrayClass*>( cls);
371 if (acls->getComponentType() != JPContext_global->_char)
372 return;
373 PyList_Append(info.implicit, (PyObject*) & PyUnicode_Type);
374 }
375
376 jvalue convert(JPMatch &match) override
377 {
378 JPJavaFrame *frame = match.frame;
379 JP_TRACE("char[]");
380 jvalue res;
381
382 // Convert to a string
383 string str = JPPyString::asStringUTF8(match.object);
384
385 // Convert to new java string
386 jstring jstr = frame->fromStringUTF8(str);
387
388 // call toCharArray()
389 res.l = frame->toCharArray(jstr);
390 return res;
391 }
392 } _charArrayConversion;
393
394 class JPConversionByteArray : public JPConversion
395 {
396 public:
397
398 JPMatch::Type matches(JPClass *cls, JPMatch &match) override
399 {
400 JP_TRACE_IN("JPConversionByteArray::matches");
401 auto* acls = dynamic_cast<JPArrayClass*>( cls);
402 if (match.frame == nullptr || !PyBytes_Check(match.object) ||
403 acls->getComponentType() != JPContext_global->_byte)
404 return match.type = JPMatch::_none;
405 match.conversion = this;
406 return match.type = JPMatch::_implicit;
407 JP_TRACE_OUT;
408 }
409
410 void getInfo(JPClass *cls, JPConversionInfo &info) override
411 {
412 auto* acls = dynamic_cast<JPArrayClass*>( cls);
413 if (acls->getComponentType() != JPContext_global->_byte)
414 return;
415 PyList_Append(info.implicit, (PyObject*) & PyBytes_Type);
416 }
417
418 jvalue convert(JPMatch &match) override
419 {
420 JPJavaFrame frame(*match.frame);
421 jvalue res;
422 Py_ssize_t size = 0;
423 char *buffer = nullptr;
424 PyBytes_AsStringAndSize(match.object, &buffer, &size); // internal reference
425 jbyteArray byteArray = frame.NewByteArray((jsize) size);
426 frame.SetByteArrayRegion(byteArray, 0, (jsize) size, (jbyte*) buffer);
427 res.l = frame.keep(byteArray);
428 return res;
429 }
430 } _byteArrayConversion;
431
432 class JPConversionBuffer : public JPConversion
433 {
434 public:
435
436 JPMatch::Type matches(JPClass *cls, JPMatch &match) override
437 {
438 JP_TRACE_IN("JPConversionBuffer::matches");
439 auto *acls = dynamic_cast<JPArrayClass*>( cls);
440 JPClass *componentType = acls->getComponentType();
441 if ( !componentType->isPrimitive())
442 return match.type = JPMatch::_none;
443 // If is isn't a buffer we can skip
444 JPPyBuffer buffer(match.object, PyBUF_ND | PyBUF_FORMAT);
445 if (!buffer.valid())
446 {
447 PyErr_Clear();
448 return match.type = JPMatch::_none;
449 }
450
451 // If it is a buffer we only need to test the first item in the list
452 JPPySequence seq = JPPySequence::use(match.object);
453 jlong length = seq.size();
454 if (length == -1 && PyErr_Occurred())
455 {
456 PyErr_Clear();
457 return match.type = JPMatch::_none;
458 }
459 match.type = JPMatch::_implicit;
460 if (length > 0)
461 {
462 JPPyObject item = seq[0];
463 JPMatch imatch(match.frame, item.get());
464 componentType->findJavaConversion(imatch);
465 if (imatch.type < match.type)
466 match.type = imatch.type;
467 }
468 match.closure = cls;
469 match.conversion = bufferConversion;
470 return match.type;
471 JP_TRACE_OUT;
472 }
473
474 void getInfo(JPClass *cls, JPConversionInfo &info) override
475 {
476 // This will be covered by Sequence
477 }
478
479 jvalue convert(JPMatch &match) override
480 {
481 JP_TRACE_IN("JPConversionBuffer::convert");
482 JPJavaFrame frame(*match.frame);
483 jvalue res;
484 auto *acls = (JPArrayClass *) match.closure;
485 auto length = (jsize) PySequence_Length(match.object);
486 JPClass *ccls = acls->getComponentType();
487 jarray array = ccls->newArrayOf(frame, (jsize) length);
488 ccls->setArrayRange(frame, array, 0, length, 1, match.object);
489 res.l = frame.keep(array);
490 return res;
491 JP_TRACE_OUT;
492 }
493 } _bufferConversion;
494
495 class JPConversionSequence : public JPConversion
496 {
497 public:
498
499 JPMatch::Type matches(JPClass *cls, JPMatch &match) override
500 {
501 JP_TRACE_IN("JPConversionSequence::matches");
502 if ( !PySequence_Check(match.object) || JPPyString::check(match.object))
503 return match.type = JPMatch::_none;
504 auto *acls = dynamic_cast<JPArrayClass*>( cls);
505 JPClass *componentType = acls->getComponentType();
506 JPPySequence seq = JPPySequence::use(match.object);
507 jlong length = seq.size();
508 if (length==-1 && PyErr_Occurred())
509 {
510 PyErr_Clear();
511 return match.type = JPMatch::_none;
512 }
513 match.type = JPMatch::_implicit;
514 for (jlong i = 0; i < length && match.type > JPMatch::_none; i++)
515 {
516 // This is a special case. Sequences produce new references
517 // so we must hold the reference in a container while the
518 // the match is caching it.
519 JPPyObject item = seq[i];
520 JPMatch imatch(match.frame, item.get());
521 componentType->findJavaConversion(imatch);
522 if (imatch.type < match.type)
523 match.type = imatch.type;
524 }
525 match.closure = cls;
526 match.conversion = sequenceConversion;
527 return match.type;
528 JP_TRACE_OUT;
529 }
530
531 void getInfo(JPClass *cls, JPConversionInfo &info) override
532 {
533 PyObject *typing = PyImport_AddModule("jpype.protocol");
534 JPPyObject proto = JPPyObject::call(PyObject_GetAttrString(typing, "Sequence"));
535 PyList_Append(info.implicit, proto.get());
536 auto* acls = dynamic_cast<JPArrayClass*>( cls);
537 if (acls->getComponentType() == JPContext_global->_char)
538 return;
539 PyList_Append(info.none, (PyObject*) & PyUnicode_Type);
540 }
541
542 jvalue convert(JPMatch &match) override
543 {
544 JPJavaFrame frame(*match.frame);
545 jvalue res;
546 auto *acls = (JPArrayClass *) match.closure;
547 auto length = (jsize) PySequence_Length(match.object);
548 JPClass *ccls = acls->getComponentType();
549 jarray array = ccls->newArrayOf(frame, (jsize) length);
550 ccls->setArrayRange(frame, array, 0, length, 1, match.object);
551 res.l = frame.keep(array);
552 return res;
553 }
554 } _sequenceConversion;
555
556 class JPConversionNull : public JPConversion
557 {
558 public:
559
560 JPMatch::Type matches(JPClass *cls, JPMatch &match) override
561 {
562 JP_TRACE_IN("JPConversionNull::matches");
563 if (match.object != Py_None)
564 return match.type = JPMatch::_none;
565 match.conversion = this;
566 return match.type = JPMatch::_implicit;
567 JP_TRACE_OUT;
568 }
569
570 void getInfo(JPClass *cls, JPConversionInfo &info) override
571 {
572 }
573
574 jvalue convert(JPMatch &match) override
575 {
576 jvalue v;
577 v.l = nullptr;
578 return v;
579 }
580 } _nullConversion;
581
582 class JPConversionClass : public JPConversion
583 {
584 public:
585
586 JPMatch::Type matches(JPClass *cls, JPMatch &match) override
587 {
588 JP_TRACE_IN("JPConversionClass::matches");
589 if (match.frame == nullptr)
590 return match.type = JPMatch::_none;
591 JPClass* cls2 = PyJPClass_getJPClass(match.object);
592 if (cls2 == nullptr)
593 return match.type = JPMatch::_none;
594 match.conversion = this;
595 match.closure = cls2;
596 return match.type = JPMatch::_implicit;
597 JP_TRACE_OUT;
598 }
599
600 void getInfo(JPClass *cls, JPConversionInfo &info) override
601 {
602 JPJavaFrame frame = JPJavaFrame::outer();
603 PyList_Append(info.implicit, (PyObject*) PyJPClass_Type);
604 }
605
606 jvalue convert(JPMatch &match) override
607 {
608 jvalue res;
609 auto* cls2 = (JPClass*) match.closure;
610 res.l = match.frame->NewLocalRef(cls2->getJavaClass());
611 return res;
612 }
613 } _classConversion;
614
615 class JPConversionObject : public JPConversion
616 {
617 public:
618
619 JPMatch::Type matches(JPClass *cls, JPMatch &match) override
620 {
621 JP_TRACE_IN("JPConversionObject::matches");
622 JPValue *value = match.getJavaSlot();
623 if (value == nullptr || match.frame == nullptr)
624 return match.type = JPMatch::_none;
625 match.conversion = this;
626 JPClass *oc = value->getClass();
627 if (oc == nullptr)
628 return match.type = JPMatch::_none;
629 if (oc == cls)
630 {
631 // hey, this is me! :)
632 return match.type = JPMatch::_exact;
633 }
634 bool assignable = match.frame->IsAssignableFrom(oc->getJavaClass(), cls->getJavaClass()) != 0;
635 JP_TRACE("assignable", assignable, oc->getCanonicalName(), cls->getCanonicalName());
636 match.type = (assignable ? JPMatch::_derived : JPMatch::_none);
637
638 // User has request a Java class to class conversion. We must pass through check it.
639 if (!assignable)
640 {
641 auto *pyhints = (PyJPClassHints*) cls->getHints();
642 JPClassHints *hints = pyhints->m_Hints;
643 if (hints->m_ConvertJava)
644 return match.type;
645 }
646
647 // This is the one except to the conversion rule patterns.
648 // If it is a Java value then we must prevent it from proceeding
649 // through the conversion rules even if it was not a match.
650 // Thus the return result and the match type differ here.
651 return JPMatch::_implicit;
652 JP_TRACE_OUT;
653 }
654
655 void getInfo(JPClass *cls, JPConversionInfo &info) override
656 {
657 JPJavaFrame frame = JPJavaFrame::outer();
658 PyList_Append(info.exact, PyJPClass_create(frame, cls).get());
659 }
660
661 jvalue convert(JPMatch &match) override
662 {
663 jvalue res;
664 JPValue* value = match.getJavaSlot();
665 res.l = match.frame->NewLocalRef(value->getValue().l);
666 return res;
667 }
668 } _objectConversion;
669
670 JPMatch::Type JPConversionJavaValue::matches(JPClass *cls, JPMatch &match)
671 {
672 JP_TRACE_IN("JPConversionJavaValue::matches");
673 JPValue *slot = match.getJavaSlot();
674 if (slot == nullptr || slot->getClass() != cls)
675 return match.type = JPMatch::_none;
676 match.conversion = this;
677 return match.type = JPMatch::_exact;
678 JP_TRACE_OUT;
679 }
680
681 void JPConversionJavaValue::getInfo(JPClass *cls, JPConversionInfo &info)
682 {
683 JPJavaFrame frame = JPJavaFrame::outer();
684 PyList_Append(info.exact, PyJPClass_create(frame, cls).get());
685 }
686
687 jvalue JPConversionJavaValue::convert(JPMatch &match)
688 {
689 JPValue* value = match.getJavaSlot();
690 return *value;
691 }
692
693 JPConversionJavaValue _javaValueConversion;
694
695 class JPConversionString : public JPConversion
696 {
697 public:
698
699 JPMatch::Type matches(JPClass *cls, JPMatch &match) override
700 {
701 JP_TRACE_IN("JPConversionString::matches");
702 if (match.frame == nullptr || !JPPyString::check(match.object))
703 return match.type = JPMatch::_none;
704 match.conversion = this;
705 if (cls == JPContext_global->_java_lang_String)
706 return match.type = JPMatch::_exact;
707 return match.type = JPMatch::_implicit;
708 JP_TRACE_OUT;
709 }
710
711 void getInfo(JPClass *cls, JPConversionInfo &info) override
712 {
713 PyList_Append(info.implicit, (PyObject*) & PyUnicode_Type);
714 }
715
716 jvalue convert(JPMatch &match) override
717 {
718 jvalue res;
719 string str = JPPyString::asStringUTF8(match.object);
720 res.l = match.frame->fromStringUTF8(str);
721 return res;
722 }
723 } _stringConversion;
724
725 class JPConversionBox : public JPConversion
726 {
727 public:
728
729 jvalue convert(JPMatch &match) override
730 {
731 jvalue res;
732 JPPyObjectVector args(match.object, nullptr);
733 auto *cls = (JPClass*) match.closure;
734 JPValue pobj = cls->newInstance(*match.frame, args);
735 res.l = pobj.getJavaObject();
736 return res;
737 }
738 } ;
739
740 class JPConversionBoxBoolean : public JPConversionBox
741 {
742 public:
743
744 JPMatch::Type matches(JPClass *cls, JPMatch &match) override
745 {
746 JP_TRACE_IN("JPConversionBoxBoolean::matches");
747 if (!PyBool_Check(match.object))
748 return match.type = JPMatch::_none;
749 match.conversion = this;
750 match.closure = JPContext_global->_java_lang_Boolean;
751 return match.type = JPMatch::_implicit;
752 JP_TRACE_OUT;
753 }
754
755 void getInfo(JPClass *cls, JPConversionInfo &info) override
756 {
757 PyList_Append(info.implicit, (PyObject*) & PyBool_Type);
758 }
759
760 } _boxBooleanConversion;
761
762 class JPConversionBoxLong : public JPConversionBox
763 {
764 public:
765
766 JPMatch::Type matches(JPClass *cls, JPMatch &match) override
767 {
768 JP_TRACE_IN("JPConversionBoxLong::matches");
769 if (match.frame == nullptr)
770 return match.type = JPMatch::_none;
771 if (PyLong_CheckExact(match.object) || PyIndex_Check(match.object))
772 {
773 match.conversion = this;
774 return match.type = JPMatch::_implicit;
775 }
776 return match.type = JPMatch::_none;
777 JP_TRACE_OUT;
778 }
779
780 void getInfo(JPClass *cls, JPConversionInfo &info) override
781 {
782 PyObject *typing = PyImport_AddModule("jpype.protocol");
783 JPPyObject proto = JPPyObject::call(PyObject_GetAttrString(typing, "SupportsIndex"));
784 PyList_Append(info.implicit, proto.get());
785 }
786
787 jvalue convert(JPMatch &match) override
788 {
789 PyTypeObject* type = Py_TYPE(match.object);
790 const char *name = type->tp_name;
791 match.closure = JPContext_global->_java_lang_Long;
792 if (strncmp(name, "numpy", 5) == 0)
793 {
794 // We only handle specific sized types, all others go to long.
795 if (strcmp(&name[5], ".int8") == 0)
796 match.closure = JPContext_global->_java_lang_Byte;
797 else if (strcmp(&name[5], ".int16") == 0)
798 match.closure = JPContext_global->_java_lang_Short;
799 else if (strcmp(&name[5], ".int32") == 0)
800 match.closure = JPContext_global->_java_lang_Integer;
801 }
802 return JPConversionBox::convert(match);
803 }
804 } _boxLongConversion;
805
806 class JPConversionBoxDouble : public JPConversionBox
807 {
808 public:
809
810 JPMatch::Type matches(JPClass *cls, JPMatch &match) override
811 {
812 JP_TRACE_IN("JPConversionBoxDouble::matches");
813 if (match.frame == nullptr)
814 return match.type = JPMatch::_none;
815 if (PyNumber_Check(match.object))
816 {
817 match.conversion = this;
818 return match.type = JPMatch::_implicit;
819 }
820 return match.type = JPMatch::_none;
821 JP_TRACE_OUT;
822 }
823
824 void getInfo(JPClass *cls, JPConversionInfo &info) override
825 {
826 PyObject *typing = PyImport_AddModule("jpype.protocol");
827 JPPyObject proto = JPPyObject::call(PyObject_GetAttrString(typing, "SupportsFloat"));
828 PyList_Append(info.implicit, proto.get());
829 }
830
831 jvalue convert(JPMatch &match) override
832 {
833 PyTypeObject* type = Py_TYPE(match.object);
834 const char *name = type->tp_name;
835 match.closure = JPContext_global->_java_lang_Double;
836 if (strncmp(name, "numpy", 5) == 0)
837 {
838 // We only handle specific sized types, all others go to double.
839 if (strcmp(&name[5], ".float32") == 0)
840 match.closure = JPContext_global->_java_lang_Float;
841 }
842 return JPConversionBox::convert(match);
843 }
844 } _boxDoubleConversion;
845
846 class JPConversionJavaObjectAny : public JPConversionBox
847 {
848 public:
849
850 JPMatch::Type matches(JPClass *cls, JPMatch &match) override
851 {
852 JP_TRACE_IN("JPConversionJavaObjectAny::matches");
853 JPValue *value = match.getJavaSlot();
854 if (value == nullptr || match.frame == nullptr || value->getClass() == nullptr)
855 return match.type = JPMatch::_none;
856 match.conversion = this;
857 if (value->getClass()->isPrimitive())
858 match.type = JPMatch::_implicit;
859 else if (value->getClass() == cls)
860 match.type = JPMatch::_exact;
861 else
862 match.type = JPMatch::_derived;
863 return match.type;
864 JP_TRACE_OUT;
865 }
866
867 void getInfo(JPClass *cls, JPConversionInfo &info) override
868 {
869 JPJavaFrame frame = JPJavaFrame::outer();
870 PyList_Append(info.implicit, PyJPClass_create(frame, JPContext_global->_java_lang_Object).get());
871 }
872
873 jvalue convert(JPMatch &match) override
874 {
875 jvalue res;
876 JPJavaFrame *frame = match.frame;
877 JPValue *value = match.getJavaSlot();
878 if (!value->getClass()->isPrimitive())
879 {
880 res.l = frame->NewLocalRef(value->getJavaObject());
881 return res;
882 } else
883 {
884 // Okay we need to box it.
885 auto* type = dynamic_cast<JPPrimitiveType*> (value->getClass());
886 match.closure = type->getBoxedClass(*frame);
887 res = JPConversionBox::convert(match);
888 return res;
889 }
890 }
891 } _javaObjectAnyConversion;
892
893 class JPConversionJavaNumberAny : public JPConversionJavaObjectAny
894 {
895 public:
896
897 JPMatch::Type matches(JPClass *cls, JPMatch &match) override
898 {
899 JP_TRACE_IN("JPConversionJavaNumberAny::matches");
900 JPContext *context = JPContext_global;
901 JPValue *value = match.getJavaSlot();
902 // This converter only works for number types, thus boolean and char
903 // are excluded.
904 if (value == nullptr || match.frame == nullptr || value->getClass() == nullptr
905 || value->getClass() == context->_boolean
906 || value->getClass() == context->_char)
907 return match.type = JPMatch::_none;
908 match.conversion = this;
909 JPClass *oc = value->getClass();
910 // If it is the exact type, then it is exact
911 if (oc == cls)
912 return match.type = JPMatch::_exact;
913 // If it is any primitive except char and boolean then implicit
914 if (oc->isPrimitive())
915 return match.type = JPMatch::_implicit;
916 // Otherwise, check if it is assignable according to Java
917 bool assignable = match.frame->IsAssignableFrom(oc->getJavaClass(), cls->getJavaClass()) != 0;
918 return match.type = (assignable ? JPMatch::_implicit : JPMatch::_none);
919 JP_TRACE_OUT;
920 }
921
922 void getInfo(JPClass *cls, JPConversionInfo &info) override
923 {
924 PyList_Append(info.implicit, (PyObject*) PyJPNumberLong_Type);
925 PyList_Append(info.implicit, (PyObject*) PyJPNumberFloat_Type);
926 }
927
928 } _javaNumberAnyConversion;
929
930 class JPConversionUnbox : public JPConversion
931 {
932 public:
933
934 JPMatch::Type matches(JPClass *cls, JPMatch &match) override
935 {
936 JPContext *context = JPContext_global;
937 if (context == nullptr)
938 return match.type = JPMatch::_none;
939 JPValue *slot = match.slot;
940 auto *pcls = dynamic_cast<JPPrimitiveType*>( cls);
941 if (slot->getClass() != pcls->getBoxedClass(*match.frame))
942 return match.type = JPMatch::_none;
943 match.conversion = this;
944 match.closure = cls;
945 return match.type = JPMatch::_implicit;
946 }
947
948 void getInfo(JPClass *cls, JPConversionInfo &info) override
949 {
950 JPJavaFrame frame = JPJavaFrame::outer();
951 auto *pcls = dynamic_cast<JPPrimitiveType*>( cls);
952 PyList_Append(info.implicit,
953 PyJPClass_create(frame, pcls->getBoxedClass(frame)).get());
954 }
955
956 jvalue convert(JPMatch &match) override
957 {
958 JPValue* value = match.getJavaSlot();
959 auto *cls = (JPClass*) match.closure;
960 return cls->getValueFromObject(*match.frame, *value);
961 }
962 } _unboxConversion;
963
964 class JPConversionProxy : public JPConversion
965 {
966 public:
967
968 JPMatch::Type matches(JPClass *cls, JPMatch &match) override
969 {
970 JP_TRACE_IN("JPConversionProxy::matches");
971 JPProxy* proxy = PyJPProxy_getJPProxy(match.object);
972 if (proxy == nullptr || match.frame == nullptr)
973 return match.type = JPMatch::_none;
974
975 // Check if any of the interfaces matches ...
976 vector<JPClass*> itf = proxy->getInterfaces();
977 for (auto & i : itf)
978 {
979 if (match.frame->IsAssignableFrom(i->getJavaClass(), cls->getJavaClass()))
980 {
981 JP_TRACE("implicit proxy");
982 match.conversion = this;
983 return match.type = JPMatch::_implicit;
984 }
985 }
986 return match.type = JPMatch::_none;
987 JP_TRACE_OUT;
988 }
989
990 void getInfo(JPClass *cls, JPConversionInfo &info) override
991 {
992 }
993
994 jvalue convert(JPMatch &match) override
995 {
996 return PyJPProxy_getJPProxy(match.object)->getProxy();
997 }
998 } _proxyConversion;
999
1000 JPConversion *hintsConversion = &_hintsConversion;
1001 JPConversion *charArrayConversion = &_charArrayConversion;
1002 JPConversion *byteArrayConversion = &_byteArrayConversion;
1003 JPConversion *bufferConversion = &_bufferConversion;
1004 JPConversion *sequenceConversion = &_sequenceConversion;
1005 JPConversion *nullConversion = &_nullConversion;
1006 JPConversion *classConversion = &_classConversion;
1007 JPConversion *objectConversion = &_objectConversion;
1008 JPConversion *javaObjectAnyConversion = &_javaObjectAnyConversion;
1009 JPConversion *javaNumberAnyConversion = &_javaNumberAnyConversion;
1010 JPConversion *javaValueConversion = &_javaValueConversion;
1011 JPConversion *stringConversion = &_stringConversion;
1012 JPConversion *boxBooleanConversion = &_boxBooleanConversion;
1013 JPConversion *boxLongConversion = &_boxLongConversion;
1014 JPConversion *boxDoubleConversion = &_boxDoubleConversion;
1015 JPConversion *unboxConversion = &_unboxConversion;
1016 JPConversion *proxyConversion = &_proxyConversion;