You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by David Kerber <dc...@verizon.net> on 2006/11/02 14:34:33 UTC

How to get fields list in order

This is more of a java question than it is Tomcat-specific, but it's the 
only java group I hang out in, so here goes:

How can I get a list of the public fields declared in a class, *in the 
order they are declared*?  Using Reflection's getFields and 
getDeclaredFields methods returns them in no particular order, and I 
need them in the order they are declared so I can get their starting 
byte offsets.

TIA!
Dave



---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How to get fields list in order

Posted by Christopher Schultz <ch...@christopherschultz.net>.
David,

>> I'm sorry to say it (again), but I think you're just asking for
>> trouble, here. What do you have implemented in Java that's so
>> useful and difficult to port to Delphi or VM or (even better) C#?
> 
> What's VM?

Whoops. That should have been "VB".

> I would really prefer to have this done in Delphi, but basically it
> has to do with where we have programmer-hours available.

I complete understand.

> The other reason (not the deciding one, but another point in its
> favor) to do it in java is for later portability to another package
> we're considering developing over the next year or so for web-based
> administration of one of our other apps.

That's an interesting strategy... do new development in Java and write
wrappers for existing software to use the new libraries. In this case, I
think you are playing with fire.

It turns out that C wrappers for Java libraries aren't that hard to
write. IF you don't know much about C, though, I wouldn't recommend it.

The bottom line to your question is that byte offsets are difficult if
not impossible to predict, and you are better off using getDeclaredField
and getField().getValue() calls. Better yet, write your own, simpler
Java wrapper and then ddl-ize /that/. You could even write a short Java
program to read your classes using reflection and write-out simpler
forms of data, like all Strings or well-ordered, binary data.

Good luck,
-chris


Re: How to get fields list in order

Posted by David Kerber <dc...@verizon.net>.
Christopher Schultz wrote:

...

>>I read the data from the structures, load it into my classes,
>>do all my processing in java with those regular classes, then write the
>>data back out of the classes into the structures, and pass it back to
>>the calling prog.
>>    
>>
>
>I'm sorry to say it (again), but I think you're just asking for trouble,
>here. What do you have implemented in Java that's so useful and
>difficult to port to Delphi or VM or (even better) C#?
>  
>
What's VM?  I would really prefer to have this done in Delphi, but 
basically it has to do with where we have programmer-hours available.  
We're a very small company (15 people total).  I'm the only VB and the 
main Java programmer.  We have two guys who do Delphi and a little java, 
but they are completely socked up with other work.  We have no other 
programmers, and can't afford the cash outlay to bring in a contract 
programmer to do it in C or Delphi.  We have no C (of any flavor) 
programmers in-house.  The other reason (not the deciding one, but 
another point in its favor) to do it in java is for later portability to 
another package we're considering developing over the next year or so 
for web-based administration of one of our other apps.

Dave



---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How to get fields list in order

Posted by Christopher Schultz <ch...@christopherschultz.net>.
Dave,

David Kerber wrote:
>> JavaDllBuilder looks like it should be used to call Java from another
>> language. Are you using this package backwards?
>
> No, I'm using it to make a .dll in java that I can call from VB and
> Delphi.

Okay, that makes way more sense.

This might be a better question on a different group. You /did/ give us
a heads-up, though ;)

> Reading and writing the data from those structures requires that
> I know the byte offsets of each piece of data inside them

Perhaps you should get someone to write simpler Java wrappers around
these things, so maybe you could deal exclusively with String data, or
maybe even with byte arrays of information that is packed in an explicit
way (for instance, Java's DataOutputStream can write binary data in
network byte order, with well-defined sizes, offsets, etc.)

> I read the data from the structures, load it into my classes,
> do all my processing in java with those regular classes, then write the
> data back out of the classes into the structures, and pass it back to
> the calling prog.

I'm sorry to say it (again), but I think you're just asking for trouble,
here. What do you have implemented in Java that's so useful and
difficult to port to Delphi or VM or (even better) C#?

-chris



Re: How to get fields list in order

Posted by David Kerber <dc...@verizon.net>.
Christopher Schultz wrote:

>Dave,
>
>David Kerber wrote:
>  
>
>>Christopher Schultz wrote:
>>    
>>
>>>Wait... I thought you said you were using JNI...?! Or, are you using
>>>someone else's JNI package to do some dirty tricks? I thought there was
>>>only a C binding for JNI thus far.
>>>      
>>>
>>I'm using a 3rd party package which handles most of the grunt-work of
>>JNI for me.  If anybody's interested, it's from www.nevaobject.com; I'm
>>using their JavaDllBuilder package, but they have other ones as well.
>>    
>>
>
>JavaDllBuilder looks like it should be used to call Java from another
>language. Are you using this package backwards?
>
>  
>
No, I'm using it to make a .dll in java that I can call from VB and 
Delphi.  The dll has to take as parameters some rather complex 
structures, modify the data in them, and return them back to the calling 
prog.  Reading and writing the data from those structures requires that 
I know the byte offsets of each piece of data inside them, but the 
Coroutine package that is part of JavaDllBuilder provides routines that 
make these retrievals one-liners once I know the offsets.  Those 
structures and their contents correspond to java classes in my dll 
source.  I read the data from the structures, load it into my classes, 
do all my processing in java with those regular classes, then write the 
data back out of the classes into the structures, and pass it back to 
the calling prog.

Dave






---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How to get fields list in order

Posted by Christopher Schultz <ch...@christopherschultz.net>.
Dave,

David Kerber wrote:
> Christopher Schultz wrote:
>> Wait... I thought you said you were using JNI...?! Or, are you using
>> someone else's JNI package to do some dirty tricks? I thought there was
>> only a C binding for JNI thus far.
>
> I'm using a 3rd party package which handles most of the grunt-work of
> JNI for me.  If anybody's interested, it's from www.nevaobject.com; I'm
> using their JavaDllBuilder package, but they have other ones as well.

JavaDllBuilder looks like it should be used to call Java from another
language. Are you using this package backwards?

-chris



Re: How to get fields list in order

Posted by David Kerber <dc...@verizon.net>.
Christopher Schultz wrote:

...

>>I don't know C, and don't have access to a C compiler.
>>    
>>
>
>Wait... I thought you said you were using JNI...?! Or, are you using
>someone else's JNI package to do some dirty tricks? I thought there was
>only a C binding for JNI thus far.
>  
>
I'm using a 3rd party package which handles most of the grunt-work of 
JNI for me.  If anybody's interested, it's from www.nevaobject.com; I'm 
using their JavaDllBuilder package, but they have other ones as well.

Dave



---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How to get fields list in order

Posted by Christopher Schultz <ch...@christopherschultz.net>.
David,

>>>> Since you mentioned byte offsets, I have to ask: are you trying to
>>>> subvert the serialization system? Or, even worse, inspect the object
>>>> memory from with JNI?
>>>>     
>>> The latter, sort of.  I'm using a package which puts a wrapper around
>>> JNI to make it easier to create windows standard .dll's from java
>>> classes, and passing them structures from Visual Basic and other
>>> external apps.  To modify return parameters, I need the byte offsets of
>>> the various pieces of the structure, which I'm mirroring in my java
>>> classes.
>>>   
>>
>> Why not just use the built-in JNI C-to-Java capabilities? If you need to
>
> I don't know C, and don't have access to a C compiler.

Wait... I thought you said you were using JNI...?! Or, are you using
someone else's JNI package to do some dirty tricks? I thought there was
only a C binding for JNI thus far.

-chris


Re: How to get fields list in order

Posted by David Kerber <dc...@verizon.net>.
Christopher Schultz wrote:

>David,
>
>  
>
>>>Since you mentioned byte offsets, I have to ask: are you trying to
>>>subvert the serialization system? Or, even worse, inspect the object
>>>memory from with JNI?
>>>      
>>>
>>The latter, sort of.  I'm using a package which puts a wrapper around
>>JNI to make it easier to create windows standard .dll's from java
>>classes, and passing them structures from Visual Basic and other
>>external apps.  To modify return parameters, I need the byte offsets of
>>the various pieces of the structure, which I'm mirroring in my java
>>classes.
>>    
>>
>
>Why not just use the built-in JNI C-to-Java capabilities? If you need to
>  
>
I don't know C, and don't have access to a C compiler.

>modify objects, why not "modify" them by creating new objects containing
>the desired data, instead of trying to lobotomize an existing object?
>
>Perhaps I don't really understand what you are doing.
>  
>
The only time I have to modify memory addresses is when I write the 
results back to the output, and the grunt work of that is handled by the 
package I bought.  I just have to supply an offset into the passed 
structure.  All the processing in java is done with standard java 
techniques, classes and methods, and I don't care about the offsets or 
actual memory used by the java field values.  I was just using the data 
type of the java class fields as an indicator to tell me what offset to 
allow in the passed structure, but not necessarily using the java type's 
actual size.

Dave



---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How to get fields list in order

Posted by Christopher Schultz <ch...@christopherschultz.net>.
David,

>> Since you mentioned byte offsets, I have to ask: are you trying to
>> subvert the serialization system? Or, even worse, inspect the object
>> memory from with JNI?
>
> The latter, sort of.  I'm using a package which puts a wrapper around
> JNI to make it easier to create windows standard .dll's from java
> classes, and passing them structures from Visual Basic and other
> external apps.  To modify return parameters, I need the byte offsets of
> the various pieces of the structure, which I'm mirroring in my java
> classes.

Why not just use the built-in JNI C-to-Java capabilities? If you need to
modify objects, why not "modify" them by creating new objects containing
the desired data, instead of trying to lobotomize an existing object?

Perhaps I don't really understand what you are doing.

-chris


Re: How to get fields list in order

Posted by David Kerber <dc...@verizon.net>.
Christopher Schultz wrote:

>David,
>
>  
>
>>Then I could just check the field type, and increment the byte
>>counter by the appropriate amount (i.e. if the java field type is
>>int, then I know the width in the passed structure is 4 bytes, etc).
>>    
>>
>
>As Chuck points out, this is unlikely to work. If the JIT for the
>platform decides to word-align your fields, and a word isn't 4-bytes
>long, you will get the wrong data. There's probably no good way to do
>this without a lot of trial and error followed by luck and an
>environment that never changes.
>  
>
That's the job of the package I bought that encapsulates all this stuff, 
so I don't have to understand it, just use it according to their 
documentation <Grin>.

Dave



---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How to get fields list in order

Posted by Christopher Schultz <ch...@christopherschultz.net>.
David,

> Then I could just check the field type, and increment the byte
> counter by the appropriate amount (i.e. if the java field type is
> int, then I know the width in the passed structure is 4 bytes, etc).

As Chuck points out, this is unlikely to work. If the JIT for the
platform decides to word-align your fields, and a word isn't 4-bytes
long, you will get the wrong data. There's probably no good way to do
this without a lot of trial and error followed by luck and an
environment that never changes.

At any rate, you'd better document the heck out of the whole thing ;)

-chris


Re: How to get fields list in order

Posted by David Kerber <dc...@verizon.net>.
Caldarale, Charles R wrote:

>>From: David Kerber [mailto:dckerber@verizon.net] 
>>Subject: Re: How to get fields list in order
>>
>>Then I could just check the field type, and increment the 
>>byte counter by the appropriate amount (i.e. if the java 
>>field type is int, then I know the width in the passed
>>structure is 4 bytes, etc).
>>    
>>
>
>If the structure is intended to mirror the layout of the Java object,
>then your assumptions are flawed.  For example, in a 64-bit JVM,
>integers are 8 bytes wide, of which only 4 are actually used.  Also,
>booleans have no defined size, and JVM implementations are free to
>allocate whatever space deemed appropriate, from 1 bit upwards.
>  
>
I'll keep that in mind; it looks like I'll have to go with my backup method.

Dave



---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: How to get fields list in order

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: David Kerber [mailto:dckerber@verizon.net] 
> Subject: Re: How to get fields list in order
> 
> Then I could just check the field type, and increment the 
> byte counter by the appropriate amount (i.e. if the java 
> field type is int, then I know the width in the passed
> structure is 4 bytes, etc).

If the structure is intended to mirror the layout of the Java object,
then your assumptions are flawed.  For example, in a 64-bit JVM,
integers are 8 bytes wide, of which only 4 are actually used.  Also,
booleans have no defined size, and JVM implementations are free to
allocate whatever space deemed appropriate, from 1 bit upwards.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
MATERIAL and is thus for use only by the intended recipient. If you
received this in error, please contact the sender and delete the e-mail
and its attachments from all computers.

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How to get fields list in order

Posted by David Kerber <dc...@verizon.net>.
Caldarale, Charles R wrote:

>>From: David Kerber [mailto:dckerber@verizon.net] 
>>Subject: Re: How to get fields list in order
>>
>>To modify return parameters, I need the byte offsets of 
>>the various pieces of the structure, which I'm mirroring
>>in my java classes.
>>    
>>
>
>That's a very scary way to do it.  Using order to determine byte offsets
>is not reliable, since that can change from JVM to JVM and across
>different platforms and CPU architectures.  What's wrong with gettng the
>field IDs during class initialization and using the GetField / PutField
>JNI methods as needed?
>  
>
That's my workaround, but it's a lot more work than being able to search 
a fields list, as long as the fields list could be retreived in the 
order it was declared.  Then I could just check the field type, and 
increment the byte counter by the appropriate amount (i.e. if the java 
field type is int, then I know the width in the passed structure is 4 
bytes, etc).  I'm not looking for the byte offsets into the java classes 
themselves, but rather am using them as indexes into the passed 
structure, for which the byte offsets ARE fixed, but could change if we 
need to modify them later on.

Dave



---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: How to get fields list in order

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: David Kerber [mailto:dckerber@verizon.net] 
> Subject: Re: How to get fields list in order
> 
> To modify return parameters, I need the byte offsets of 
> the various pieces of the structure, which I'm mirroring
> in my java classes.

That's a very scary way to do it.  Using order to determine byte offsets
is not reliable, since that can change from JVM to JVM and across
different platforms and CPU architectures.  What's wrong with gettng the
field IDs during class initialization and using the GetField / PutField
JNI methods as needed?

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
MATERIAL and is thus for use only by the intended recipient. If you
received this in error, please contact the sender and delete the e-mail
and its attachments from all computers.

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How to get fields list in order

Posted by David Kerber <dc...@verizon.net>.
Christopher Schultz wrote:

>David,
>
>  
>
>>How can I get a list of the public fields declared in a class, *in the
>>order they are declared*?  Using Reflection's getFields and
>>getDeclaredFields methods returns them in no particular order, and I
>>need them in the order they are declared so I can get their starting
>>byte offsets.
>>    
>>
>
>Since you mentioned byte offsets, I have to ask: are you trying to
>subvert the serialization system? Or, even worse, inspect the object
>memory from with JNI?
>  
>
The latter, sort of.  I'm using a package which puts a wrapper around 
JNI to make it easier to create windows standard .dll's from java 
classes, and passing them structures from Visual Basic and other 
external apps.  To modify return parameters, I need the byte offsets of 
the various pieces of the structure, which I'm mirroring in my java classes.

>I would guess that if you can't get the proper order of the fields from
>Class.getDeclaredFields, then you can't get it at all.
>  
>
That's what I'm afraid of.  I know how I can work around this, but it's 
a lot more typing when setting up the class fields.

Thanks for the response!



---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How to get fields list in order

Posted by Christopher Schultz <ch...@christopherschultz.net>.
David,

> How can I get a list of the public fields declared in a class, *in the
> order they are declared*?  Using Reflection's getFields and
> getDeclaredFields methods returns them in no particular order, and I
> need them in the order they are declared so I can get their starting
> byte offsets.

Since you mentioned byte offsets, I have to ask: are you trying to
subvert the serialization system? Or, even worse, inspect the object
memory from with JNI?

I would guess that if you can't get the proper order of the fields from
Class.getDeclaredFields, then you can't get it at all.

-chris