/**
Monitoring whether a class has been loaded already using Instrumentation
a TraceAgent and a ClassFileTransformer.
@author Arne Vajhøj
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,
*/
import java.lang.instrument.Instrumentation;
public class TraceAgent
{
public static void premain(String args, Instrumentation inst)
{
inst.addTransformer(new TraceTransformer());
}
}
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 );
}
}
TraceTransformer.isLoaded( clznam )