Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Class.forName (http://www.velocityreviews.com/forums/t627731-class-forname.html)

Roedy Green 07-26-2008 10:39 PM

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

--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green 07-26-2008 11:58 PM

Re: Class.forName
 
On Sat, 26 Jul 2008 16:24:33 -0700, Patricia Shanahan <pats@acm.org>
wrote, quoted or indirectly quoted someone who said :

>ClassLoader has a method findLoadedClass(String). Is that what you need

That looks like it. thanks.

--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Arne Vajh°j 07-27-2008 04:00 AM

Re: Class.forName
 
Patricia Shanahan wrote:
> Roedy Green wrote:
>> Is there a way to ask if a class has been loaded without actually
>> requesting it be loaded? similar to Class.forName

>
> 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

Arne Vajh°j 07-27-2008 04:03 AM

Re: Class.forName
 
Roedy Green wrote:
> 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

Daniel Pitts 07-27-2008 09:16 AM

Re: Class.forName
 
Arne Vajh°j wrote:
> Patricia Shanahan wrote:
>> Roedy Green wrote:
>>> Is there a way to ask if a class has been loaded without actually
>>> requesting it be loaded? similar to Class.forName

>>
>> 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

It is possible to extend ClassLoader, and then you have access to
protected members. Reflection also works, but is less elegant :-)
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Arne Vajh°j 07-27-2008 01:53 PM

Re: Class.forName
 
Daniel Pitts wrote:
> Arne Vajh°j wrote:
>> Patricia Shanahan wrote:
>>> Roedy Green wrote:
>>>> Is there a way to ask if a class has been loaded without actually
>>>> requesting it be loaded? similar to Class.forName
>>>
>>> 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 ?

> 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

softwarepearls_com 07-29-2008 07:38 AM

Re: Class.forName
 
On Jul 27, 6:03*am, Arne Vajh°j <a...@vajhoej.dk> wrote:
> Roedy Green wrote:
> > 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.

Roedy Green 07-30-2008 05:29 PM

Re: Class.forName
 
On Tue, 29 Jul 2008 00:38:36 -0700 (PDT), softwarepearls_com
<info@softwarepearls.com> wrote, quoted or indirectly quoted someone
who said :

>On Jul 27, 6:03*am, Arne Vajh°j <a...@vajhoej.dk> wrote:
>> Roedy Green wrote:
>> > 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;

I have posted that code at
http://mindprod.com/jgloss/classforname.html

Thanks. I gather you are working with Mr. Vanselheuve (sp?). I am so
glad he is still around . I had so much fun reading his under the hood
books on the PC and Windows way back when.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Arne Vajh°j 08-02-2008 07:43 PM

Re: Class.forName
 
softwarepearls_com wrote:
> 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


All times are GMT. The time now is 05:55 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.