Last active
February 15, 2024 04:25
-
-
Save mazrean/dda0b20d1eb72ec1f165912ae8d0a365 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@GrabConfig(systemClassLoader=true) | |
@Grab(group='info.picocli', module='picocli', version='4.7.5') | |
@Grab(group='io.github.classgraph', module='classgraph', version='4.8.90') | |
import groovy.cli.picocli.CliBuilder | |
import io.github.classgraph.ClassGraph | |
import io.github.classgraph.ScanResult | |
import java.lang.reflect.Modifier | |
def cli = new CliBuilder(name:'MethodList', header:'List all public methods in the specified classpath') | |
cli.pkg(longOpt:'package', type:List, args:'*', valueSeparator:',', 'Packages to scan') | |
// 標準ライブラリを対象とする時、通常のクラスパスではSecurityExceptionが発生するのを回避ためのオプション | |
cli.cp(longOpt:'classpath', type:String, args:1, 'Classpath to scan') | |
def options = cli.parse(args) | |
ClassGraph classGraph = new ClassGraph() | |
if (options.cp) { | |
println "Scanning classpath: ${options.cp}" | |
classGraph.overrideClasspath(options.cp) | |
} | |
if (options.pkgs) { | |
println "Scanning packages: ${options.pkgs}" | |
classGraph.acceptPackages(*options.pkgs) | |
} | |
ScanResult scanResult = classGraph | |
.enableAllInfo() | |
.scan() | |
try { | |
scanResult.getAllClasses() | |
.filter { it.isPublic() } | |
.each { classInfo -> | |
println "Class: ${classInfo.name}" | |
classInfo.getMethodInfo() | |
.filter { Modifier.isPublic(it.getModifiers()) || Modifier.isProtected(it.getModifiers()) } | |
.each { | |
println " - Method: ${it.name}" | |
} | |
} | |
} finally { | |
scanResult.close() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment