Class.forName

Discussion in 'Java' started by Roedy Green, Jul 26, 2008.

  1. Roedy Green

    Roedy Green Guest

    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, Jul 26, 2008
    #1
    1. Advertising

  2. Roedy Green

    Roedy Green Guest

    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
    Roedy Green, Jul 27, 2008
    #2
    1. Advertising

  3. Roedy Green

    Arne Vajhøj Guest

    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, Jul 27, 2008
    #3
  4. Roedy Green

    Arne Vajhøj Guest

    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
    Arne Vajhøj, Jul 27, 2008
    #4
  5. Roedy Green

    Daniel Pitts Guest

    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/>
    Daniel Pitts, Jul 27, 2008
    #5
  6. Roedy Green

    Arne Vajhøj Guest

    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
    Arne Vajhøj, Jul 27, 2008
    #6
  7. On Jul 27, 6:03 am, Arne Vajhøj <> 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.
    softwarepearls_com, Jul 29, 2008
    #7
  8. Roedy Green

    Roedy Green Guest

    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 <> 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
    Roedy Green, Jul 30, 2008
    #8
  9. Roedy Green

    Arne Vajhøj Guest

    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
    Arne Vajhøj, Aug 2, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. iksrazal

    Re: Class.forname() vs clone()

    iksrazal, Jun 27, 2003, in forum: Java
    Replies:
    0
    Views:
    1,603
    iksrazal
    Jun 27, 2003
  2. Ahmed Moustafa

    performance of Class.forname()?

    Ahmed Moustafa, Jul 20, 2003, in forum: Java
    Replies:
    14
    Views:
    5,901
    Chris Smith
    Jul 29, 2003
  3. H.MuthuKumaraRajan

    .class and Class.forName

    H.MuthuKumaraRajan, Nov 6, 2003, in forum: Java
    Replies:
    11
    Views:
    1,727
    H.MuthuKumaraRajan
    Nov 7, 2003
  4. cyril
    Replies:
    2
    Views:
    3,857
    cyril
    Aug 25, 2004
  5. Madni
    Replies:
    1
    Views:
    5,878
Loading...

Share This Page