66import io .github .lukehutch .fastclasspathscanner .FastClasspathScanner ;
77import io .github .lukehutch .fastclasspathscanner .scanner .ScanResult ;
88import java .lang .reflect .*;
9+ import java .net .URLClassLoader ;
910import java .util .*;
1011import java .util .regex .Matcher ;
1112import java .util .regex .Pattern ;
@@ -31,20 +32,20 @@ public static Input from(Type... types) {
3132 return new Input (sourceTypes );
3233 }
3334
34- public static Input fromClassNamesAndJaxrsApplication (List <String > classNames , List <String > classNamePatterns , String jaxrsApplicationClassName , boolean automaticJaxrsApplication , Predicate <String > isClassNameExcluded , ClassLoader classLoader ) {
35+ public static Input fromClassNamesAndJaxrsApplication (List <String > classNames , List <String > classNamePatterns , String jaxrsApplicationClassName , boolean automaticJaxrsApplication , Predicate <String > isClassNameExcluded , URLClassLoader classLoader , boolean debug ) {
3536 final ClassLoader originalContextClassLoader = Thread .currentThread ().getContextClassLoader ();
3637 try {
3738 Thread .currentThread ().setContextClassLoader (classLoader );
38- final ClasspathScanner classpathScanner = new ClasspathScanner ();
39+ final ClasspathScanner classpathScanner = new ClasspathScanner (classLoader , debug );
3940 final List <SourceType <Type >> types = new ArrayList <>();
4041 if (classNames != null ) {
41- types .addAll (fromClassNames (classNames ). getSourceTypes () );
42+ types .addAll (fromClassNames (classNames ));
4243 }
4344 if (classNamePatterns != null ) {
44- types .addAll (fromClassNamePatterns (classpathScanner .scanClasspath (), classNamePatterns ). getSourceTypes () );
45+ types .addAll (fromClassNamePatterns (classpathScanner .scanClasspath (), classNamePatterns ));
4546 }
4647 if (jaxrsApplicationClassName != null ) {
47- types .addAll (fromClassNames (Arrays .asList (jaxrsApplicationClassName )). sourceTypes );
48+ types .addAll (fromClassNames (Arrays .asList (jaxrsApplicationClassName )));
4849 }
4950 if (automaticJaxrsApplication ) {
5051 types .addAll (JaxrsApplicationScanner .scanAutomaticJaxrsApplication (classpathScanner .scanClasspath (), isClassNameExcluded ));
@@ -62,13 +63,23 @@ public static Input fromClassNamesAndJaxrsApplication(List<String> classNames, L
6263
6364 private static class ClasspathScanner {
6465
66+ private final URLClassLoader classLoader ;
67+ private final boolean verbose ;
6568 private ScanResult scanResult = null ;
6669
70+ public ClasspathScanner (URLClassLoader classLoader , boolean verbose ) {
71+ this .classLoader = classLoader ;
72+ this .verbose = verbose ;
73+ }
74+
6775 public ScanResult scanClasspath () {
6876 if (scanResult == null ) {
6977 System .out .println ("Scanning classpath" );
7078 final Date scanStart = new Date ();
71- final ScanResult result = new FastClasspathScanner ().scan ();
79+ final ScanResult result = new FastClasspathScanner ()
80+ .overrideClasspath (classLoader .getURLs ())
81+ .verbose (verbose )
82+ .scan ();
7283 final int count = result .getNamesOfAllClasses ().size ();
7384 final Date scanEnd = new Date ();
7485 final double timeInSeconds = (scanEnd .getTime () - scanStart .getTime ()) / 1000.0 ;
@@ -80,7 +91,7 @@ public ScanResult scanClasspath() {
8091
8192 }
8293
83- private static Input fromClassNamePatterns (ScanResult scanResult , List <String > classNamePatterns ) {
94+ private static List < SourceType < Type >> fromClassNamePatterns (ScanResult scanResult , List <String > classNamePatterns ) {
8495 final List <String > allClassNames = new ArrayList <>();
8596 allClassNames .addAll (scanResult .getNamesOfAllStandardClasses ());
8697 allClassNames .addAll (scanResult .getNamesOfAllInterfaceClasses ());
@@ -90,21 +101,30 @@ private static Input fromClassNamePatterns(ScanResult scanResult, List<String> c
90101 return fromClassNames (classNames );
91102 }
92103
93- private static Input fromClassNames (List <String > classNames ) {
94- try {
95- final List <SourceType <Type >> types = new ArrayList <>();
96- for (String className : classNames ) {
104+ private static List <SourceType <Type >> fromClassNames (List <String > classNames ) {
105+ final List <SourceType <Type >> types = new ArrayList <>();
106+ for (Class <?> cls : loadClasses (classNames )) {
107+ // skip synthetic classes (as those generated by java compiler for switch with enum)
108+ // and anonymous classes (should not be processed and they do not have SimpleName)
109+ if (!cls .isSynthetic () && !cls .isAnonymousClass ()) {
110+ types .add (new SourceType <Type >(cls , null , null ));
111+ }
112+ }
113+ return types ;
114+ }
115+
116+ static List <Class <?>> loadClasses (List <String > classNames ) {
117+ final List <Class <?>> classes = new ArrayList <>();
118+ for (String className : classNames ) {
119+ try {
97120 final Class <?> cls = Thread .currentThread ().getContextClassLoader ().loadClass (className );
98- // skip synthetic classes (as those generated by java compiler for switch with enum)
99- // and anonymous classes (should not be processed and they do not have SimpleName)
100- if (!cls .isSynthetic () && !cls .isAnonymousClass ()) {
101- types .add (new SourceType <Type >(cls , null , null ));
102- }
121+ classes .add (cls );
122+ } catch (ReflectiveOperationException e ) {
123+ System .out .println (String .format ("Error: Cannot load class '%s'" , className ));
124+ e .printStackTrace (System .out );
103125 }
104- return new Input (types );
105- } catch (ReflectiveOperationException e ) {
106- throw new RuntimeException (e );
107126 }
127+ return classes ;
108128 }
109129
110130 static List <String > filterClassNames (List <String > classNames , List <String > globs ) {
0 commit comments