How to access the .class of a java class in clojure

Viewed 73

I want to take use of the reflection in clojure.

Suppose there is a part of java code

import com.example.SampleClass;
import java.lang.reflect.Field;

public class Sample {
    public static void main(String[] args) {
        Field field = SampleClass.class.getDeclaredField("someFiledName");
    }
}

I can not find how to express the meaning of SampleClass.class where there is not any instance of SampleClass. It is just imported so I can not use (.class SampleClass) or (.getClass SampleClass)

1 Answers

The class itself is the class - only Java, the language, needs .class to get to that.

So you import the class and then you can call your function on it.

user=> (import 'java.lang.Math) ; that's not really needed
java.lang.Math
user=> (.getDeclaredField java.lang.Math "PI")
#object[java.lang.reflect.Field 0x74ad8d05 "public static final double java.lang.Math.PI"]
Related