Class.forName

R

Roedy Green

Is there a way to ask if a class has been loaded without actually
requesting it be loaded? similar to Class.forName
 
A

Arne Vajhøj

Patricia said:
ClassLoader has a method findLoadedClass(String). Is that what you need?

I would have thought it would be a problem to get to that
for all the classloaders because it is protected ?

Arne
 
A

Arne Vajhøj

Roedy said:
Is there a way to ask if a class has been loaded without actually
requesting it be loaded? similar to Class.forName

My suggestion would be to use an agent.

import java.lang.instrument.Instrumentation;

public class TraceAgent {
public static void premain(String args, Instrumentation inst) {
inst.addTransformer(new TraceTransformer());
}
}

and

import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.security.ProtectionDomain;
import java.util.HashSet;
import java.util.Set;

public class TraceTransformer implements ClassFileTransformer {
private static Set<String> clz = new HashSet<String>();
public byte[] transform(ClassLoader loader,
String className,
Class<?> classBeingRedefined,
ProtectionDomain protectionDomain,
byte[] classfileBuffer) throws
IllegalClassFormatException {
clz.add(className.replace("/", "."));
return null;
}
public static boolean isLoaded(String className) {
return clz.contains(className);
}
}

and then check with:

TraceTransformer.isLoaded(clznam)

Arne
 
D

Daniel Pitts

Arne said:
I would have thought it would be a problem to get to that
for all the classloaders because it is protected ?

Arne
It is possible to extend ClassLoader, and then you have access to
protected members. Reflection also works, but is less elegant :)
 
A

Arne Vajhøj

Daniel said:
It is possible to extend ClassLoader, and then you have access to
protected members.

It is possible.

But it does not solve the problem.

Arne
 
S

softwarepearls_com

Roedy said:
Is there a way to ask if a class has been loaded without actually
requesting it be loaded? similar to Class.forName

My suggestion would be to use an agent.

import java.lang.instrument.Instrumentation;

public class TraceAgent {
     public static void premain(String args, Instrumentation inst) {
         inst.addTransformer(new TraceTransformer());
     }

}

and

import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.security.ProtectionDomain;
import java.util.HashSet;
import java.util.Set;

public class TraceTransformer implements ClassFileTransformer {
     private static Set<String> clz = new HashSet<String>();
     public byte[] transform(ClassLoader loader,
                             String className,
                             Class<?> classBeingRedefined,
                             ProtectionDomain protectionDomain,
                             byte[] classfileBuffer) throws
IllegalClassFormatException {
         clz.add(className.replace("/", "."));
         return null;
     }
     public static boolean isLoaded(String className) {
         return clz.contains(className);
     }

}

and then check with:

TraceTransformer.isLoaded(clznam)

Arne

Couple of comments on this approach.. the instrumentation subsystem
sometimes passes a null for the classname, so that needs checking to
avoid NPE. Secondly, to avoid totally unnecessary, and increasingly
expensive, rehashing of the Set as it grows, maybe give it an initial
capacity of 10'000 or so. Launching the smallest Java program loads
thousands of classes... Finally, the agent approach requires extra
command line arguments, a JAR etc.. so not really programmer-friendly,
IMO.
 
A

Arne Vajhøj

softwarepearls_com said:
Couple of comments on this approach.. the instrumentation subsystem
sometimes passes a null for the classname, so that needs checking to
avoid NPE.

I was not aware of that. Good point. When do it send null
as class name ?
Secondly, to avoid totally unnecessary, and increasingly
expensive, rehashing of the Set as it grows, maybe give it an initial
capacity of 10'000 or so. Launching the smallest Java program loads
thousands of classes...

Optimization at the milliseconds level was not exactly a priority
when I wrote the example.
Finally, the agent approach requires extra
command line arguments, a JAR etc.. so not really programmer-friendly,
IMO.

IMO it is really programmer friendly because it is non-intrusive
except where the code need to use the info.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top