java method

获得反射方法
import java.lang.reflect.Method;


public class CoreObjectUtils {
	public static Method getMethod(Class<?> clazz, String name, Class<?>... parameterTypes) throws SecurityException, NoSuchMethodException {
		Method m;
		try {
			m = clazz.getDeclaredMethod(name, parameterTypes);
		} catch (SecurityException e) {
			m = null;
			throw e;
		} catch (NoSuchMethodException e) {
			m = null;
		}
		if (m == null) {
			if (clazz.equals(Object.class)) {
				throw new NoSuchMethodException("Method not found in any super class.");
			}
			return getMethod(clazz.getSuperclass(), name, parameterTypes);
		}
		return m;
	}
}


具体使用
	private IVenderInternalJclqService  verderInternalJclqService;
	private IVenderConfig config;
	private Map<String,String> paramMap;
	private String methodName;
	@Override
	protected Object execute() throws Exception {
		if(verderInternalJclqService==null||config==null||paramMap==null||methodName==null){
			throw new Exception("缺少必要的参数");
		}
		Method m=CoreObjectUtils.getMethod(verderInternalJclqService.getClass(), methodName, new Class[]{config.getClass(),paramMap.getClass()});
		return m.invoke(verderInternalJclqService, new Object[]{config,paramMap});
	}


 //第三种:推荐,尤其是容量大时
  System.out.println("通过Map.entrySet遍历key和value");
  for (Map.Entry<String, String> entry : map.entrySet()) {
   System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
  }

你可能感兴趣的:(method)