Retrieving the bytes of platform classes

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Retrieving the bytes of platform classes

Mark Raynsford
Hello.

I'm writing a small tool to analyze the APIs of Java 9 modules. I'm
analyzing the bytecode statically with ASM and have run into a slight
problem: In order to properly analyze a class, I also need to analyze
the superclasses and, in some cases, the superclasses can be platform
classes.

Given a platform class name such as "java/io/InputStream", is it
possible to retrieve the raw bytes for the named class in a portable
(read: non-platform-specific, non-JVM-specific) manner?

In the olden days, we'd probably have gone rummaging in rt.jar, but
that was obviously a hack and wasn't future-proof. My tool is only
designed to work with JDK 9 and up.

Note that I'm *not* asking to get from loaded Class<?> instances to
bytes: I'm analyzing classes statically and none of the analyzed
classes will actually be loaded by any ClassLoader.

--
Mark Raynsford | http://www.io7m.com

Reply | Threaded
Open this post in threaded view
|

Re: Retrieving the bytes of platform classes

Alan Bateman
On 23/10/2017 18:48, Mark Raynsford wrote:

> :
>
> In the olden days, we'd probably have gone rummaging in rt.jar, but
> that was obviously a hack and wasn't future-proof. My tool is only
> designed to work with JDK 9 and up.
>
> Note that I'm *not* asking to get from loaded Class<?> instances to
> bytes: I'm analyzing classes statically and none of the analyzed
> classes will actually be loaded by any ClassLoader.
>
Have you looked at the jrt file system provider? Details in JEP 261 [1].

-Alan

[1] http://openjdk.java.net/jeps/261
Reply | Threaded
Open this post in threaded view
|

Re: Retrieving the bytes of platform classes

Alan Bateman


On 23/10/2017 18:55, Alan Bateman wrote:
> Details in JEP 261 [1].
Sorry, I meant JEP 220 (http://openjdk.java.net/jeps/220).


Reply | Threaded
Open this post in threaded view
|

Re: Retrieving the bytes of platform classes

mark.reinhold
In reply to this post by Alan Bateman
2017/10/23 10:55:15 -0700, [hidden email]:

> On 23/10/2017 18:48, Mark Raynsford wrote:
>> :
>>
>> In the olden days, we'd probably have gone rummaging in rt.jar, but
>> that was obviously a hack and wasn't future-proof. My tool is only
>> designed to work with JDK 9 and up.
>>
>> Note that I'm *not* asking to get from loaded Class<?> instances to
>> bytes: I'm analyzing classes statically and none of the analyzed
>> classes will actually be loaded by any ClassLoader.
>
> Have you looked at the jrt file system provider? Details in JEP 261 [1].

See also JEP 220:

  http://openjdk.java.net/jeps/220#New-URI-scheme-for-naming-stored-modules-classes-and-resources

- Mark
Reply | Threaded
Open this post in threaded view
|

Re: Retrieving the bytes of platform classes

Mark Raynsford
On 2017-10-23T11:00:22 -0700
[hidden email] wrote:
> 2017/10/23 10:55:15 -0700, [hidden email]:
> > Have you looked at the jrt file system provider? Details in JEP 261 [1].  
>
> See also JEP 220:
>
>   http://openjdk.java.net/jeps/220#New-URI-scheme-for-naming-stored-modules-classes-and-resources

Ah, thanks! I hadn't seen this. Looks like exactly what I need.

--
Mark Raynsford | http://www.io7m.com

Reply | Threaded
Open this post in threaded view
|

Re: Retrieving the bytes of platform classes

Sundararajan Athijegannathan
In reply to this post by Alan Bateman
A simple nashorn sample to extract single .class using jrt file system
provider:

https://blogs.oracle.com/sundararajan/extracting-a-single-class-file-from-java9-platform-jimage-modules-file

-Sundar

On 23/10/17, 11:25 PM, Alan Bateman wrote:

> On 23/10/2017 18:48, Mark Raynsford wrote:
>> :
>>
>> In the olden days, we'd probably have gone rummaging in rt.jar, but
>> that was obviously a hack and wasn't future-proof. My tool is only
>> designed to work with JDK 9 and up.
>>
>> Note that I'm *not* asking to get from loaded Class<?> instances to
>> bytes: I'm analyzing classes statically and none of the analyzed
>> classes will actually be loaded by any ClassLoader.
>>
> Have you looked at the jrt file system provider? Details in JEP 261 [1].
>
> -Alan
>
> [1] http://openjdk.java.net/jeps/261
Reply | Threaded
Open this post in threaded view
|

Re: Retrieving the bytes of platform classes

Volker Simonis
From within Java (i.e. for the current VM) you could simply do:

ClassLoader.getSystemResourceAsStream("java/io/InputStream.class")

Regards,
Volker


On Tue, Oct 24, 2017 at 3:47 AM, Sundararajan Athijegannathan
<[hidden email]> wrote:

> A simple nashorn sample to extract single .class using jrt file system
> provider:
>
> https://blogs.oracle.com/sundararajan/extracting-a-single-class-file-from-java9-platform-jimage-modules-file
>
> -Sundar
>
>
> On 23/10/17, 11:25 PM, Alan Bateman wrote:
>>
>> On 23/10/2017 18:48, Mark Raynsford wrote:
>>>
>>> :
>>>
>>> In the olden days, we'd probably have gone rummaging in rt.jar, but
>>> that was obviously a hack and wasn't future-proof. My tool is only
>>> designed to work with JDK 9 and up.
>>>
>>> Note that I'm *not* asking to get from loaded Class<?> instances to
>>> bytes: I'm analyzing classes statically and none of the analyzed
>>> classes will actually be loaded by any ClassLoader.
>>>
>> Have you looked at the jrt file system provider? Details in JEP 261 [1].
>>
>> -Alan
>>
>> [1] http://openjdk.java.net/jeps/261