Search in sources :

Example 56 with BasicFileAttributes

use of java.nio.file.attribute.BasicFileAttributes in project bazel by bazelbuild.

the class VanillaJavaBuilder method getSources.

/** Returns the sources to compile, including any source jar entries. */
private ImmutableList<JavaFileObject> getSources(OptionsParser optionsParser, StandardJavaFileManager fileManager) throws IOException {
    final ImmutableList.Builder<JavaFileObject> sources = ImmutableList.builder();
    sources.addAll(fileManager.getJavaFileObjectsFromStrings(optionsParser.getSourceFiles()));
    for (String sourceJar : optionsParser.getSourceJars()) {
        for (final Path root : getJarFileSystem(Paths.get(sourceJar)).getRootDirectories()) {
            Files.walkFileTree(root, new SimpleFileVisitor<Path>() {

                @Override
                public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
                    if (path.getFileName().toString().endsWith(".java")) {
                        sources.add(new SourceJarFileObject(root, path));
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
        }
    }
    return sources.build();
}
Also used : Path(java.nio.file.Path) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) ImmutableList(com.google.common.collect.ImmutableList) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 57 with BasicFileAttributes

use of java.nio.file.attribute.BasicFileAttributes in project bazel by bazelbuild.

the class SimpleJavaLibraryBuilder method buildJar.

public void buildJar(JavaLibraryBuildRequest build) throws IOException {
    JarCreator jar = new JarCreator(build.getOutputJar());
    try {
        jar.setNormalize(true);
        jar.setCompression(build.compressJar());
        for (String resourceJar : build.getResourceJars()) {
            for (Path root : getJarFileSystem(Paths.get(resourceJar)).getRootDirectories()) {
                Files.walkFileTree(root, new SimpleFileVisitor<Path>() {

                    @Override
                    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                        // TODO(b/28452451): omit directories entries from jar files
                        if (dir.getNameCount() > 0) {
                            jar.addEntry(root.relativize(dir).toString(), dir);
                        }
                        return FileVisitResult.CONTINUE;
                    }

                    @Override
                    public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
                        jar.addEntry(root.relativize(path).toString(), path);
                        return FileVisitResult.CONTINUE;
                    }
                });
            }
        }
        jar.addDirectory(build.getClassDir());
        jar.addRootEntries(build.getRootResourceFiles());
        addResourceEntries(jar, build.getResourceFiles());
        addMessageEntries(jar, build.getMessageFiles());
    } finally {
        jar.execute();
    }
}
Also used : Path(java.nio.file.Path) JarCreator(com.google.devtools.build.buildjar.jarhelper.JarCreator) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 58 with BasicFileAttributes

use of java.nio.file.attribute.BasicFileAttributes in project bazel by bazelbuild.

the class JavacTurbineTest method compileLib.

private void compileLib(Path jar, Collection<Path> classpath, Iterable<? extends JavaFileObject> units) throws IOException {
    final Path outdir = temp.newFolder().toPath();
    JavacFileManager fm = new JavacFileManager(new Context(), false, UTF_8);
    fm.setLocationFromPaths(StandardLocation.CLASS_OUTPUT, Collections.singleton(outdir));
    fm.setLocationFromPaths(StandardLocation.CLASS_PATH, classpath);
    List<String> options = Arrays.asList("-d", outdir.toString());
    JavacTool tool = JavacTool.create();
    JavacTask task = tool.getTask(new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.err, UTF_8)), true), fm, null, options, null, units);
    assertThat(task.call()).isTrue();
    try (JarOutputStream jos = new JarOutputStream(Files.newOutputStream(jar))) {
        Files.walkFileTree(outdir, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
                JarEntry je = new JarEntry(outdir.relativize(path).toString());
                jos.putNextEntry(je);
                Files.copy(path, jos);
                return FileVisitResult.CONTINUE;
            }
        });
    }
}
Also used : Path(java.nio.file.Path) Context(com.sun.tools.javac.util.Context) JavacTool(com.sun.tools.javac.api.JavacTool) JarOutputStream(java.util.jar.JarOutputStream) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) BufferedWriter(java.io.BufferedWriter) JavacFileManager(com.sun.tools.javac.file.JavacFileManager) OutputStreamWriter(java.io.OutputStreamWriter) JavacTask(com.sun.source.util.JavacTask) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) PrintWriter(java.io.PrintWriter)

Example 59 with BasicFileAttributes

use of java.nio.file.attribute.BasicFileAttributes in project cryptomator by cryptomator.

the class UpgradeVersion4to5 method upgrade.

@Override
protected void upgrade(Vault vault, Cryptor cryptor) throws UpgradeFailedException {
    Path dataDir = vault.getPath().resolve("d");
    if (!Files.isDirectory(dataDir)) {
        // empty vault. no migration needed.
        return;
    }
    try {
        Files.walkFileTree(dataDir, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (BASE32_PATTERN.matcher(file.getFileName().toString()).find() && attrs.size() > cryptor.fileHeaderCryptor().headerSize()) {
                    migrate(file, attrs, cryptor);
                } else {
                    LOG.info("Skipping irrelevant file {}.", file);
                }
                return FileVisitResult.CONTINUE;
            }
        });
    } catch (IOException e) {
        LOG.error("Migration failed.", e);
        throw new UpgradeFailedException(localization.getString("upgrade.version4to5.err.io"));
    }
    LOG.info("Migration finished.");
}
Also used : Path(java.nio.file.Path) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 60 with BasicFileAttributes

use of java.nio.file.attribute.BasicFileAttributes in project che by eclipse.

the class Resource method accept.

@Override
public void accept(final IResourceProxyVisitor visitor, final int depth, final int memberFlags) throws CoreException {
    java.io.File file = workspace.getFile(getFullPath());
    int maxDepth = depth == IResource.DEPTH_INFINITE ? Integer.MAX_VALUE : depth;
    try {
        final ResourceProxy resourceProxy = new ResourceProxy();
        final int workspacePath = workspace.getAbsoluteWorkspacePath().length();
        Files.walkFileTree(file.toPath(), Collections.<FileVisitOption>emptySet(), maxDepth, new FileVisitor<java.nio.file.Path>() {

            @Override
            public FileVisitResult preVisitDirectory(java.nio.file.Path dir, BasicFileAttributes attrs) throws IOException {
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(java.nio.file.Path file, BasicFileAttributes attrs) throws IOException {
                FileVisitResult result = FileVisitResult.CONTINUE;
                try {
                    String string = file.toString();
                    IPath path = new Path(string.substring(workspacePath));
                    resourceProxy.info = workspace.getResourceInfo(path);
                    resourceProxy.fullPath = path;
                    boolean shouldContinue = true;
                    switch(depth) {
                        case DEPTH_ZERO:
                            shouldContinue = false;
                            break;
                        case DEPTH_ONE:
                            shouldContinue = !Resource.this.path.equals(path.removeLastSegments(1));
                            break;
                        case DEPTH_INFINITE:
                            shouldContinue = true;
                            break;
                    }
                    boolean visit = visitor.visit(resourceProxy) && shouldContinue;
                    result = visit ? FileVisitResult.CONTINUE : FileVisitResult.SKIP_SUBTREE;
                } catch (CoreException e) {
                    throw new WrappedRuntimeException(e);
                } finally {
                    resourceProxy.reset();
                }
                return result;
            }

            @Override
            public FileVisitResult visitFileFailed(java.nio.file.Path file, IOException exc) throws IOException {
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(java.nio.file.Path dir, IOException exc) throws IOException {
                return FileVisitResult.CONTINUE;
            }
        });
    } catch (IOException e) {
        throw new CoreException(new Status(IStatus.ERROR, ResourcesPlugin.getPluginId(), e.getMessage(), e));
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IResourceStatus(org.eclipse.core.resources.IResourceStatus) IPath(org.eclipse.core.runtime.IPath) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) IResourceProxy(org.eclipse.core.resources.IResourceProxy) CoreException(org.eclipse.core.runtime.CoreException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) WrappedRuntimeException(org.eclipse.core.internal.utils.WrappedRuntimeException)

Aggregations

BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)119 Path (java.nio.file.Path)93 IOException (java.io.IOException)87 FileVisitResult (java.nio.file.FileVisitResult)66 File (java.io.File)18 Test (org.junit.Test)13 SimpleFileVisitor (java.nio.file.SimpleFileVisitor)11 ArrayList (java.util.ArrayList)11 HashSet (java.util.HashSet)8 FileNotFoundException (java.io.FileNotFoundException)7 InputStream (java.io.InputStream)6 HashMap (java.util.HashMap)6 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)5 BasicFileAttributeView (java.nio.file.attribute.BasicFileAttributeView)5 SourcePath (com.facebook.buck.rules.SourcePath)4 ImmutableList (com.google.common.collect.ImmutableList)4 OutputStream (java.io.OutputStream)4 URI (java.net.URI)4 FileSystem (java.nio.file.FileSystem)4 FileVisitor (java.nio.file.FileVisitor)4