In my clojure program I cannot access package scoped fields of the java class com.foo.Foo although I am in the namespace “com.foo” (via “(ns com.foo)” at the top of my clojure program). However, public fields in com.foo.Foo are accessible.
Why?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Two problems here:
First, the namespace com.foo is compiled to a class foo_whatever in package com; it’s not compiled to a class in package com.foo.
See:
Second, when looking for constructors or fields, the Clojure compiler uses methods getConstructor and getFields from java.lang.Class, which, by spec, only return the public constructors and public fields.
So, bad luck here. It seems you won’t be able to access package-protected fields.
Edit, answering comments. The best approach for accessing package-level fields in legacy Java code would be to write a class in Java that wraps the existing class and which exposes the package protected methods and fields from that class with public methods. This way, you have more control over the name and package of the generated class.
This wrapper is a small amount of Java code, and from there you can access the fields from Clojure code.