1 17 18 package org.sape.carbon.core.util.reflection; 19 20 import java.lang.reflect.InvocationHandler ; 21 import java.lang.reflect.Method ; 22 23 24 40 public abstract class GenericProxy implements InvocationHandler { 41 42 43 private static Method hashCodeMethod; 44 45 46 private static Method equalsMethod; 47 48 49 private static Method toStringMethod; 50 51 52 static { 55 try { 56 hashCodeMethod = Object .class.getMethod("hashCode", null); 57 equalsMethod = 58 Object .class.getMethod("equals", new Class [] { Object .class }); 59 toStringMethod = Object .class.getMethod("toString", null); 60 } catch (NoSuchMethodException e) { 61 throw new NoSuchMethodError (e.getMessage()); 62 } 63 } 64 65 66 67 68 88 public Object invoke(Object proxy, Method m, Object [] args) 89 throws Throwable { 90 Class declaringClass = m.getDeclaringClass(); 91 92 if (declaringClass == Object .class) { 93 if (m.equals(hashCodeMethod)) { 94 return proxyHashCode(proxy); 95 } else if (m.equals(equalsMethod)) { 96 return proxyEquals(proxy, args[0]); 97 } else if (m.equals(toStringMethod)) { 98 return proxyToString(proxy); 99 } else { 100 throw new InternalError ( 101 "unexpected Object method dispatched: " + m); 102 } 103 } else { 104 return handleInvoke(proxy, m, args); 105 } 106 } 107 108 124 protected abstract Object handleInvoke( 125 Object proxy, Method m, Object [] args) 126 throws Throwable ; 127 128 135 protected Integer proxyHashCode(Object proxy) { 136 return new Integer (System.identityHashCode(proxy)); 137 } 138 139 149 protected Boolean proxyEquals(Object proxy, Object other) { 150 return (proxy == other ? Boolean.TRUE : Boolean.FALSE); 151 } 152 153 161 protected String proxyToString(Object proxy) { 162 return proxy.getClass().getName() + '@' + 163 Integer.toHexString(proxy.hashCode()); 164 } 165 } | Popular Tags |