Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Class.forName

Reply
Thread Tools

Class.forName

 
 
Roedy Green
Guest
Posts: n/a
 
      07-26-2008
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
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      07-26-2008
On Sat, 26 Jul 2008 16:24:33 -0700, Patricia Shanahan <>
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
 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      07-27-2008
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
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      07-27-2008
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
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      07-27-2008
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/>
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      07-27-2008
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
 
Reply With Quote
 
softwarepearls_com
Guest
Posts: n/a
 
      07-29-2008
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.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      07-30-2008
On Tue, 29 Jul 2008 00:38:36 -0700 (PDT), 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
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      08-02-2008
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57