java.lang.Object
java.lang.reflect.Proxy
- All Implemented Interfaces:
- Serializable
- See Also:
- Top Examples, Source Code,
Proxy.getInvocationHandler
, Proxy.getProxyClass
, Proxy.newInstance
, Proxy.isProxyClass
, invoke
public static InvocationHandler getInvocationHandler(Object proxy)
throws IllegalArgumentException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1954]Why can't I declare a static method in an interface?
By Anonymous on 2008/02/18 15:12:03 Rate
The reason for this prohibition is a fundamental one: Java resolves static method calls at compile-time ( based on the type of the reference through which they're called ) , rather than at run time ( based on the type of referenced object ) .
To clarify, consider this code:
class A {
public static void foo ( ) { System.out.println ( "A" ) ; }
}
class B extends A {
public static void foo ( ) { System.out.println ( "B" ) ; }
}
public class StaticTest {
public static void main ( String [ ] args ) {
A ref = new B ( ) ;
ref.foo ( ) ;
}
}
It prints ``A'', the type of the reference through which foo ( ) was invoked, even though the object that ref refererences is actually a B.
This is very fundamental stuff and there is absolutely nothing we can do about it. What could it mean to invoke an static method on an object whose reference is of interface type?
By the way, the same issues explain why you can't have static abstract methods ( Jikes says ``An abstract method may not also contain the keyword "static"'', which it a bit of a roundabout way of saying it, but unambiguous enough. )
public static Class<?> getProxyClass(ClassLoader loader,
Class<?>... interfaces)
throws IllegalArgumentException
- See Also:
- NullPointerException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static Class getProxyClass(ClassLoader loader,
Class[] interfaces)
throws IllegalArgumentException
- See Also:
- NullPointerException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
protected InvocationHandler h
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static boolean isProxyClass(Class<?> cl)
- See Also:
- NullPointerException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static Object newProxyInstance(ClassLoader loader,
Class<?>[] interfaces,
InvocationHandler h)
throws IllegalArgumentException
- See Also:
- NullPointerException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[930]A sample proxy implementation
By sunfmin { at } gmail { dot } com on 2005/03/21 06:18:44 Rate
package proxy;
public class JDKProxy {
public static void main ( String [ ] args ) {
try {
Foo foo = ( Foo ) JDKTraceProxy.newInstance ( new FooImpl ( ) ) ;
foo.printFoo ( ) ;
foo.printBar ( "HelloBar" ) ;
} catch ( Exception e ) {
e.printStackTrace ( ) ;
}
}
}
package proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class JDKTraceProxy implements InvocationHandler {
private Object obj;
public JDKTraceProxy ( Object obj ) {
this.obj = obj;
}
public static Object newInstance ( Object obj ) {
Object result = Proxy.newProxyInstance ( obj.getClass ( ) .getClassLoader ( ) ,
obj.getClass ( ) .getInterfaces ( ) , new JDKTraceProxy ( obj ) ) ;
return ( result ) ;
}
public Object invoke ( Object proxy, Method method, Object [ ] args ) throws Throwable {
Object result;
try {
System.out.print ( "begin method "
+ method.getName ( ) + " ( " ) ;
for ( int i = 0; args != null && i < args.length; i++ ) {
if ( i > 0 ) System.out.print ( "," ) ;
System.out.print ( " " +
args [ i ] .toString ( ) ) ;
}
System.out.println ( " ) " ) ;
result = method.invoke ( obj, args ) ;
} catch ( InvocationTargetException e ) {
throw e.getTargetException ( ) ;
} catch ( Exception e ) {
throw new RuntimeException
( "unexpected invocation exception: " +
e.getMessage ( ) ) ;
} finally {
System.out.println ( "end method " + method.getName ( ) ) ;
}
return result;
}
}
package proxy;
public interface Foo {
public void printFoo ( ) ;
public void printBar ( String barName ) ;
}
package proxy;
public class FooImpl implements Foo {
public void printFoo ( ) {
System.out.println ( " [ FooImpl.printFoo ] " ) ;
}
public void printBar ( String barName ) {
System.out.println ( " [ FooImpl.printBar ] barName = " + barName ) ;
}
}
protected Proxy(InvocationHandler h)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples