| 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_primitive_accessor.h" |
| 19 | #include "jp_classloader.h" |
| 20 | #include "jp_boxedtype.h" |
| 21 | #include "jp_field.h" |
| 22 | #include "jp_objecttype.h" |
| 23 | #include "jp_numbertype.h" |
| 24 | #include "jp_classtype.h" |
| 25 | #include "jp_method.h" |
| 26 | #include "jp_methoddispatch.h" |
| 27 | #include "jp_buffertype.h" |
| 28 | #include "jp_typemanager.h" |
| 29 | #include "jp_arrayclass.h" |
| 30 | #include "jp_stringtype.h" |
| 31 | #include "jp_voidtype.h" |
| 32 | #include "jp_booleantype.h" |
| 33 | #include "jp_bytetype.h" |
| 34 | #include "jp_chartype.h" |
| 35 | #include "jp_shorttype.h" |
| 36 | #include "jp_inttype.h" |
| 37 | #include "jp_longtype.h" |
| 38 | #include "jp_floattype.h" |
| 39 | #include "jp_doubletype.h" |
| 40 | #include "jp_functional.h" |
| 41 | #include "jp_proxy.h" |
| 42 | |
| 43 | void JPTypeFactory_rethrow(JPJavaFrame& frame) |
| 44 | { |
| 45 | try |
| 46 | { |
| 47 | throw; |
| 48 | } catch (JPypeException& ex) |
| 49 | { |
| 50 | ex.toJava(); |
| 51 | } catch (...) // GCOVR_EXCL_LINE |
| 52 | { |
| 53 | // GCOVR_EXCL_START |
| 54 | frame.ThrowNew(frame.getContext()->m_RuntimeException.get(), |
| 55 | "unknown error occurred"); |
| 56 | // GCOVR_EXCL_STOP |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | template <class T> void convert(JPJavaFrame& frame, jlongArray array, vector<T>& out) |
| 61 | { |
| 62 | JPPrimitiveArrayAccessor<jlongArray, jlong*> accessor(frame, array, |
| 63 | &JPJavaFrame::GetLongArrayElements, &JPJavaFrame::ReleaseLongArrayElements); |
| 64 | jlong* values = accessor.get(); |
| 65 | jsize sz = frame.GetArrayLength(array); |
| 66 | out.resize(sz); |
| 67 | for (int i = 0; i < sz; ++i) |
| 68 | { |
| 69 | out[i] = (T) values[i]; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | #ifdef JP_TRACING_ENABLE |
| 74 | #define JP_JAVA_TRY(...) \ |
| 75 | JPypeTracer _trace(__VA_ARGS__); \ |
| 76 | try { |
| 77 | #define JP_JAVA_CATCH(...) \ |
| 78 | } \ |
| 79 | catch(...) { \ |
| 80 | _trace.gotError(JP_STACKINFO()); \ |
| 81 | JPTypeFactory_rethrow(frame); } return __VA_ARGS__ |
| 82 | #else |
| 83 | #define JP_JAVA_TRY(...) try { |
| 84 | #define JP_JAVA_CATCH(...) } catch(...) { JPTypeFactory_rethrow(frame); } return __VA_ARGS__ |
| 85 | #endif |
| 86 | |
| 87 | extern "C" |
| 88 | { |
| 89 | |
| 90 | JNIEXPORT void JNICALL Java_org_jpype_manager_TypeFactoryNative_newWrapper( |
| 91 | JNIEnv *env, jobject self, jlong contextPtr, jlong jcls) |
| 92 | { |
| 93 | JPJavaFrame frame = JPJavaFrame::external(env); |
| 94 | JP_JAVA_TRY("JPTypeFactory_newWrapper"); |
| 95 | JPPyCallAcquire callback; |
| 96 | auto* cls = (JPClass*) jcls; |
| 97 | PyJPClass_hook(frame, cls); |
| 98 | JP_JAVA_CATCH(); // GCOVR_EXCL_LINE |
| 99 | } |
| 100 | |
| 101 | JNIEXPORT void JNICALL Java_org_jpype_manager_TypeFactoryNative_destroy( |
| 102 | JNIEnv *env, jobject self, jlong contextPtr, |
| 103 | jlongArray resources, |
| 104 | jint sz) |
| 105 | { |
| 106 | JPJavaFrame frame = JPJavaFrame::external(env); |
| 107 | auto* context = frame.getContext(); |
| 108 | JP_JAVA_TRY("JPTypeFactory_destroy"); |
| 109 | JPPrimitiveArrayAccessor<jlongArray, jlong*> accessor(frame, resources, |
| 110 | &JPJavaFrame::GetLongArrayElements, &JPJavaFrame::ReleaseLongArrayElements); |
| 111 | jlong* values = accessor.get(); |
| 112 | for (int i = 0; i < sz; ++i) |
| 113 | { |
| 114 | context->m_Resources.push_back((JPResource*) values[i]); |
| 115 | } |
| 116 | return; |
| 117 | JP_JAVA_CATCH(); // GCOVR_EXCL_LINE |
| 118 | } |
| 119 | |
| 120 | JNIEXPORT jlong JNICALL Java_org_jpype_manager_TypeFactoryNative_defineMethodDispatch( |
| 121 | JNIEnv *env, jobject self, jlong contextPtr, |
| 122 | jlong clsPtr, |
| 123 | jstring name, |
| 124 | jlongArray overloadPtrs, |
| 125 | jint modifiers) |
| 126 | { |
| 127 | JPJavaFrame frame = JPJavaFrame::external(env); |
| 128 | JP_JAVA_TRY("JPTypeFactory_defineMethodDispatch"); |
| 129 | auto* cls = (JPClass*) clsPtr; |
| 130 | JPMethodList overloadList; |
| 131 | convert(frame, overloadPtrs, overloadList); |
| 132 | string cname = frame.toStringUTF8(name); |
| 133 | JP_TRACE(cname); |
| 134 | auto* dispatch = new JPMethodDispatch(cls, cname, overloadList, modifiers); |
| 135 | return (jlong) dispatch; |
| 136 | JP_JAVA_CATCH(0); // GCOVR_EXCL_LINE |
| 137 | } |
| 138 | |
| 139 | JNIEXPORT jlong JNICALL Java_org_jpype_manager_TypeFactoryNative_defineArrayClass( |
| 140 | JNIEnv *env, jobject self, jlong contextPtr, |
| 141 | jclass cls, |
| 142 | jstring name, |
| 143 | jlong superClass, |
| 144 | jlong componentClass, |
| 145 | jint modifiers) |
| 146 | { |
| 147 | JPJavaFrame frame = JPJavaFrame::external(env); |
| 148 | JP_JAVA_TRY("JPTypeFactory_defineArrayClass"); |
| 149 | string cname = frame.toStringUTF8(name); |
| 150 | JP_TRACE(cname); |
| 151 | auto* result = new JPArrayClass(frame, cls, |
| 152 | cname, |
| 153 | (JPClass*) superClass, |
| 154 | (JPClass*) componentClass, |
| 155 | modifiers); |
| 156 | return (jlong) result; |
| 157 | JP_JAVA_CATCH(0); // GCOVR_EXCL_LINE |
| 158 | } |
| 159 | |
| 160 | JNIEXPORT jlong JNICALL Java_org_jpype_manager_TypeFactoryNative_defineObjectClass( |
| 161 | JNIEnv *env, jobject self, jlong contextPtr, |
| 162 | jclass cls, |
| 163 | jstring name, |
| 164 | jlong superClass, |
| 165 | jlongArray interfacePtrs, |
| 166 | jint modifiers) |
| 167 | { |
| 168 | // All resources are created here are owned by Java and deleted by Java shutdown routine |
| 169 | JPJavaFrame frame = JPJavaFrame::external(env); |
| 170 | auto* context = frame.getContext(); |
| 171 | JP_JAVA_TRY("JPTypeFactory_defineObjectClass"); |
| 172 | string className = frame.toStringUTF8(name); |
| 173 | JP_TRACE(className); |
| 174 | JPClassList interfaces; |
| 175 | if (interfacePtrs != nullptr) |
| 176 | convert(frame, interfacePtrs, interfaces); |
| 177 | JPClass* result = nullptr; |
| 178 | if (!JPModifier::isSpecial(modifiers)) |
| 179 | { |
| 180 | // Create a normal class |
| 181 | return (jlong) new JPClass(frame, cls, className, (JPClass*) superClass, interfaces, modifiers); |
| 182 | } |
| 183 | |
| 184 | if (JPModifier::isFunctional(modifiers)) |
| 185 | return (jlong) new JPFunctional(frame, cls, className, (JPClass*) superClass, interfaces, modifiers); |
| 186 | if (JPModifier::isBuffer(modifiers)) |
| 187 | return (jlong) new JPBufferType(frame, cls, className, (JPClass*) superClass, interfaces, modifiers); |
| 188 | // Certain classes require special implementations |
| 189 | if (className == "java.lang.Object") |
| 190 | return (jlong) (context->_java_lang_Object |
| 191 | = new JPObjectType(frame, cls, className, (JPClass*) superClass, interfaces, modifiers)); |
| 192 | if (className == "java.lang.Class") |
| 193 | return (jlong) (context->_java_lang_Class |
| 194 | = new JPClassType(frame, cls, className, (JPClass*) superClass, interfaces, modifiers)); |
| 195 | if (className == "java.lang.CharSequence") |
| 196 | return (jlong) (new JPStringType(frame, cls, className, |
| 197 | (JPClass*) superClass, interfaces, modifiers)); |
| 198 | if (className == "java.lang.String") |
| 199 | return (jlong) (context->_java_lang_String |
| 200 | = new JPStringType(frame, cls, className, |
| 201 | (JPClass*) superClass, interfaces, modifiers)); |
| 202 | if (className == "java.lang.Throwable") |
| 203 | return (jlong) (context->_java_lang_Throwable |
| 204 | = new JPClassType(frame, cls, className, |
| 205 | (JPClass*) superClass, interfaces, modifiers)); |
| 206 | |
| 207 | if (className == "java.lang.Number") |
| 208 | return (jlong) new JPNumberType(frame, cls, className, (JPClass*) superClass, interfaces, modifiers); |
| 209 | |
| 210 | |
| 211 | // Register the box types |
| 212 | if (className == "java.lang.Void") |
| 213 | { |
| 214 | context->_void = new JPVoidType(); |
| 215 | return (jlong) (context->_java_lang_Void |
| 216 | = new JPBoxedType(frame, cls, className, |
| 217 | (JPClass*) superClass, interfaces, modifiers, context->_void)); |
| 218 | } |
| 219 | if (className == "java.lang.Boolean") |
| 220 | { |
| 221 | context->_boolean = new JPBooleanType(); |
| 222 | return (jlong) (context->_java_lang_Boolean |
| 223 | = new JPBoxedType(frame, cls, className, |
| 224 | (JPClass*) superClass, interfaces, modifiers, context->_boolean)); |
| 225 | } |
| 226 | if (className == "java.lang.Byte") |
| 227 | { |
| 228 | context->_byte = new JPByteType(); |
| 229 | return (jlong) (context->_java_lang_Byte |
| 230 | = new JPBoxedType(frame, cls, className, |
| 231 | (JPClass*) superClass, interfaces, modifiers, context->_byte)); |
| 232 | } |
| 233 | if (className == "java.lang.Character") |
| 234 | { |
| 235 | context->_char = new JPCharType(); |
| 236 | return (jlong) (context->_java_lang_Character |
| 237 | = new JPBoxedType(frame, cls, className, |
| 238 | (JPClass*) superClass, interfaces, modifiers, context->_char)); |
| 239 | } |
| 240 | if (className == "java.lang.Short") |
| 241 | { |
| 242 | context->_short = new JPShortType(); |
| 243 | return (jlong) (context->_java_lang_Short |
| 244 | = new JPBoxedType(frame, cls, className, |
| 245 | (JPClass*) superClass, interfaces, modifiers, context->_short)); |
| 246 | } |
| 247 | if (className == "java.lang.Integer") |
| 248 | { |
| 249 | context->_int = new JPIntType(); |
| 250 | return (jlong) (context->_java_lang_Integer |
| 251 | = new JPBoxedType(frame, cls, className, |
| 252 | (JPClass*) superClass, interfaces, modifiers, context->_int)); |
| 253 | } |
| 254 | if (className == "java.lang.Long") |
| 255 | { |
| 256 | context->_long = new JPLongType(); |
| 257 | return (jlong) (context->_java_lang_Long |
| 258 | = new JPBoxedType(frame, cls, className, |
| 259 | (JPClass*) superClass, interfaces, modifiers, context->_long)); |
| 260 | } |
| 261 | if (className == "java.lang.Float") |
| 262 | { |
| 263 | context->_float = new JPFloatType(); |
| 264 | return (jlong) (context->_java_lang_Float |
| 265 | = new JPBoxedType(frame, cls, className, |
| 266 | (JPClass*) superClass, interfaces, modifiers, context->_float)); |
| 267 | } |
| 268 | if (className == "java.lang.Double") |
| 269 | { |
| 270 | context->_double = new JPDoubleType(); |
| 271 | return (jlong) (context->_java_lang_Double |
| 272 | = new JPBoxedType(frame, cls, className, |
| 273 | (JPClass*) superClass, interfaces, modifiers, context->_double)); |
| 274 | } |
| 275 | if (className == "org.jpype.proxy.JPypeProxy") |
| 276 | return (jlong) |
| 277 | new JPProxyType(frame, cls, className, |
| 278 | (JPClass*) superClass, interfaces, modifiers); |
| 279 | |
| 280 | // Register reflection types for later use |
| 281 | if (className == "java.lang.reflect.Method") |
| 282 | return (jlong) (context->_java_lang_reflect_Method |
| 283 | = new JPClass(frame, cls, className, (JPClass*) superClass, interfaces, modifiers)); |
| 284 | |
| 285 | if (className == "java.lang.reflect.Field") |
| 286 | return (jlong) (context->_java_lang_reflect_Field |
| 287 | = new JPClass(frame, cls, className, (JPClass*) superClass, interfaces, modifiers)); |
| 288 | |
| 289 | std::stringstream ss; |
| 290 | ss << "Special class not defined for " << className; |
| 291 | JP_RAISE(PyExc_RuntimeError, ss.str()); |
| 292 | return (jlong) result; |
| 293 | JP_JAVA_CATCH(0); // GCOVR_EXCL_LINE |
| 294 | } |
| 295 | |
| 296 | JNIEXPORT jlong JNICALL Java_org_jpype_manager_TypeFactoryNative_definePrimitive( |
| 297 | JNIEnv *env, jobject self, jlong contextPtr, |
| 298 | jstring name, |
| 299 | jclass cls, |
| 300 | jlong boxedPtr, |
| 301 | jint modifiers) |
| 302 | { |
| 303 | // These resources are created by the boxed types |
| 304 | JPJavaFrame frame = JPJavaFrame::external(env); |
| 305 | auto* context = frame.getContext(); |
| 306 | JP_JAVA_TRY("JPTypeFactory_definePrimitive"); |
| 307 | string cname = frame.toStringUTF8(name); |
| 308 | JP_TRACE(cname); |
| 309 | if (cname == "void") |
| 310 | { |
| 311 | context->_void->setClass(frame, cls); |
| 312 | return (jlong) (context->_void); |
| 313 | } |
| 314 | if (cname == "byte") |
| 315 | { |
| 316 | context->_byte->setClass(frame, cls); |
| 317 | return (jlong) (context->_byte); |
| 318 | } |
| 319 | if (cname == "boolean") |
| 320 | { |
| 321 | context->_boolean->setClass(frame, cls); |
| 322 | return (jlong) (context->_boolean); |
| 323 | } |
| 324 | if (cname == "char") |
| 325 | { |
| 326 | context->_char->setClass(frame, cls); |
| 327 | return (jlong) (context->_char); |
| 328 | } |
| 329 | if (cname == "short") |
| 330 | { |
| 331 | context->_short->setClass(frame, cls); |
| 332 | return (jlong) (context->_short); |
| 333 | } |
| 334 | if (cname == "int") |
| 335 | { |
| 336 | context->_int->setClass(frame, cls); |
| 337 | return (jlong) (context->_int); |
| 338 | } |
| 339 | if (cname == "long") |
| 340 | { |
| 341 | context->_long->setClass(frame, cls); |
| 342 | return (jlong) (context->_long); |
| 343 | } |
| 344 | if (cname == "float") |
| 345 | { |
| 346 | context->_float->setClass(frame, cls); |
| 347 | return (jlong) (context->_float); |
| 348 | } |
| 349 | if (cname == "double") |
| 350 | { |
| 351 | context->_double->setClass(frame, cls); |
| 352 | return (jlong) (context->_double); |
| 353 | } |
| 354 | return 0; |
| 355 | JP_JAVA_CATCH(0); // GCOVR_EXCL_LINE |
| 356 | } |
| 357 | |
| 358 | JNIEXPORT void JNICALL Java_org_jpype_manager_TypeFactoryNative_assignMembers( |
| 359 | JNIEnv *env, jobject self, |
| 360 | jlong contextPtr, |
| 361 | jlong clsPtr, |
| 362 | jlong ctorMethod, |
| 363 | jlongArray methodPtrs, |
| 364 | jlongArray fieldPtrs) |
| 365 | { |
| 366 | JPJavaFrame frame = JPJavaFrame::external(env); |
| 367 | JP_JAVA_TRY("JPTypeFactory_assignMembers"); |
| 368 | auto* cls = (JPClass*) clsPtr; |
| 369 | JPMethodDispatchList methodList; |
| 370 | convert(frame, methodPtrs, methodList); |
| 371 | |
| 372 | JPFieldList fieldList; |
| 373 | convert(frame, fieldPtrs, fieldList); |
| 374 | cls->assignMembers( |
| 375 | (JPMethodDispatch*) ctorMethod, |
| 376 | methodList, |
| 377 | fieldList); |
| 378 | return; |
| 379 | JP_JAVA_CATCH(); // GCOVR_EXCL_LINE |
| 380 | } |
| 381 | |
| 382 | JNIEXPORT jlong JNICALL Java_org_jpype_manager_TypeFactoryNative_defineField( |
| 383 | JNIEnv *env, jobject self, jlong contextPtr, |
| 384 | jlong cls, |
| 385 | jstring name, |
| 386 | jobject field, |
| 387 | jlong fieldType, |
| 388 | jint modifiers) |
| 389 | { |
| 390 | JPJavaFrame frame = JPJavaFrame::external(env); |
| 391 | JP_JAVA_TRY("JPTypeFactory_defineField"); |
| 392 | string cname = frame.toStringUTF8(name); |
| 393 | JP_TRACE("class", cls); |
| 394 | JP_TRACE(cname); |
| 395 | jfieldID fid = frame.FromReflectedField(field); |
| 396 | return (jlong) (new JPField( |
| 397 | frame, |
| 398 | (JPClass*) cls, |
| 399 | cname, |
| 400 | field, fid, |
| 401 | (JPClass*) fieldType, |
| 402 | modifiers)); |
| 403 | JP_JAVA_CATCH(0); // GCOVR_EXCL_LINE |
| 404 | } |
| 405 | |
| 406 | JNIEXPORT jlong JNICALL Java_org_jpype_manager_TypeFactoryNative_defineMethod( |
| 407 | JNIEnv *env, jobject self, jlong contextPtr, |
| 408 | jlong cls, jstring name, |
| 409 | jobject method, |
| 410 | jlongArray overloadList, jint modifiers) |
| 411 | { |
| 412 | JPJavaFrame frame = JPJavaFrame::external(env); |
| 413 | JP_JAVA_TRY("JPTypeFactory_defineMethod"); |
| 414 | jmethodID mid = frame.FromReflectedMethod(method); |
| 415 | JPMethodList cover; |
| 416 | convert(frame, overloadList, cover); |
| 417 | string cname = frame.toStringUTF8(name); |
| 418 | JP_TRACE(cname); |
| 419 | return (jlong) (new JPMethod( |
| 420 | frame, |
| 421 | (JPClass*) cls, |
| 422 | cname, |
| 423 | method, mid, |
| 424 | cover, |
| 425 | modifiers)); |
| 426 | JP_JAVA_CATCH(0); |
| 427 | } |
| 428 | |
| 429 | JNIEXPORT void JNICALL Java_org_jpype_manager_TypeFactoryNative_populateMethod( |
| 430 | JNIEnv *env, jobject self, jlong contextPtr, |
| 431 | jlong method, |
| 432 | jlong returnType, |
| 433 | jlongArray argumentTypes |
| 434 | ) |
| 435 | { |
| 436 | JPJavaFrame frame = JPJavaFrame::external(env); |
| 437 | JP_JAVA_TRY("JPTypeFactory_populateMethod"); |
| 438 | JPClassList cargs; |
| 439 | convert(frame, argumentTypes, cargs); |
| 440 | auto *methodPtr = (JPMethod*) method; |
| 441 | methodPtr->setParameters((JPClass*) returnType, std::move(cargs)); |
| 442 | JP_JAVA_CATCH(); // GCOVR_EXCL_LINE |
| 443 | } |
| 444 | |
| 445 | } // extern "C" |
| 446 |