importing a java class which 'requires' your namespace in a static block

2013-06-05 Thread Jim - FooBar();

Hello everyone,

weirdness strikes again!

I've got the following situation:

- a namespace core.clj which imports a java class, let's call it Foo.java
- Foo.java requires core.clj in the usual way :

 private static IFn requireFn =   RT.var(clojure.core, 
require).fn();

 static { requireFn.invoke(Symbol.intern(someNamespace.core)); }

Now, the first time I (load-file xxx.core.clj) everything is perfectly 
fine. The minute I make a change and re-load I get:


NoClassDefFoundError Could not initialize class yyy.Foo

have you ever had that? what do you do when one of your java sources 
delegates back to a namespace of yours? is that completely bad design 
perhaps?


thanks for your time,

Jim

--
--
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups Clojure group.

To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: importing a java class which 'requires' your namespace in a static block

2013-06-05 Thread Gary Trakhman
One problem with doing this in a static initializer is that you lose the
relevant exception.  I would try moving this to a constructor or lazy-load
it, and you might get a better error message.


On Wed, Jun 5, 2013 at 1:28 PM, Jim - FooBar(); jimpil1...@gmail.comwrote:

 Hello everyone,

 weirdness strikes again!

 I've got the following situation:

 - a namespace core.clj which imports a java class, let's call it Foo.java
 - Foo.java requires core.clj in the usual way :

  private static IFn requireFn =   RT.var(clojure.core,
 require).fn();
  static { requireFn.invoke(Symbol.**intern(someNamespace.core)); }

 Now, the first time I (load-file xxx.core.clj) everything is perfectly
 fine. The minute I make a change and re-load I get:

 NoClassDefFoundError Could not initialize class yyy.Foo

 have you ever had that? what do you do when one of your java sources
 delegates back to a namespace of yours? is that completely bad design
 perhaps?

 thanks for your time,

 Jim

 --
 --
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To post to this group, send email to clojure@googlegroups.com
 Note that posts from new members are moderated - please be patient with
 your first post.
 To unsubscribe from this group, send email to
 clojure+unsubscribe@**googlegroups.comclojure%2bunsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/**group/clojure?hl=enhttp://groups.google.com/group/clojure?hl=en
 --- You received this message because you are subscribed to the Google
 Groups Clojure group.
 To unsubscribe from this group and stop receiving emails from it, send an
 email to 
 clojure+unsubscribe@**googlegroups.comclojure%2bunsubscr...@googlegroups.com
 .
 For more options, visit 
 https://groups.google.com/**groups/opt_outhttps://groups.google.com/groups/opt_out
 .




-- 
-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
Clojure group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: importing a java class which 'requires' your namespace in a static block

2013-06-05 Thread Jim - FooBar();

On 05/06/13 18:40, Gary Trakhman wrote:
One problem with doing this in a static initializer is that you lose 
the relevant exception.  I would try moving this to a constructor or 
lazy-load it, and you might get a better error message.


doing what you suggested seems to alleviate the problem! I'm still not 
quite sure what happened but I can reload my namespace just fine!


thanks! :)

Jim

--
--
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups Clojure group.

To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: importing a java class which 'requires' your namespace in a static block

2013-06-05 Thread Softaddicts
The other alternative is to extend the class loader to add a trap.
I do not have the code handy but you might find it using a search on google.


Luc


 One problem with doing this in a static initializer is that you lose the
 relevant exception.  I would try moving this to a constructor or lazy-load
 it, and you might get a better error message.
 
 
 On Wed, Jun 5, 2013 at 1:28 PM, Jim - FooBar(); jimpil1...@gmail.comwrote:
 
  Hello everyone,
 
  weirdness strikes again!
 
  I've got the following situation:
 
  - a namespace core.clj which imports a java class, let's call it Foo.java
  - Foo.java requires core.clj in the usual way :
 
   private static IFn requireFn =   RT.var(clojure.core,
  require).fn();
   static { requireFn.invoke(Symbol.**intern(someNamespace.core)); }
 
  Now, the first time I (load-file xxx.core.clj) everything is perfectly
  fine. The minute I make a change and re-load I get:
 
  NoClassDefFoundError Could not initialize class yyy.Foo
 
  have you ever had that? what do you do when one of your java sources
  delegates back to a namespace of yours? is that completely bad design
  perhaps?
 
  thanks for your time,
 
  Jim
 
  --
  --
  You received this message because you are subscribed to the Google
  Groups Clojure group.
  To post to this group, send email to clojure@googlegroups.com
  Note that posts from new members are moderated - please be patient with
  your first post.
  To unsubscribe from this group, send email to
  clojure+unsubscribe@**googlegroups.comclojure%2bunsubscr...@googlegroups.com
  For more options, visit this group at
  http://groups.google.com/**group/clojure?hl=enhttp://groups.google.com/group/clojure?hl=en
  --- You received this message because you are subscribed to the Google
  Groups Clojure group.
  To unsubscribe from this group and stop receiving emails from it, send an
  email to 
  clojure+unsubscribe@**googlegroups.comclojure%2bunsubscr...@googlegroups.com
  .
  For more options, visit 
  https://groups.google.com/**groups/opt_outhttps://groups.google.com/groups/opt_out
  .
 
 
 
 
 -- 
 -- 
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To post to this group, send email to clojure@googlegroups.com
 Note that posts from new members are moderated - please be patient with your 
 first post.
 To unsubscribe from this group, send email to
 clojure+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en
 --- 
 You received this message because you are subscribed to the Google Groups 
 Clojure group.
 To unsubscribe from this group and stop receiving emails from it, send an 
 email to clojure+unsubscr...@googlegroups.com.
 For more options, visit https://groups.google.com/groups/opt_out.
 
 
 
--
Softaddictslprefonta...@softaddicts.ca sent by ibisMail from my ipad!

-- 
-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
Clojure group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: importing a java class which 'requires' your namespace in a static block

2013-06-05 Thread Marshall Bockrath-Vandegrift
Jim - FooBar(); jimpil1...@gmail.com writes:

 Now, the first time I (load-file xxx.core.clj) everything is
 perfectly fine. The minute I make a change and re-load I get:

 NoClassDefFoundError Could not initialize class yyy.Foo

This confuses me, because the JVM should only be loading and
initializing the Java class once.  Re-loading the Clojure namespace
shouldn’t report an error unless initialization actually failed the
first time around.

That said, you will definitely get errors if you have a Java class
`require` a namespace in a static initializer which then `import`s the
class.  Clojure `import` causes referenced classes to be initialized,
which runs static initializers, which means a circular namespace -
initializer - namespace dependency effectively reduces to a circular
namespace dependency.

The solution I’ve been using lately is to push the Java-side
namespace-loading into a private static inner class of the original Java
class.  This provides the benefits of JVM-managed single-initialization,
but defers execution of that initialization code until something
actually needs one of the imported Vars.  Example:


https://github.com/damballa/abracad/blob/master/src/java/abracad/avro/ClojureDatumReader.java

HTH,

-Marshall

-- 
-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
Clojure group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.