📄 propertyresolver.java
字号:
{ return int.class; } } private static final class ArrayPropertyGetSet extends AbstractGetAndSet { final private Integer index; final private Method getMethod; private Method setMethod; ArrayPropertyGetSet(Method method, int index) { this.index = new Integer(index); getMethod = method; } private final static Method findSetter(Method getMethod, Class clz) { String name = getMethod.getName(); name = "set" + name.substring(3); try { return clz.getMethod(name, new Class[] { int.class, getMethod.getReturnType() }); } catch (Exception e) { log.debug("Cannot find setter method corresponding to " + getMethod, e); } return null; } /** * @see org.apache.wicket.util.lang.PropertyResolver.IGetAndSet#getValue(java.lang.Object) */ public Object getValue(Object object) { Object ret = null; try { ret = getMethod.invoke(object, new Object[] { index }); } catch (InvocationTargetException ex) { throw new WicketRuntimeException("Error calling index property method: " + getMethod + " on object: " + object, ex.getCause()); } catch (Exception ex) { throw new WicketRuntimeException("Error calling index property method: " + getMethod + " on object: " + object, ex); } return ret; } /** * @see org.apache.wicket.util.lang.PropertyResolver.IGetAndSet#setValue( Object, Object, * PropertyResolverConverter) */ public void setValue(Object object, Object value, PropertyResolverConverter converter) { if (setMethod == null) { setMethod = findSetter(getMethod, object.getClass()); } if (setMethod != null) { Object converted = converter.convert(value, getMethod.getReturnType()); if (converted == null && value != null) { throw new ConversionException("Can't convert value: " + value + " to class: " + getMethod.getReturnType() + " for setting it on " + object); } try { setMethod.invoke(object, new Object[] { index, converted }); } catch (InvocationTargetException ex) { throw new WicketRuntimeException("Error index property calling method: " + setMethod + " on object: " + object, ex.getCause()); } catch (Exception ex) { throw new WicketRuntimeException("Error index property calling method: " + setMethod + " on object: " + object, ex); } } else { throw new WicketRuntimeException("no set method defined for value: " + value + " on object: " + object); } } /** * @see org.apache.wicket.util.lang.PropertyResolver.IGetAndSet#getTargetClass() */ public Class getTargetClass() { return getMethod.getReturnType(); } /** * @see org.apache.wicket.util.lang.PropertyResolver.IGetAndSet#newValue(java.lang.Object) */ public Object newValue(Object object) { if (setMethod == null) { setMethod = findSetter(getMethod, object.getClass()); } if (setMethod == null) { log.warn("Null setMethod"); return null; } Class clz = getMethod.getReturnType(); Object value = null; try { value = clz.newInstance(); setMethod.invoke(object, new Object[] { index, value }); } catch (Exception e) { log.warn("Cannot set new value " + value + " at index " + index, e); } return value; } } private static final class MethodGetAndSet extends AbstractGetAndSet { private final Method getMethod; private final Method setMethod; private final Field field; MethodGetAndSet(Method getMethod, Method setMethod, Field field) { this.getMethod = getMethod; this.getMethod.setAccessible(true); this.field = field; this.setMethod = setMethod; } /** * @see org.apache.wicket.util.lang.PropertyResolver.IGetAndSet#getValue(java.lang.Object) */ public final Object getValue(final Object object) { Object ret = null; try { ret = getMethod.invoke(object, null); } catch (InvocationTargetException ex) { throw new WicketRuntimeException("Error calling method: " + getMethod + " on object: " + object, ex.getCause()); } catch (Exception ex) { throw new WicketRuntimeException("Error calling method: " + getMethod + " on object: " + object, ex); } return ret; } /** * @param object * @param value * @param converter */ public final void setValue(final Object object, final Object value, PropertyResolverConverter converter) { if (setMethod != null) { Object converted = converter.convert(value, getMethod.getReturnType()); if (converted == null) { if (value != null) { throw new ConversionException("Can't convert value: " + value + " to class: " + getMethod.getReturnType() + " for setting it on " + object); } else if (getMethod.getReturnType().isPrimitive()) { throw new ConversionException( "Can't convert null value to a primitive class: " + getMethod.getReturnType() + " for setting it on " + object); } } try { setMethod.invoke(object, new Object[] { converted }); } catch (InvocationTargetException ex) { throw new WicketRuntimeException("Error calling method: " + setMethod + " on object: " + object, ex.getCause()); } catch (Exception ex) { throw new WicketRuntimeException("Error calling method: " + setMethod + " on object: " + object, ex); } } else { throw new WicketRuntimeException("no set method defined for value: " + value + " on object: " + object); } } private final static Method findSetter(Method getMethod, Class clz) { String name = getMethod.getName(); if (name.startsWith("get")) { name = "set" + name.substring(3); } else { name = "set" + name.substring(2); } try { Method method = clz.getMethod(name, new Class[] { getMethod.getReturnType() }); if (method != null) { method.setAccessible(true); } return method; } catch (NoSuchMethodException e) { Method[] methods = clz.getMethods(); for (int i = 0; i < methods.length; i++) { if (methods[i].getName().equals(name)) { Class[] parameterTypes = methods[i].getParameterTypes(); if (parameterTypes.length == 1) { if (parameterTypes[0].isAssignableFrom(getMethod.getReturnType())) { return methods[i]; } } } } log.debug("Cannot find setter corresponding to " + getMethod, e); } catch (Exception e) { log.debug("Cannot find setter corresponding to " + getMethod, e); } return null; } /** * @see org.apache.wicket.util.lang.PropertyResolver.IGetAndSet#newValue(java.lang.Object) */ public Object newValue(Object object) { if (setMethod == null) { log.warn("Null setMethod"); return null; } Class clz = getMethod.getReturnType(); Object value = null; try { value = clz.newInstance(); setMethod.invoke(object, new Object[] { value }); } catch (Exception e) { log.warn("Cannot set new value " + value, e); } return value; } /** * @see org.apache.wicket.util.lang.PropertyResolver.IGetAndSet#getTargetClass() */ public Class getTargetClass() { return getMethod.getReturnType(); } /** * @see org.apache.wicket.util.lang.PropertyResolver.AbstractGetAndSet#getGetter() */ public Method getGetter() { return getMethod; } /** * @see org.apache.wicket.util.lang.PropertyResolver.AbstractGetAndSet#getSetter() */ public Method getSetter() { return setMethod; } /** * @see org.apache.wicket.util.lang.PropertyResolver.AbstractGetAndSet#getField() */ public Field getField() { return field; } } /** * @author jcompagner */ private static class FieldGetAndSetter extends AbstractGetAndSet { private final Field field; /** * Construct. * * @param field */ public FieldGetAndSetter(Field field) { super(); this.field = field; this.field.setAccessible(true); } /** * @see org.apache.wicket.util.lang.PropertyResolver.IGetAndSet#getValue(java.lang.Object) */ public Object getValue(Object object) { try { return field.get(object); } catch (Exception ex) { throw new WicketRuntimeException("Error getting field value of field " + field + " from object " + object, ex); } } /** * @see org.apache.wicket.util.lang.PropertyResolver.IGetAndSet#newValue(java.lang.Object) */ public Object newValue(Object object) { Class clz = field.getType(); Object value = null; try { value = clz.newInstance(); field.set(object, value); } catch (Exception e) { log.warn("Cannot set field " + field + " to " + value, e); } return value; } /** * @see org.apache.wicket.util.lang.PropertyResolver.IGetAndSet#setValue( Object, Object, * PropertyResolverConverter) */ public void setValue(Object object, Object value, PropertyResolverConverter converter) { value = converter.convert(value, field.getType()); try { field.set(object, value); } catch (Exception ex) { throw new WicketRuntimeException("Error setting field value of field " + field + " on object " + object + ", value " + value, ex); } } /** * @see org.apache.wicket.util.lang.PropertyResolver.IGetAndSet#getTargetClass() */ public Class getTargetClass() { return field.getType(); } public Field getField() { return field; } } private static Map getClassesToGetAndSetters() { Object key = null; if (Application.exists()) { key = Application.get(); } else { key = PropertyResolver.class; } Map result = (Map)applicationToClassesToGetAndSetters.get(key); if (result == null) { // Don't synchronize this - Doesn't matter if we create two of them, // as it's only a cache and the first will go out of scope and get // GC'ed. applicationToClassesToGetAndSetters.put(key, result = new ConcurrentHashMap(64)); } return result; } /** * Clean up cache for this app. * * @param application */ public static void destroy(Application application) { applicationToClassesToGetAndSetters.remove(application); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -