This problem has irritated me for a long time and I finally thought of a
solution using annotations. Working from this tutorial
<http://www.mkyong.com/java/java-custom-annotations-example/> I created
MasonProperty.java and modified SimpleProperties.java to use it. In your
SimState, you'd do the following:
public int numYouth = 140;
@MasonProperty(order=1)
public int getNumYouth() { return numYouth; }
public void setNumYouth(int val) { numYouth = val; }
public int classSize = 18;
@MasonProperty(order=2)
public int getClassSize() { return classSize; }
public void setClassSize(int val) { classSize = val; }
...and so on. The annotation gets attached to the method that follows it
and it needs to go with the get method, so make sure it's in the right
place. If you don't include the annotation, the order will be the same as
before. If you put the annotation on some of your properties but not on
others, the non-annotated ones will come afterward (the default order
number is Integer.MAX_VALUE).
For Sean, here's what I did to SimpleProperties: In the loop where it goes
through all the methods looking for valid getters and setters, I changed it
so it just adds the method to getMethods but doesn't do the <other stuff>.
(It does currently print out debug info.) Next, it sorts the methods in the
getMethods list. Finally, it loops through the now-sorted getMethods list
and does the rest of the <other stuff>. That's it.
I haven't used annotations before, so it's possible I've done something
wrong or unnecessary. Feedback is certainly welcome. That said, my initial
tests looked great.
Hope that helps,
Joey
On Mon, Jun 22, 2015 at 11:22 PM, Joey Harrison <[log in to unmask]>
wrote:
> This problem has irritated me for a long time and I finally thought of a
> solution using annotations. Working from this tutorial
> <http://www.mkyong.com/java/java-custom-annotations-example/> I created
> MasonProperty.java and modified SimpleProperties.java to use it. In your
> SimState, you'd do the following:
>
> public int numYouth = 140;
> @MasonProperty(order=1)
> public int getNumYouth() { return numYouth; }
> public void setNumYouth(int val) { numYouth = val; }
>
> public int classSize = 18;
> @MasonProperty(order=2)
> public int getClassSize() { return classSize; }
> public void setClassSize(int val) { classSize = val; }
>
> ...and so on. The annotation gets attached to the method that follows it
> and it needs to go with the get method, so make sure it's in the right
> place. If you don't include the annotation, the order will be the same as
> before. If you put the annotation on some of your properties but not on
> others, the non-annotated ones will come afterward (the default order
> number is Integer.MAX_VALUE).
>
> For Sean, here's what I did to SimpleProperties: In the loop where it goes
> through all the methods looking for valid getters and setters, I changed it
> so it just adds the method to getMethods but doesn't do the <other stuff>.
> (It does currently print out debug info.) Next, it sorts the methods in the
> getMethods list. Finally, it loops through the now-sorted getMethods list
> and does the rest of the <other stuff>. That's it.
>
> I haven't used annotations before, so it's possible I've done something
> wrong or unnecessary. That said, my initial tests looked great.
>
> Hope that helps,
> Joey
>
>
> On Mon, Jun 22, 2015 at 9:09 PM, Sean Luke <[log in to unmask]> wrote:
>
>> On Jun 22, 2015, at 5:34 PM, Axel Kowald <[log in to unmask]> wrote:
>>
>> > well the ordering in the model tab seems to be unrelated to the
>> ordering in the source code.
>>
>> Again, it depends on the compiler. :-(
>>
>> > The really weird thing is that the ordering changes arbitrarily with
>> each program start.
>>
>> Now that *is* strange. Is this a Windows thing? Definitely does not
>> happen on the Mac.
>>
>> Sean
>>
>
>
|