jpype-project / jpype

JPype is cross language bridge to allow Python programs full access to Java class libraries.
http://www.jpype.org
Apache License 2.0
1.12k stars 183 forks source link

JArray of JClass #1190

Closed marlow-fawn closed 5 months ago

marlow-fawn commented 5 months ago

Trying to create an array of classes, getting the following errors:

Traceback (most recent call last):

  File "test.py", line 17, in callTrade
    classesTest = JArray(jpype.types.JClass)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mfawn/miniconda3/envs/rl/lib/python3.12/site-packages/jpype/_jarray.py", line 89, in __new__
    return _jpype._newArrayType(jc, dims)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Java class required
Traceback (most recent call last):
  File "test.py", line 16, in callTrade
    classesTest = jpype.types.JClass[5]
                  ~~~~~~~~~~~~~~~~~~^^^
TypeError: type 'JClass' is not subscriptable

JClass does seem to be in types.py, and _toJavaClass seems to properly return JClass. Is there something improper in how I'm forming this array?

Thrameos commented 5 months ago

I believe this is an edge case. JClass is a front end for the class accessor but is not actually a Java class itself as it must exist before the JVM is started. I believe what you want is actually something more like.

classArray = JArray(java.lang.Class)

or

classArray = java.lang.Class[:]

In other words as the error message is indicating JArray expects an actually Java class. I tested a few different combinations. It does appear that JString, JObject, and the primitive types work as they have special logic to look up the required type. It may be possible to add the same logic to JClass, though I am not sure it is worth the effort as arrays of classes is a fairly rare case.

marlow-fawn commented 5 months ago

Using java.lang.Class is what I wanted and fixes the issue, thank you!