| 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 <Python.h> |
| 17 | #include <jpype.h> |
| 18 | #include <pyjp.h> |
| 19 | #include <jp_classloader.h> |
| 20 | |
| 21 | jobject JPClassLoader::getBootLoader() |
| 22 | { |
| 23 | return m_BootLoader.get(); |
| 24 | } |
| 25 | |
| 26 | JPClassLoader::JPClassLoader(JPJavaFrame& frame) |
| 27 | { |
| 28 | JP_TRACE_IN("JPClassLoader::JPClassLoader"); |
| 29 | |
| 30 | // Define the class loader |
| 31 | m_ClassClass = JPClassRef(frame, frame.FindClass("java/lang/Class")); |
| 32 | m_ForNameID = frame.GetStaticMethodID(m_ClassClass.get(), "forName", |
| 33 | "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"); |
| 34 | jclass classLoaderClass = frame.FindClass("java/lang/ClassLoader"); |
| 35 | jmethodID getSystemClassLoader |
| 36 | = frame.GetStaticMethodID(classLoaderClass, "getSystemClassLoader", "()Ljava/lang/ClassLoader;"); |
| 37 | m_SystemClassLoader = JPObjectRef(frame, |
| 38 | frame.CallStaticObjectMethodA(classLoaderClass, getSystemClassLoader, nullptr)); |
| 39 | |
| 40 | jclass dynamicLoaderClass = frame.getEnv()->FindClass("org/jpype/JPypeClassLoader"); |
| 41 | if (dynamicLoaderClass != nullptr) |
| 42 | { |
| 43 | // Use the one in place already |
| 44 | if (frame.IsInstanceOf(m_SystemClassLoader.get(), dynamicLoaderClass)) |
| 45 | { |
| 46 | m_BootLoader = m_SystemClassLoader; |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | // Easy the Dynamic loader is already in the path, so just use it as the bootloader |
| 51 | jmethodID newDyLoader = frame.GetMethodID(dynamicLoaderClass, "<init>", |
| 52 | "(Ljava/lang/ClassLoader;)V"); |
| 53 | jvalue v; |
| 54 | v.l = m_SystemClassLoader.get(); |
| 55 | m_BootLoader = JPObjectRef(frame, frame.NewObjectA(dynamicLoaderClass, newDyLoader, &v)); |
| 56 | return; |
| 57 | } |
| 58 | frame.ExceptionClear(); |
| 59 | |
| 60 | // org.jpype was not loaded already so we can't proceed |
| 61 | JP_RAISE(PyExc_RuntimeError, "Can't find org.jpype.jar support library"); |
| 62 | JP_TRACE_OUT; // GCOVR_EXCL_LINE |
| 63 | } |
| 64 | |
| 65 | jclass JPClassLoader::findClass(JPJavaFrame& frame, const string& name) |
| 66 | { |
| 67 | #ifdef ANDROID |
| 68 | string cname = name; |
| 69 | for (int i = 0; i < cname.size(); ++i) |
| 70 | if (cname[i] == '.') |
| 71 | cname[i] = '/'; |
| 72 | return frame.FindClass(cname); |
| 73 | #else |
| 74 | jvalue v[3]; |
| 75 | v[0].l = frame.NewStringUTF(name.c_str()); |
| 76 | v[1].z = true; |
| 77 | v[2].l = m_BootLoader.get(); |
| 78 | return (jclass) frame.CallStaticObjectMethodA(m_ClassClass.get(), m_ForNameID, v); |
| 79 | #endif |
| 80 | } |