use of org.gradle.api.file.FileVisitDetails 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");
}
}
});
}
use of org.gradle.api.file.FileVisitDetails in project gradle by gradle.
the class CompositeFileTreeTest method visitsEachTreeWithAction.
@Test
public void visitsEachTreeWithAction() {
final Action<FileVisitDetails> visitor = Actions.doNothing();
context.checking(new Expectations() {
{
oneOf(source1).visit(visitor);
oneOf(source2).visit(visitor);
}
});
tree.visit(visitor);
}
use of org.gradle.api.file.FileVisitDetails in project gradle by gradle.
the class DirectoryFileTree method processSingleFile.
private void processSingleFile(File file, FileVisitor visitor, Spec<FileTreeElement> spec, AtomicBoolean stopFlag) {
RelativePath path = new RelativePath(true, file.getName());
FileVisitDetails details = new DefaultFileVisitDetails(file, path, stopFlag, fileSystem, fileSystem, false);
if (isAllowed(details, spec)) {
visitor.visitFile(details);
}
}
use of org.gradle.api.file.FileVisitDetails in project gradle by gradle.
the class SingleIncludePatternFileTree method doVisitDirOrFile.
private void doVisitDirOrFile(FileVisitor visitor, File file, LinkedList<String> relativePath, int segmentIndex, AtomicBoolean stopFlag) {
if (file.isFile()) {
if (segmentIndex == patternSegments.size()) {
RelativePath path = new RelativePath(true, relativePath.toArray(new String[relativePath.size()]));
FileVisitDetails details = new DefaultFileVisitDetails(file, path, stopFlag, fileSystem, fileSystem);
if (!excludeSpec.isSatisfiedBy(details)) {
visitor.visitFile(details);
}
}
} else if (file.isDirectory()) {
RelativePath path = new RelativePath(false, relativePath.toArray(new String[relativePath.size()]));
FileVisitDetails details = new DefaultFileVisitDetails(file, path, stopFlag, fileSystem, fileSystem);
if (!excludeSpec.isSatisfiedBy(details)) {
visitor.visitDir(details);
}
if (segmentIndex < patternSegments.size()) {
doVisit(visitor, file, relativePath, segmentIndex, stopFlag);
}
}
}
use of org.gradle.api.file.FileVisitDetails in project gradle by gradle.
the class ValidateTaskProperties method validateTaskClasses.
private void validateTaskClasses(final ClassLoader classLoader) throws IOException {
final Map<String, Boolean> taskValidationProblems = Maps.newTreeMap();
final Class<?> taskInterface;
final Method validatorMethod;
try {
taskInterface = classLoader.loadClass(Task.class.getName());
Class<?> validatorClass = classLoader.loadClass("org.gradle.api.internal.project.taskfactory.TaskPropertyValidationAccess");
validatorMethod = validatorClass.getMethod("collectTaskValidationProblems", Class.class, Map.class);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
getServices().get(DirectoryFileTreeFactory.class).create(getClassesDir()).visit(new FileVisitor() {
@Override
public void visitDir(FileVisitDetails dirDetails) {
}
@Override
public void visitFile(FileVisitDetails fileDetails) {
if (!fileDetails.getPath().endsWith(".class")) {
return;
}
ClassReader reader;
try {
reader = new Java9ClassReader(Files.asByteSource(fileDetails.getFile()).read());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
List<String> classNames = Lists.newArrayList();
reader.accept(new TaskNameCollectorVisitor(classNames), ClassReader.SKIP_CODE);
for (String className : classNames) {
Class<?> clazz;
try {
clazz = classLoader.loadClass(className);
} catch (IllegalAccessError e) {
throw new GradleException("Could not load class: " + className, e);
} catch (ClassNotFoundException e) {
throw new GradleException("Could not load class: " + className, e);
} catch (NoClassDefFoundError e) {
throw new GradleException("Could not load class: " + className, e);
}
if (!Modifier.isPublic(clazz.getModifiers())) {
continue;
}
if (Modifier.isAbstract(clazz.getModifiers())) {
continue;
}
if (!taskInterface.isAssignableFrom(clazz)) {
continue;
}
Class<? extends Task> taskClass = Cast.uncheckedCast(clazz);
try {
validatorMethod.invoke(null, taskClass, taskValidationProblems);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
}
}
});
List<String> problemMessages = toProblemMessages(taskValidationProblems);
storeResults(problemMessages, getOutputFile());
communicateResult(problemMessages, taskValidationProblems.values().contains(Boolean.TRUE));
}
Aggregations