Bug: `Class`.forName doesn't work

51 views
Skip to first unread message

Wei Liu

unread,
Jul 19, 2012, 7:28:35 PM7/19/12
to loop...@googlegroups.com
Loop is beautiful language, easy to learn and powerful at concurrency.

Thanks Dhanji,

When I played with loop, I found the following issue, please take a look.

>> `Class`.forName("java.lang.String").newInstance()
`Class`.forName("java.lang.String").newInstance()
Exception in thread "main" java.lang.VerifyError: (class: _shell, method: main signature: ()Ljava/lang/Object;) Incompatible argument to function
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2427)
    at java.lang.Class.getDeclaredMethod(Class.java:1935)
    at loop.Executable.main(Executable.java:230)
    at loop.Loop.safeEval(Loop.java:63)
    at loop.LoopShell.evalInFunction(LoopShell.java:275)
    at loop.LoopShell.shell(LoopShell.java:190)
    at loop.Loop.main(Loop.java:20)

Dhanji R. Prasanna

unread,
Jul 20, 2012, 1:37:15 AM7/20/12
to loop...@googlegroups.com
Thank you so much for the kind words!

You can achieve the same result by doing:

`java.lang.String`.newInstance()

This actually performs the Class.forName() for you underneath (and at compile time). Can you plz file a bug for this particular report, however? I will then chase it up.

Wei Liu

unread,
Jul 20, 2012, 2:19:38 AM7/20/12
to loop...@googlegroups.com
post an issue on github.

Look forward to your fix.

Wei Liu

unread,
Jul 30, 2012, 10:48:59 PM7/30/12
to loop...@googlegroups.com
I have found the root cause and issued a pull request on github.

Please take a look.

Cheers,
Leo

Dhanji R. Prasanna

unread,
Jul 31, 2012, 6:17:39 PM7/31/12
to loop...@googlegroups.com
Hi, the correct statement is:

`java.lang.String`.class.newInstance()

The chained Class.forName() is actually generating the wrong bytecode so your bugfix in Caller.java won't work I'm afraid =( I.e. it is not valid bytecode, so the JVM won't even load the loop program. I will have a closer look and try to fix it, in the meantime plz try my example above.

Dhanji.

Dhanji R. Prasanna

unread,
Jul 31, 2012, 6:35:24 PM7/31/12
to loop...@googlegroups.com
OK I have fixed this problem, it was actually a parser-problem, Loop was incorrectly treating the entire call chain as static instead of just the first method.

Wei Liu

unread,
Aug 1, 2012, 12:44:32 AM8/1/12
to loop...@googlegroups.com
The problem is still not fix completely, I tried the following scripts:

>> `java.util.GregorianCalendar`.class.newInstance().getClass()
java.util.GregorianCalendar
>> `java.util.GregorianCalendar`.class.newInstance().getTime()
java.util.GregorianCalendar
>> `java.util.GregorianCalendar`.getClass().newInstance()  # this returns a String

>> `java.util.GregorianCalendar`.getClass().newInstance().getTime()
java.lang.RuntimeException: Method not found: java.lang.String#getTime([])
    at _shell.main(Unknown Source)

Dhanji R. Prasanna

unread,
Aug 2, 2012, 12:17:37 AM8/2/12
to loop...@googlegroups.com
Ahh I see what you're trying to do. Ok this has been fixed now, I've changed the API a bit:

`java.util.GregorianCalendar::class`.newInstance().getTime()

// you need to use the special form ::class to resolve the java literal into a class.

Plz let me know if this works.
Reply all
Reply to author
Forward
0 new messages