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

B

Bogdan Tudor

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
 
S

Steve W. Jackson

"Bogdan Tudor said:
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 =
 
O

Oliver Wong

Steve W. Jackson said:
Bogdan Tudor said:
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
 
M

Mark Rafn

Bogdan Tudor said:
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.).
 
D

Daniel Pitts

Bogdan said:
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.
 
H

Hemal Pandya

Oliver said:
Steve W. Jackson said:
Bogdan Tudor said:
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.
 
M

M.J. Dance

Bogdan said:
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?
 
T

Tom Hawtin

Hemal said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top