當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java java.lang.reflect.Proxy.getInvocationHandler()用法及代碼示例


描述

這個java.lang.reflect.Proxy.getInvocationHandler(Object proxy)方法返回指定代理實例的調用處理程序。

聲明

以下是聲明java.lang.reflect.Proxy.getInvocationHandler(Object proxy)方法。

public static InvocationHandler getInvocationHandler(Object proxy)
   throws IllegalArgumentException

參數

proxy- 要為其返回調用處理程序的代理實例。

返回

代理實例的調用處理程序。

異常

IllegalArgumentException- 如果參數不是代理實例。

示例

下麵的例子展示了 java.lang.reflect.Proxy.getInvocationHandler(Object proxy) 方法的用法。

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");   
   }
}

讓我們編譯並運行上麵的程序,這將產生以下結果 -

com.tutorialspoint.SampleInvocationHandler

相關用法


注:本文由純淨天空篩選整理自 java.lang.reflect.Proxy.getInvocationHandler() Method Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。