Re: using static methods in WebObjects Builder?
Re: using static methods in WebObjects Builder?
- Subject: Re: using static methods in WebObjects Builder?
- From: Jean-François Veillette <email@hidden>
- Date: Mon, 7 Mar 2005 23:11:25 -0500
Arturo,
no, you can't directly access static methods, but if you have an object
(an instance) of a class, NSSelector will work even for static methods
on that class.
we have built bindings accessors to static methods (with args) using
NSSelector, I do not know if nsselector will work for static variables
(public constants). If not, you could look at the java introspection
api (on our 'to-do').
Here is a modified version of your code, simplified (and not tested)
assuming that nsselector would work for static variables. If
nsselector won't work for static vars, i know it will works for static
accessors ("static ... AConstant()" ) since we do use a variation of
this code to access static methods with arguments.
All you have to do is create an instance of the class, then nsselector
will let you access static methods on that class.
for example, you could, in the component where you want bindings to
access other class static's methods, trap kvc to help you write your
bindings, here we use the "@@" prefix to identify static calls.
// for example : WOString { value = @@com_cie_Member.AConstant; }
public Object valueForKeyPath(String kp) {
if(kp.startsWith("@@")) {
return invokeStatic(removeDoubleAtSign(kp)); // with some
string manipulation to remove the "@@" prefix.
}
return super.valueForKeyPath(kp);
}
// for example : kp = "com_cie_Member.AConstant";
private Object invokeStatic(String kp) {
String cll = getClass(kp); // string manipulation to get "Member" :
the class you are looking for
String me = getAccessor(kp); // string manipulation to get
"AConstant" : a method name
NSSelector sel = new NSSelector(me);
String cl = cll.replace('_', '.');
try {
Class cla = Class.forName(cl);
obj = cla.newInstance();
return sel.invoke(obj);
} catch(Exception e) {
// problem !
}
return null;
}
We are looking for a better implementation that would not require the
instantiation of an object. We have already optimized this code so
that it will cache the instances created, but we would like to get rid
of the need for instantiation ... if you do, let us know !
- jfv
Le 05-03-07, à 22:20, Arturo Pérez a écrit :
Hi folx,
Having just tried this to no avail.. Is there no way to use static
methods as bindings?
-arturo
______________________________________________________________________
Post your free ad now! http://personals.yahoo.ca
_______________________________________________
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