Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access to a Java static inner class with Clojure

I'm trying access to a static inner class method, but I can't find the right way.

I need to write this java code in Clojure:

SessionProperties sessionProperties = SessionProperties.Builder().mediaMode(MediaMode.ROUTED).build();

My code is:

(:import [com.opentok OpenTok MediaMode SessionProperties SessionProperties$Builder]))

(def sessionProperties (.build (.mediaMode SessionProperties$Builder MediaMode/ROUTED))

And this is the error:

java.lang.IllegalArgumentException: No matching method found: mediaMode for class java.lang.Class

I'm using the opentok Java library and I don't understand how to access to mediaMode method.

like image 912
Germán Goldenstein Avatar asked May 07 '15 13:05

Germán Goldenstein


People also ask

How do you access a static inner class?

And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class: it can use them only through an object reference. They are accessed using the enclosing class name. To instantiate an inner class, you must first instantiate the outer class.

Can Java inner class be static?

Inner Classes As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.

How do I access inner private class?

Accessing the Private Members Write an inner class in it, return the private members from a method within the inner class, say, getValue(), and finally from another class (from which you want to access the private members) call the getValue() method of the inner class.

Is clojure interoperable with Java?

Clojure solved this new language library problem by running on the JVM and having interoperability with Java classes. When you use Clojure, you can use Java classes and Java libraries. Clojure builds on the strength of the production-hardened and tested JVM and existing Java libraries.


1 Answers

Your Java code does not work. To fix the remedy, add the new keyword between = and SessionProperties.Builder(). It should be:

SessionProperties sessionProperties = new SessionProperties.Builder()
  .mediaMode(MediaMode.ROUTED)
  .build();

You can do this in Clojure as follows.

user> (import '(com.opentok SessionProperties$Builder MediaMode))
com.opentok.MediaMode

user> (def session-properties (.. (SessionProperties$Builder.)
                                  (mediaMode MediaMode/ROUTED)
                                  build))
#'user/session-properties

user> session-properties
#<SessionProperties com.opentok.SessionProperties@54fc58ee>
like image 165
tnoda Avatar answered Sep 23 '22 00:09

tnoda