Java tutorial
/* * @(#)ClassFinder.java 2011-11-17 * ?us?? 2008-2011, Inc. All rights reserved. * s.server. Use is subject to license terms. */ package com.us.reflect; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.LinkedList; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.beanutils.BeanUtils; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.ActionProxy; import com.us.dao.DaoHelper; import com.us.util.ArrayHelper; /** * ? * * @author <a href="mailto:monlyu.hong@gmail.com">monlyu</a> * @version --0.0.0--, 2011-11-17 * @since JDK6.0 */ public class ClassFinder { /** * before * @param invocation */ public static void beforeAction(ActionInvocation invc, String actionPath) throws Exception { ActionInvocation invocation = (ActionInvocation) invc; final ActionProxy proxy = invocation.getProxy(); Class<?> classFromPath = findClass(actionPath);// ?Class Class<?> targetClass = classFromPath == null ? proxy.getAction().getClass() : classFromPath; String methodName = findMethodName(actionPath);// ??? checkExist(sessionKey) String[] paramers = findParameters(actionPath); Class<?>[] paramersType = findParameterTypes(targetClass, paramers); Method method = targetClass.getDeclaredMethod(methodName, paramersType); Object runner = targetClass.newInstance(); Field[] allFiled = targetClass.getDeclaredFields(); for (Field field : allFiled) { String beanName = field.getName(); Object bean = DaoHelper.getBeanById(beanName); if (bean != null) { BeanUtils.setProperty(runner, beanName, bean); } } method.invoke(runner, getRuntimeArgs(invocation, paramers)); } private static Object[] getRuntimeArgs(ActionInvocation invocation, String[] paramers) { List<Object> runtime = new LinkedList<Object>(); HttpServletRequest request = ServletActionContext.getRequest();// HttpServletRequest for (String param : paramers) { char index0 = param.charAt(0); String parname = param.substring(1); if (index0 == '#') { // Session ?? runtime.add(request.getSession().getAttribute(parname)); } else if (index0 == '$') { runtime.add(request.getAttribute(parname)); } else if (index0 == '@' && parname.equals("request")) { runtime.add(request); } else if (index0 == '@' && parname.equals("response")) { runtime.add(ServletActionContext.getResponse()); } else { runtime.add(DaoHelper.getBeanById(param)); } } return runtime.toArray(new Object[] {}); } /** * ?? * * @param path * @return * @throws ClassNotFoundException */ private static Class<?> findClass(String path) throws ClassNotFoundException { int methodIndex = path.indexOf("("); if (methodIndex != -1) { int dot = path.lastIndexOf(".", methodIndex); if (dot == -1) return null; path = path.substring(0, dot); } return Class.forName(path); } /** * ?? <br/> * com.us.interfaces.MethodInvoke.showMessage(name) showMessage * * @param path * @return ?? */ private static String findMethodName(String path) { int methodIndex = path.indexOf("("); if (methodIndex == -1) throw new IllegalArgumentException("Not Found Mthod Flag \"(\""); int lastDot = path.lastIndexOf(".", methodIndex); return path.substring(lastDot + 1, methodIndex); } /** * ?? * * @param path * @return */ private static String[] findParameters(String path) { int methodStart = path.indexOf("("); if (methodStart == -1) { return ArrayHelper.make(); } int methodEnd = path.indexOf(")"); String list = path.substring(methodStart + 1, methodEnd); if (list.trim().equals("")) { return ArrayHelper.make(); } List<String> plist = new LinkedList<String>(); for (String param : list.split(",")) { if (param.trim().equals("")) throw new IllegalArgumentException("Parameters error"); plist.add(param.trim()); } return plist.toArray(new String[] {}); } /** * ? * * @param container * @param parnames ? * @return * @throws SecurityException * @throws NoSuchFieldException */ private static Class<?>[] findParameterTypes(Class<?> container, String[] parnames) throws SecurityException, NoSuchFieldException { List<Class<?>> parames = new LinkedList<Class<?>>(); for (String param : parnames) { char index0 = param.charAt(0); String filed = param.substring(1); if (index0 == '#') { parames.add(Object.class); } else if (index0 == '@' && filed.equals("request")) { parames.add(HttpServletRequest.class); } else if (index0 == '@' && filed.equals("response")) { parames.add(HttpServletResponse.class); } else if (index0 == '$') { parames.add(container.getDeclaredField(filed).getType()); } else { parames.add(container.getDeclaredField(param).getType()); } } return parames.toArray(new Class<?>[] {}); } public static void main(String[] args) { // System.out.println(StringUtil.join(",", findParameters("checkExist(sessionKey)"))); } }