patch available: add option to allow RT/classForName to initialize the class

9 views
Skip to first unread message

Stephen C. Gilardi

unread,
Oct 26, 2008, 11:18:09 AM10/26/08
to clo...@googlegroups.com
RT/classForName currently hardwires the "initialize" option of
Class.forName to false. In working with jdbc drivers, the need to
initialize classes in some circumstances came up--in some JRE
configurations, the driver class needs to be initialized explicitly to
work.

I don't understand all the subtleties of class loading, but I gather
from previous discussions here that it's good practice for Clojure to
provide a class loader and for Clojure code to use it in the absence
of a compelling reason not to. Use of Class.forName directly is (at
least mildly) discouraged.

To address both of those concerns, classForName-initialize.patch
(uploaded to the group and in-line below) changes RT.java to expose an
optional "initialize" flag for RT/classForName.

After the change, existing calls to RT/classForName will work as they
always have, but callers that need to ensure the class is initialized
are now able to.

--Steve

Index: src/jvm/clojure/lang/RT.java
===================================================================
--- src/jvm/clojure/lang/RT.java (revision 1080)
+++ src/jvm/clojure/lang/RT.java (working copy)
@@ -1510,10 +1510,15 @@

static public Class classForName(String name) throws
ClassNotFoundException{

- return Class.forName(name, false, baseLoader());
+ return classForName(name, false);
}

+static public Class classForName(String name, boolean initialize)
throws ClassNotFoundException{

+ return Class.forName(name, initialize, baseLoader());
+}
+
+
static public float aget(float[] xs, int i){
return xs[i];
}

Rich Hickey

unread,
Oct 26, 2008, 11:50:14 AM10/26/08
to Clojure


On Oct 26, 11:18 am, "Stephen C. Gilardi" <squee...@mac.com> wrote:
> RT/classForName currently hardwires the "initialize" option of
> Class.forName to false. In working with jdbc drivers, the need to
> initialize classes in some circumstances came up--in some JRE
> configurations, the driver class needs to be initialized explicitly to
> work.
>
> I don't understand all the subtleties of class loading, but I gather
> from previous discussions here that it's good practice for Clojure to
> provide a class loader and for Clojure code to use it in the absence
> of a compelling reason not to. Use of Class.forName directly is (at
> least mildly) discouraged.
>
> To address both of those concerns, classForName-initialize.patch
> (uploaded to the group and in-line below) changes RT.java to expose an
> optional "initialize" flag for RT/classForName.
>
> After the change, existing calls to RT/classForName will work as they
> always have, but callers that need to ensure the class is initialized
> are now able to.
>

I was wondering if there is an argument against using initialize ==
true in all cases?

Rich

Bhaskar Maddala

unread,
Oct 26, 2008, 4:14:13 PM10/26/08
to clo...@googlegroups.com
> I was wondering if there is an argument against using initialize ==
> true in all cases?

Here are a couple of examples, in the strictly java world (java
programming language and vm) when you want initialize to be false.

The idiomatic use case being byte code analysis
* FindBugs
* Code obfuscators

In these cases you do not want the static block to execute.

Thanks
Bhaskar

Reply all
Reply to author
Forward
0 new messages