aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes unload class from JVM Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "unload class from JVM" Watch "unload class from JVM" New topic
Author

unload class from JVM

Andy Jung
Ranch Hand

Joined: Feb 07, 2010
Posts: 150
Howdy ranchers!

By executing the statement we have the possibility to explicitly load a class by the ClassLoader to the JVM.
But how can I do the reverse thing, i.e. unload the class from the JVM?

The problem I have is the following:
One of my singletons contains a private and static reference to some Collection to store some dynamically created information.
Furthermore I have a JUnit-TestClass, testing several methods of this class.
By using its setup() and tearDownd() methods I can guarantee, that all methods have the identical test-fixture as a starting point, except for the private static field. Since it is private I can't expicitly set it to null, and since it is static there's no chance to reset it, if I set the singelton-instance in my TestCase to null.

I even tried to call again in the tearDown()-method in the hope that the JVM will re-initialize this class, but this didn't help.
There's also a class named ClassLoader in package java.lang and it has some load()-methods but where the heck are the corresponding unload-methods()?

So, what do you suggest in order to re-initialize or reload a class respectively?

Kind regards,
Andy


SCJP, SCJD
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2267
    
    3

Howdy, Andy!

One of my singletons...


Wow! How many singletons do you have?

So, what do you suggest in order to re-initialize or reload a class respectively?


As far as I know, you can't. Once it is loaded, it's loaded. Maybe there can be some sorts of tricks to do it with custom class loaders, but I'd say that that would be way too complicated.

Well... is this question related to the SCJD? Which part exactly are you testing?


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Andy Jung
Ranch Hand

Joined: Feb 07, 2010
Posts: 150
Roberto Perillo wrote:
Wow! How many singletons do you have?

Just two . My DAO is a facade holding two singeltons, one for the database file access and one for all logical locking issues.
Roberto Perillo wrote:
Well... is this question related to the SCJD? Which part exactly are you testing?

Yes and no :
Yes, because I want to test my URLyBird server classes automatically at any time and this helped me already to identify lots of problems.
No, because Unit-testing is actually not a requirement of SCJD.

Kind regards,
Andy
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5545
    
  13

Hi Andy,

Roberto Perillo wrote:is this question related to the SCJD? Which part exactly are you testing?
I think Andy is creating some tests for his Data class and wants to start with the same starting point for each test.

I think your test approach is ok: using setup and teardown methods to always start with same starting situation, but maybe you have to tweak it a bit. I struggled a bit too in creating the same starting point for each test. This is what I came up with: before each test I copy the (original) database file to a temporary file and i use that temporary file to initialize my data class. And after each test the temporary file is saved. So I created the same starting point for each test.

Hopefully it helps!
Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Andy Jung
Ranch Hand

Joined: Feb 07, 2010
Posts: 150
Roel De Nijs wrote:
This is what I came up with: before each test I copy the (original) database file to a temporary file and i use that temporary file to initialize my data class. And after each test the temporary file is saved. So I created the same starting point for each test.


That's exactly the way I did it :


But actually that's not the only issue you have to do in setUp() and tearDown() in order to retrieve the same starting point as I posted.

Regards,
Andy
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5545
    
  13

Hi Andy,

A final remark: if you use a singleton it becomes unnecessary to have a (private) static field, because your singleton approach guarantees that there will be just one instance of that field.

Kind regards,
Roel
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5545
    
  13

Hi Andy,

Maybe for your test cases you can use the FileChannel class from the nio-package:

Kind regards,
Roel
Andy Jung
Ranch Hand

Joined: Feb 07, 2010
Posts: 150
Hi Roel,

actually this problem is independent of singeltons.
How can you achieve the same starting point in setUp(), when a class (not a singleton) to be tested has a private static field with a certain state? How can you reset that state?
In my DAO I hold a private static field for the record cache (the DAO is not the singelton).
I want the cache being emptied every time I call setUp().

Regards,
Andy
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5545
    
  13

Hi Andy,

I would say: just create a new instance of that class (your dao).

Kind regards,
Roel
Andy Jung
Ranch Hand

Joined: Feb 07, 2010
Posts: 150
Hi Roel,

creating a new instance actually doesn't reset the private static field. It belongs to the class and not to an instance.
Once loaded by the class loader there seems to be no chance of resetting those class fields .

Kind regards,
Andy
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5545
    
  13

Hi Andy,

True, it's a static one. Sunday afternoon, apparently I'm still

I believe you could use reflection to alter the state of the static field (a lot of examples can be found here)

Kind regards,
Roel

Andy Jung
Ranch Hand

Joined: Feb 07, 2010
Posts: 150
Hi Roel,

good boy, reflection is fine:



As you can see recordCache is my private static field and the call to clear() resets this class variable.

Thanks,
Andy
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5545
    
  13

Andy Jung wrote:good boy
Sounds you are talking to your dog, I'm waiting on my biscuit, already sitting down
Andy Jung
Ranch Hand

Joined: Feb 07, 2010
Posts: 150
... at least you are reliable and loyal as a dog in this saloon .
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2267
    
    3

Andy Jung wrote:... at least you are reliable and loyal as a dog in this saloon .


A black bird told me that pretty soon he'll be the bartender around here!

PS: the bird that told me is black because I'm a Beatles fan (got it? Blackbird? The song? )
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5545
    
  13

Roberto Perillo wrote:A black bird told me that pretty soon he'll be the bartender around here!
(I only heard your name through the grapevine ) (got it? Marvin Gaye, not a fan btw)

Sorry Roberto, no Beatles fan, so blackbird is unknown to me
Andy Jung
Ranch Hand

Joined: Feb 07, 2010
Posts: 150
Roberto Perillo wrote:
PS: the bird that told me is black because I'm a Beatles fan (got it? Blackbird? The song? )


very nice song actually BlackBird Song,
maybe I cover this sometime, naming it "URLyBird Song", if I pass the exam :
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: unload class from JVM