Can you find all classes in a package using reflection?

前端 未结 27 2282
不知归路
不知归路 2020-11-21 05:24

Is it possible to find all classes or interfaces in a given package? (Quickly looking at e.g. Package, it would seem like no.)

27条回答
  •  时光取名叫无心
    2020-11-21 05:44

    I put together a simple github project that solves this problem:

    https://github.com/ddopson/java-class-enumerator

    It should work for BOTH file-based classpaths AND for jar files.

    If you run 'make' after checking out the project it will print this out:

     Cleaning...
    rm -rf build/
     Building...
    javac -d build/classes src/pro/ddopson/ClassEnumerator.java src/test/ClassIShouldFindOne.java src/test/ClassIShouldFindTwo.java src/test/subpkg/ClassIShouldFindThree.java src/test/TestClassEnumeration.java
     Making JAR Files...
    jar cf build/ClassEnumerator_test.jar -C build/classes/ . 
    jar cf build/ClassEnumerator.jar -C build/classes/ pro
     Running Filesystem Classpath Test...
    java -classpath build/classes test.TestClassEnumeration
    ClassDiscovery: Package: 'test' becomes Resource: 'file:/Users/Dopson/work/other/java-class-enumeration/build/classes/test'
    ClassDiscovery: Reading Directory '/Users/Dopson/work/other/java-class-enumeration/build/classes/test'
    ClassDiscovery: FileName 'ClassIShouldFindOne.class'  =>  class 'test.ClassIShouldFindOne'
    ClassDiscovery: FileName 'ClassIShouldFindTwo.class'  =>  class 'test.ClassIShouldFindTwo'
    ClassDiscovery: FileName 'subpkg'  =>  class 'null'
    ClassDiscovery: Reading Directory '/Users/Dopson/work/other/java-class-enumeration/build/classes/test/subpkg'
    ClassDiscovery: FileName 'ClassIShouldFindThree.class'  =>  class 'test.subpkg.ClassIShouldFindThree'
    ClassDiscovery: FileName 'TestClassEnumeration.class'  =>  class 'test.TestClassEnumeration'
     Running JAR Classpath Test...
    java -classpath build/ClassEnumerator_test.jar  test.TestClassEnumeration
    ClassDiscovery: Package: 'test' becomes Resource: 'jar:file:/Users/Dopson/work/other/java-class-enumeration/build/ClassEnumerator_test.jar!/test'
    ClassDiscovery: Reading JAR file: '/Users/Dopson/work/other/java-class-enumeration/build/ClassEnumerator_test.jar'
    ClassDiscovery: JarEntry 'META-INF/'  =>  class 'null'
    ClassDiscovery: JarEntry 'META-INF/MANIFEST.MF'  =>  class 'null'
    ClassDiscovery: JarEntry 'pro/'  =>  class 'null'
    ClassDiscovery: JarEntry 'pro/ddopson/'  =>  class 'null'
    ClassDiscovery: JarEntry 'pro/ddopson/ClassEnumerator.class'  =>  class 'null'
    ClassDiscovery: JarEntry 'test/'  =>  class 'null'
    ClassDiscovery: JarEntry 'test/ClassIShouldFindOne.class'  =>  class 'test.ClassIShouldFindOne'
    ClassDiscovery: JarEntry 'test/ClassIShouldFindTwo.class'  =>  class 'test.ClassIShouldFindTwo'
    ClassDiscovery: JarEntry 'test/subpkg/'  =>  class 'null'
    ClassDiscovery: JarEntry 'test/subpkg/ClassIShouldFindThree.class'  =>  class 'test.subpkg.ClassIShouldFindThree'
    ClassDiscovery: JarEntry 'test/TestClassEnumeration.class'  =>  class 'test.TestClassEnumeration'
     Tests Passed. 
    

    See also my other answer

提交回复
热议问题