suggestion for new feature on fieldaccess

77 views
Skip to first unread message

Gabriel Ferreira

unread,
Nov 29, 2013, 2:50:52 PM11/29/13
to reflecta...@googlegroups.com
Hi. I noticed that a method to return the field type was missing on FieldAccess, so i decided to fork your project and i implemented it. I also implemented a method to return the field count. This is very useful to iterate all fields for the same object instance.
If you would like to see the changes, please check here: ReflectASM-fork-gabrikid

Would you like to merge my changes into your project?

By the way, i want to suggest something more. The sample source codes that you have at README.md are useful to manage the same field/method on different instances. But if you want to iterate all fields/methods on the same instance, and if you use exactly that source code, you will never get the maximum performance. So, i suggest you to add some more sample codes like this one:

FieldAccess access = FieldAccess.get(SomeClass.class);
String[] fieldNames = access.getFieldNames();
    for(int i = 0; i < fieldNames.length; i++) {
        access.set(instanceObject, i, valueToPut);              
    }
 }

(with my changes, note the getFieldCount() call to perform the iteration)
FieldAccess access = FieldAccess.get(SomeClass.class);
    for(int i = 0; i < access.getFieldCount(); i++) {
        access.set(instanceObject, i, valueToPut);              
    }
 }

Nate

unread,
Dec 2, 2013, 4:44:54 AM12/2/13
to reflecta...@googlegroups.com
On Fri, Nov 29, 2013 at 8:50 PM, Gabriel Ferreira <gabri...@gmail.com> wrote:
Hi. I noticed that a method to return the field type was missing on FieldAccess, so i decided to fork your project and i implemented it. I also implemented a method to return the field count. This is very useful to iterate all fields for the same object instance.
If you would like to see the changes, please check here: ReflectASM-fork-gabrikid

Would you like to merge my changes into your project?

Isn't the field count just access.getFieldNames().length? If you create a PR I can see exact what you propose to merge.

 

By the way, i want to suggest something more. The sample source codes that you have at README.md are useful to manage the same field/method on different instances. But if you want to iterate all fields/methods on the same instance, and if you use exactly that source code, you will never get the maximum performance. So, i suggest you to add some more sample codes like this one:

FieldAccess access = FieldAccess.get(SomeClass.class);
String[] fieldNames = access.getFieldNames();
    for(int i = 0; i < fieldNames.length; i++) {
        access.set(instanceObject, i, valueToPut);              
    }
 }

The above should be faster than the below. The below calls a method each loop iteration.
 

(with my changes, note the getFieldCount() call to perform the iteration)
FieldAccess access = FieldAccess.get(SomeClass.class);
    for(int i = 0; i < access.getFieldCount(); i++) {
        access.set(instanceObject, i, valueToPut);              
    }
 }

The fastest way you can write this is:

FieldAccess access = FieldAccess.get(SomeClass.class);
String[] fieldNames = access.getFieldNames();
for (int i = 0, n = fieldNames.length; i < n; i++) {
   access.set(instanceObject, i, valueToPut);              
 }

Cheers!
-Nate


Gabriel Ferreira

unread,
Dec 2, 2013, 6:18:26 AM12/2/13
to reflecta...@googlegroups.com
Hi. You are right about the loop. Actually, i never realized that if i do "for(int i = 0; i < getCount(); i++)", that would call the "getCount" function every iteration. Cool.

By the way, what do u want to say with "PR"?

Nate

unread,
Dec 2, 2013, 6:46:14 AM12/2/13
to reflecta...@googlegroups.com


On Mon, Dec 2, 2013 at 12:18 PM, Gabriel Ferreira <gabri...@gmail.com> wrote:
Hi. You are right about the loop. Actually, i never realized that if i do "for(int i = 0; i < getCount(); i++)", that would call the "getCount" function every iteration. Cool.

By the way, what do u want to say with "PR"?

--
--
You received this message because you are subscribed to the "reflectasm-users" group.
http://groups.google.com/group/reflectasm-users
---
You received this message because you are subscribed to the Google Groups "reflectasm-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to reflectasm-use...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Gabriel Ferreira

unread,
Dec 3, 2013, 6:59:35 AM12/3/13
to reflecta...@googlegroups.com
I did the pull request.

When i was using reflectasm, i had the need to know both the field name and field type, so i added the feature for this purpose. And, to make the iteration separated from the field names and field types implementation (array), i decided to add the method, so that it can return the field count. This way, even if the FieldAccess implementation might change (for instance, instead an array, use an ArrayList), the iteration interface will be the same for the outside.

Nate

unread,
Dec 3, 2013, 7:29:27 AM12/3/13
to reflecta...@googlegroups.com
On Tue, Dec 3, 2013 at 12:59 PM, Gabriel Ferreira <gabri...@gmail.com> wrote:
I did the pull request.

Thanks! Merged, with some edits.
 

When i was using reflectasm, i had the need to know both the field name and field type, so i added the feature for this purpose. And, to make the iteration separated from the field names and field types implementation (array), i decided to add the method, so that it can return the field count. This way, even if the FieldAccess implementation might change (for instance, instead an array, use an ArrayList), the iteration interface will be the same for the outside.

The field names (and types) are already exposed as an array, so the fact that an array is used isn't hidden by providing methods to access the same data. I'd rather have just getFieldNames, getFieldTypes, and getFieldCount. This makes it clearer what is happening, there is less magic. Eg, your getFieldType(String) calls getIndex which is slow, so should be javadoc'ed... better to just keep the API simple and have one way to access the data. Also, method calls are more expensive than array access, especially for some certain platforms (Android).

Thanks for your contributions!

-Nate


Gabriel Ferreira

unread,
Dec 3, 2013, 9:34:18 AM12/3/13
to reflecta...@googlegroups.com
Cool. Thanks for the opportunity to let me contribute to this project. It is my first contribution to an open-source project :)
Reply all
Reply to author
Forward
0 new messages