1
1
package kg .ash .javavi .searchers ;
2
2
3
- import kg .ash .javavi .apache .logging .log4j .LogManager ;
4
- import kg .ash .javavi .apache .logging .log4j .Logger ;
5
-
6
3
import java .io .File ;
4
+ import java .io .IOException ;
5
+ import java .nio .file .Files ;
6
+ import java .nio .file .Paths ;
7
7
import java .util .ArrayList ;
8
8
import java .util .Enumeration ;
9
9
import java .util .List ;
10
10
import java .util .Optional ;
11
+ import java .util .stream .Stream ;
11
12
import java .util .zip .ZipFile ;
12
13
14
+ import kg .ash .javavi .apache .logging .log4j .LogManager ;
15
+ import kg .ash .javavi .apache .logging .log4j .Logger ;
16
+
13
17
public class ClasspathPackageSearcher implements PackageSeacherIFace {
14
18
15
19
public static final Logger logger = LogManager .getLogger ();
@@ -18,56 +22,85 @@ public List<PackageEntry> loadEntries() {
18
22
List <PackageEntry > result = new ArrayList <>();
19
23
20
24
List <String > knownPaths = new ArrayList <>();
21
- new ClasspathCollector ().collectClassPath ().stream ().forEach (filePath -> {
22
- if (filePath .toLowerCase ().endsWith (".class" )) {
23
- String path = filePath .substring (0 , filePath .length () - 6 ).replaceAll ("/" , "." );
24
- String newPath = path .substring (0 , path .lastIndexOf ("." ));
25
- String fileName = path .substring (path .lastIndexOf ("." ) + 1 , path .length ());
26
- Optional <PackageEntry > kp = knownPaths .parallelStream ()
27
- .filter (s -> newPath .endsWith (s ))
28
- .findFirst ()
29
- .map (p -> p + File .separator + fileName + ".class" )
30
- .map (p -> new PackageEntry (p , JavaClassMap .SOURCETYPE_CLASSPATH , filePath ,
31
- PackageEntry .FILETYPE_CLASS ));
32
- if (kp .isPresent ()) {
33
- result .add (kp .get ());
34
- return ;
35
- }
25
+ new ClasspathCollector ().collectClassPath ()
26
+ .stream ()
27
+ .forEach (filePath -> {
28
+ if (filePath .toLowerCase ().endsWith (".class" )) {
29
+ String path = filePath .substring (
30
+ 0 , filePath .length () - 6 )
31
+ .replaceAll ("/" , "." );
32
+ String newPath = path .substring (
33
+ 0 , path .lastIndexOf ("." ));
34
+ String fileName = path .substring (
35
+ path .lastIndexOf ("." ) + 1 , path .length ());
36
+ Optional <PackageEntry > kp = knownPaths .parallelStream ()
37
+ .filter (s -> newPath .endsWith (s ))
38
+ .findFirst ()
39
+ .map (p -> p + File .separator + fileName + ".class" )
40
+ .map (p -> new PackageEntry (
41
+ p ,
42
+ JavaClassMap .SOURCETYPE_CLASSPATH ,
43
+ filePath ,
44
+ PackageEntry .FILETYPE_CLASS ));
45
+ if (kp .isPresent ()) {
46
+ result .add (kp .get ());
47
+ return ;
48
+ }
36
49
37
- String [] split = path .split ("\\ ." );
38
- int j = split .length - 2 ;
39
- while (j > 0 ) {
40
- path = "" ;
41
- for (int i = j ; i <= split .length - 2 ; i ++) {
42
- path += split [i ] + "." ;
50
+ String [] split = path .split ("\\ ." );
51
+ int j = split .length - 2 ;
52
+ while (j > 0 ) {
53
+ path = "" ;
54
+ for (int i = j ; i <= split .length - 2 ; i ++) {
55
+ path += split [i ] + "." ;
56
+ }
57
+ String pkg = getPackageByFile (path + fileName );
58
+ if (pkg != null ) {
59
+ result .add (
60
+ new PackageEntry (
61
+ pkg + File .separator +
62
+ fileName + ".class" ,
63
+ JavaClassMap .SOURCETYPE_CLASSPATH ,
64
+ filePath ,
65
+ PackageEntry .FILETYPE_CLASS ));
66
+ knownPaths .add (pkg );
67
+ break ;
68
+ } else {
69
+ j --;
70
+ }
43
71
}
44
- String pkg = getPackageByFile (path + fileName );
45
- if (pkg != null ) {
46
- result .add (new PackageEntry (pkg + File .separator + fileName + ".class" ,
47
- JavaClassMap .SOURCETYPE_CLASSPATH , filePath ,
48
- PackageEntry .FILETYPE_CLASS ));
49
- knownPaths .add (pkg );
50
- break ;
51
- } else {
52
- j --;
72
+ } else if (filePath .endsWith ("classlist" )) {
73
+ try (Stream <String > stream =
74
+ Files .lines (Paths .get (filePath ))) {
75
+ stream .forEach (l -> {
76
+ result .add (
77
+ new PackageEntry (l + ".class" ,
78
+ JavaClassMap .SOURCETYPE_CLASSPATH ,
79
+ filePath ));
80
+ });
81
+ } catch (IOException ex ) {
82
+ logger .warn ("error read classlist file" , ex );
53
83
}
54
- }
55
- } else {
56
- try {
57
- for (Enumeration entries = new ZipFile (filePath ).entries ();
58
- entries .hasMoreElements (); ) {
59
- String entry = entries .nextElement ().toString ();
60
- if (filePath .endsWith (".jmod" ) && entry .startsWith ("classes/" )) {
61
- entry = entry .substring (8 );
84
+ } else {
85
+ try {
86
+ for (Enumeration entries =
87
+ new ZipFile (filePath ).entries ();
88
+ entries .hasMoreElements (); ) {
89
+ String entry = entries .nextElement ().toString ();
90
+ if (filePath .endsWith (".jmod" )
91
+ && entry .startsWith ("classes/" )) {
92
+ entry = entry .substring (8 );
93
+ }
94
+ result .add (
95
+ new PackageEntry (entry ,
96
+ JavaClassMap .SOURCETYPE_CLASSPATH ,
97
+ filePath ));
62
98
}
63
- result . add (
64
- new PackageEntry ( entry , JavaClassMap . SOURCETYPE_CLASSPATH , filePath ) );
99
+ } catch ( Exception e ) {
100
+ logger . error ( e , e );
65
101
}
66
- } catch (Exception e ) {
67
- logger .error (e , e );
68
102
}
69
- }
70
- });
103
+ });
71
104
72
105
return result ;
73
106
}
@@ -76,7 +109,9 @@ private String getPackageByFile(String path) {
76
109
try {
77
110
Class clazz = Class .forName (path );
78
111
return clazz .getPackage ().getName ();
79
- } catch (ExceptionInInitializerError | ClassNotFoundException | NoClassDefFoundError ex ) {
112
+ } catch (ExceptionInInitializerError |
113
+ ClassNotFoundException |
114
+ NoClassDefFoundError ex ) {
80
115
return null ;
81
116
}
82
117
}
0 commit comments