Search in sources :

Example 1 with ClassRemapper

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");
            }
        }
    });
}
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)

Aggregations

JarFile (java.util.jar.JarFile)1 JarOutputStream (java.util.jar.JarOutputStream)1 FileVisitDetails (org.gradle.api.file.FileVisitDetails)1 FileVisitor (org.gradle.api.file.FileVisitor)1 ClassReader (org.objectweb.asm.ClassReader)1 ClassWriter (org.objectweb.asm.ClassWriter)1 ClassRemapper (org.objectweb.asm.commons.ClassRemapper)1 Remapper (org.objectweb.asm.commons.Remapper)1