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();
}
});
}
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);
}
}
}
Aggregations