|
| 1 | +/***************************************************************************** |
| 2 | +Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 3 | +use this file except in compliance with the License. You may obtain a copy |
| 4 | +of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 5 | +Unless required by applicable law or agreed to in writing, software distributed |
| 6 | +under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 7 | +CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 8 | +specific language governing permissions and limitations under the License. |
| 9 | +See NOTICE file for details. |
| 10 | +*****************************************************************************/ |
| 11 | + |
| 12 | +#include "jpype.h" |
| 13 | +#include "jp_classloader.h" |
| 14 | + |
| 15 | +/** |
| 16 | + * Constructor for JPTypeManager. |
| 17 | + * Initializes the JPTypeManager by loading the necessary Java methods |
| 18 | + * from the TypeManager class in the JVM. |
| 19 | + * |
| 20 | + * @param frame - A reference to the current JPJavaFrame. |
| 21 | + */ |
| 22 | +JPTypeManager::JPTypeManager(JPJavaFrame& frame) |
| 23 | +{ |
| 24 | + JP_TRACE_IN("JPTypeManager::init"); |
| 25 | + jclass cls = frame.getContext()->getClassLoader()->findClass(frame, "org.jpype.manager.TypeManager"); |
| 26 | + m_FindClass = frame.GetMethodID(cls, "findClass", "(Ljava/lang/Class;)J"); |
| 27 | + m_FindClassByName = frame.GetMethodID(cls, "findClassByName", "(Ljava/lang/String;)J"); |
| 28 | + m_FindClassForObject = frame.GetMethodID(cls, "findClassForObject", "(Ljava/lang/Object;)J"); |
| 29 | + m_PopulateMethod = frame.GetMethodID(cls, "populateMethod", "(JLjava/lang/reflect/Executable;)V"); |
| 30 | + m_PopulateMembers = frame.GetMethodID(cls, "populateMembers", "(Ljava/lang/Class;)V"); |
| 31 | + m_InterfaceParameterCount = frame.GetMethodID(cls, "interfaceParameterCount", "(Ljava/lang/Class;)I"); |
| 32 | + // The object instance will be loaded later |
| 33 | + JP_TRACE_OUT; |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Finds a JPClass object for a given Java class object. |
| 38 | + * |
| 39 | + * @param obj - The Java class object to find. |
| 40 | + * @return A pointer to the JPClass object corresponding to the Java class. |
| 41 | + */ |
| 42 | +JPClass* JPTypeManager::findClass(jclass obj) |
| 43 | +{ |
| 44 | + JP_TRACE_IN("JPTypeManager::findClass"); |
| 45 | + JPJavaFrame frame = JPJavaFrame::outer(); |
| 46 | + jvalue val; |
| 47 | + val.l = obj; |
| 48 | + return (JPClass*) (frame.CallLongMethodA(m_JavaTypeManager.get(), m_FindClass, &val)); |
| 49 | + JP_TRACE_OUT; |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Finds a JPClass object by its name. |
| 54 | + * |
| 55 | + * @param name - The name of the Java class to find. |
| 56 | + * @return A pointer to the JPClass object corresponding to the class name. |
| 57 | + * @throws PyExc_TypeError if the class cannot be found. |
| 58 | + */ |
| 59 | +JPClass* JPTypeManager::findClassByName(const string& name) |
| 60 | +{ |
| 61 | + JP_TRACE_IN("JPTypeManager::findClassByName"); |
| 62 | + JPJavaFrame frame = JPJavaFrame::outer(); |
| 63 | + jvalue val; |
| 64 | + val.l = (jobject) frame.fromStringUTF8(name); |
| 65 | + auto* out = (JPClass*) (frame.CallLongMethodA(m_JavaTypeManager.get(), m_FindClassByName, &val)); |
| 66 | + if (out == nullptr) |
| 67 | + { |
| 68 | + std::stringstream err; |
| 69 | + err << "Class " << name << " is not found"; |
| 70 | + JP_RAISE(PyExc_TypeError, err.str()); |
| 71 | + } |
| 72 | + return out; |
| 73 | + JP_TRACE_OUT; |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Finds a JPClass object for a given Java object instance. |
| 78 | + * |
| 79 | + * @param obj - The Java object instance to find the class for. |
| 80 | + * @return A pointer to the JPClass object corresponding to the object's class. |
| 81 | + */ |
| 82 | +JPClass* JPTypeManager::findClassForObject(jobject obj) |
| 83 | +{ |
| 84 | + JP_TRACE_IN("JPTypeManager::findClassForObject"); |
| 85 | + JPJavaFrame frame = JPJavaFrame::outer(); |
| 86 | + jvalue val; |
| 87 | + val.l = obj; |
| 88 | + auto *cls = (JPClass*) (frame.CallLongMethodA(m_JavaTypeManager.get(), m_FindClassForObject, &val)); |
| 89 | + frame.check(); |
| 90 | + JP_TRACE("ClassName", cls == NULL ? "null" : cls->getCanonicalName()); |
| 91 | + return cls; |
| 92 | + JP_TRACE_OUT; |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Populates a method with details from a Java executable object. |
| 97 | + * |
| 98 | + * @param method - Pointer to the method to populate. |
| 99 | + * @param obj - The Java executable object to populate the method from. |
| 100 | + */ |
| 101 | +void JPTypeManager::populateMethod(void* method, jobject obj) |
| 102 | +{ |
| 103 | + JP_TRACE_IN("JPTypeManager::populateMethod"); |
| 104 | + JPJavaFrame frame = JPJavaFrame::outer(); |
| 105 | + jvalue val[2]; |
| 106 | + val[0].j = (jlong) method; |
| 107 | + val[1].l = obj; |
| 108 | + JP_TRACE("Method", method); |
| 109 | + frame.CallVoidMethodA(m_JavaTypeManager.get(), m_PopulateMethod, val); |
| 110 | + JP_TRACE_OUT; |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * Populates the members of a JPClass object. |
| 115 | + * |
| 116 | + * @param cls - The JPClass object to populate members for. |
| 117 | + */ |
| 118 | +void JPTypeManager::populateMembers(JPClass* cls) |
| 119 | +{ |
| 120 | + JP_TRACE_IN("JPTypeManager::populateMembers"); |
| 121 | + JPJavaFrame frame = JPJavaFrame::outer(); |
| 122 | + jvalue val[1]; |
| 123 | + val[0].l = (jobject) cls->getJavaClass(); |
| 124 | + frame.CallVoidMethodA(m_JavaTypeManager.get(), m_PopulateMembers, val); |
| 125 | + JP_TRACE_OUT; |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * Counts the number of parameters for a given class interface. |
| 130 | + * |
| 131 | + * @param cls - The JPClass object representing the interface. |
| 132 | + * @return The number of parameters for the interface. |
| 133 | + */ |
| 134 | +int JPTypeManager::interfaceParameterCount(JPClass *cls) |
| 135 | +{ |
| 136 | + JP_TRACE_IN("JPTypeManager::interfaceParameterCount"); |
| 137 | + JPJavaFrame frame = JPJavaFrame::outer(); |
| 138 | + jvalue val[1]; |
| 139 | + val[0].l = (jobject) cls->getJavaClass(); |
| 140 | + return frame.CallIntMethodA(m_JavaTypeManager.get(), m_InterfaceParameterCount, val); |
| 141 | + JP_TRACE_OUT; |
| 142 | +} |
0 commit comments