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

Calljava doesn't appear to work with arguments when class accepts an interface #6

Closed
Taywee opened this issue Dec 16, 2016 · 8 comments · Fixed by #7
Closed

Calljava doesn't appear to work with arguments when class accepts an interface #6

Taywee opened this issue Dec 16, 2016 · 8 comments · Fixed by #7

Comments

@Taywee
Copy link
Collaborator

Taywee commented Dec 16, 2016

> put -to @string ${ this is a test }$
> put -to @test test
> calljava -obj @string -method contains -arg @test
Ublu:1:UbluInterpreter.Thread[main,5,main]:SEVERE:ublu.command.CmdCallJava.cmdCallJava():Error looking up method in calljava/0 [-to @datasink] [-new ~@{classname}] [--,-obj ~@object] [-method ~@{methodname}] [-arg ~@argobj [-arg ..]] [-primarg ~@argobj [-primarg ..]] : call Java method
java.lang.String.contains(java.lang.String)
java.lang.NoSuchMethodException: java.lang.String.contains(java.lang.String)
	at java.lang.Class.getDeclaredMethod(Class.java:2130)
	at ublu.util.JavaCallHelper.<init>(JavaCallHelper.java:70)
	at ublu.command.CmdCallJava.cmdCallJava(CmdCallJava.java:118)
	at ublu.command.CmdCallJava.cmd(CmdCallJava.java:173)
	at ublu.util.Interpreter.loop(Interpreter.java:1140)
	at ublu.util.Interpreter.interpret(Interpreter.java:1317)
	at ublu.Ublu.runMainInterpreter(Ublu.java:194)
	at ublu.Ublu.niam(Ublu.java:249)
	at ublu.Ublu.main(Ublu.java:229)
> 

This should work. I've noticed some similar issues using calljava to create and work with HashMaps.

@Taywee
Copy link
Collaborator Author

Taywee commented Dec 16, 2016

https://stackoverflow.com/questions/19313259/java-get-method-with-interface-parameter-with-its-implementation

It seems that this can be resolved by working through permutations of the argument's base classes in the case of a NoSuchMethodException. This can explode exponentially in the performance impact (In the case of an actual missing method when using 3 arguments with 10 base classes each, it will incur 1000 lookups before actually giving up), but normal use shouldn't cause any of these kinds of issues. In any case memoization can curtail most expense in repeated calls.

@Taywee
Copy link
Collaborator Author

Taywee commented Dec 16, 2016

It's also notable that the JavaCallHelper won't work when a method is declared in an interface or superclass, because it uses getDeclaredMethod instead of getMethod.

@jwoehr
Copy link
Owner

jwoehr commented Dec 16, 2016 via email

@Taywee
Copy link
Collaborator Author

Taywee commented Dec 16, 2016

prrimarg doesn't work, because it still manages it as a String, and doesn't find the method that takes a CharSequence, as String.contains requires (CharSequence is an interface that String implements). I can make these changes, I was just opening the issue somewhere so I wouldn't forget.

@jwoehr
Copy link
Owner

jwoehr commented Dec 16, 2016 via email

@jwoehr
Copy link
Owner

jwoehr commented Dec 16, 2016

Anyway, please fix calljava if you are inspired to do so and can do so without breaking existing code. Try to avoid a performance hit. I want to use calljava more and more to create extensions rather than expand the base system.

@Taywee
Copy link
Collaborator Author

Taywee commented Dec 16, 2016

It shouldn't break existing code. It should not cause a performance hit, because the classes will be checked as-is first before checking interfaces and superclasses, so if there is an exact match, it should operate in constant time as before. With memoization, the individual call worst-case is polynomial time, but the amortized time would reduce to constant, but this only really helps for long-running programs and in loops.

Either way, existing working uses should continue to work as expected with no performance impact, and cases that returned a NoSuchMethodException will be fixed with a one-time performance impact per session.

@jwoehr
Copy link
Owner

jwoehr commented Dec 16, 2016 via email

@Taywee Taywee mentioned this issue Dec 16, 2016
@jwoehr jwoehr closed this as completed in #7 Dec 16, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants