[Swift-devel] Problems calling java.lang.String format method

Mihael Hategan hategan at mcs.anl.gov
Fri Oct 24 03:32:09 CDT 2014


I changed java() to accept arrays, be able to wrap primitives, and
invoke methods with varargs, either as arrays or not.

Both of the following should now work:

string r1 = java("java.lang.String", "format", "%4d", [9876]);
string r2 = java("java.lang.String", "format", "%4d", 9876);

This wasn't easy. There are many rules that the java compiler follows to
find the right method to invoke, and Class.getMethod(...) does very
little of that. For example, f(1) could match either f(int), or
f(Integer) or f(Object) or f(Object...) (which really is just
f(Object[])). The more parameters, the more combinations possible.

I'm not entirely sure about the end product, so it will have to get some
testing.

But we should really fix sprintf().

Mihael

On Thu, 2014-10-23 at 18:45 -0500, Michael Wilde wrote:
> Im trying to call .format( ) from java.lang.String, from the Swift 
> function java( ).
> 
> Is it possible to call Java functions whose args are typed Object? (such 
> as .format)
> 
>  From this code:
> 
> // String result = String.format("%4d", i * j);
> 
> string result = java("java.lang.String","format","%4d",9876);
> tracef("result=%s\n", result);
> 
> I get the error below, which suggests that the java( ) function can only 
> find a fairly narrow range of methods whose signatures exactly match the 
> signature that the Swift args are directly mapped to.
> 
> T$ swift s.swift
> Swift trunk git-rev: 870bd2114e6bdcab18278d44208defbf7c077fdc 
> heads/master 6143
> RunID: run013
> Progress: Thu, 23 Oct 2014 19:35:52-0400
> java.lang.NoSuchMethodException: 
> java.lang.String.format(java.lang.String, int)
>      at java.lang.Class.getMethod(Class.java:1665)
>      at org.griphyn.vdl.karajan.lib.swiftscript.Java.getMethod(Java.java:88)
>      at org.griphyn.vdl.karajan.lib.swiftscript.Java.function(Java.java:56)
>      at 
> org.griphyn.vdl.karajan.lib.SwiftFunction.runBody(SwiftFunction.java:77)
>      at 
> org.globus.cog.karajan.compiled.nodes.InternalFunction.run(InternalFunction.java:175)
>      at 
> org.globus.cog.karajan.compiled.nodes.CompoundNode.runChild(CompoundNode.java:110)
>      at 
> org.globus.cog.karajan.compiled.nodes.InternalFunction.run(InternalFunction.java:165)
>      at 
> org.globus.cog.karajan.compiled.nodes.CompoundNode.runChild(CompoundNode.java:110)
>      at 
> org.globus.cog.karajan.compiled.nodes.Sequential.run(Sequential.java:41)
>      at 
> org.globus.cog.karajan.compiled.nodes.CompoundNode.runChild(CompoundNode.java:110)
>      at 
> org.globus.cog.karajan.compiled.nodes.UParallel$1.run(UParallel.java:91)
>      at k.thr.LWThread.run(LWThread.java:247)
>      at 
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
>      at 
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
>      at java.lang.Thread.run(Thread.java:744)
> dir=/lustre/atlas2/mat049/proj-shared/wildemj/bes-diffuse-demo-2014/swift-discus/swiftk
> 
> Execution failed:
> @java(): Error attempting to use: java.lang.String
>      swiftscript:java @ s, line: 51
> 
> Or better yet: is there already an undocumented sprintf( ) function 
> (which is what Im trying to do here).
> 
> Thanks,
> 
> - Mike
> 
> 
> 





More information about the Swift-devel mailing list