use of org.objectweb.asm.commons.ClassRemapper in project gradle by gradle.
the class ShadedJar method analyse.
private void analyse(FileCollection sourceFiles, final ClassGraph classes, final PrintWriter writer) {
final PackagePatterns ignored = new PackagePatterns(Collections.singleton("java"));
sourceFiles.getAsFileTree().visit(new FileVisitor() {
boolean seenManifest;
public void visitDir(FileVisitDetails dirDetails) {
}
public void visitFile(FileVisitDetails fileDetails) {
writer.print(fileDetails.getPath() + ": ");
if (fileDetails.getPath().endsWith(".class")) {
try {
ClassReader reader;
try (InputStream inputStream = new BufferedInputStream(fileDetails.open())) {
reader = new ClassReader(inputStream);
}
final ClassDetails details = classes.get(reader.getClassName());
details.visited = true;
ClassWriter classWriter = new ClassWriter(0);
reader.accept(new ClassRemapper(classWriter, new Remapper() {
public String map(String name) {
if (ignored.matches(name)) {
return name;
}
ClassDetails dependencyDetails = classes.get(name);
if (dependencyDetails != details) {
details.dependencies.add(dependencyDetails);
}
return dependencyDetails.outputClassName;
}
}), ClassReader.EXPAND_FRAMES);
writer.println("mapped class name: " + details.outputClassName);
File outputFile = new File(classesDir, details.outputClassName.concat(".class"));
outputFile.getParentFile().mkdirs();
try (OutputStream outputStream = new FileOutputStream(outputFile)) {
outputStream.write(classWriter.toByteArray());
}
} catch (Exception exception) {
throw new ClassAnalysisException("Could not transform class from " + fileDetails.getFile(), exception);
}
} else if (fileDetails.getPath().endsWith(".properties") && classes.unshadedPackages.matches(fileDetails.getPath())) {
writer.println("include");
classes.addResource(new ResourceDetails(fileDetails.getPath(), fileDetails.getFile()));
} else if (fileDetails.getPath().equals(JarFile.MANIFEST_NAME) && !seenManifest) {
seenManifest = true;
classes.manifest = new ResourceDetails(fileDetails.getPath(), fileDetails.getFile());
} else {
writer.println("skipped");
}
}
});
}
Aggregations