Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable automatic type conversion in order to continuously calling java method? #373

Closed
passerbythesun opened this issue Nov 6, 2018 · 3 comments
Assignees
Labels
bug Unable to deliver desired behavior (crash, fail, untested)

Comments

@passerbythesun
Copy link

passerbythesun commented Nov 6, 2018

I would like to do some string processing in a continuous way, e.g., java.lang.String(" some string").trim().replaceAll("regex", "replacement"), so that re module is not needed and code consistency with java side is better.

But I find that trim() returns unicode type, which result in an AttributeError: 'unicode' object has no attribute 'replaceAll' error.

To my understanding, Javatrim() returns java.lang.String type, so this should be the effort of automatic type conversion of java string to python unicode. Is it right?

If so, how to disable automatic type conversion in this situation? Or if not, is there another way to do so?

@Thrameos Thrameos self-assigned this Nov 6, 2018
@Thrameos
Copy link
Contributor

Thrameos commented Nov 6, 2018

There appears to be a bug in version 0.6.2. There is supposed to be an option that allows you to select if you want conversion or not. However, no mater which way I tried the switch it gives a buggy behavior.

import jpype
import jpype.imports
import _jpype

jpype.startJVM(jpype.getDefaultJVMPath())
_jpype.setConvertStringObjects(True)  # True gives unicode, False gives JString

from java.lang import String
f=String('foobar').trim().replaceAll('bar','foo')
print(f)

Unfortunately JString is only useful to pass to a java method and doesn't have any String methods exposed.

Thus the best I can recommend is either use another string cast to put the wrapper back as a string
f=String(String('foobar').trim()).replaceAll('bar','foo') or use the devel branch which we will release as 0.7.0 shortly.

@Thrameos Thrameos added the bug Unable to deliver desired behavior (crash, fail, untested) label Nov 6, 2018
@passerbythesun
Copy link
Author

@Thrameos Thanks for answering. I just used the way you recommend. Waiting for 0.7.0~

@Thrameos
Copy link
Contributor

Fixed and tested in 0.7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Unable to deliver desired behavior (crash, fail, untested)
Projects
None yet
Development

No branches or pull requests

2 participants