33
44import cz .habarta .typescript .generator .parser .*;
55import io .github .lukehutch .fastclasspathscanner .FastClasspathScanner ;
6- import java .io .File ;
76import java .lang .reflect .*;
8- import java .net .URLClassLoader ;
97import java .util .*;
108import java .util .regex .Matcher ;
119import java .util .regex .Pattern ;
@@ -31,11 +29,11 @@ public static Input from(Type... types) {
3129 return new Input (sourceTypes );
3230 }
3331
34- private static Input fromClassNames (List <String > classNames , ClassLoader classLoader ) {
32+ private static Input fromClassNames (List <String > classNames ) {
3533 try {
3634 final List <SourceType <Type >> types = new ArrayList <>();
3735 for (String className : classNames ) {
38- final Class <?> cls = classLoader .loadClass (className );
36+ final Class <?> cls = Thread . currentThread (). getContextClassLoader () .loadClass (className );
3937 // skip synthetic classes (as those generated by java compiler for switch with enum)
4038 // and anonymous classes (should not be processed and they do not have SimpleName)
4139 if (!cls .isSynthetic () && !cls .isAnonymousClass ()) {
@@ -48,48 +46,49 @@ private static Input fromClassNames(List<String> classNames, ClassLoader classLo
4846 }
4947 }
5048
51- private static Input fromClassNamePatterns (List <String > classNamePatterns , ClassLoader classLoader ) {
49+ private static Input fromClassNamePatterns (List <String > classNamePatterns ) {
5250 System .out .println ("Scanning classpath" );
53- if (!(classLoader instanceof URLClassLoader )) {
54- throw new RuntimeException ("Class name globbing only works with 'URLClassLoader'" );
55- }
56- final URLClassLoader urlClassLoader = (URLClassLoader ) classLoader ;
57- final FastClasspathScanner scanner = new FastClasspathScanner ().overrideClasspath (classpathFromURLClassLoader (urlClassLoader )).scan ();
51+ final FastClasspathScanner scanner = new FastClasspathScanner ().scan ();
5852 final List <String > allClassNames = new ArrayList <>();
5953 allClassNames .addAll (scanner .getNamesOfAllStandardClasses ());
6054 allClassNames .addAll (scanner .getNamesOfAllInterfaceClasses ());
6155 Collections .sort (allClassNames );
6256 final List <String > classNames = filterClassNames (allClassNames , classNamePatterns );
6357 System .out .println (String .format ("Matched: %d, total: %d." , classNames .size (), allClassNames .size ()));
64- return fromClassNames (classNames , classLoader );
65- }
66-
67- private static String classpathFromURLClassLoader (URLClassLoader classLoader ) {
68- return Utils .join (Arrays .asList (classLoader .getURLs ()), File .pathSeparator );
58+ return fromClassNames (classNames );
6959 }
7060
71- private static Input fromJaxrsApplication (String jaxrsApplicationClassName , List <String > excludedClassNames , ClassLoader classLoader ) {
72- final List <SourceType <Type >> sourceTypes = new JaxrsApplicationScanner (classLoader ).scanJaxrsApplication (jaxrsApplicationClassName , excludedClassNames );
61+ private static Input fromJaxrsApplication (String jaxrsApplicationClassName , List <String > excludedClassNames ) {
62+ final List <SourceType <Type >> sourceTypes = new JaxrsApplicationScanner ().scanJaxrsApplication (jaxrsApplicationClassName , excludedClassNames );
7363 return new Input (sourceTypes );
7464 }
7565
76- public static Input fromClassNamesAndJaxrsApplication (List <String > classNames , List <String > classNamePatterns , String jaxrsApplicationClassName , List <String > excludedClassNames , ClassLoader classLoader ) {
77- final List <SourceType <Type >> types = new ArrayList <>();
78- if (classNames != null ) {
79- types .addAll (fromClassNames (classNames , classLoader ).getSourceTypes ());
80- }
81- if (classNamePatterns != null ) {
82- types .addAll (fromClassNamePatterns (classNamePatterns , classLoader ).getSourceTypes ());
83- }
84- if (jaxrsApplicationClassName != null ) {
85- types .addAll (fromJaxrsApplication (jaxrsApplicationClassName , excludedClassNames , classLoader ).getSourceTypes ());
86- }
87- if (types .isEmpty ()) {
88- final String errorMessage = "No input classes found." ;
89- System .out .println (errorMessage );
90- throw new RuntimeException (errorMessage );
66+ public static Input fromClassNamesAndJaxrsApplication (List <String > classNames , List <String > classNamePatterns , String jaxrsApplicationClassName , boolean automaticJaxrsApplication , List <String > excludedClassNames , ClassLoader classLoader ) {
67+ final ClassLoader originalContextClassLoader = Thread .currentThread ().getContextClassLoader ();
68+ try {
69+ Thread .currentThread ().setContextClassLoader (classLoader );
70+ final List <SourceType <Type >> types = new ArrayList <>();
71+ if (classNames != null ) {
72+ types .addAll (fromClassNames (classNames ).getSourceTypes ());
73+ }
74+ if (classNamePatterns != null ) {
75+ types .addAll (fromClassNamePatterns (classNamePatterns ).getSourceTypes ());
76+ }
77+ if (jaxrsApplicationClassName != null ) {
78+ types .addAll (fromJaxrsApplication (jaxrsApplicationClassName , excludedClassNames ).getSourceTypes ());
79+ }
80+ if (automaticJaxrsApplication ) {
81+ types .addAll (fromJaxrsApplication (null , excludedClassNames ).getSourceTypes ());
82+ }
83+ if (types .isEmpty ()) {
84+ final String errorMessage = "No input classes found." ;
85+ System .out .println (errorMessage );
86+ throw new RuntimeException (errorMessage );
87+ }
88+ return new Input (types );
89+ } finally {
90+ Thread .currentThread ().setContextClassLoader (originalContextClassLoader );
9191 }
92- return new Input (types );
9392 }
9493
9594 static List <String > filterClassNames (List <String > classNames , List <String > globs ) {
0 commit comments