forked from helios-decompiler/transformer-api
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFileLoader.java
More file actions
123 lines (100 loc) · 4.41 KB
/
FileLoader.java
File metadata and controls
123 lines (100 loc) · 4.41 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* Copyright 2026 Apache License, Version 2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.heliosdecompiler.transformerapi.common;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.jd.core.v1.api.loader.Loader;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Objects;
import java.util.regex.Pattern;
public class FileLoader implements Loader {
protected static final Pattern CLASS_SUFFIX_PATTERN = Pattern.compile("\\.class$");
private final Path rootDirectory;
private final HashMap<String, byte[]> map = new HashMap<>();
public FileLoader(String rootLocation, String pkg, String className) throws IOException {
Objects.requireNonNull(rootLocation, "rootLocation");
Objects.requireNonNull(className, "className");
this.rootDirectory = Paths.get(rootLocation);
Validate.isTrue(Files.isDirectory(rootDirectory), "Not a directory: " + rootDirectory);
String topLevelTypeName = stripClassSuffix(className);
String primaryInternalName = buildInternalName(pkg, topLevelTypeName);
Path classDirectory = pkg.isEmpty() ? rootDirectory : rootDirectory.resolve(pkg);
Path primaryClassFile = classDirectory.resolve(toClassFileName(topLevelTypeName));
Validate.isTrue(Files.isRegularFile(primaryClassFile), "Class file not found: " + primaryClassFile);
map.put(primaryInternalName, Files.readAllBytes(primaryClassFile));
loadInnerClasses(classDirectory, pkg, topLevelTypeName);
}
protected void loadInnerClasses(Path classDirectory, String packagePath, String topLevelTypeName) throws IOException {
String pattern = topLevelTypeName + "$*.class";
try (DirectoryStream<Path> stream = Files.newDirectoryStream(classDirectory, pattern)) {
for (Path innerClassFile : stream) {
if (Files.isRegularFile(innerClassFile)) {
String fileName = String.valueOf(innerClassFile.getFileName());
String innerTypeName = makeEntryName(fileName);
String internalName = buildInternalName(packagePath, innerTypeName);
map.put(internalName, Files.readAllBytes(innerClassFile));
}
}
}
}
protected String buildInternalName(String packagePath, String typeName) {
Objects.requireNonNull(typeName, "typeName");
StringBuilder sb = new StringBuilder();
if (StringUtils.isNotEmpty(packagePath)) {
sb.append(packagePath);
sb.append("/");
}
sb.append(typeName);
return sb.toString();
}
protected String stripClassSuffix(String name) {
return CLASS_SUFFIX_PATTERN.matcher(name).replaceFirst("");
}
protected String toClassFileName(String typeName) {
return stripClassSuffix(typeName) + ".class";
}
protected String makeEntryName(String entryName) {
return CLASS_SUFFIX_PATTERN.matcher(entryName).replaceFirst("");
}
@Override
public byte[] load(String internalName) throws IOException {
String key = stripClassSuffix(internalName);
byte[] data = map.get(key);
if (data != null) {
return data;
}
Path classFile = classFilePath(key);
if (!Files.isRegularFile(classFile)) {
return data;
}
data = Files.readAllBytes(classFile);
map.put(key, data);
return data;
}
@Override
public boolean canLoad(String internalName) {
String key = stripClassSuffix(internalName);
return map.containsKey(key) || Files.isRegularFile(classFilePath(key));
}
protected Path classFilePath(String internalName) {
return rootDirectory.resolve(internalName + ".class");
}
}