| 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_field.h" |
| 19 | #include "jp_methoddispatch.h" |
| 20 | #include "jp_method.h" |
| 21 | |
| 22 | JPClass::JPClass( |
| 23 | const string& name, |
| 24 | jint modifiers) |
| 25 | { |
| 26 | m_CanonicalName = name; |
| 27 | m_SuperClass = nullptr; |
| 28 | m_Interfaces = JPClassList(); |
| 29 | m_Modifiers = modifiers; |
| 30 | } |
| 31 | |
| 32 | JPClass::JPClass(JPJavaFrame& frame, |
| 33 | jclass clss, |
| 34 | const string& name, |
| 35 | JPClass* super, |
| 36 | const JPClassList& interfaces, |
| 37 | jint modifiers) |
| 38 | : m_Class(frame, clss) |
| 39 | { |
| 40 | m_CanonicalName = name; |
| 41 | m_SuperClass = super; |
| 42 | m_Interfaces = interfaces; |
| 43 | m_Modifiers = modifiers; |
| 44 | } |
| 45 | |
| 46 | JPClass::~JPClass()= default; |
| 47 | |
| 48 | void JPClass::setHost(PyObject* host) |
| 49 | { |
| 50 | m_Host = JPPyObject::use(host); |
| 51 | } |
| 52 | |
| 53 | void JPClass::setHints(PyObject* host) |
| 54 | { |
| 55 | m_Hints = JPPyObject::use(host); |
| 56 | } |
| 57 | |
| 58 | jclass JPClass::getJavaClass() const |
| 59 | { |
| 60 | jclass cls = m_Class.get(); |
| 61 | // This sanity check should not be possible to exercise |
| 62 | if (cls == nullptr) |
| 63 | JP_RAISE(PyExc_RuntimeError, "Class is null"); // GCOVR_EXCL_LINE |
| 64 | return cls; |
| 65 | } |
| 66 | |
| 67 | void JPClass::ensureMembers(JPJavaFrame& frame) |
| 68 | { |
| 69 | JPContext* context = JPContext_global; |
| 70 | JPTypeManager* typeManager = context->getTypeManager(); |
| 71 | typeManager->populateMembers(this); |
| 72 | } |
| 73 | |
| 74 | void JPClass::assignMembers(JPMethodDispatch* ctor, |
| 75 | JPMethodDispatchList& methods, |
| 76 | JPFieldList& fields) |
| 77 | { |
| 78 | m_Constructors = ctor; |
| 79 | m_Methods = methods; |
| 80 | m_Fields = fields; |
| 81 | } |
| 82 | |
| 83 | //<editor-fold desc="new" defaultstate="collapsed"> |
| 84 | |
| 85 | JPValue JPClass::newInstance(JPJavaFrame& frame, JPPyObjectVector& args) |
| 86 | { |
| 87 | if (m_Constructors == nullptr) |
| 88 | { |
| 89 | if (this->isInterface()) |
| 90 | { |
| 91 | JP_RAISE(PyExc_TypeError, "Cannot create Java interface instances"); |
| 92 | } else |
| 93 | { |
| 94 | JP_RAISE(PyExc_TypeError, "Java class has no constructors"); |
| 95 | } |
| 96 | } |
| 97 | return m_Constructors->invokeConstructor(frame, args); |
| 98 | } |
| 99 | |
| 100 | JPClass* JPClass::newArrayType(JPJavaFrame &frame, long d) |
| 101 | { |
| 102 | if (d < 0 || d > 255) |
| 103 | JP_RAISE(PyExc_ValueError, "Invalid array dimensions"); |
| 104 | std::stringstream ss; |
| 105 | for (long i = 0; i < d; ++i) |
| 106 | ss << "["; |
| 107 | if (isPrimitive()) |
| 108 | ss << (dynamic_cast<JPPrimitiveType*>( this))->getTypeCode(); |
| 109 | else if (isArray()) |
| 110 | ss << getName(); |
| 111 | else |
| 112 | ss << "L" << getName() << ";"; |
| 113 | return frame.findClassByName(ss.str()); |
| 114 | } |
| 115 | |
| 116 | jarray JPClass::newArrayOf(JPJavaFrame& frame, jsize sz) |
| 117 | { |
| 118 | return frame.NewObjectArray(sz, getJavaClass(), nullptr); |
| 119 | } |
| 120 | //</editor-fold> |
| 121 | //<editor-fold desc="acccessors" defaultstate="collapsed"> |
| 122 | |
| 123 | // GCOVR_EXCL_START |
| 124 | // This is currently only used in tracing |
| 125 | |
| 126 | string JPClass::toString() const |
| 127 | { |
| 128 | // This sanity check will not be hit in normal operation |
| 129 | if (JPContext_global == nullptr) |
| 130 | return m_CanonicalName; // GCOVR_EXCL_LINE |
| 131 | JPJavaFrame frame = JPJavaFrame::outer(); |
| 132 | return frame.toString(m_Class.get()); |
| 133 | } |
| 134 | // GCOVR_EXCL_STOP |
| 135 | |
| 136 | string JPClass::getName() const |
| 137 | { |
| 138 | // This sanity check will not be hit in normal operation |
| 139 | if (JPContext_global == nullptr) |
| 140 | return m_CanonicalName; // GCOVR_EXCL_LINE |
| 141 | JPJavaFrame frame = JPJavaFrame::outer(); |
| 142 | return frame.toString(frame.CallObjectMethodA( |
| 143 | (jobject) m_Class.get(), JPContext_global->m_Class_GetNameID, nullptr)); |
| 144 | } |
| 145 | |
| 146 | //</editor-fold> |
| 147 | //<editor-fold desc="as return type" defaultstate="collapsed"> |
| 148 | |
| 149 | JPPyObject JPClass::getStaticField(JPJavaFrame& frame, jclass c, jfieldID fid) |
| 150 | { |
| 151 | JP_TRACE_IN("JPClass::getStaticField"); |
| 152 | jobject r = frame.GetStaticObjectField(c, fid); |
| 153 | JPClass* type = this; |
| 154 | if (r != nullptr) |
| 155 | type = frame.findClassForObject(r); |
| 156 | jvalue v; |
| 157 | v.l = r; |
| 158 | return type->convertToPythonObject(frame, v, false); |
| 159 | JP_TRACE_OUT; |
| 160 | } |
| 161 | |
| 162 | JPPyObject JPClass::getField(JPJavaFrame& frame, jobject c, jfieldID fid) |
| 163 | { |
| 164 | JP_TRACE_IN("JPClass::getField"); |
| 165 | jobject r = frame.GetObjectField(c, fid); |
| 166 | JPClass* type = this; |
| 167 | if (r != nullptr) |
| 168 | type = frame.findClassForObject(r); |
| 169 | jvalue v; |
| 170 | v.l = r; |
| 171 | return type->convertToPythonObject(frame, v, false); |
| 172 | JP_TRACE_OUT; |
| 173 | } |
| 174 | |
| 175 | JPPyObject JPClass::invokeStatic(JPJavaFrame& frame, jclass claz, jmethodID mth, jvalue* val) |
| 176 | { |
| 177 | JP_TRACE_IN("JPClass::invokeStatic"); |
| 178 | jvalue v; |
| 179 | { |
| 180 | JPPyCallRelease call; |
| 181 | v.l = frame.CallStaticObjectMethodA(claz, mth, val); |
| 182 | } |
| 183 | |
| 184 | JPClass *type = this; |
| 185 | if (v.l != nullptr) |
| 186 | type = frame.findClassForObject(v.l); |
| 187 | |
| 188 | return type->convertToPythonObject(frame, v, false); |
| 189 | |
| 190 | JP_TRACE_OUT; |
| 191 | } |
| 192 | |
| 193 | JPPyObject JPClass::invoke(JPJavaFrame& frame, jobject obj, jclass clazz, jmethodID mth, jvalue* val) |
| 194 | { |
| 195 | JP_TRACE_IN("JPClass::invoke"); |
| 196 | jvalue v; |
| 197 | |
| 198 | // Call method |
| 199 | { |
| 200 | JPPyCallRelease call; |
| 201 | if (obj == nullptr) |
| 202 | JP_RAISE(PyExc_ValueError, "method called on null object"); |
| 203 | if (clazz == nullptr) |
| 204 | v.l = frame.CallObjectMethodA(obj, mth, val); |
| 205 | else |
| 206 | v.l = frame.CallNonvirtualObjectMethodA(obj, clazz, mth, val); |
| 207 | } |
| 208 | |
| 209 | // Get the return type |
| 210 | JPClass *type = this; |
| 211 | if (v.l != nullptr) |
| 212 | type = frame.findClassForObject(v.l); |
| 213 | |
| 214 | return type->convertToPythonObject(frame, v, false); |
| 215 | |
| 216 | JP_TRACE_OUT; |
| 217 | } |
| 218 | |
| 219 | void JPClass::setStaticField(JPJavaFrame& frame, jclass c, jfieldID fid, PyObject* obj) |
| 220 | { |
| 221 | JP_TRACE_IN("JPClass::setStaticField"); |
| 222 | JPMatch match(&frame, obj); |
| 223 | if (findJavaConversion(match) < JPMatch::_implicit) |
| 224 | { |
| 225 | std::stringstream err; |
| 226 | err << "unable to convert to " << getCanonicalName(); |
| 227 | JP_RAISE(PyExc_TypeError, err.str()); |
| 228 | } |
| 229 | jobject val = match.convert().l; |
| 230 | frame.SetStaticObjectField(c, fid, val); |
| 231 | JP_TRACE_OUT; |
| 232 | } |
| 233 | |
| 234 | void JPClass::setField(JPJavaFrame& frame, jobject c, jfieldID fid, PyObject* obj) |
| 235 | { |
| 236 | JP_TRACE_IN("JPClass::setField"); |
| 237 | JPMatch match(&frame, obj); |
| 238 | if (findJavaConversion(match) < JPMatch::_implicit) |
| 239 | { |
| 240 | std::stringstream err; |
| 241 | err << "unable to convert to " << getCanonicalName(); |
| 242 | JP_RAISE(PyExc_TypeError, err.str()); |
| 243 | } |
| 244 | jobject val = match.convert().l; |
| 245 | frame.SetObjectField(c, fid, val); |
| 246 | JP_TRACE_OUT; |
| 247 | } |
| 248 | |
| 249 | void JPClass::setArrayRange(JPJavaFrame& frame, jarray a, |
| 250 | jsize start, jsize length, jsize step, |
| 251 | PyObject* vals) |
| 252 | { |
| 253 | JP_TRACE_IN("JPClass::setArrayRange"); |
| 254 | auto array = (jobjectArray) a; |
| 255 | |
| 256 | // Verify before we start the conversion, as we wont be able |
| 257 | // to abort once we start |
| 258 | JPPySequence seq = JPPySequence::use(vals); |
| 259 | JP_TRACE("Verify argument types"); |
| 260 | for (int i = 0; i < length; i++) |
| 261 | { |
| 262 | JPPyObject v = seq[i]; |
| 263 | JPMatch match(&frame, v.get()); |
| 264 | if (findJavaConversion(match) < JPMatch::_implicit) |
| 265 | JP_RAISE(PyExc_TypeError, "Unable to convert"); |
| 266 | } |
| 267 | |
| 268 | JP_TRACE("Copy"); |
| 269 | int index = start; |
| 270 | for (int i = 0; i < length; i++, index += step) |
| 271 | { |
| 272 | JPPyObject v = seq[i]; |
| 273 | JPMatch match(&frame, v.get()); |
| 274 | findJavaConversion(match); |
| 275 | frame.SetObjectArrayElement(array, index, match.convert().l); |
| 276 | } |
| 277 | JP_TRACE_OUT; |
| 278 | } |
| 279 | |
| 280 | void JPClass::setArrayItem(JPJavaFrame& frame, jarray a, jsize ndx, PyObject* val) |
| 281 | { |
| 282 | JP_TRACE_IN("JPClass::setArrayItem"); |
| 283 | JPMatch match(&frame, val); |
| 284 | findJavaConversion(match); |
| 285 | JP_TRACE("Type", getCanonicalName()); |
| 286 | if ( match.type < JPMatch::_implicit) |
| 287 | { |
| 288 | JP_RAISE(PyExc_TypeError, "Unable to convert"); |
| 289 | } |
| 290 | jvalue v = match.convert(); |
| 291 | frame.SetObjectArrayElement((jobjectArray) a, ndx, v.l); |
| 292 | JP_TRACE_OUT; |
| 293 | } |
| 294 | |
| 295 | JPPyObject JPClass::getArrayItem(JPJavaFrame& frame, jarray a, jsize ndx) |
| 296 | { |
| 297 | JP_TRACE_IN("JPClass::getArrayItem"); |
| 298 | auto array = (jobjectArray) a; |
| 299 | |
| 300 | jobject obj = frame.GetObjectArrayElement(array, ndx); |
| 301 | JPClass *retType = this; |
| 302 | jvalue v; |
| 303 | v.l = obj; |
| 304 | if (obj != nullptr) |
| 305 | retType = frame.findClassForObject(v.l); |
| 306 | return retType->convertToPythonObject(frame, v, false); |
| 307 | JP_TRACE_OUT; |
| 308 | } |
| 309 | |
| 310 | //</editor-fold> |
| 311 | //<editor-fold desc="conversion" defaultstate="collapsed"> |
| 312 | |
| 313 | JPValue JPClass::getValueFromObject(JPJavaFrame& frame, const JPValue& obj) |
| 314 | { |
| 315 | JP_TRACE_IN("JPClass::getValueFromObject"); |
| 316 | return JPValue(this, obj.getJavaObject()); |
| 317 | JP_TRACE_OUT; |
| 318 | } |
| 319 | |
| 320 | JPPyObject JPClass::convertToPythonObject(JPJavaFrame& frame, jvalue value, bool cast) |
| 321 | { |
| 322 | JP_TRACE_IN("JPClass::convertToPythonObject"); |
| 323 | JPClass *cls = this; |
| 324 | if (!cast) |
| 325 | { |
| 326 | // Returning None likely incorrect from java prospective. |
| 327 | // Java still knows the type of null objects thus |
| 328 | // converting to None would pose a problem as we lose type. |
| 329 | // We would need subclass None for this to make sense so we |
| 330 | // can carry both the type and the null, but Python considers |
| 331 | // None a singleton so this is not an option. |
| 332 | // |
| 333 | // Of course if we don't mind that "Object is None" would |
| 334 | // fail, but "Object == None" would be true, the we |
| 335 | // could support null objects properly. However, this would |
| 336 | // need to work as "None == Object" which may be hard to |
| 337 | // achieve. |
| 338 | // |
| 339 | // We will still need to have the concept of null objects |
| 340 | // but we can get those through JObject(None, cls). |
| 341 | if (value.l == nullptr) |
| 342 | { |
| 343 | return JPPyObject::getNone(); |
| 344 | } |
| 345 | |
| 346 | cls = frame.findClassForObject(value.l); |
| 347 | if (cls != this) |
| 348 | return cls->convertToPythonObject(frame, value, true); |
| 349 | } |
| 350 | |
| 351 | JPPyObject obj; |
| 352 | JPPyObject wrapper = PyJPClass_create(frame, cls); |
| 353 | |
| 354 | if (isThrowable()) |
| 355 | { |
| 356 | JPPyObject tuple0; |
| 357 | if (value.l == nullptr) |
| 358 | { |
| 359 | tuple0 = JPPyObject::call(PyTuple_New(0)); |
| 360 | } else |
| 361 | { |
| 362 | jstring m = frame.getMessage((jthrowable) value.l); |
| 363 | if (m != nullptr) |
| 364 | { |
| 365 | tuple0 = JPPyTuple_Pack( |
| 366 | JPPyString::fromStringUTF8(frame.toStringUTF8(m)).get()); |
| 367 | } else |
| 368 | { |
| 369 | tuple0 = JPPyTuple_Pack( |
| 370 | JPPyString::fromStringUTF8(frame.toString(value.l)).get()); |
| 371 | } |
| 372 | } |
| 373 | JPPyObject tuple1 = JPPyTuple_Pack(_JObjectKey, tuple0.get()); |
| 374 | // Exceptions need new and init |
| 375 | obj = JPPyObject::call(PyObject_Call(wrapper.get(), tuple1.get(), nullptr)); |
| 376 | } else |
| 377 | { |
| 378 | PyTypeObject *type = ((PyTypeObject*) wrapper.get()); |
| 379 | // Simple objects don't have a new or init function |
| 380 | PyObject *obj2 = type->tp_alloc(type, 0); |
| 381 | JP_PY_CHECK(); |
| 382 | obj = JPPyObject::claim(obj2); |
| 383 | } |
| 384 | |
| 385 | // Fill in the Java slot |
| 386 | PyJPValue_assignJavaSlot(frame, obj.get(), JPValue(cls, value)); |
| 387 | return obj; |
| 388 | JP_TRACE_OUT; |
| 389 | } |
| 390 | |
| 391 | JPMatch::Type JPClass::findJavaConversion(JPMatch &match) |
| 392 | { |
| 393 | JP_TRACE_IN("JPClass::findJavaConversion"); |
| 394 | if (nullConversion->matches(this, match) |
| 395 | || objectConversion->matches(this, match) |
| 396 | || proxyConversion->matches(this, match) |
| 397 | || hintsConversion->matches(this, match)) |
| 398 | return match.type; |
| 399 | JP_TRACE("No match"); |
| 400 | return match.type = JPMatch::_none; |
| 401 | JP_TRACE_OUT; |
| 402 | } |
| 403 | |
| 404 | PyObject* JPClass::getHints() |
| 405 | { |
| 406 | PyObject* out = m_Hints.get(); |
| 407 | if (out != nullptr) |
| 408 | return out; |
| 409 | // Force creation |
| 410 | JPJavaFrame frame = JPJavaFrame::outer(); |
| 411 | PyJPClass_create(frame, this); |
| 412 | return m_Hints.get(); |
| 413 | } |
| 414 | |
| 415 | void JPClass::getConversionInfo(JPConversionInfo &info) |
| 416 | { |
| 417 | JP_TRACE_IN("JPClass::getConversionInfo"); |
| 418 | JPJavaFrame frame = JPJavaFrame::outer(); |
| 419 | objectConversion->getInfo(this, info); |
| 420 | hintsConversion->getInfo(this, info); |
| 421 | PyList_Append(info.ret, PyJPClass_create(frame, this).get()); |
| 422 | JP_TRACE_OUT; |
| 423 | } |
| 424 | |
| 425 | //</editor-fold> |
| 426 | //<editor-fold desc="hierarchy" defaultstate="collapsed"> |
| 427 | |
| 428 | bool JPClass::isAssignableFrom(JPJavaFrame& frame, JPClass* o) |
| 429 | { |
| 430 | return frame.IsAssignableFrom(m_Class.get(), o->getJavaClass()) != 0; |
| 431 | } |
| 432 | |
| 433 | //</editor-fold> |