More progress on the distributed object stuff.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@19 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2001-06-01 19:56:13 +00:00
parent 825b36c0ff
commit 45c2dc143f
6 changed files with 666 additions and 16 deletions
@@ -0,0 +1,66 @@
//
// $Id: DEventUtil.java,v 1.1 2001/06/01 19:56:13 mdb Exp $
package com.threerings.cocktail.cher.dobj;
import java.lang.reflect.Method;
import java.util.HashMap;
/**
* A repository for distributed object event related utility functions.
*/
public class DEventUtil
{
/**
* Looks up and returns the method used to set an attribute of the
* specified name on an instance of the specified distributed object
* class.
*/
public static Method getSetter (Class clazz, String name)
throws ObjectAccessException
{
// first see if it's cached
Method setter = (Method)_setterCache.get(name);
if (setter != null) {
return setter;
}
// convert the attribute name into a setter name
String sname = setterName(name);
try {
// look up that method (we don't have the parameter types, so
// we have to do our own name matching)
Method[] methods = clazz.getMethods();
for (int i = 0; i < methods.length; i++) {
if (methods[i].getName().equals(sname)) {
setter = methods[i];
break;
}
}
} catch (SecurityException se) {
throw new ObjectAccessException("Reflection error: " + se);
}
// make sure we found a matching method
if (setter == null) {
String errmsg = "No setter for attribute '" + name + "'.";
throw new ObjectAccessException(errmsg);
}
// and cache it
_setterCache.put(name, setter);
return setter;
}
protected static String setterName (String name)
{
StringBuffer sname = new StringBuffer();
sname.append("get");
sname.append(Character.toUpperCase(name.charAt(0)));
sname.append(name.substring(1));
return sname.toString();
}
protected static HashMap _setterCache = new HashMap();
}