Re: Enum method overrides and WO bindings
Re: Enum method overrides and WO bindings
- Subject: Re: Enum method overrides and WO bindings
- From: Chuck Hill <email@hidden>
- Date: Fri, 07 Jun 2013 09:07:02 -0700
This would be my suggestion of how to make this work.
Chuck
On 2013-06-07, at 4:34 AM, Musall Maik wrote:
> Good one!
>
> Although this isn't exactly reducing boilerplate, it shows up a completely different way that actually works. I didn't really think of implementing NSKeyValueCoding in an enum class. I think this is worthwhile at least for long complicated enums where two more methods are ok. Thanks!
>
> Anyone else? :)
>
> Maik
>
>
> Am 07.06.2013 um 13:16 schrieb Bogdan Zlatanov <email@hidden>:
>
>> And this is a more general way, but you probably guessed that already :)
>>
>> public enum Status implements NSKeyValueCoding {
>> one {
>> @Override public String description() { return "one"; }
>> @Override public Integer code() { return 1; }
>> },
>> two {
>> @Override public String description() { return "two"; }
>> @Override public Integer code() { return 2; }
>> };
>>
>> public abstract String description();
>> public abstract Integer code();
>>
>> @Override
>> public Object valueForKey(String s) {
>> System.out.println("Status.valueForKey(): " + s);
>> try {
>> return this.getClass().getMethod(s, (Class<?>[]) null).invoke(this, (Object[]) null);
>> } catch (Exception e) {
>> e.printStackTrace();
>> throw new RuntimeException(e);
>> }
>> }
>>
>> @Override
>> public void takeValueForKey(Object obj, String s) {
>> // TODO Auto-generated method stub
>> System.out.println("Status.takeValueForKey()");
>> }
>> }
>>
>> On 7 Jun 2013, at 13:07, Bogdan Zlatanov wrote:
>>
>>> This should work, hopefully.
>>>
>>> public enum Status implements NSKeyValueCoding {
>>> one { @Override public String description() { return "one"; }},
>>> two { @Override public String description() { return "two"; }};
>>> public abstract String description();
>>>
>>> @Override
>>> public Object valueForKey(String s) {
>>> System.out.println("Status.valueForKey(): " + s);
>>> return description();
>>> }
>>>
>>> @Override
>>> public void takeValueForKey(Object obj, String s) {
>>> // TODO Auto-generated method stub
>>> System.out.println("Status.takeValueForKey()");
>>> }
>>> }
>>>
>>> On 7 Jun 2013, at 12:34, Musall Maik wrote:
>>>
>>>>
>>>> Got that already by private mail. All right, let's modify the example. Returning a fixed string was oversimplifying.
>>>>
>>>>
>>>> public enum Status {
>>>> one { @Override public DateTime computeValue() { new DateTime().plusDays( 1 ); },
>>>> two { @Override public DateTime computeValue() { new DateTime().plusDays( 2 ); };
>>>> public abstract DateTime computeValue();
>>>> }
>>>>
>>>>
>>>> Maik
>>>>
>>>>
>>>> Am 07.06.2013 um 12:27 schrieb D Tim Cummings <email@hidden>:
>>>>
>>>>> Another workaround which is less ugly.
>>>>>
>>>>> public enum Status {
>>>>> one ("eins"),
>>>>> two ("zwei");
>>>>> private final String description;
>>>>> Status(String description) {
>>>>> this.description = description;
>>>>> }
>>>>> public String description() {
>>>>> return description;
>>>>> }
>>>>> }
>>>>>
>>>>> Tim
>>>>>
>>>>>
>>>>> On 07/06/2013, at 5:58 PM, Musall Maik wrote:
>>>>>
>>>>>> Hi,
>>>>>>
>>>>>> some time ago, I discovered the following problem with Enums and WO bindings (broken down to a simple example):
>>>>>>
>>>>>> package com.foo.bar;
>>>>>> public class MyClass {
>>>>>> public static enum Status {
>>>>>> one { @Override public String description() { return "eins"; } },
>>>>>> two { @Override public String description() { return "zwei"; } };
>>>>>> public abstract String description();
>>>>>> }
>>>>>> }
>>>>>>
>>>>>> While this works nicely in all Java code, WO bindings will not see the overridden description() implementations. At least not when using Java packages (it seems to work if everything is in the default package, but that doesn't help me). You get an error like:
>>>>>>
>>>>>> java.lang.IllegalAccessException: Class com.webobjects.foundation.NSKeyValueCoding$ValueAccessor$1 can not access
>>>>>> a member of class com.foo.bar.MyClass$Status$1 with modifiers "public"
>>>>>>
>>>>>> or, if using JRebel, you get
>>>>>>
>>>>>> java.lang.IllegalAccessException: Class<?> com.webobjects.foundation.NSKeyValueCoding$ValueAccessor$1 can not access
>>>>>> com.foo.bar.MyClass$Status$1!
>>>>>>
>>>>>> My current workaround:
>>>>>>
>>>>>> package com.foo.bar;
>>>>>> public class MyClass {
>>>>>> public static enum Status {
>>>>>> one { @Override String descriptionImpl() { return "eins"; } },
>>>>>> two { @Override String descriptionImpl() { return "zwei"; } };
>>>>>> abstract String descriptionImpl();
>>>>>> public String description() { return descriptionImpl(); }
>>>>>> }
>>>>>> }
>>>>>>
>>>>>> which works but is ugly. Now I'm about to implement another Enum with a lot of methods and it bothers me. Anyone an idea how to improve the situation?
>>>>>>
>>>>>> Thanks
>>>>>> Maik
>>>>>> _______________________________________________
>>>>>> Do not post admin requests to the list. They will be ignored.
>>>>>> Webobjects-dev mailing list (email@hidden)
>>>>>> Help/Unsubscribe/Update your Subscription:
>>>>>>
>>>>>> This email sent to email@hidden
>>>>>
>>>>
>>>> _______________________________________________
>>>> Do not post admin requests to the list. They will be ignored.
>>>> Webobjects-dev mailing list (email@hidden)
>>>> Help/Unsubscribe/Update your Subscription:
>>>>
>>>> This email sent to email@hidden
>>>
>>
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Webobjects-dev mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
--
Chuck Hill
Executive Managing Partner, VP Development and Technical Services
Practical WebObjects - for developers who want to increase their overall knowledge of WebObjects or who are trying to solve specific problems.
http://www.global-village.net/gvc/practical_webobjects
Global Village Consulting ranks 13th in 2012 in BIV's Top 100 Fastest Growing Companies in B.C!
Global Village Consulting ranks 44th in 25th annual PROFIT 500 ranking of Canada’s Fastest-Growing Companies by PROFIT Magazine!
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden