-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava.ts
More file actions
67 lines (64 loc) · 2.28 KB
/
java.ts
File metadata and controls
67 lines (64 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import parser from './java-class-parser';
import decompress from 'adm-zip';
import * as fs from 'fs';
export function loadClasses(jarPath: string): Promise<any> {
return new Promise(async (resolve, reject) => {
fs.rmdirSync("temp", { recursive: true });
const zip = new decompress(jarPath);
zip.extractAllTo("temp");
const classes = findClassFiles("temp");
console.log(classes);
let classMap: any = {};
// Batch into 4
let classBatches = [];
for (let i = 0; i < classes.length; i += 4) {
classBatches.push(classes.slice(i, i + 4));
}
const batchSize = 16;
for (let i = 0; i < classBatches.length; i+=batchSize) {
console.log(i/batchSize, '/', classBatches.length/batchSize, classMap.length);
const batch = classBatches.slice(i, i + batchSize);
const parsed = await Promise.all(batch.map(parseClass));
for (let i = 0; i < parsed.length; i++) {
const parsedBatch = parsed[i];
for (let i = 0; i < parsedBatch.length; i++) {
const parsedClass = parsedBatch[i];
classMap[parsedClass.name] = parsedClass;
}
}
}
resolve(classMap);
});
}
function parseClass(paths: string[]): Promise<any[]> {
return new Promise((resolve, reject) => {
parser(paths, '-p', function(err: any, rs: any[]) {
if (err) {
reject(err);
} else {
// The result is a map of class names to class objects, convert to a list
let list = [];
for (let [key, value] of Object.entries(rs)) {
list.push(value);
}
resolve(list);
}
});
});
}
function findClassFiles(dir: string): string[] {
let results: string[] = [];
const list = fs.readdirSync(dir);
list.forEach(function(file) {
file = dir + '/' + file;
const stat = fs.statSync(file);
if (stat && stat.isDirectory()) {
results = results.concat(findClassFiles(file));
} else {
if(file.endsWith(".class")) {
results.push(file);
}
}
});
return results;
}