Java interop: "Can't call public method of non-public class"

149 views
Skip to first unread message

Ben Smith-Mannschott

unread,
Mar 1, 2013, 4:13:57 AM3/1/13
to clo...@googlegroups.com
Simplified, from a more complex example:

abstract class Bytes {
    public toHexString() { return "..."; }
    Bytes { }
}

public class Hash extends Bytes {
    public Hash() { super(); }
}

This works in Java:

    new Hash().toHexString();

This fails in Clojure:

    (.toHexString (Hash.))

IllegalArgumentException Can't call public method of non-public class: public final java.lang.String at.gv.brz.bjuvj.hashpass.Bytes.toHexString()  clojure.lang.Reflector.invokeMatchingMethod (Reflector.java:88)

Bug?

// ben

Marko Topolnik

unread,
Mar 1, 2013, 4:20:57 AM3/1/13
to clo...@googlegroups.com
I'd say it's a bug. You are invoking a public class's method, which happens to be inherited from a package-private class. Clojure's reflective code accesses the superclass method directly so there's no distinction between direct invocation and invocation through inheritance. 

If you are interseted in a workaround, type-hinting the code will work (my guess).

Vladimir Tsichevski

unread,
Mar 4, 2013, 4:32:21 PM3/4/13
to clo...@googlegroups.com
I think not. But upgrading to clojure 1.5 will do.

Ben Smith-Mannschott

unread,
Mar 5, 2013, 7:22:57 AM3/5/13
to clo...@googlegroups.com
I found the following work-around since I have access to the java code I am calling:

I had the package-visible ABC Bytes implement a (public) interface declaring the toHexString method. This placated the compiler. 
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 
Reply all
Reply to author
Forward
0 new messages