引言


HTTP状态码),由于这些东西前台写一份后台写一份会导致后期维护起来麻烦,所以就设置页面登录的时候发一个cookie,里面包含响应码的信息。


正文


想多写文字解释这个程序,想来想去还是放到代码里面吧。我个人平时也不是很喜欢看文字,看代码更高效一点感觉。



/**
     * 扫描制定目录下所有的.class文件,得到全路径名
     * @param basePack 为.java的路径
     * @return
     * @throws ClassNotFoundException
     */
    private static List<String> searchClass(String basePack) throws ClassNotFoundException
    {
        List<String> classPaths = new ArrayList<String>();
        // 包名转换为路径,首先得到项目的classpath
        String projectClasspath = ConstsUtils.class.getResource("/").getPath();
        // 包名basPach转换为路径名
        basePack = basePack.replace(".", File.separator);
        // 合并classpath和basePack
        String searchPath = projectClasspath + basePack;
        doPath(projectClasspath, new File(searchPath), classPaths);

        return classPaths;
    }

    /**
     * 该方法会得到所有的类,将类的绝对路径写入到classPaths中
     * @param file
     */
    private static void doPath(String projectClasspath, File file, List<String> classPaths)
    {
        if (file.isDirectory())
        {// 文件夹
         // 文件夹就递归
            File[] files = file.listFiles();
            for (File fileTemp : files)
            {
                doPath(projectClasspath, fileTemp, classPaths);
            }
        }
        else
        {// 标准文件
         // 标准文件判断是否是class文件
            if (file.getName().endsWith(".class"))
            {
                // 如果是class文件的绝对路径转换为全类名放入集合中。
                classPaths
                        .add(file.getPath().replace(projectClasspath.replace("/", "\\").replaceFirst("\\\\", ""), "").replace("\\", ".").replace(".class", ""));
            }
        }
    }

根据下面两个方法,就可以将制定包下文件的全路径名,加载到集合中。具体思想就是,拿到项目运行的classpath然后拼接到传入的basePack查找此目录下所有类已经加载的class文件,然后根据文件得到路径添加到集合中返回。




package com.huihui.consts.orders;

public enum OrderStatus
{
	DAIFU((short)1, "待付款"),
	YIFU((short)2, "已付款"),
	WEIFA((short)3, "未发货"),
	YIFA((short)4, "已发货"),
	SUCCESS((short)5, "交易成功"),
	CANCEL((short)6, "交易取消"),
	SUCCESS_SHOP_CHANGE((short)7, "交易成功-店铺变更"),
	CANCEL_SHOP_CHANGE((short)8, "交易取消-店铺变更");
	
	private short code;

	private String name;

	private OrderStatus(short code, String name)
	{
		this.code = code;
		this.name = name;
	}

	public short getCode()
	{
		return this.code;
	}

	public String getName()
	{
		return this.name;
	}
}




/**
     * 通过类的全路径名反射得到已经定义的属性的值
     * @param totalConsts 返回的map
     * @param searchClass 类的全路径名的集合
     * @throws ClassNotFoundException
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     * @throws InvocationTargetException
     */
    private static void classPathsByData(Map<String,Object> totalConsts,List<String> searchClass)
            throws ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
    {
        Map<String, Object> map = null;
        ConstsUtils consts = null;
        // 循环每一个路径
        for (String className : searchClass)
        {
            // 得到Class对象,通过forName会执行static语句,枚举定义中的变量会自动加载
            Class<?> forName = Class.forName(className);
            // 得到方法名的集合
            Method[] methods = forName.getDeclaredMethods();
            if (forName.isEnum())
            {// 是枚举类
                map = new HashMap<>();
                consts = new ConstsUtils();
                // 返回枚举类中所有的元素
                List<?> list = Arrays.asList(forName.getEnumConstants());
                // 依次迭代
                for (Object enu : list)
                {
                    ConstsUtils.Status status = consts.new Status();
                    for (Method method : methods)
                    {
                        if (method.getName().startsWith("getName"))
                        {
                            status.setName((String) method.invoke(enu));
                        }
                        if (method.getName().startsWith("getCode"))
                        {
                            status.setCode((Short) method.invoke(enu));
                        }
                    }
                    map.put(enu.toString(), status);
                }
                // 通过全路径名切割字符串变成类名
                totalConsts.put(className.substring(className.lastIndexOf(".") + 1,className.length()),map);
            }
        }
    }