Search in sources :

Example 6 with FileVisitDetails

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

Example 7 with FileVisitDetails

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);
}
Also used : Expectations(org.jmock.Expectations) FileVisitDetails(org.gradle.api.file.FileVisitDetails) Test(org.junit.Test)

Example 8 with FileVisitDetails

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);
    }
}
Also used : RelativePath(org.gradle.api.file.RelativePath) DefaultFileVisitDetails(org.gradle.api.internal.file.DefaultFileVisitDetails) FileVisitDetails(org.gradle.api.file.FileVisitDetails) DefaultFileVisitDetails(org.gradle.api.internal.file.DefaultFileVisitDetails)

Example 9 with FileVisitDetails

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);
        }
    }
}
Also used : RelativePath(org.gradle.api.file.RelativePath) DefaultFileVisitDetails(org.gradle.api.internal.file.DefaultFileVisitDetails) FileVisitDetails(org.gradle.api.file.FileVisitDetails) DefaultFileVisitDetails(org.gradle.api.internal.file.DefaultFileVisitDetails)

Example 10 with FileVisitDetails

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));
}
Also used : Task(org.gradle.api.Task) DefaultTask(org.gradle.api.DefaultTask) ParallelizableTask(org.gradle.api.tasks.ParallelizableTask) VerificationTask(org.gradle.api.tasks.VerificationTask) CacheableTask(org.gradle.api.tasks.CacheableTask) UncheckedIOException(org.gradle.api.UncheckedIOException) FileVisitor(org.gradle.api.file.FileVisitor) Java9ClassReader(org.gradle.util.internal.Java9ClassReader) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Method(java.lang.reflect.Method) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) FileVisitDetails(org.gradle.api.file.FileVisitDetails) GradleException(org.gradle.api.GradleException) ClassReader(org.objectweb.asm.ClassReader) Java9ClassReader(org.gradle.util.internal.Java9ClassReader) Map(java.util.Map)

Aggregations

FileVisitDetails (org.gradle.api.file.FileVisitDetails)12 FileVisitor (org.gradle.api.file.FileVisitor)6 IOException (java.io.IOException)4 UncheckedIOException (org.gradle.api.UncheckedIOException)4 File (java.io.File)3 RelativePath (org.gradle.api.file.RelativePath)3 DefaultFileVisitDetails (org.gradle.api.internal.file.DefaultFileVisitDetails)3 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 GradleException (org.gradle.api.GradleException)2 EmptyFileVisitor (org.gradle.api.file.EmptyFileVisitor)2 ClassReader (org.objectweb.asm.ClassReader)2 ImmutableList (com.google.common.collect.ImmutableList)1 HashCode (com.google.common.hash.HashCode)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 HashSet (java.util.HashSet)1 List (java.util.List)1