Skip to content

Instantly share code, notes, and snippets.

@zskamljic
Last active July 19, 2018 14:32
Show Gist options
  • Save zskamljic/7750388788f1148afc515ae629058f17 to your computer and use it in GitHub Desktop.
Save zskamljic/7750388788f1148afc515ae629058f17 to your computer and use it in GitHub Desktop.
Set<String> getClassNames(Collection<TransformInput> inputs) {
Set<String> classNames = new HashSet<>();
for (TransformInput input : inputs) {
classNames.addAll(getDirectoryInputs(input.getDirectoryInputs()));
classNames.addAll(getJarInputs(input.getJarInputs()));
}
return classNames;
}
Set<String> getDirectoryInputs(Collection<DirectoryInput> directoryInputs) {
Set<String> classNames = new HashSet<>();
for (DirectoryInput input : directoryInputs) {
try {
classNames.addAll(processDirectoryInput(input));
} catch (IOException e) {
throw new GradleException(e.getMessage());
}
}
return classNames;
}
Set<String> processDirectoryInput(DirectoryInput input) throws IOException {
String dirPath = input.getFile().getAbsolutePath();
return Files.walk(input.getFile().toPath())
.map(file -> file.toAbsolutePath().toString())
.filter(path -> path.endsWith(SdkConstants.DOT_CLASS))
.map(path -> path.substring(dirPath.length() + 1,
path.length() - SdkConstants.DOT_CLASS.length()))
.map(path -> path.replaceAll(File.separator, "."))
.collect(Collectors.toSet());
}
Set<String> getJarInputs(Collection<JarInput> jarInputs) {
return jarInputs.stream().map(QualifiedContent::getFile)
.map(this::toJar)
.map(JarFile::entries)
.flatMap(this::toStream)
.filter(entry -> !entry.isDirectory() && entry.getName().endsWith(SdkConstants.DOT_CLASS))
.map(ZipEntry::getName)
.map(name -> name.substring(0, name.length() - SdkConstants.DOT_CLASS.length()))
.map(name -> name.replaceAll(File.separator, "."))
.collect(Collectors.toSet());
}
ClassPool createClassPool(Collection<TransformInput> inputs,
Collection<TransformInput> referencedInputs) {
ClassPool classPool = new ClassPool();
classPool.appendSystemPath();
classPool.appendClassPath(new LoaderClassPath(getClass().getClassLoader()));
Stream.concat(inputs.stream(), referencedInputs.stream())
.flatMap(input -> Stream.concat(input.getDirectoryInputs().stream(),
input.getJarInputs().stream()))
.map(input -> input.getFile().getAbsolutePath())
.forEach(entry -> {
try {
classPool.appendClassPath(entry);
} catch (NotFoundException e) {
throw new GradleException(e.getMessage());
}
});
return classPool;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment