Re: WOB and class static variables
Re: WOB and class static variables
- Subject: Re: WOB and class static variables
- From: Jonathan Rochkind <email@hidden>
- Date: Mon, 26 Apr 2004 11:16:57 -0500
At 12:13 AM -0400 4/25/04, Arturo Pirez wrote:
Am I correct in believing that WOBuilder won't use/show class static
variables for bindings?
Well, the important thing is that key/value coding does not handle
static variables. WOBuilder also won't show them as available keys,
but this is less significant, as we all know sometimes there _are_
available keys that WOBuilder still isn't showing, even though they
are available! But in this case, class static variables/methods are
not available through key value coding.
But wait, don't despair yet. I have a trick I've used to give KVC
access to static final variables. In the class that contains the
static variable, you can over-ride handleQueryWithUnboundKey to catch
the key lookup failure, and do the lookup yourself on the static
variable.
Before the code, some warnings and notes:
1. This still won't make the key show up as available in the
WOBuilder GUI. But, if you type in the key manually, it will work at
run-time.
2. The default implementation of KVC is likely highly optimized. My
code is not, and may be less efficient than ordinary KVC lookup, as
it manually uses the java reflection API on every call. (I believe
default KVC caches some stuff).
3. The code I provide below is only for _getting_ rather than
_setting_, and is only for static _variables_ rather than _methods_.
This same technique could be extended to cover those other situations
(over-ride handleTakeValueForUnboundKey for setting; use the
appropriate java reflection API for methods). But, as always when
using static data, be careful to deal with
synchronization/multi-threading issues appropriately! As I'm just
using this for _static final_ variables (that is, constants,
essentially), I don't need to worry about this. If dealing with any
static variables that can change, you do need to worry about this,
which probably means you would always want to bind to static
_methods_ with proper synchronization logic, rather than directly to
the static variable.
And now, the quite simple code, which I put in my WOComponent that
includes static final variables I want to access through KVC:
//Invoked when key/value coding access fails. We use it here to look up the
//_static_ variable with the given name, to provide access to our static
//slotkey values through .wod bindings.
public Object handleQueryWithUnboundKey(String key) {
try {
//reflection getField works on static fields too!
Field field = getClass().getField( key );
return field.get(this);
}
catch (NoSuchFieldException e) {
//Nevermind, we'll throw the exception after all.
}
catch ( IllegalAccessException e) {
//Nevermind, we'll throw the exception after all.
}
//if we get this far, throw the key value coding failure
exception after all:
throw new NSKeyValueCoding.UnknownKeyException("Key/Value coding
failed with key " + key + " on object " + this,
this,
key);
}
----
WO in philadelphia - wanna cheesesteak with that?
Please visit webobjects.meetup.com.
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.