The Artima Developer Community
Sponsored Link

Java Answers Forum
instanceExists() ...

6 replies on 1 page. Most recent reply: Jan 22, 2004 7:59 PM by Adam Connor

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

instanceExists() ... Posted: Jan 21, 2004 12:30 AM
Reply to this message Reply
Advertisement
How can I check if a instance of a class exists (without knowing the instance variable)?


mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: instanceExists() ... Posted: Jan 21, 2004 5:55 AM
Reply to this message Reply
If you have control to the code of the class whose instance is to be generated, you can keep a record of the instances created.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: instanceExists() ... Posted: Jan 22, 2004 4:50 AM
Reply to this message Reply
You mean doing it with a conter using a static variable and incrementing the counter in the constructor, right?


I hoped there was a way not to do it using static variables.


When using a standard class loader, the class will only be loaded into RAM when using it for the first time (please correct me if I'm wrong). Is there no way to check if the class is loaded?

mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: instanceExists() ... Posted: Jan 22, 2004 5:26 AM
Reply to this message Reply
JVM must be having a record of the existing instances of classes.

It can initialize garbage collection depenidng on that info.

But is there a way to know that? I doubt ..pardon my lil knowledge.

Adam Connor

Posts: 12
Nickname: adamconnor
Registered: Jan, 2004

Re: instanceExists() ... Posted: Jan 22, 2004 5:54 PM
Reply to this message Reply
I think this is a tricky question to answer. Assuming that finding out if the class has been loaded is good enough, the method you want is findLoadedClass(String name) in ClassLoader; it will return null if the class has not been loaded.

However, it is a protected final method, so you can't call it directly. The following program outlines one approach for accessing it:

public class TestClassIsLoaded {
public static void main(String[] args) throws Exception {
TestClassIsLoaded test = new TestClassIsLoaded();
System.out.println("BigDecimal loaded? " + test.isLoaded("java.math.BigDecimal") );
System.out.println("String loaded? " + test.isLoaded("java.lang.String"));
BigDecimal bd = new BigDecimal(0);
System.out.println("BigDecimal loaded? " + test.isLoaded("java.math.BigDecimal") );
}

public boolean isLoaded(String className)
throws IllegalAccessException, InvocationTargetException {

ClassLoader loader = TestClassIsLoaded.class.getClassLoader();
Class lc = loader.getClass();
while( lc != null ){
try {
Method m = lc.getDeclaredMethod( "findLoadedClass", new Class[]{String.class} );
// next line requires "suppressAccessChecks" permission if security is on
m.setAccessible(true);
Object result = m.invoke(loader, new Object[]{className});
return result != null;
}
catch (NoSuchMethodException e) {
lc = lc.getSuperclass();
continue;
}
}
throw new IllegalStateException("Never found \"findLoadedClass\".");
}
}



If you are running with the SecurityManager turned on, you will need sufficient permissions to perform these operations.

Sachin Joshi

Posts: 12
Nickname: aspire
Registered: Jan, 2004

Re: instanceExists() ... Posted: Jan 22, 2004 7:07 PM
Reply to this message Reply
Hi Adam Connor ,

I was watching this thread. Your given program works well. I hope it clears the question of the author of this thread.

Sachin

Adam Connor

Posts: 12
Nickname: adamconnor
Registered: Jan, 2004

Re: instanceExists() ... Posted: Jan 22, 2004 7:59 PM
Reply to this message Reply
Thanks. Of course, as luck would have it I realized after writing it that the loop was dumb, since I know exactly where the method is declared and it's final. This version is simpler:

public class TestClassIsLoaded {
public static void main(String[] args) throws Exception {
TestClassIsLoaded test = new TestClassIsLoaded();
System.out.println("BigDecimal loaded? " + test.isLoaded("java.math.BigDecimal") );
System.out.println("String loaded? " + test.isLoaded("java.lang.String"));
BigDecimal bd = new BigDecimal(0);
System.out.println("BigDecimal loaded? " + test.isLoaded("java.math.BigDecimal") );
}

public boolean isLoaded(String className)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
ClassLoader loader = TestClassIsLoaded.class.getClassLoader();
Method m = ClassLoader.class.getDeclaredMethod( "findLoadedClass", new Class[]{String.class} );
// next line requires "suppressAccessChecks" permission if security is on
m.setAccessible(true);
Object result = m.invoke(loader, new Object[]{className});
return result != null;
}
}

Flat View: This topic has 6 replies on 1 page
Topic: Event throw Previous Topic   Next Topic Topic: server side authentication

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use