Search in sources :

Example 1 with ApiClassExtractor

use of org.gradle.api.internal.tasks.compile.ApiClassExtractor in project gradle by gradle.

the class ApiJar method createApiJar.

@TaskAction
public void createApiJar() throws IOException {
    // Make sure all entries are always written in the same order
    final File[] sourceFiles = sortedSourceFiles();
    final ApiClassExtractor apiClassExtractor = new ApiClassExtractor(getExportedPackages());
    withResource(new JarOutputStream(new BufferedOutputStream(new FileOutputStream(getOutputFile()), 65536)), new ErroringAction<JarOutputStream>() {

        @Override
        protected void doExecute(final JarOutputStream jos) throws Exception {
            writeManifest(jos);
            writeClasses(jos);
        }

        private void writeManifest(JarOutputStream jos) throws IOException {
            writeEntry(jos, "META-INF/MANIFEST.MF", "Manifest-Version: 1.0\n".getBytes());
        }

        private void writeClasses(JarOutputStream jos) throws Exception {
            for (File sourceFile : sourceFiles) {
                if (!isClassFile(sourceFile)) {
                    continue;
                }
                ClassReader classReader = new Java9ClassReader(readFileToByteArray(sourceFile));
                if (!apiClassExtractor.shouldExtractApiClassFrom(classReader)) {
                    continue;
                }
                byte[] apiClassBytes = apiClassExtractor.extractApiClassFrom(classReader);
                if (apiClassBytes == null) {
                    // Should be excluded
                    continue;
                }
                String internalClassName = classReader.getClassName();
                String entryPath = internalClassName + ".class";
                writeEntry(jos, entryPath, apiClassBytes);
            }
        }

        private void writeEntry(JarOutputStream jos, String name, byte[] bytes) throws IOException {
            JarEntry je = new JarEntry(name);
            // Setting time to 0 because we need API jars to be identical independently of
            // the timestamps of class files
            je.setTime(0);
            je.setSize(bytes.length);
            jos.putNextEntry(je);
            jos.write(bytes);
            jos.closeEntry();
        }
    });
}
Also used : JarOutputStream(java.util.jar.JarOutputStream) ApiClassExtractor(org.gradle.api.internal.tasks.compile.ApiClassExtractor) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) IOException(java.io.IOException) FileOutputStream(java.io.FileOutputStream) ClassReader(org.objectweb.asm.ClassReader) Java9ClassReader(org.gradle.util.internal.Java9ClassReader) Java9ClassReader(org.gradle.util.internal.Java9ClassReader) File(java.io.File) OutputFile(org.gradle.api.tasks.OutputFile) BufferedOutputStream(java.io.BufferedOutputStream) TaskAction(org.gradle.api.tasks.TaskAction)

Example 2 with ApiClassExtractor

use of org.gradle.api.internal.tasks.compile.ApiClassExtractor in project gradle by gradle.

the class AbiExtractingClasspathContentHasher method hashClassBytes.

private void hashClassBytes(InputStream inputStream, Hasher hasher) throws IOException {
    // Use the ABI as the hash
    byte[] classBytes = ByteStreams.toByteArray(inputStream);
    ApiClassExtractor extractor = new ApiClassExtractor(Collections.<String>emptySet());
    Java9ClassReader reader = new Java9ClassReader(classBytes);
    if (extractor.shouldExtractApiClassFrom(reader)) {
        byte[] signature = extractor.extractApiClassFrom(reader);
        if (signature != null) {
            hasher.putBytes(signature);
        }
    }
}
Also used : Java9ClassReader(org.gradle.util.internal.Java9ClassReader) ApiClassExtractor(org.gradle.api.internal.tasks.compile.ApiClassExtractor)

Aggregations

ApiClassExtractor (org.gradle.api.internal.tasks.compile.ApiClassExtractor)2 Java9ClassReader (org.gradle.util.internal.Java9ClassReader)2 BufferedOutputStream (java.io.BufferedOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 JarEntry (java.util.jar.JarEntry)1 JarOutputStream (java.util.jar.JarOutputStream)1 OutputFile (org.gradle.api.tasks.OutputFile)1 TaskAction (org.gradle.api.tasks.TaskAction)1 ClassReader (org.objectweb.asm.ClassReader)1