2011-08-12 2 views
4

나는 내 자신의 주석을 위해 자바 패키지를 살펴볼 수있는 쉬운 방법을 찾고있다.주석 스캐너 패키지에 있음

내가하려는 것은 일부 수업에 내 자신의 특수 효과를 표시하는 것입니다. 매분마다 스캐너는이 주석을 위해 일부 패키지를 스캔해야합니다.

는 scanotation 항아리와 나는이 클래스 (FileWatcher.class가) 주석이있는 경우

URL url = ClasspathUrlFinder.findClassBase(FileWatcher.class); 

하고 AnnotationDB와 내가 확인할 수 있다고했습니다. 이제는 그들을 모두 스캔하기 위해 클래스 목록이 필요합니다.

일부 기사를 읽고 대부분의 사람들은 클래스 로더가 외부 jar 파일을로드 할 수 있기 때문에 가능하지 않다고 말했다.

내가 가지고있는 다른 옵션은 해당 주석을 포함하는 모든 EJB 프로젝트를 서브 디렉토리에 복사하고 모든 jar 파일을 스캔하는 것입니다. 나는 그것이 효과가있을 것이라고 생각하지만, 더 좋은 방법은 없을까요?

많은 인사, Hauke ​​

답변

1

감사합니다. 그것은 많은 도움이되었고 매우 쉽게 작동합니다. 누군가가 필요로하는 경우 :

 String packagename = "de.whatever"; 

     final Reflections reflections = new Reflections(packagename); 

     Set<Class<? extends CargoServiceListenerInterface>> subTypes = reflections.getSubTypesOf(CargoServiceListenerInterface.class); 

     for (Class<? extends CargoServiceListenerInterface> class1 : subTypes) { 
     System.out.println("Class : " + class1.getName()); 
     } 

getSubtypes 대신 annotation 메소드를 호출 할 수도 있습니다.

당신이 (종속성, 간단한 API, 15킬로바이트 jar 파일) 및 매우 빠른 솔루션 정말 경량을하지 않으 받는다는는 pom.xml 파일

<repositories> 
    <repository> 
     <id>reflections-repo</id> 
     <name>Reflections Maven2 Repository</name> 
     <url>http://reflections.googlecode.com/svn/repo</url> 
    </repository> 
</repositories> 

    <dependency> 
    <groupId>org.reflections</groupId> 
    <artifactId>reflections</artifactId> 
    <version>0.9.5</version> 
</dependency>