3
3
import top .wavelength .betterreflection .BetterReflectionClass ;
4
4
import top .wavelength .betterreflection .exceptions .CannotReadJarException ;
5
5
6
+ import java .io .File ;
6
7
import java .io .IOException ;
7
- import java .util .*;
8
+ import java .util .ArrayList ;
9
+ import java .util .Enumeration ;
10
+ import java .util .List ;
11
+ import java .util .Objects ;
8
12
import java .util .jar .JarEntry ;
9
13
import java .util .jar .JarFile ;
10
14
11
- import static top .wavelength .betterreflection .BetterReflectionUtils .LAUNCH_JAR_FILE ;
12
-
13
15
/**
14
16
* JarClassFinder is a concrete implementation of the ClassFinder abstract class that is used to
15
17
* find classes in a package from a JAR file based on certain criteria.
@@ -40,14 +42,13 @@ public JarClassFinder(String packageName) {
40
42
@ SuppressWarnings ("unchecked" )
41
43
@ Override
42
44
public List <BetterReflectionClass <T >> findClasses () throws IOException {
43
- if (basePackage == null || basePackage .trim ().isEmpty ())
44
- return Collections .emptyList ();
45
45
List <BetterReflectionClass <T >> classes = new ArrayList <>();
46
- String basePackage = this .basePackage .replace ('.' , '/' );
47
- if (LAUNCH_JAR_FILE == null || !LAUNCH_JAR_FILE .exists () || !LAUNCH_JAR_FILE .canRead ())
48
- throw new CannotReadJarException (LAUNCH_JAR_FILE == null ? "[NO NAME]" : LAUNCH_JAR_FILE .getName ());
46
+ String basePackage = this .basePackage == null ? "" : this .basePackage .replace ('.' , '/' );
47
+ File jarFile = getJarFile ();
48
+ if (jarFile == null || !jarFile .exists () || !jarFile .canRead ())
49
+ throw new CannotReadJarException (jarFile == null ? "[NO NAME]" : jarFile .getName ());
49
50
50
- try (JarFile jar = new JarFile (LAUNCH_JAR_FILE )) {
51
+ try (JarFile jar = new JarFile (jarFile )) {
51
52
Enumeration <JarEntry > entries = jar .entries ();
52
53
while (entries .hasMoreElements ()) {
53
54
JarEntry entry = entries .nextElement ();
@@ -60,14 +61,19 @@ public List<BetterReflectionClass<T>> findClasses() throws IOException {
60
61
String packageName = entry .getName ().substring (0 , name .lastIndexOf ('/' )).replace ('/' , '.' );
61
62
String className = entry .getName ().substring (packageName .length () + 1 , entry .getName ().lastIndexOf ('.' ));
62
63
String fullName = packageName + '.' + className ;
63
- BetterReflectionClass <?> clasz = BetterReflectionClass .forName (fullName );
64
- if (getType () == null || getType ().isAssignableFrom (Objects .requireNonNull (clasz )))
65
- classes .add ((BetterReflectionClass <T >) clasz );
66
64
67
- if (isRecursive ()) {
68
- if (packageName .startsWith (basePackage ))
69
- classes .add ((BetterReflectionClass <T >) clasz );
70
- } else if (packageName .equals (basePackage ))
65
+ if ((isRecursive () && !packageName .startsWith (basePackage ))
66
+ || (!isRecursive () && !packageName .equalsIgnoreCase (basePackage )))
67
+ continue ;
68
+
69
+ BetterReflectionClass <?> clasz ;
70
+ try {
71
+ clasz = new BetterReflectionClass <>(Class .forName (fullName , true , getClassLoader ()));
72
+ } catch (ClassNotFoundException | NoClassDefFoundError e ) {
73
+ continue ;
74
+ }
75
+
76
+ if (getType () == null || getType ().isAssignableFrom (Objects .requireNonNull (clasz )))
71
77
classes .add ((BetterReflectionClass <T >) clasz );
72
78
}
73
79
}
0 commit comments