📄 jrempropertyinvoker.java
字号:
/* * Copyright (C) 2007 Filippo Di Vattimo - See LICENSE * */package fildiv.jremcntl.server.core;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.lang.reflect.Modifier;import fildiv.jremcntl.common.core.JRemRuntimeException;public class JRemPropertyInvoker { public void invokeSet(String propertyName, Object object, Object value) { Method m = null; try { String setterName = buildPropertyName(propertyName, "set"); Class clazz = object.getClass(); m = clazz.getMethod(setterName, new Class[] { getPrimitive(value .getClass()) }); } catch (NoSuchMethodException e) { String msg = buildErrorMsg(object.getClass(), propertyName, e); throw new JRemRuntimeException(msg, e); } int mod = m.getModifiers(); if (!Modifier.isPublic(mod)) return; invokeSet(m, object, value); } public Object invokeGet(String propertyName, Object object) { // Try with get try { return invokeGet(propertyName, object, "get"); } catch(Exception e) { // Ignore } // Try with get return invokeGet(propertyName, object, "is"); } protected Object invokeGet(String propertyName, Object object, String getMethodPrefix) { Method m = null; try { String getterName = buildPropertyName(propertyName, getMethodPrefix); Class clazz = object.getClass(); m = clazz.getMethod(getterName, new Class[] {} ); } catch (NoSuchMethodException e) { String msg = buildErrorMsg(object.getClass(), propertyName, e); throw new JRemRuntimeException(msg, e); } int mod = m.getModifiers(); if (!Modifier.isPublic(mod)) throw new IllegalAccessError(); return invokeGet(m, object); } private Object invokeGet(Method m, Object object) { assert m != null; try { return m.invoke(object , new Object[] { }); } catch (IllegalAccessException e) { String msg = buildErrorMsg(object.getClass(), m.getName(), e); throw new JRemRuntimeException(msg, e); } catch (InvocationTargetException e) { String msg = buildErrorMsg(object.getClass(), m.getName(), e); throw new JRemRuntimeException(msg, e); } } private void invokeSet(Method m, Object object, Object value) { assert m != null; try { m.invoke(object, new Object[] { value }); } catch (IllegalAccessException e) { String msg = buildErrorMsg(object.getClass(), m.getName(), e); throw new JRemRuntimeException(msg, e); } catch (InvocationTargetException e) { String msg = buildErrorMsg(object.getClass(), m.getName(), e); throw new JRemRuntimeException(msg, e); } } private String buildPropertyName(String propertyName, String type) { StringBuffer sb = new StringBuffer(type); sb.append(propertyName.substring(0, 1).toUpperCase()); sb.append(propertyName.substring(1)); return sb.toString(); } private Class getPrimitive(Class clazz) { if (clazz == Integer.class) return int.class; if (clazz == Short.class) return short.class; if (clazz == Boolean.class) return boolean.class; return clazz; } private String buildErrorMsg(Class clazz, String propertyName, Exception e) { String msg = "Error invoking " + propertyName + " on " + clazz.getName() + " has get/is/" + propertyName + " been defined ?"; return msg; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -