How can I run my code upon class load?

If you want to base the behavior on an interface, you could use a static initializer in that interface.

If you want to base the behavior on an interface, you could use a static initializer in that interface. Public interface Foo{ static{ // do initializing here } } I'm not saying it's good practice, but it will definitely initialize the first time one of the implementing classes is loaded. Reference: Initializers (Sun Java Tutorial) But if I understand you right, you want the initialization to happen once per implementing class.

That will be tricky. You definitely can't do that with an interface based solution. You could do it with an abstract base class that has a dynamic initializer (or constructor), that checks whether the requested mapping already exists and adds it if it doesn't, but doing such things in constructors is quite a hack.

I'd say you cleanest options are either to generate Code at build time (through annotation processing with apt or through bytecode analysis with a tool like asm) or to use an agent at class load time to dynamically create the mapping. Ah, more input. Very good.So clients use your library and provide mappings based on annotations.

Then I'd say your library should provide an initializer method, where client code can register classes. Something like this: YourLibrary.getInstance(). RegisterMappedClasses( CustomClass1.

Class, CustomClass2. Class, CustomClass3. Class, CustomClass4.

Class ) Or, even better, a package scanning mechanism (example code to implement this can be found at this question): YourLibrary.getInstance(). RegisterMappedClassesFromPackages( "com.mycompany.myclientcode. Abc", "com.mycompany.myclientcode.

Def" ) Anyway, there is basically no way to avoid having your clients do that kind of work, because you can't control their build process nor their classloader for them (but you could of course provide guides for classloader or build configuration).

(Just added more info to the OP) static blocks or abstract classes won't do. You understood right - this would be done per client-code interface. – Henrik Paul Oct 26 '10 at 9:05 I'm doing the registering thing currently (except I have no annotations yet, and they are registered in client-defined pairs) which works well.

Thought to automatize this. But that package-scanning method seems pretty appealing (apart from the refactoring made harder). I'll take a look at that solution.

Alas, no accepting here either, yet. – Henrik Paul Oct 26 '10 at 9:33 1 Relax, you just asked the question an hour ago, no need to accept anything just now. BTW: even if you do, you can un-accept an answer later.

– Sean Patrick Floyd Oct 26 '10 at 9:40.

If you want some piece of code to be run on any class loading, you should: overwrite the ClassLoader, adding your own custom code at the loadClass methods (don't forget forwarding to the parent ClassLoader after or before your custom code). Define this custom ClassLoader as the default for your system (here you got how to do it: stackoverflow.com/q/3801714/212952). Run and check it.

Depending on what kind of environment you are, there are chances that not all the classes be loaded trouugh your custom ClassLoader (some utility packages use their own CL, some JEE containers handle some spacific areas with specific classLoaders, etc. ), but it's a kind of aproximation to what you are asking.

Thanks. I'll give this a shot. I won't accept this answer just yet, though.

– Henrik Paul Oct 26 '10 at 9:09.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions