- java.lang.reflect - Home
- java.lang.reflect - AccessibleObject
- java.lang.reflect - Array
- java.lang.reflect - Constructor<T>
- java.lang.reflect - Field
- java.lang.reflect - Method
- java.lang.reflect - Modifier
- java.lang.reflect - Proxy
java.lang.reflect.Proxy.getInvocationHandler() Method Example
Description
The java.lang.reflect.Proxy.getInvocationHandler(Object proxy) method returns the invocation handler for the specified proxy instance.
Declaration
Following is the declaration for java.lang.reflect.Proxy.getInvocationHandler(Object proxy) method.
public static InvocationHandler getInvocationHandler(Object proxy) throws IllegalArgumentException
Parameters
proxy − the proxy instance to return the invocation handler for.
Returns
the invocation handler for the proxy instance.
Exceptions
IllegalArgumentException − if the argument is not a proxy instance.
Example
The following example shows the usage of java.lang.reflect.Proxy.getInvocationHandler(Object proxy) method.
package com.tutorialspoint;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class ProxyDemo {
public static void main(String[] args) throws IllegalArgumentException {
InvocationHandler handler = new SampleInvocationHandler() ;
SampleInterface proxy = (SampleInterface) Proxy.newProxyInstance(
SampleInterface.class.getClassLoader(),
new Class[] { SampleInterface.class },
handler);
Class invocationHandler = Proxy.getInvocationHandler(proxy).getClass();
System.out.println(invocationHandler.getName());
}
}
class SampleInvocationHandler implements InvocationHandler {
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("Welcome to TutorialsPoint");
return null;
}
}
interface SampleInterface {
void showMessage();
}
class SampleClass implements SampleInterface {
public void showMessage(){
System.out.println("Hello World");
}
}
Let us compile and run the above program, this will produce the following result −
com.tutorialspoint.SampleInvocationHandler
java_reflect_proxy.htm
Advertisements