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

📄 jpathexecutor.java

📁 pojo的mvc框架
💻 JAVA
字号:
package xyz.frame.jpath;import java.lang.reflect.Method;import java.util.Arrays;import java.util.Collection;import org.apache.log4j.Logger;import xyz.frame.LogicRequest;import xyz.frame.annotations.Conversion;import xyz.frame.component.ComponentInstantiationException;import xyz.frame.converter.ConversionException;import xyz.frame.converter.Converter;import xyz.frame.converter.ConverterManager;import xyz.frame.introspector.FieldReadParameter;import xyz.frame.util.MethodInvocationException;import xyz.frame.util.ReflectionUtil;import xyz.frame.util.SettingException;/** * JPath setter execution for a specific logiccontext. *  * @author Guilherme Silveira */public class JPathExecutor {	private static final Logger logger = Logger.getLogger(JPathExecutor.class);	private ConverterManager converters;	private LogicRequest context;	public JPathExecutor(ConverterManager converters, LogicRequest logicRequest) {		this.converters = converters;		this.context = logicRequest;	}	/**	 * Tries to set some property in the current object. It uses the path array	 * to walk in the object graph, the matching field is called field and the	 * value is either completeValue or arrayValue	 * 	 * @param object	 *            the object to set the property	 * @param path	 *            the path to walk	 * @param completeValue	 *            the completeValue	 * @param arrayValue	 *            the array value	 * @param field	 *            the field to use	 * @throws SettingException	 *             some exception ocurred while trying to set a value	 * @throws ConversionException	 *             some convesion exception occurred	 */	public void set(Object component, String[] path, String completeValue,			String[] arrayValue, FieldReadParameter read)			throws SettingException, ConversionException {		if (path.length == 1) {			setField(component, path, completeValue, arrayValue, read);			return;		}		// moves to the first field		Object object = read.guaranteeExistence(component);		if (object == null) {			return;		}		int start = 1, len = path.length;		// navigates on the first array, one depth only		if (len != 2 && Character.isDigit(path[start].charAt(0))) {			SetDealer dealer = SetDealerFactory.getDealer(object);			try {				int arrayPosition = Integer.parseInt(path[start]);				object = dealer.resizeAndSet(object, arrayPosition, read						.mightCreate(), read.getField().getGenericType());				// calls the setter				ReflectionUtil.setField(component, read.getField(), object);				// retrieves the specified position				object = dealer.getPosition(object, arrayPosition, read						.mightCreate());				start++;			} catch (NumberFormatException e) {				throw new SettingException("Invalid array index: " + path[1]);			}		}		if (object == null) {			return;		}		try {			for (int i = start; i < path.length - 1; i++) {				Object currentObject = object;				// for each parameter, calls the getter method				Method method = ReflectionUtil.findGetter(object.getClass(),						path[i]);				// if no getter found, forget it!				if (method == null) {					return;				}				object = ReflectionUtil.invoke(object, method);				Class returnType = method.getReturnType();				if (object == null) {					if (read.mightCreate()) {						try {							// my getter returned null... i should instantiate							// it							if (isCollection(returnType)) {								object = ReflectionUtil										.instantiateCollection(returnType);							} else {								object = ReflectionUtil										.genericInstantiate(returnType);							}							// calls the setter							ReflectionUtil									.invoke(currentObject,											ReflectionUtil.findSetter(													currentObject, path[i]),											object);						} catch (ComponentInstantiationException e) {							throw new SettingException(e.getMessage(), e);						}					} else {						return;					}				}				// if the next is an array index, use it				while (i < path.length						&& Character.isDigit(path[i + 1].charAt(0))) {					try {						int arrayPosition = Integer.parseInt(path[i + 1]);						SetDealer dealer = SetDealerFactory.getDealer(object);						object = dealer.resizeAndSet(object, arrayPosition,								read.mightCreate(), method										.getGenericReturnType());						// calls the setter						ReflectionUtil.invoke(currentObject, ReflectionUtil								.findSetter(currentObject, path[i]), object);						// retrieves the specified position						object = dealer.getPosition(object, arrayPosition, read								.mightCreate());						if (object == null) {							return;						}					} catch (NumberFormatException e) {						throw new SettingException("Invalid array index: "								+ path[1]);					}					i++;				}			}			String lastPath = path[path.length - 1];			if (Character.isDigit(lastPath.charAt(0))) {				SetDealer dealer = SetDealerFactory.getDealer(object);				try {					int arrayPosition = Integer.parseInt(lastPath);					// last level... convert and set array position					Object value = convert(completeValue, arrayValue, dealer.getType(read							.getField().getGenericType()), read							.getOverridenConverter());					object = dealer.resizeAndSet(object, arrayPosition, value);					// calls the setter					ReflectionUtil.setField(component, read.getField(), object);					return;				} catch (NumberFormatException e) {					throw new SettingException("Invalid array index: "							+ path[1]);				}			}			// calls the setter for the last one			Method setter = ReflectionUtil.findSetter(object, lastPath);			if (setter == null) {				return;			}			logger.debug("ready to use parameter " + Arrays.toString(path));			Conversion annotation = setter.getAnnotation(Conversion.class);			Object result = convert(completeValue, arrayValue, setter					.getParameterTypes()[0], annotation == null ? null					: annotation.value());			ReflectionUtil.invoke(object, setter, result);		} catch (MethodInvocationException e) {			throw new SettingException(e.getMessage(), e);		} catch (IllegalArgumentException e) {			throw new SettingException(e.getMessage(), e);		} catch (SecurityException e) {			throw new SettingException(e.getMessage(), e);		} catch (ConversionException e) {			e.setCategory(path[path.length - 1]);			throw e;		}	}	private void setField(Object component, String[] path,			String completeValue, String[] arrayValue, FieldReadParameter read)			throws SettingException, ConversionException {		// just call the setter		try {			Object value = convert(completeValue, arrayValue, read.getField()					.getType(), read.getOverridenConverter());			ReflectionUtil.setField(component, read.getField(), value);		} catch (ConversionException e) {			e.setCategory(path[0]);			throw e;		}	}	/**	 * Converts a value either by its type or overriden converter	 * 	 * @param completeValue	 *            complete value	 * @param arrayValue	 *            array value	 * @param type	 *            target type	 * @param annotation	 *            annotation (may be null)	 * @return the converted value	 * @throws ConversionException	 *             something wrong occurred	 */	private Object convert(String completeValue, String[] arrayValue,			Class type, Class<? extends Converter> converter)			throws ConversionException {		return this.converters.convert(arrayValue, completeValue, type,				context, converter);	}	private boolean isCollection(Class<?> type) {		return Collection.class.isAssignableFrom(type);	}}

⌨️ 快捷键说明

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