D. Ataro D. Ataro - 2 months ago 9
Java Question

Importing Java class in Java class from Clojure



How does one go about importing a Java class located inside another Java class from Clojure?

The class I am attempting to import is the Pixmap.Format located in the LibGDX library. As play-clj handles the integration with LibGDX for Clojure, there is no problem whatsoever actually importing classes from LibGDX itself. The problem lies in importing the Format class located inside the Pixmap class.

I have tried a couple of different things:

; This I have tried
(ns pfft.test
(:import com.badlogic.gdx.graphics Pixmap.Format))

; And this
(ns pfft.test
(:import com.badlogic.gdx.graphics Pixmap/Format))

; This
(ns pfft.test
(:import com.badlogic.gdx.graphics.Pixmap.Format))


I have also attempted calling the class like this:

(Pixmap/Format/RGBA8888)


But Clojure tells me there is no such field, which there clearly is.

This too, does not work:

((Pixmap/Format)/RGBA8888)


As the...

/RGBA8888


... is not recognized at all.

Answer

The separator you're looking for is $, so you can import the nested class like this:

(import com.badlogic.gdx.graphics.Pixmap$Format)

Or like this:

(import (com.badlogic.gdx.graphics Pixmap$Format))

Then you can access static fields of Pixmap$Format like this:

Pixmap$Format/RGBA8888

See this Stack Overflow question and this part of the Clojure docs for more information.

Comments