@@ -141,38 +141,55 @@ private static Class<?>[] getClasses(String packageName) throws ClassNotFoundExc
141141 Enumeration <URL > resources = classLoader .getResources (path );
142142 List <File > dirs = new ArrayList <>();
143143 List <JarFile > jars = new ArrayList <>();
144+ try {
145+ while (resources .hasMoreElements ())
146+ {
147+ URL resource = resources .nextElement ();
148+ String protocol = resource .getProtocol ();
149+
150+ if ("jar" .equals (protocol ) || "wsjar" .equals (protocol ))
151+ {
152+ String jarFileName = URLDecoder .decode (resource .getFile (), "UTF-8" );
153+ jarFileName = jarFileName .substring (5 ,jarFileName .indexOf ("!" ));
154+
155+ jars .add (new JarFile (jarFileName ));
156+ }
157+ else
158+ {
159+ dirs .add (new File (resource .toURI ()));
160+ }
161+ }
144162
145- while (resources .hasMoreElements ())
146- {
147- URL resource = resources .nextElement ();
148- String protocol = resource .getProtocol ();
163+ ArrayList <Class <?>> classes = new ArrayList <Class <?>>();
149164
150- if ( "jar" . equals ( protocol ) || "wsjar" . equals ( protocol ) )
165+ for ( File directory : dirs )
151166 {
152- String jarFileName = URLDecoder .decode (resource .getFile (), "UTF-8" );
153- jarFileName = jarFileName .substring (5 ,jarFileName .indexOf ("!" ));
154-
155- jars .add (new JarFile (jarFileName ));
167+ classes .addAll (findClasses (directory , packageName ));
156168 }
157- else
169+
170+ for (JarFile jarFile : jars )
158171 {
159- dirs . add ( new File ( resource . toURI () ));
172+ classes . addAll ( findClasses ( jarFile , path ));
160173 }
161- }
162-
163- ArrayList <Class <?>> classes = new ArrayList <Class <?>>();
164174
165- for (File directory : dirs )
166- {
167- classes .addAll (findClasses (directory , packageName ));
168- }
169-
170- for (JarFile jarFile : jars )
171- {
172- classes .addAll (findClasses (jarFile , path ));
175+ return classes .toArray (new Class [classes .size ()]);
176+ } finally {
177+ IOException first = null ;
178+ for (JarFile jarFile : jars ) {
179+ try {
180+ jarFile .close ();
181+ } catch (IOException e ) {
182+ if (first == null ) {
183+ first = e ;
184+ } else {
185+ first .addSuppressed (e );
186+ }
187+ }
188+ }
189+ if (first != null ) {
190+ throw first ;
191+ }
173192 }
174-
175- return classes .toArray (new Class [classes .size ()]);
176193 }
177194
178195
0 commit comments