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 protected]>: > 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 protected]>: >>> >>>> 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 protected]) >>>>> Help/Unsubscribe/Update your Subscription: >>>>> https://lists.apple.com/mailman/options/webobjects-dev/tim%40triptera.com.au >>>>> >>>>> This email sent to [email protected] >>>> >>> >>> _______________________________________________ >>> Do not post admin requests to the list. They will be ignored. >>> Webobjects-dev mailing list ([email protected]) >>> Help/Unsubscribe/Update your Subscription: >>> https://lists.apple.com/mailman/options/webobjects-dev/bogdan.zlatanov%40gmail.com >>> >>> This email sent to [email protected] >> >
_______________________________________________ Do not post admin requests to the list. They will be ignored. Webobjects-dev mailing list ([email protected]) Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com This email sent to [email protected]
