Obtaining the bytecode for a class without reading it from .class file

Discussion in 'Java' started by Bogdan Tudor, Jan 10, 2007.

  1. Bogdan Tudor

    Bogdan Tudor Guest

    Hello everybody,

    I was wondering if anyone tried to obtain the bytecode of a class (
    obtain it as a byte[] for example), but without having to read it from
    the .class file found in the classpath.
    I am asking this because sometimes the classpath could contain some
    non-disk locations, so reading from disk the .class won't always work.

    Thanks,
    Bogdan Tudor
     
    Bogdan Tudor, Jan 10, 2007
    #1
    1. Advertising

  2. In article <>,
    "Bogdan Tudor" <> wrote:

    > Hello everybody,
    >
    > I was wondering if anyone tried to obtain the bytecode of a class (
    > obtain it as a byte[] for example), but without having to read it from
    > the .class file found in the classpath.
    > I am asking this because sometimes the classpath could contain some
    > non-disk locations, so reading from disk the .class won't always work.
    >
    > Thanks,
    > Bogdan Tudor


    I haven't done anything of the sort myself, but I recall seeing some
    sample code when I researched how to use a ClassLoader. I can't say
    where, but it was "out there" on the Internet someplace. The idea I
    remember was that the bytes were received from some source or another
    and written locally so that a custom ClassLoader could then load them
    and proceed. But if you know enough about how a ClassLoader works you
    could just as easily do the same with a non-disk (in-memory) source of
    bytes, I'm sure.

    Lotsa luck.

    = Steve =
    --
    Steve W. Jackson
    Montgomery, Alabama
     
    Steve W. Jackson, Jan 10, 2007
    #2
    1. Advertising

  3. Bogdan Tudor

    Oliver Wong Guest

    "Steve W. Jackson" <> wrote in message
    news:...
    > In article <>,
    > "Bogdan Tudor" <> wrote:
    >
    >> Hello everybody,
    >>
    >> I was wondering if anyone tried to obtain the bytecode of a class (
    >> obtain it as a byte[] for example), but without having to read it from
    >> the .class file found in the classpath.
    >> I am asking this because sometimes the classpath could contain some
    >> non-disk locations, so reading from disk the .class won't always work.

    >
    > I haven't done anything of the sort myself, but I recall seeing some
    > sample code when I researched how to use a ClassLoader. I can't say
    > where, but it was "out there" on the Internet someplace. The idea I
    > remember was that the bytes were received from some source or another
    > and written locally so that a custom ClassLoader could then load them
    > and proceed. But if you know enough about how a ClassLoader works you
    > could just as easily do the same with a non-disk (in-memory) source of
    > bytes, I'm sure.


    Might not be that easy. There are a bunch of methods in ClassLoader
    where you can pass in a byte[], and out pops a Class<?>, but I don't see any
    methods that provide the reverse (i.e. you provide a Class<?> and it returns
    a byte[]).

    - Oliver
     
    Oliver Wong, Jan 10, 2007
    #3
  4. Bogdan Tudor

    Mark Rafn Guest

    Bogdan Tudor <> wrote:
    >I was wondering if anyone tried to obtain the bytecode of a class (
    >obtain it as a byte[] for example), but without having to read it from
    >the .class file found in the classpath.


    Where else would you get it? If it's a class that doesn't exist as static
    bytecode on the classpath (say, an on-demand class that your ClassLoader
    generates for you), it'll depend on the implementation whether you can get it
    or not. This is pretty rare, though.

    >I am asking this because sometimes the classpath could contain some
    >non-disk locations, so reading from disk the .class won't always work.


    Oh, you mean you don't want to reimpliment what the classloader already does
    to read classfiles from various places. Good, just use the classloader.
    getClass().getClassLoader().getResourceAsStream()
    is probably what you're looking for. You'll need to munge the classname to
    get the path (translate '.' to '/', only look for outer classes, etc.).
    --
    Mark Rafn <http://www.dagon.net/>
     
    Mark Rafn, Jan 10, 2007
    #4
  5. Bogdan Tudor

    Daniel Pitts Guest

    Bogdan Tudor wrote:
    > Hello everybody,
    >
    > I was wondering if anyone tried to obtain the bytecode of a class (
    > obtain it as a byte[] for example), but without having to read it from
    > the .class file found in the classpath.
    > I am asking this because sometimes the classpath could contain some
    > non-disk locations, so reading from disk the .class won't always work.
    >
    > Thanks,
    > Bogdan Tudor


    You can use a ClassLoader to get any resource in the classpath (local
    or not, and class or not). Usually you get the resource as a Stream.
    You can read the bytes from the stream.
     
    Daniel Pitts, Jan 10, 2007
    #5
  6. Bogdan Tudor

    Hemal Pandya Guest

    Oliver Wong wrote:
    > "Steve W. Jackson" <> wrote in message
    > news:...
    > > In article <>,
    > > "Bogdan Tudor" <> wrote:
    > >
    > >> Hello everybody,
    > >>
    > >> I was wondering if anyone tried to obtain the bytecode of a class (
    > >> obtain it as a byte[] for example), but without having to read it from
    > >> the .class file found in the classpath.
    > >> I am asking this because sometimes the classpath could contain some
    > >> non-disk locations, so reading from disk the .class won't always work.

    > >
    > > I haven't done anything of the sort myself, but I recall seeing some
    > > sample code when I researched how to use a ClassLoader. I can't say
    > > where, but it was "out there" on the Internet someplace. The idea I
    > > remember was that the bytes were received from some source or another
    > > and written locally so that a custom ClassLoader could then load them
    > > and proceed. But if you know enough about how a ClassLoader works you
    > > could just as easily do the same with a non-disk (in-memory) source of
    > > bytes, I'm sure.

    >
    > Might not be that easy. There are a bunch of methods in ClassLoader
    > where you can pass in a byte[], and out pops a Class<?>, but I don't see any
    > methods that provide the reverse (i.e. you provide a Class<?> and it returns
    > a byte[]).


    I assume the OP is looking to get to the byte array of previously
    loaded classes.

    It seems the ClassLoader eventually defines a class from a byte array
    or ByteBuffer. Maybe you can have a custom class loader, that saves the
    byte array in a map somewhere before using the default class loader.

    >
    > - Oliver
     
    Hemal Pandya, Jan 11, 2007
    #6
  7. Bogdan Tudor

    M.J. Dance Guest

    Re: Obtaining the bytecode for a class without reading it from .classfile

    Bogdan Tudor wrote:
    > Hello everybody,
    >
    > I was wondering if anyone tried to obtain the bytecode of a class (
    > obtain it as a byte[] for example), but without having to read it from
    > the .class file found in the classpath.
    > I am asking this because sometimes the classpath could contain some
    > non-disk locations, so reading from disk the .class won't always work.


    One example is Instrumentation. This way you get to meddle with the bytes even
    before ClassLoader gets them. Is that what you had in mind?
     
    M.J. Dance, Jan 11, 2007
    #7
  8. Bogdan Tudor

    Tom Hawtin Guest

    Re: Obtaining the bytecode for a class without reading it from .classfile

    Hemal Pandya wrote:
    >
    > It seems the ClassLoader eventually defines a class from a byte array
    > or ByteBuffer. Maybe you can have a custom class loader, that saves the
    > byte array in a map somewhere before using the default class loader.


    For common class loaders, can't you just load mypackage/MyClass.class as
    a resource. That probably isn't going to work with dynamically generated
    classes (for instance from java.lang.reflect.Proxy) but I believe should
    for java.net.URLClassLoader derived class loaders.

    Tom Hawtin
     
    Tom Hawtin, Jan 11, 2007
    #8
    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. Mac
    Replies:
    1
    Views:
    454
    Roedy Green
    Sep 3, 2003
  2. Lukas Weibel

    Re: Getting bytecode of a class

    Lukas Weibel, Jul 13, 2004, in forum: Java
    Replies:
    0
    Views:
    610
    Lukas Weibel
    Jul 13, 2004
  3. Luca Rosellini
    Replies:
    6
    Views:
    729
    John C. Bollinger
    Nov 22, 2004
  4. Nathan Duran
    Replies:
    1
    Views:
    340
    Arnaud Delobelle
    Apr 10, 2008
  5. John Lam
    Replies:
    12
    Views:
    229
    Rick DeNatale
    Aug 31, 2006
Loading...

Share This Page