clojure-java-9 icon indicating copy to clipboard operation
clojure-java-9 copied to clipboard

Calling static interface methods does not compile

Open ghadishayban opened this issue 7 years ago • 1 comments

Calls to static interface methods compile on Java 8 but not on Java 9 because of a new bytecode restrictrion.

public interface JDK8InterfaceMethods {
    // cannot call either of these from Clojure
    public static long staticMethod0(long v) { return v; }
    public static String staticMethod1(String s) { return s; }
}

https://dev.clojure.org/jira/browse/CLJ-2284

Interface default methods are unaffected.

ghadishayban avatar Feb 26 '18 01:02 ghadishayban

workaround macro

(defmacro interface-static-call
  [sym & argtypes]
  `(let [m# (.getMethod ~(symbol (namespace sym))
                        ~(name sym)
                        (into-array Class ~argtypes))]
     (fn [& args#]
       (.invoke m# nil (to-array args#)))))

(def client
  (interface-static-call S3Client/create))

(client)

ghadishayban avatar Mar 19 '18 16:03 ghadishayban