Skip to content

Commit e78aad0

Browse files
committed
Work on internal documentation
1 parent f3419c2 commit e78aad0

File tree

3 files changed

+223
-1
lines changed

3 files changed

+223
-1
lines changed

native/common/jp_exception.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,18 @@ void JPypeException::toJava()
421421
JPTracer::trace("Fatal error in exception handling");
422422
JPStackInfo info = ex.m_Trace.front();
423423
JPTracer::trace(info.getFile(), info.getFunction(), info.getLine());
424+
JPTracer::trace("Exception type: ", m_Type);
425+
JPTracer::trace("Exception message: ", what());
424426

425427
// Take one for the team.
428+
429+
// This deliberate crash is used exclusively during startup in catastrophic scenarios
430+
// where resources are unavailable, and all other exception handling mechanisms fail.
431+
// Its purpose is to produce a gdb backtrace for debugging.
432+
// DO NOT REMOVE OR MODIFY WITHOUT UNDERSTANDING THE IMPLICATIONS.
433+
434+
// This deliberate crash is used to produce a gdb backtrace in catastrophic scenarios.
435+
// Refer to the developer guide section "Deliberate Crash for Debugging" for details.
426436
int *i = nullptr;
427437
*i = 0;
428438
// GCOVR_EXCL_STOP
@@ -431,8 +441,18 @@ void JPypeException::toJava()
431441
// GCOVR_EXCL_START
432442
// urp?!
433443
JPTracer::trace("Fatal error in exception handling");
444+
JPTracer::trace("Exception type: ", m_Type);
445+
JPTracer::trace("Exception message: ", what());
434446

435447
// It is pointless, I can't go on.
448+
449+
// This deliberate crash is used exclusively during startup in catastrophic scenarios
450+
// where resources are unavailable, and all other exception handling mechanisms fail.
451+
// Its purpose is to produce a gdb backtrace for debugging.
452+
// DO NOT REMOVE OR MODIFY WITHOUT UNDERSTANDING THE IMPLICATIONS.
453+
454+
// This deliberate crash is used to produce a gdb backtrace in catastrophic scenarios.
455+
// Refer to the developer guide section "Deliberate Crash for Debugging" for details.
436456
int *i = nullptr;
437457
*i = 0;
438458
// GCOVR_EXCL_STOP

native/common/jp_typemanager.cpp

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
#include "jpype.h"
1717
#include "jp_classloader.h"
1818

19+
/**
20+
* Constructor for JPTypeManager.
21+
* Initializes the JPTypeManager by loading the necessary Java methods
22+
* from the TypeManager class in the JVM.
23+
*
24+
* @param frame - A reference to the current JPJavaFrame.
25+
*/
1926
JPTypeManager::JPTypeManager(JPJavaFrame& frame)
2027
{
2128
JP_TRACE_IN("JPTypeManager::init");
@@ -31,20 +38,40 @@ JPTypeManager::JPTypeManager(JPJavaFrame& frame)
3138
JP_TRACE_OUT;
3239
}
3340

41+
/**
42+
* Finds a JPClass object for a given Java class object.
43+
*
44+
* @param obj - The Java class object to find.
45+
* @return A pointer to the JPClass object corresponding to the Java class.
46+
*/
3447
JPClass* JPTypeManager::findClass(jclass obj)
3548
{
3649
JP_TRACE_IN("JPTypeManager::findClass");
3750
JPJavaFrame frame = JPJavaFrame::outer();
51+
if (obj == nullptr)
52+
{
53+
JP_RAISE(PyExc_RuntimeError, "Null class object passed to findClass");
54+
}
3855
jvalue val;
3956
val.l = obj;
4057
return (JPClass*) (frame.CallLongMethodA(m_JavaTypeManager.get(), m_FindClass, &val));
4158
JP_TRACE_OUT;
4259
}
4360

61+
/**
62+
* Finds a JPClass object by its name.
63+
*
64+
* @param name - The name of the Java class to find.
65+
* @return A pointer to the JPClass object corresponding to the class name.
66+
* @throws PyExc_TypeError if the class cannot be found.
67+
*/
4468
JPClass* JPTypeManager::findClassByName(const string& name)
4569
{
4670
JP_TRACE_IN("JPTypeManager::findClassByName");
4771
JPJavaFrame frame = JPJavaFrame::outer();
72+
if (name.empty())
73+
JP_RAISE(PyExc_ValueError, "Empty class name passed to findClassByName");
74+
4875
jvalue val;
4976
val.l = (jobject) frame.fromStringUTF8(name);
5077
auto* out = (JPClass*) (frame.CallLongMethodA(m_JavaTypeManager.get(), m_FindClassByName, &val));
@@ -58,23 +85,41 @@ JPClass* JPTypeManager::findClassByName(const string& name)
5885
JP_TRACE_OUT;
5986
}
6087

88+
/**
89+
* Finds a JPClass object for a given Java object instance.
90+
*
91+
* @param obj - The Java object instance to find the class for.
92+
* @return A pointer to the JPClass object corresponding to the object's class.
93+
*/
6194
JPClass* JPTypeManager::findClassForObject(jobject obj)
6295
{
6396
JP_TRACE_IN("JPTypeManager::findClassForObject");
6497
JPJavaFrame frame = JPJavaFrame::outer();
98+
if (obj == nullptr)
99+
JP_RAISE(PyExc_RuntimeError, "Null object passed to findClassForObject");
65100
jvalue val;
66101
val.l = obj;
67102
auto *cls = (JPClass*) (frame.CallLongMethodA(m_JavaTypeManager.get(), m_FindClassForObject, &val));
68103
frame.check();
69-
JP_TRACE("ClassName", cls == NULL ? "null" : cls->getCanonicalName());
104+
JP_TRACE("ClassName", cls == nullptr ? "null" : cls->getCanonicalName());
70105
return cls;
71106
JP_TRACE_OUT;
72107
}
73108

109+
/**
110+
* Populates a method with details from a Java executable object.
111+
*
112+
* @param method - Pointer to the method to populate.
113+
* @param obj - The Java executable object to populate the method from.
114+
*/
74115
void JPTypeManager::populateMethod(void* method, jobject obj)
75116
{
76117
JP_TRACE_IN("JPTypeManager::populateMethod");
77118
JPJavaFrame frame = JPJavaFrame::outer();
119+
if (method == nullptr)
120+
JP_RAISE(PyExc_RuntimeError, "Null method pointer passed to populateMethod");
121+
if (obj == nullptr)
122+
JP_RAISE(PyExc_RuntimeError, "Null Java object passed to populateMethod");
78123
jvalue val[2];
79124
val[0].j = (jlong) method;
80125
val[1].l = obj;
@@ -83,20 +128,35 @@ void JPTypeManager::populateMethod(void* method, jobject obj)
83128
JP_TRACE_OUT;
84129
}
85130

131+
/**
132+
* Populates the members of a JPClass object.
133+
*
134+
* @param cls - The JPClass object to populate members for.
135+
*/
86136
void JPTypeManager::populateMembers(JPClass* cls)
87137
{
88138
JP_TRACE_IN("JPTypeManager::populateMembers");
89139
JPJavaFrame frame = JPJavaFrame::outer();
140+
if (cls == nullptr)
141+
JP_RAISE(PyExc_RuntimeError, "Null JPClass object passed to populateMembers");
90142
jvalue val[1];
91143
val[0].l = (jobject) cls->getJavaClass();
92144
frame.CallVoidMethodA(m_JavaTypeManager.get(), m_PopulateMembers, val);
93145
JP_TRACE_OUT;
94146
}
95147

148+
/**
149+
* Counts the number of parameters for a given class interface.
150+
*
151+
* @param cls - The JPClass object representing the interface.
152+
* @return The number of parameters for the interface.
153+
*/
96154
int JPTypeManager::interfaceParameterCount(JPClass *cls)
97155
{
98156
JP_TRACE_IN("JPTypeManager::interfaceParameterCount");
99157
JPJavaFrame frame = JPJavaFrame::outer();
158+
if (cls == nullptr)
159+
JP_RAISE(PyExc_RuntimeError, "Null JPClass object passed to interfaceParameterCount");
100160
jvalue val[1];
101161
val[0].l = (jobject) cls->getJavaClass();
102162
return frame.CallIntMethodA(m_JavaTypeManager.get(), m_InterfaceParameterCount, val);

native/common/jp_typemanager2.cpp

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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

Comments
 (0)