Go Back   Velocity Reviews > Newsgroups > Java
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Java - Re: Reflection and parsing Java classfiles

 
Thread Tools Search this Thread
Old 10-21-2005, 10:46 AM   #1
Default Re: Reflection and parsing Java classfiles


"SAD" <> wrote in news:1129777988.703099.191080
@g47g2000cwa.googlegroups.com:

> This article describes a grammar and simple parser for processing Java
> classfiles:
> http://www.webservicessummit.com/Art...uOct2005_1.htm


It seems like abuse of reflection to me, especially since it depends on
undefined behaviour. I'm sure it works, but it is assuming that the
fields of a class are ordered when they are not. It's just lucky that
getDeclaredFields() happens to return the fields in the order that they
appear in the source, otherwise this whole concept would be totally
useless.


Brendan Guild
  Reply With Quote
Old 10-21-2005, 11:05 AM   #2
Chris Uppal
 
Posts: n/a
Default Re: Reflection and parsing Java classfiles
Brendan Guild wrote:

> > http://www.webservicessummit.com/Art...uOct2005_1.htm

>
> It seems like abuse of reflection to me, especially since it depends on
> undefined behaviour. I'm sure it works, but it is assuming that the
> fields of a class are ordered when they are not. It's just lucky that
> getDeclaredFields() happens to return the fields in the order that they
> appear in the source, otherwise this whole concept would be totally
> useless.


Yes, the JavaDoc for getDeclaredFields() specifically states that the order of
entries is undefined. Bad Madhu ! Bad !

But still, it'd not be hard to fix up by adding some explicit metadata to the
target class. The /concept's/ OK, even if the implementation takes one
short-cut too many.

-- chris






Chris Uppal
  Reply With Quote
Old 10-21-2005, 11:22 AM   #3
Ross Bamford
 
Posts: n/a
Default Re: Reflection and parsing Java classfiles
On Fri, 21 Oct 2005 10:46:10 +0100, Brendan Guild <> wrote:

> "SAD" <> wrote in news:1129777988.703099.191080
> @g47g2000cwa.googlegroups.com:
>
>> This article describes a grammar and simple parser for processing Java
>> classfiles:
>> http://www.webservicessummit.com/Art...uOct2005_1.htm

>
> It seems like abuse of reflection to me, especially since it depends on
> undefined behaviour. I'm sure it works, but it is assuming that the
> fields of a class are ordered when they are not. It's just lucky that
> getDeclaredFields() happens to return the fields in the order that they
> appear in the source, otherwise this whole concept would be totally
> useless.



I'd be very careful about the term 'reflection' from the start, since this
blatantly isn't it. I've had some fairly heated discussions during the
development of Jen about the use of that term, mainly because "it's a
common term", but I think it's deeper than that and it's wrong to claim
inaccurate facts, simply because people are likely to "know what you mean".

In docs and stuff we sometimes refer to these techniques as an
"alternative to reflection", but are always careful to make sure that we
don't claim it's reflection, because it's not (e.g. there's no mirroring
going on, and no link real between the values you get and any runtime
values).

--
Ross Bamford -


Ross Bamford
  Reply With Quote
Old 10-21-2005, 11:33 AM   #4
Ross Bamford
 
Posts: n/a
Default Re: Reflection and parsing Java classfiles
On Fri, 21 Oct 2005 11:05:31 +0100, Chris Uppal
<> wrote:

> Brendan Guild wrote:
>
>> > http://www.webservicessummit.com/Art...uOct2005_1.htm

>>
>> It seems like abuse of reflection to me, especially since it depends on
>> undefined behaviour. I'm sure it works, but it is assuming that the
>> fields of a class are ordered when they are not. It's just lucky that
>> getDeclaredFields() happens to return the fields in the order that they
>> appear in the source, otherwise this whole concept would be totally
>> useless.

>
> Yes, the JavaDoc for getDeclaredFields() specifically states that the
> order of
> entries is undefined. Bad Madhu ! Bad !
>
> But still, it'd not be hard to fix up by adding some explicit metadata
> to the
> target class. The /concept's/ OK, even if the implementation takes one
> short-cut too many.
>


Thinking more about it, there isn't even any guarantee that the constant
pool is the same at runtime - the JVM is free to reorder and remove
entries as it sees fit, as long as it fixes up the references. For
example, when using the java.lang.instrument API in Mustang, the new
retransform classes feature specifically states that the constant pool in
the byte[] passed in is unlikely to be the same as the .class file, owing
to these optimizations.

Also, I don't think theres all that much of use to the programmer in the
pool - beyond classes, primitives and strings it's mainly the (internal)
method descriptors and stuff for invokexxxx instructions. Most of the
'constants' (I expect) in many classes are actually initialized in the
class init method.

I stand by my original comment, though, that it was a nice article. The
concept is good, and the implementation wasn't the point I guess.

--
Ross Bamford -


Ross Bamford
  Reply With Quote
Old 10-21-2005, 12:26 PM   #5
Chris Uppal
 
Posts: n/a
Default Re: Reflection and parsing Java classfiles
Ross Bamford wrote:

> > It seems like abuse of reflection to me, [...]


> I'd be very careful about the term 'reflection' from the start, since this
> blatantly isn't it.


You may already have realised this, but I think Brendan was referring
specifically to the use of reflection (java.lang.Class.getDeclaredFields()) to
drive the parser from a list of the fields in the target/template class
(ClassFile).

-- chris




Chris Uppal
  Reply With Quote
Old 10-21-2005, 12:35 PM   #6
Ross Bamford
 
Posts: n/a
Default Re: Reflection and parsing Java classfiles
On Fri, 21 Oct 2005 12:26:57 +0100, Chris Uppal
<> wrote:

> Ross Bamford wrote:
>
>> > It seems like abuse of reflection to me, [...]

>
>> I'd be very careful about the term 'reflection' from the start, since
>> this
>> blatantly isn't it.

>
> You may already have realised this, but I think Brendan was referring
> specifically to the use of reflection
> (java.lang.Class.getDeclaredFields()) to
> drive the parser from a list of the fields in the target/template class
> (ClassFile).
>
> -- chris
>


Oh. I'll shut up then.

I thought OP was referring to the technique itself, but reading more
carefully now I see I should have read more carefully in the first place

--
Ross Bamford -


Ross Bamford
  Reply With Quote
Old 10-21-2005, 09:47 PM   #7
madhu.siddalingaiah@gmail.com
 
Posts: n/a
Default Re: Reflection and parsing Java classfiles
Well I'm glad someone thought it was a nice article!
The issue that getDeclaredFields() doesn't guarantee the order of
fields in the source concerned and annoyed me. I chose not to get into
it at the time, that's one of the reasons I didn't release the source
just yet. I wanted to address that issue (and a couple of others)
before releasing the code.

The point I wanted to make was that a small amount of code could solve
a significant class of problems. Looks like you got the point.

--
Madhu Siddalingaiah
http://www.madhu.com



madhu.siddalingaiah@gmail.com
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

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