How to find annotated methods in a given package?

前端 未结 3 1040
再見小時候
再見小時候 2020-11-28 23:25

I have a simple marker annotation for methods (similar to the first example in Item 35 in Effective Java (2nd ed)):

/**
 * Marker annotation for met         


        
3条回答
  •  一向
    一向 (楼主)
    2020-11-29 00:06

    You should probably take a look at the open source Reflections library. With it you can easily achieve what you want with few lines of code:

    Reflections reflections = new Reflections( 
        new ConfigurationBuilder().setUrls( 
        ClasspathHelper.forPackage( "com.acme.installer" ) ).setScanners(
        new MethodAnnotationsScanner() ) );
    Set methods = reflections.getMethodsAnnotatedWith(InstallerMethod.class);
    

提交回复
热议问题