Search in sources :

Example 6 with FileVisitor

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");
            }
        }
    });
}
Also used : JarOutputStream(java.util.jar.JarOutputStream) ClassRemapper(org.objectweb.asm.commons.ClassRemapper) ClassWriter(org.objectweb.asm.ClassWriter) FileVisitDetails(org.gradle.api.file.FileVisitDetails) Remapper(org.objectweb.asm.commons.Remapper) ClassRemapper(org.objectweb.asm.commons.ClassRemapper) ClassReader(org.objectweb.asm.ClassReader) FileVisitor(org.gradle.api.file.FileVisitor) JarFile(java.util.jar.JarFile)

Example 7 with FileVisitor

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();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FileVisitDetails(org.gradle.api.file.FileVisitDetails) EmptyFileVisitor(org.gradle.api.file.EmptyFileVisitor) FileVisitor(org.gradle.api.file.FileVisitor)

Example 8 with FileVisitor

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()));
}
Also used : HashCode(org.gradle.internal.hash.HashCode) FileVisitDetails(org.gradle.api.file.FileVisitDetails) ClassDependentsAccumulator(org.gradle.api.internal.tasks.compile.incremental.deps.ClassDependentsAccumulator) InputStream(java.io.InputStream) FileVisitor(org.gradle.api.file.FileVisitor) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(org.gradle.api.UncheckedIOException) ClassAnalysis(org.gradle.api.internal.tasks.compile.incremental.deps.ClassAnalysis)

Aggregations

FileVisitor (org.gradle.api.file.FileVisitor)8 FileVisitDetails (org.gradle.api.file.FileVisitDetails)6 IOException (java.io.IOException)3 UncheckedIOException (org.gradle.api.UncheckedIOException)3 InputStream (java.io.InputStream)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Expectations (org.jmock.Expectations)2 Test (org.junit.Test)2 Closure (groovy.lang.Closure)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 ArrayList (java.util.ArrayList)1 JarFile (java.util.jar.JarFile)1 JarOutputStream (java.util.jar.JarOutputStream)1 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1 TarEntry (org.apache.tools.tar.TarEntry)1 EmptyFileVisitor (org.gradle.api.file.EmptyFileVisitor)1 FileTree (org.gradle.api.file.FileTree)1 RelativeFile (org.gradle.api.internal.file.RelativeFile)1