-
Enhancement
-
Resolution: Duplicate
-
P4
-
None
-
1.2.1
-
generic
-
generic
Name: vi73552 Date: 04/14/99
I would like to propose a special case of reflection API when the method name and signature are known at compile time. Suppose there is interface myint that has method void mymethod() and class myclass that has the same method but doesn't implement the interface. Now lets suppose we introduced a new method in java.lang.Class, boolean addInterface(Class cls). This method would verify that the class implements all the methods of the interface. If true, it would modify the class image in memory to actually implement the interface. If not, the negative result could also be cached to allow faster return in future. Now I can write the code like this:
void myInvoke(Object o) {
if (o instanceof myint
|| o.getClass().addInterface(myint.class))
((myint)o).mymethod();
else
// Do nothing or maybe provide default implementation
}
After the first successful invocation with a given class, o instanceof myint will return true and mymethod can be invoked with just several bytecode instuctions. By the way, in this case returning a boolean value is really better than throwing an exception because the object not implementing the interface can be the most common case.
This would be very useful in frameworks like AWT, when a single class could usefully implement a hundred of different callbacks Certainly adapter and inner classes solve part of the problem, but this approach will introduce a lot less clutter. In addition, AWT framework could choose, for example, not to generate mouseDown event if only mouseClicked is implemented.
(Review ID: 56940)
======================================================================
I would like to propose a special case of reflection API when the method name and signature are known at compile time. Suppose there is interface myint that has method void mymethod() and class myclass that has the same method but doesn't implement the interface. Now lets suppose we introduced a new method in java.lang.Class, boolean addInterface(Class cls). This method would verify that the class implements all the methods of the interface. If true, it would modify the class image in memory to actually implement the interface. If not, the negative result could also be cached to allow faster return in future. Now I can write the code like this:
void myInvoke(Object o) {
if (o instanceof myint
|| o.getClass().addInterface(myint.class))
((myint)o).mymethod();
else
// Do nothing or maybe provide default implementation
}
After the first successful invocation with a given class, o instanceof myint will return true and mymethod can be invoked with just several bytecode instuctions. By the way, in this case returning a boolean value is really better than throwing an exception because the object not implementing the interface can be the most common case.
This would be very useful in frameworks like AWT, when a single class could usefully implement a hundred of different callbacks Certainly adapter and inner classes solve part of the problem, but this approach will introduce a lot less clutter. In addition, AWT framework could choose, for example, not to generate mouseDown event if only mouseClicked is implemented.
(Review ID: 56940)
======================================================================
- duplicates
-
JDK-4191243 Method forwarding - Subclassing without subtyping
-
- Closed
-