Search in sources :

Example 6 with EmptyFileVisitor

use of org.gradle.api.file.EmptyFileVisitor in project gradle by gradle.

the class AbstractFileTree method isEmpty.

@Override
public boolean isEmpty() {
    final AtomicBoolean found = new AtomicBoolean();
    visit(new EmptyFileVisitor() {

        public void visitFile(FileVisitDetails fileDetails) {
            found.set(true);
            fileDetails.stopVisiting();
        }
    });
    return !found.get();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FileVisitDetails(org.gradle.api.file.FileVisitDetails) EmptyFileVisitor(org.gradle.api.file.EmptyFileVisitor)

Example 7 with EmptyFileVisitor

use of org.gradle.api.file.EmptyFileVisitor 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.tasks.properties.PropertyValidationAccess");
        validatorMethod = validatorClass.getMethod("collectTaskValidationProblems", Class.class, Map.class);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(e);
    }
    getClasses().getAsFileTree().visit(new EmptyFileVisitor() {

        @Override
        public void visitFile(FileVisitDetails fileDetails) {
            if (!fileDetails.getPath().endsWith(".class")) {
                return;
            }
            ClassReader reader;
            try {
                reader = new PatchedClassReader(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);
    communicateResult(problemMessages, taskValidationProblems.values().contains(Boolean.TRUE));
}
Also used : Task(org.gradle.api.Task) VerificationTask(org.gradle.api.tasks.VerificationTask) ConventionTask(org.gradle.api.internal.ConventionTask) CacheableTask(org.gradle.api.tasks.CacheableTask) UncheckedIOException(org.gradle.api.UncheckedIOException) PatchedClassReader(org.gradle.util.internal.PatchedClassReader) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) EmptyFileVisitor(org.gradle.api.file.EmptyFileVisitor) 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) PatchedClassReader(org.gradle.util.internal.PatchedClassReader) ClassReader(org.objectweb.asm.ClassReader) Map(java.util.Map)

Example 8 with EmptyFileVisitor

use of org.gradle.api.file.EmptyFileVisitor in project gradle by gradle.

the class DefaultHeaderDependenciesCollector method addIncludeRoots.

private void addIncludeRoots(String taskPath, List<File> includeRoots, final Set<File> headerDependencies) {
    for (final File includeRoot : includeRoots) {
        logger.info("adding files in {} to header dependencies for {}", includeRoot, taskPath);
        directoryFileTreeFactory.create(includeRoot).visit(new EmptyFileVisitor() {

            @Override
            public void visitFile(FileVisitDetails fileDetails) {
                headerDependencies.add(fileDetails.getFile());
            }
        });
    }
}
Also used : FileVisitDetails(org.gradle.api.file.FileVisitDetails) EmptyFileVisitor(org.gradle.api.file.EmptyFileVisitor) File(java.io.File)

Aggregations

EmptyFileVisitor (org.gradle.api.file.EmptyFileVisitor)8 FileVisitDetails (org.gradle.api.file.FileVisitDetails)8 File (java.io.File)6 HashSet (java.util.HashSet)2 ImmutableList (com.google.common.collect.ImmutableList)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Map (java.util.Map)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 GradleException (org.gradle.api.GradleException)1 Task (org.gradle.api.Task)1 UncheckedIOException (org.gradle.api.UncheckedIOException)1 ConventionTask (org.gradle.api.internal.ConventionTask)1 CacheableTask (org.gradle.api.tasks.CacheableTask)1 VerificationTask (org.gradle.api.tasks.VerificationTask)1 PatchedClassReader (org.gradle.util.internal.PatchedClassReader)1