Forgot that .java attachments don't go through. Here it is:
package pivot.wtkx.test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class BindingPrototype {
public static void main(String[] args) {
C c = new C();
Class<?> type = c.getClass();
while (type != Object.class) {
try {
Method __bindMethod =
type.getDeclaredMethod("__bind", new Class<?>[] {type});
try {
__bindMethod.invoke(null, new Object[] {c});
} catch(IllegalAccessException exception) {
throw new RuntimeException(exception);
} catch(InvocationTargetException exception) {
throw new RuntimeException(exception);
}
} catch(NoSuchMethodException exception) {
// No-op
}
type = type.getSuperclass();
}
}
}
class A {
public static void __bind(A a) {
System.out.println("A");
}
}
class B extends A {
}
class C extends B {
public static void __bind(C c) {
System.out.println("C");
}
}
On Jun 9, 2009, at 1:00 PM, Greg Brown wrote:
You are correct - it doesn't work. The Javadoc for Method#invoke()
says this:
"If the underlying method is an instance method, it is invoked using
dynamic method lookup as documented in The Java Language
Specification, Second Edition, section 15.12.4.4; in particular,
overriding based on the runtime type of the target object will occur."
However, maybe it could be static:
public static __bind(T t, WTKXSerializer serializer) { ... }
where T is the type of the class that contains an @Bind annotation
(not a generic - the actual type). It could be Object, but there's
no need for it to be since we know what type it is. Since it is a
static method of the class, it will have access to the class's
internal variables, but won't be overridden by a subclass.
On Jun 9, 2009, at 12:33 PM, Noel Grandin wrote:
Unfortunately in Java, you can't invoke a specific subclass version
of
an overridden method.
That means that either
(a) you have to trust that each method correctly invokes super.__bind
(b) we need a more complex scheme.
Something like having each class X define a method
public void __bind(X myself, WTKXSerializer serializer)
And then WTKXSerializer#bind() could walk up the class hierarchy,
and use reflection to find the __bind methods and invoke them.
But maybe someone can come up with a scheme that involves less magic?
(I like reflection, but it's best used in small doses).
On Tue, Jun 9, 2009 at 17:14, Greg Brown<[email protected]> wrote:
I think this will actually work, and won't require too many
changes. The
bind processor can add the Bindable interface to every class that
uses an
@Bind annotation. The interface would contain a single method:
public void __bind(WTKXSerializer serializer);
WTKXSerializer#bind() would check to see if the bound object
implemented
Bindable; if so, it would walk up the object's class hierarchy
looking for
subclasses that implement Bindable. When found, it would invoke
the specific
__bind() method defined by the subclass.
Sound right?