001package headfirst.proxy.javaproxy; 002 003import java.lang.reflect.InvocationHandler; 004import java.lang.reflect.InvocationTargetException; 005import java.lang.reflect.Method; 006 007public class OwnerInvocationHandler implements InvocationHandler { 008 PersonBean person; 009 010 public OwnerInvocationHandler(PersonBean person) { 011 this.person = person; 012 } 013 014 public Object invoke(Object proxy, Method method, Object[] args) 015 throws IllegalAccessException { 016 017 try { 018 if (method.getName().startsWith("get")) { 019 return method.invoke(person, args); 020 } else if (method.getName().equals("setHotOrNotRating")) { 021 throw new IllegalAccessException(); 022 } else if (method.getName().startsWith("set")) { 023 return method.invoke(person, args); 024 } 025 } catch (InvocationTargetException e) { 026 e.printStackTrace(); 027 } 028 return null; 029 } 030}