How to call a Java method that expects a ".class" parameter.

447 views
Skip to first unread message

mmwaikar

unread,
Jan 6, 2013, 12:00:22 AM1/6/13
to clo...@googlegroups.com
Hi,

I can't figure out a way to call the below method using Clojure - Files.readAttributes(file, BasicFileAttributes.class)
because I don't know what to pass for the second parameter BasicFileAttributes.class.

Also when I am calling the other method of the same class - (Files/getAttribute (.toPath song) "title" LinkOption/NOFOLLOW_LINKS)
I get the below exception -

java.nio.file.LinkOption cannot be cast to [Ljava.nio.file.LinkOption;
  [Thrown class java.lang.ClassCastException]

Can you please point out what I am doing wrong? Thanks in advance.

Regards,
Manoj.

Softaddicts

unread,
Jan 6, 2013, 12:33:57 AM1/6/13
to clo...@googlegroups.com
BasicFileAttributes is an interface. You need to pass a class that implements
this interface. I am not yet familiar with java 7, just the way the javadoc is
organized gives me a headache but I am trying to find a decent answer.

As for the error, the getAttributes expects a variable length argument for the options.
which from Clojure can be created as an array of type LinkOption.

Look at into-array, you will be able to create it using:

(into-array java.nio.file.LinkOption [ java.nio.file.LinkOption/NOFOLLOW])

of the top of my head, Clojure does not run on my iPad yet :)

Luc
> --
> 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
--
Softaddicts<lprefo...@softaddicts.ca> sent by ibisMail from my ipad!

Ambrose Bonnaire-Sergeant

unread,
Jan 6, 2013, 12:37:18 AM1/6/13
to clo...@googlegroups.com
Variable arguments in Java are just a sugar over passing an array of arguments.

The final argument should be something like (into-array LinkOption [LinkOption/NOFOLLOW_LINKS]).
(untested)

Thanks,
Ambrose


--

Softaddicts

unread,
Jan 6, 2013, 12:44:35 AM1/6/13
to clo...@googlegroups.com
Went through the doc, you can pass the interface as is, it qualified the object
returned, no need for a class implementing the interface here.

So

(Files/readAttributes file java.nio.file.attribute.BasicFileAttributes)

would do it. Of course you can shorten the above by importing the
interface as you did for the Files class.

Luc P.
> --
> 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
Reply all
Reply to author
Forward
0 new messages