2013-08-12 1 views
0

반사를 사용하여 다음 코드로 Api 주석이있는 모든 클래스를 얻고 있습니다. 이클립스 모든 것이 잘 작동하고 getTypesAnnotatedWith 올바른 클래스를 반환에서주어진 패키지 외부의 클래스를 포함하는 반사 (Dropwizard + Swagger)

Reflections reflections = new Reflections(ClasspathHelper.forPackage("my.package"),new TypeAnnotationsScanner()); 
Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(Api.class); 

나는 서버를 실행합니다. 그러나 명령 줄에서 서버를 실행하면 해당 패키지 외부에 클래스가 생성됩니다. Swagger 라이브러리의 클래스.

내가 진행하는 대신 사전에 WOLK 주위

감사하고 있습니다 무엇을 찾기 위해 노력하고있다!

+0

나는이 똑같은 행동을보고 있습니다. 그것은 나를 미치게합니다! – RockMeetHardplace

답변

1

마지막으로 Java ClassLoader를 사용하여이를 해결합니다. 스파게티 코드! 죄송합니다 ..

package com.glass.core.doc; 

import java.io.File; 
import java.io.IOException; 
import java.net.URL; 
import java.util.ArrayList; 
import java.util.Collection; 
import java.util.Enumeration; 
import java.util.List; 

import com.wordnik.swagger.annotations.Api; 

public class AnnotatedClassFinder { 

    public Collection<Class> getAnnotatedClasess(Class annotation, 
      String packageName) throws ClassNotFoundException, IOException { 

     Collection<Class> allClasses = getClasses(packageName); 
     List<Class> annotatedClass = new ArrayList<Class>(); 

     for (Class aClass : allClasses) { 
      if (aClass.isAnnotationPresent(annotation)) { 
       annotatedClass.add(aClass); 
       System.out.println("!!!!!!!!!!!!!!!!!!!!!!!----------------!" 
         + aClass.getName()); 
      } 
     } 

     return annotatedClass; 
    } 

    /** 
    * Scans all classes accessible from the context class loader which belong 
    * to the given package and subpackages. 
    * 
    * @param packageName 
    *   The base package 
    * @return The classes 
    * @throws ClassNotFoundException 
    * @throws IOException 
    */ 
    private Collection<Class> getClasses(String packageName) 
      throws ClassNotFoundException, IOException { 

     ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 
     String path = packageName.replace('.', '/'); 
     System.out.println("PATH:" + path); 
     Enumeration<URL> resources = classLoader.getResources(path); 
     List<File> dirs = new ArrayList<File>(); 
     while (resources.hasMoreElements()) { 
      URL resource = resources.nextElement(); 
      dirs.add(new File(resource.getFile())); 
     } 
     List<Class> classes = new ArrayList<Class>(); 
     for (File directory : dirs) { 
      classes.addAll(findClasses(directory, packageName)); 
     } 

     return classes; 
    } 

    /** 
    * Recursive method used to find all classes in a given directory and 
    * subdirs. 
    * 
    * @param directory 
    *   The base directory 
    * @param packageName 
    *   The package name for classes found inside the base directory 
    * @return The classes 
    * @throws ClassNotFoundException 
    */ 
    private List<Class> findClasses(File directory, String packageName) 
      throws ClassNotFoundException { 
     List<Class> classes = new ArrayList<Class>(); 
     if (!directory.exists()) { 
      return classes; 
     } 
     File[] files = directory.listFiles(); 
     for (File file : files) { 
      if (file.isDirectory()) { 
       classes.addAll(findClasses(file, 
         packageName + "." + file.getName())); 
      } else if (file.getName().endsWith(".class")) { 
       classes.add(Class.forName(packageName 
         + '.' 
         + file.getName().substring(0, 
           file.getName().length() - 6))); 
      } 
     } 
     return classes; 
    } 
} 
관련 문제