小程序:扫描包中所有的类并获取每个类中特定的注解的值

需求:今天做项目的时候,需要获取某个包下面所有的类里面的@Resources字段的名字以及所注入的接口所对应的路径

175543_dXy0_2505383.png

要是这文件夹只有几个Action的话,我倒是可以考虑直接手动,可是 有整整70多个Action,如果我手动输入的话岂不是要花很多很多时间,而且也不保证效率,于是乎就索性写了个小程序来得到这些数据

180058_rgy3_2505383.png

具体步骤代码如下:

 一:获取指定包下所有的类

public static void scan(String packageName ,List<Class<?>> list) throws Exception{ 
//packageName为指定的目录如com.xxx.xxx
    String path="E:\\dev\\eachserver\\eachpre\\src\\main\\java\\com\\each\\http\\business\\action";
    File dir=new File(path);
    File[] files=dir.listFiles(); //获取此目录下的文件列表
    Class<?> cla=null;
    for(File f:files){
            cla=Class.forName(packageName+"."+f.getName().split("\\.")[0]);
            list.add(cla);\\class添加进list
    }
}

二:扫瞄类里面特定的注解所对应的类

public static void getAnnotion(List<Class<?>> list) throws Exception{
    System.out.println(list.size());  //打印文件数量
    Map<String,String> map = new HashMap<String,String>();
    for(Class<?> cla:list){
        Field[] field = Class.forName(cla.getName()).getDeclaredFields();\\获取class里面所声明的字段

        for (Field field1:field){
            if (field1.getAnnotation(Resource.class)!=null){ \\判断此字段是否有@Resource
                map.put(field1.getAnnotation(Resource.class).name(),field1.getType().getName());
           \\以防重复 这个地方是用map储存
            }
        }

    }
    for ( Map.Entry<String,String> entry:map.entrySet()){
//        System.out.println("<dubbo:reference group=\""+entry.getKey()+"\" id=\""+entry.getKey()+"\" interface=\""+entry.getValue()+"\" version=\"1.0\"/>");
        System.out.println(entry.getKey()+" "+entry.getValue()); \\遍历打印
    }
}

源代码:

package com.each;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhu on 2016/4/28.
 */
public class TestMain {

public static void scan(String packageName ,List<Class<?>> list) throws Exception{
    String path="E:\\dev\\eachserver\\eachpre\\src\\main\\java\\com\\each\\http\\business\\action";
    File dir=new File(path);
    File[] files=dir.listFiles();
    Class<?> cla=null;
    for(File f:files){//E:\dev\eachserver\eachpre\src\main\java\com\each\http\business\action
        if(f.isDirectory()){
            String childName=packageName+"."+f.getName();
            scan(childName, list);

        }else{
            cla=Class.forName(packageName+"."+f.getName().split("\\.")[0]);
            list.add(cla);
        }
    }
}
public static void getAnnotion(List<Class<?>> list) throws Exception{
    System.out.println(list.size());
    Map<String,String> map = new HashMap<String,String>();
    for(Class<?> cla:list){
        Field[] field = Class.forName(cla.getName()).getDeclaredFields();

        for (Field field1:field){
            if (field1.getAnnotation(Resource.class)!=null){
                map.put(field1.getAnnotation(Resource.class).name(),field1.getType().getName());
            }
        }

    }
    for ( Map.Entry<String,String> entry:map.entrySet()){
//        System.out.println("<dubbo:reference group=\""+entry.getKey()+"\" id=\""+entry.getKey()+"\" interface=\""+entry.getValue()+"\" version=\"1.0\"/>");
        System.out.println(entry.getKey()+" "+entry.getValue());
    }
}


    public static void main(String[] args) throws Exception {
        List<Class<?>> list=new ArrayList<Class<?>>();
        scan("com.each.http.business.action",list);//把这个对象的路径写入,就可以了。
        getAnnotion(list);

        }
    }


转载于:https://my.oschina.net/u/2505383/blog/668261

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值