1 23 24 29 30 package com.sun.appserv.management.j2ee.statistics; 31 32 import java.io.Serializable ; 33 34 import java.lang.reflect.InvocationHandler ; 35 import java.lang.reflect.Method ; 36 37 import com.sun.appserv.management.util.jmx.JMXUtil; 38 import com.sun.appserv.management.util.misc.StringUtil; 39 40 44 public abstract class GetterInvocationHandler<T> implements InvocationHandler ,Serializable 45 { 46 static final long serialVersionUID = 7293181901362984709L; 47 48 50 public 51 GetterInvocationHandler() 52 { 53 } 54 55 protected abstract T getValue( String name ); 56 protected abstract boolean containsValue( String name ); 57 58 60 public Object 61 invoke( 62 Object myProxy, 63 Method method, 64 Object [] args ) 65 throws java.lang.Throwable 66 { 67 Object result = null; 68 final String methodName = method.getName(); 69 final int numArgs = args == null ? 0 : args.length; 70 71 if ( numArgs == 0 && JMXUtil.isGetter( method ) ) 72 { 73 final String name = StringUtil.stripPrefix( methodName, JMXUtil.GET ); 74 75 result = getValue( name ); 76 if ( result == null && ! containsValue( name ) ) 77 { 78 throw new NoSuchMethodException ( methodName ); 79 } 80 } 81 else if ( method.getName().equals( "equals" ) && 82 numArgs == 1 ) 83 { 84 result = new Boolean ( equals( args[ 0 ] ) ); 85 } 86 else if ( numArgs == 0 && method.getName().equals( "toString" ) && 87 method.getReturnType() == String .class ) 88 { 89 result = this.toString(); 90 } 91 else if ( numArgs == 0 && method.getName().equals( "hashCode" ) && 92 method.getReturnType() == int.class ) 93 { 94 result = new Integer ( this.hashCode() ); 95 } 96 else 97 { 98 throw new IllegalArgumentException ( methodName ); 99 } 100 101 return( result ); 102 } 103 104 } 105 106 107 108 109 110 | Popular Tags |