| 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 "jp_classloader.h" |
| 18 | |
| 19 | JPTypeManager::JPTypeManager(JPJavaFrame& frame) |
| 20 | { |
| 21 | JP_TRACE_IN("JPTypeManager::init"); |
| 22 | jclass cls = frame.getContext()->getClassLoader()->findClass(frame, "org.jpype.manager.TypeManager"); |
| 23 | m_FindClass = frame.GetMethodID(cls, "findClass", "(Ljava/lang/Class;)J"); |
| 24 | m_FindClassByName = frame.GetMethodID(cls, "findClassByName", "(Ljava/lang/String;)J"); |
| 25 | m_FindClassForObject = frame.GetMethodID(cls, "findClassForObject", "(Ljava/lang/Object;)J"); |
| 26 | m_PopulateMethod = frame.GetMethodID(cls, "populateMethod", "(JLjava/lang/reflect/Executable;)V"); |
| 27 | m_PopulateMembers = frame.GetMethodID(cls, "populateMembers", "(Ljava/lang/Class;)V"); |
| 28 | m_InterfaceParameterCount = frame.GetMethodID(cls, "interfaceParameterCount", "(Ljava/lang/Class;)I"); |
| 29 | |
| 30 | // The object instance will be loaded later |
| 31 | JP_TRACE_OUT; |
| 32 | } |
| 33 | |
| 34 | JPClass* JPTypeManager::findClass(jclass obj) |
| 35 | { |
| 36 | JP_TRACE_IN("JPTypeManager::findClass"); |
| 37 | JPJavaFrame frame = JPJavaFrame::outer(); |
| 38 | jvalue val; |
| 39 | val.l = obj; |
| 40 | return (JPClass*) (frame.CallLongMethodA(m_JavaTypeManager.get(), m_FindClass, &val)); |
| 41 | JP_TRACE_OUT; |
| 42 | } |
| 43 | |
| 44 | JPClass* JPTypeManager::findClassByName(const string& name) |
| 45 | { |
| 46 | JP_TRACE_IN("JPTypeManager::findClassByName"); |
| 47 | JPJavaFrame frame = JPJavaFrame::outer(); |
| 48 | jvalue val; |
| 49 | val.l = (jobject) frame.fromStringUTF8(name); |
| 50 | auto* out = (JPClass*) (frame.CallLongMethodA(m_JavaTypeManager.get(), m_FindClassByName, &val)); |
| 51 | if (out == nullptr) |
| 52 | { |
| 53 | std::stringstream err; |
| 54 | err << "Class " << name << " is not found"; |
| 55 | JP_RAISE(PyExc_TypeError, err.str()); |
| 56 | } |
| 57 | return out; |
| 58 | JP_TRACE_OUT; |
| 59 | } |
| 60 | |
| 61 | JPClass* JPTypeManager::findClassForObject(jobject obj) |
| 62 | { |
| 63 | JP_TRACE_IN("JPTypeManager::findClassForObject"); |
| 64 | JPJavaFrame frame = JPJavaFrame::outer(); |
| 65 | jvalue val; |
| 66 | val.l = obj; |
| 67 | auto *cls = (JPClass*) (frame.CallLongMethodA(m_JavaTypeManager.get(), m_FindClassForObject, &val)); |
| 68 | frame.check(); |
| 69 | JP_TRACE("ClassName", cls == NULL ? "null" : cls->getCanonicalName()); |
| 70 | return cls; |
| 71 | JP_TRACE_OUT; |
| 72 | } |
| 73 | |
| 74 | void JPTypeManager::populateMethod(void* method, jobject obj) |
| 75 | { |
| 76 | JP_TRACE_IN("JPTypeManager::populateMethod"); |
| 77 | JPJavaFrame frame = JPJavaFrame::outer(); |
| 78 | jvalue val[2]; |
| 79 | val[0].j = (jlong) method; |
| 80 | val[1].l = obj; |
| 81 | JP_TRACE("Method", method); |
| 82 | frame.CallVoidMethodA(m_JavaTypeManager.get(), m_PopulateMethod, val); |
| 83 | JP_TRACE_OUT; |
| 84 | } |
| 85 | |
| 86 | void JPTypeManager::populateMembers(JPClass* cls) |
| 87 | { |
| 88 | JP_TRACE_IN("JPTypeManager::populateMembers"); |
| 89 | JPJavaFrame frame = JPJavaFrame::outer(); |
| 90 | jvalue val[1]; |
| 91 | val[0].l = (jobject) cls->getJavaClass(); |
| 92 | frame.CallVoidMethodA(m_JavaTypeManager.get(), m_PopulateMembers, val); |
| 93 | JP_TRACE_OUT; |
| 94 | } |
| 95 | |
| 96 | int JPTypeManager::interfaceParameterCount(JPClass *cls) |
| 97 | { |
| 98 | JP_TRACE_IN("JPTypeManager::interfaceParameterCount"); |
| 99 | JPJavaFrame frame = JPJavaFrame::outer(); |
| 100 | jvalue val[1]; |
| 101 | val[0].l = (jobject) cls->getJavaClass(); |
| 102 | return frame.CallIntMethodA(m_JavaTypeManager.get(), m_InterfaceParameterCount, val); |
| 103 | JP_TRACE_OUT; |
| 104 | } |