

![]() | Start a set with this search |
![]() | Include this search in one of my sets |
![]() | Exclude this search from one of my sets |
![]() | Permalink to these results Paste this link in email or IM: |
| Atom feed for tracking future search results Paste this URL into your reader: |
11 messages in net.java.dev.jna.usersRe: Structure Order| From | Sent On | Attachments |
|---|---|---|
| Nicolas Vienne | Jan 10, 2008 2:15 am | |
| Albert Strasheim | Jan 10, 2008 2:32 am | |
| Nicolas Vienne | Jan 10, 2008 2:56 am | |
| Albert Strasheim | Jan 10, 2008 3:02 am | |
| Albert Strasheim | Jan 10, 2008 3:08 am | |
| Timothy Wall | Jan 12, 2008 10:49 am | |
| Nicolas Vienne | Jan 17, 2008 3:45 am | |
| Albert Strasheim | Jan 17, 2008 3:48 am | |
| Timothy Wall | Jan 17, 2008 5:15 am | |
| Nicolas Vienne | Jan 17, 2008 5:53 am | |
| Nicolas Vienne | Jan 17, 2008 5:56 am |

![]() | Permalink for this message Paste this link in email or IM: |
![]() | Permalink for this thread Paste this link in email or IM: |
| Atom feed for this thread Paste this URL into your reader: |
| Subject: | Re: Structure Order | Actions... |
|---|---|---|
| From: | Nicolas Vienne (ikar...@tagazok.net) | |
| Date: | Jan 17, 2008 5:56:11 am | |
| List: | net.java.dev.jna.users | |
Albert Strasheim <fullung@...> writes:
This is a pretty cool idea. This can prevent some problems that could be caused by code cleanup features in IDEs like Eclipse (the "Sort Members" feature comes to mind).
Cheers,
Albert
Hello,
Well, I do not pretend that this solution is well implemented or bug free, since it took me not that much time to realize :).
Two annotations are defined :
- IgnoreField to specify that the following field must not be included in the equivalent structure. The wanted behavior is to include all fields by default and exclude some of them if necessary. - FieldOrder to specify with an integer with is the field position in the resulting structure
For instance, the following class :
public class MyStruct extends Structure { @IgnoreField public int[] pouet = new int[4];
@FieldOrder(2) public int two;
@FieldOrder(1) public int one; }
is expected to be seen as :
struct MyStruct { int one; int two; };
The fields are sorted according to their FieldOrder value. To help with this task, a class CustomField is provided (the Field class is made Comparable). There may be fields without the FieldOrder annotation. They are placed at the end of the ordered list, keeping the java definition order : This modification remains compatible with the existing implementation. [1][2]
Here are some code excerpt :
// The IgnoreField annotation @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface IgnoreField { }
// The FieldOrder annotation @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface FieldOrder { int value() default 0; }
/** * The CustomField class * @author nvienne * */ public class CustomField implements Comparable<CustomField>{ private int order = 0; private Field field = null; private boolean active = false; private boolean ordered = false;
public CustomField(Field f) { field = f; if(f.getAnnotation(IgnoreField.class) == null) { active = true; } if(f.getAnnotation(FieldOrder.class) != null) { order = f.getAnnotation(FieldOrder.class).value(); ordered = true; } }
public boolean isActive() { return active; } public boolean isOrdered() { return ordered; } public int getOrder() { return order; } public Field getField() { return field; }
public int compareTo(CustomField arg0) { if(order < arg0.getOrder()) return -1; else if (order == arg0.getOrder()) return 0; else return 1; } }
Structure.java was modified in the beginning of the function calculateSize :
/** * BEGIN CODE MODIFICATION */ Field[] class_fields = getClass().getFields(); List<CustomField> ordered_fields = new ArrayList<CustomField>(); List<CustomField> unordered_fields = new ArrayList<CustomField>(); for (int i=0; i<class_fields.length; i++) { CustomField cf = new CustomField(class_fields[i]); if(cf.isActive()) { if(cf.isOrdered()) ordered_fields.add(cf); else { unordered_fields.add(cf); } } } Collections.sort(ordered_fields); ordered_fields.addAll(unordered_fields); CustomField[] fields = new CustomField[ordered_fields.size()]; fields = ordered_fields.toArray(fields);
//Field[] fields = getClass().getFields();
if (REVERSE_FIELDS) { for (int i=0;i < fields.length/2;i++) { int idx = fields.length-1-i; //Field tmp = fields[i]; CustomField tmp = fields[i]; fields[i] = fields[idx]; fields[idx] = tmp; } }
for (int i=0; i<fields.length; i++) { //Field field = fields[i]; Field field = fields[i].getField();
/** * END CODE MODIFICATION */
This last part is far from being perfect and - I think - could be improved.
Two more remarks : [1] The behavior of leaving the unannotated field at the end in their definition order may not be the best one. It's also possible to give them a field order equal to 0 (with the same field order value, the fields are ordered according to their definition order). Then, negative order values are placed before and positive ones after. [2] Only the public non-static fields are taken into account. This method may be a starting point to extend the concerned fields to protected or private annotated attributes. I've not tried this for now.
Regards,
Nicolas Vienne







