⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 propertyresolver.java

📁 Wicket一个开发Java Web应用程序框架。它使得开发web应用程序变得容易而轻松。 Wicket利用一个POJO data beans组件使得它可以与任何持久层技术相结合。
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
					else					{						getAndSetter = new FieldGetAndSetter(field);					}				}			}			else			{				field = findField(clz, exp);				getAndSetter = new MethodGetAndSet(method, MethodGetAndSet.findSetter(method, clz),					field);			}			getAndSetters.put(exp, getAndSetter);		}		return getAndSetter;	}	/**	 * @param clz	 * @param expression	 * @return introspected field	 */	private static Field findField(Class clz, String expression)	{		Field field = null;		try		{			field = clz.getField(expression);		}		catch (Exception e)		{			Class tmp = clz;			while (tmp != null && tmp != Object.class)			{				Field[] fields = tmp.getDeclaredFields();				for (int i = 0; i < fields.length; i++)				{					if (fields[i].getName().equals(expression))					{						fields[i].setAccessible(true);						return fields[i];					}				}				tmp = tmp.getSuperclass();			}			log.debug("Cannot find field " + clz + "." + expression, e);		}		return field;	}	/**	 * @param clz	 * @param expression	 * @return The method for the expression null if not found	 */	private final static Method findGetter(Class clz, String expression)	{		String name = Character.toUpperCase(expression.charAt(0)) + expression.substring(1);		Method method = null;		try		{			method = clz.getMethod("get" + name, null);		}		catch (Exception e)		{		}		if (method == null)		{			try			{				method = clz.getMethod("is" + name, null);			}			catch (Exception e)			{				log.debug("Cannot find getter " + clz + "." + expression);			}		}		return method;	}	private final static Method findMethod(Class clz, String expression)	{		if (expression.endsWith("()"))		{			expression = expression.substring(0, expression.length() - 2);		}		Method method = null;		try		{			method = clz.getMethod(expression, null);		}		catch (Exception e)		{			log.debug("Cannot find method " + clz + "." + expression, e);		}		return method;	}	/**	 * Utility class: instantiation not allowed.	 */	private PropertyResolver()	{	}	/**	 * @author jcompagner	 * 	 */	private final static class ObjectAndGetSetter	{		private final IGetAndSet getAndSetter;		private final Object value;		/**		 * @param getAndSetter		 * @param value		 */		public ObjectAndGetSetter(IGetAndSet getAndSetter, Object value)		{			this.getAndSetter = getAndSetter;			this.value = value;		}		/**		 * @param value		 * @param converter		 */		public void setValue(Object value, PropertyResolverConverter converter)		{			getAndSetter.setValue(this.value, value, converter);		}		/**		 * @return The value		 */		public Object getValue()		{			return getAndSetter.getValue(value);		}		/**		 * @return class of property value		 */		public Class getTargetClass()		{			return getAndSetter.getTargetClass();		}		/**		 * @return Field or null if no field exists for expression		 */		public Field getField()		{			return getAndSetter.getField();		}		/**		 * @return Getter method or null if no getter exists for expression		 */		public Method getGetter()		{			return getAndSetter.getGetter();		}		/**		 * @return Setter method or null if no setter exists for expression		 */		public Method getSetter()		{			return getAndSetter.getSetter();		}	}	private static interface IGetAndSet	{		/**		 * @param object		 *            The object where the value must be taken from.		 * 		 * @return The value of this property		 */		public Object getValue(final Object object);		/**		 * @return The target class of the object that as to be set.		 */		public Class getTargetClass();		/**		 * @param object		 *            The object where the new value must be set on.		 * 		 * @return The new value for the property that is set back on that object.		 */		public Object newValue(Object object);		/**		 * @param object		 * @param value		 * @param converter		 */		public void setValue(final Object object, final Object value,			PropertyResolverConverter converter);		/**		 * @return Field or null if there is no field		 */		public Field getField();		/**		 * @return Getter method or null if there is no getter		 */		public Method getGetter();		/**		 * @return Setter of null if there is no setter		 */		public Method getSetter();	}	private static abstract class AbstractGetAndSet implements IGetAndSet	{		/**		 * @see org.apache.wicket.util.lang.PropertyResolver.IGetAndSet#getField()		 */		public Field getField()		{			return null;		}		/**		 * @see org.apache.wicket.util.lang.PropertyResolver.IGetAndSet#getGetter()		 */		public Method getGetter()		{			return null;		}		/**		 * @see org.apache.wicket.util.lang.PropertyResolver.IGetAndSet#getSetter()		 */		public Method getSetter()		{			return null;		}		/**		 * @see org.apache.wicket.util.lang.PropertyResolver.IGetAndSet#getTargetClass()		 */		public Class getTargetClass()		{			return null;		}	}	private static final class MapGetSet extends AbstractGetAndSet	{		final private String key;		MapGetSet(String key)		{			this.key = key;		}		/**		 * @see org.apache.wicket.util.lang.PropertyResolver.IGetAndSet#getValue(java.lang.Object)		 */		public Object getValue(Object object)		{			return ((Map)object).get(key);		}		/**		 * @see org.apache.wicket.util.lang.PropertyResolver.IGetAndSet#setValue( Object, Object,		 *      PropertyResolverConverter)		 */		public void setValue(Object object, Object value, PropertyResolverConverter converter)		{			((Map)object).put(key, value);		}		/**		 * @see org.apache.wicket.util.lang.PropertyResolver.IGetAndSet#newValue(Object)		 */		public Object newValue(Object object)		{			// Map can't make a newValue or should it look what is more in the			// map and try to make one of the class if finds?			return null;		}	}	private static final class ListGetSet extends AbstractGetAndSet	{		final private int index;		ListGetSet(int index)		{			this.index = index;		}		/**		 * @see org.apache.wicket.util.lang.PropertyResolver.IGetAndSet#getValue(java.lang.Object)		 */		public Object getValue(Object object)		{			return ((List)object).get(index);		}		/**		 * @see org.apache.wicket.util.lang.PropertyResolver.IGetAndSet#setValue( Object, Object,		 *      PropertyResolverConverter)		 */		public void setValue(Object object, Object value, PropertyResolverConverter converter)		{			List lst = (List)object;			if (lst.size() > index)			{				lst.set(index, value);			}			else if (lst.size() == index)			{				lst.add(value);			}			else			{				while (lst.size() < index)				{					lst.add(null);				}				lst.add(value);			}		}		/**		 * @see org.apache.wicket.util.lang.PropertyResolver.IGetAndSet#newValue(Object)		 */		public Object newValue(Object object)		{			// List can't make a newValue or should it look what is more in the			// list and try to make one of the class if finds?			return null;		}	}	private static final class ArrayGetSet extends AbstractGetAndSet	{		private final int index;		private final Class clzComponentType;		ArrayGetSet(Class clzComponentType, int index)		{			this.clzComponentType = clzComponentType;			this.index = index;		}		/**		 * @see org.apache.wicket.util.lang.PropertyResolver.IGetAndSet#getValue(java.lang.Object)		 */		public Object getValue(Object object)		{			return Array.get(object, index);		}		/**		 * @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, clzComponentType);			Array.set(object, index, value);		}		/**		 * @see org.apache.wicket.util.lang.PropertyResolver.IGetAndSet#newValue(java.lang.Object)		 */		public Object newValue(Object object)		{			Object value = null;			try			{				value = clzComponentType.newInstance();				Array.set(object, index, value);			}			catch (Exception e)			{				log.warn("Cannot set new value " + value + " at index " + index +					" for array holding elements of class " + clzComponentType, e);			}			return value;		}		/**		 * @see org.apache.wicket.util.lang.PropertyResolver.IGetAndSet#getTargetClass()		 */		public Class getTargetClass()		{			return clzComponentType;		}	}	private static final class ArrayLengthGetSet extends AbstractGetAndSet	{		ArrayLengthGetSet()		{		}		/**		 * @see org.apache.wicket.util.lang.PropertyResolver.IGetAndSet#getValue(java.lang.Object)		 */		public Object getValue(Object object)		{			return new Integer(Array.getLength(object));		}		/**		 * @see org.apache.wicket.util.lang.PropertyResolver.IGetAndSet#setValue( Object, Object,		 *      PropertyResolverConverter)		 */		public void setValue(Object object, Object value, PropertyResolverConverter converter)		{			throw new WicketRuntimeException("Cant set the length on an array");		}		/**		 * @see org.apache.wicket.util.lang.PropertyResolver.IGetAndSet#newValue(java.lang.Object)		 */		public Object newValue(Object object)		{			throw new WicketRuntimeException("Cant get a new value from a length of an array");		}		/**		 * @see org.apache.wicket.util.lang.PropertyResolver.IGetAndSet#getTargetClass()		 */		public Class getTargetClass()

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -