use of org.gradle.api.file.FileVisitor 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");
}
}
});
}
use of org.gradle.api.file.FileVisitor in project gradle by gradle.
the class AbstractFileTree method visitAll.
/**
* Visits all the files of this tree.
*/
protected boolean visitAll() {
final AtomicBoolean hasContent = new AtomicBoolean();
visit(new FileVisitor() {
public void visitDir(FileVisitDetails dirDetails) {
dirDetails.getFile();
hasContent.set(true);
}
public void visitFile(FileVisitDetails fileDetails) {
fileDetails.getFile();
hasContent.set(true);
}
});
return hasContent.get();
}
use of org.gradle.api.file.FileVisitor in project gradle by gradle.
the class DefaultJarSnapshotter method createSnapshot.
public JarSnapshot createSnapshot(HashCode hash, JarArchive jarArchive) {
final Map<String, HashCode> hashes = Maps.newHashMap();
final ClassDependentsAccumulator accumulator = new ClassDependentsAccumulator();
jarArchive.contents.visit(new FileVisitor() {
public void visitDir(FileVisitDetails dirDetails) {
}
public void visitFile(FileVisitDetails fileDetails) {
if (!fileDetails.getName().endsWith(".class")) {
return;
}
HashCode classFileHash;
InputStream inputStream = fileDetails.open();
try {
classFileHash = hasher.hash(inputStream);
} finally {
try {
inputStream.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
ClassAnalysis analysis = analyzer.getClassAnalysis(classFileHash, fileDetails);
accumulator.addClass(analysis);
hashes.put(analysis.getClassName(), classFileHash);
}
});
return new JarSnapshot(new JarSnapshotData(hash, hashes, accumulator.getAnalysis()));
}
Aggregations