test/tools/javac/diags/RunExamples.java

changeset 1669
d3648557391b
parent 1409
33abf479f202
child 2525
2eb010b6cb22
     1.1 --- a/test/tools/javac/diags/RunExamples.java	Thu Mar 28 10:49:39 2013 -0700
     1.2 +++ b/test/tools/javac/diags/RunExamples.java	Thu Mar 28 10:58:45 2013 -0700
     1.3 @@ -1,5 +1,5 @@
     1.4  /*
     1.5 - * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
     1.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.8   *
     1.9   * This code is free software; you can redistribute it and/or modify it
    1.10 @@ -33,7 +33,8 @@
    1.11   */
    1.12  
    1.13  import java.io.*;
    1.14 -import java.text.SimpleDateFormat;
    1.15 +import java.nio.file.*;
    1.16 +import java.nio.file.attribute.BasicFileAttributes;
    1.17  import java.util.*;
    1.18  import java.util.regex.Matcher;
    1.19  import java.util.regex.Pattern;
    1.20 @@ -56,16 +57,18 @@
    1.21  public class RunExamples {
    1.22      public static void main(String... args) throws Exception {
    1.23          jtreg = (System.getProperty("test.src") != null);
    1.24 -        File tmpDir;
    1.25 +        Path tmpDir;
    1.26 +        boolean deleteOnExit;
    1.27          if (jtreg) {
    1.28              // use standard jtreg scratch directory: the current directory
    1.29 -            tmpDir = new File(System.getProperty("user.dir"));
    1.30 +            tmpDir = Paths.get(System.getProperty("user.dir"));
    1.31 +            deleteOnExit = false;
    1.32          } else {
    1.33 -            tmpDir = new File(System.getProperty("java.io.tmpdir"),
    1.34 -                    RunExamples.class.getName()
    1.35 -                    + (new SimpleDateFormat("yyMMddHHmmss")).format(new Date()));
    1.36 +            tmpDir = Files.createTempDirectory(Paths.get(System.getProperty("java.io.tmpdir")),
    1.37 +                    RunExamples.class.getName());
    1.38 +            deleteOnExit = true;
    1.39          }
    1.40 -        Example.setTempDir(tmpDir);
    1.41 +        Example.setTempDir(tmpDir.toFile());
    1.42  
    1.43          RunExamples r = new RunExamples();
    1.44  
    1.45 @@ -73,15 +76,8 @@
    1.46              if (r.run(args))
    1.47                  return;
    1.48          } finally {
    1.49 -            /* VERY IMPORTANT NOTE. In jtreg mode, tmpDir is set to the
    1.50 -             * jtreg scratch directory, which is the current directory.
    1.51 -             * In case someone is faking jtreg mode, make sure to only
    1.52 -             * clean tmpDir when it is reasonable to do so.
    1.53 -             */
    1.54 -            if (tmpDir.isDirectory() &&
    1.55 -                    tmpDir.getName().startsWith(RunExamples.class.getName())) {
    1.56 -                if (clean(tmpDir))
    1.57 -                    tmpDir.delete();
    1.58 +            if (deleteOnExit) {
    1.59 +                clean(tmpDir);
    1.60              }
    1.61          }
    1.62  
    1.63 @@ -203,14 +199,20 @@
    1.64      /**
    1.65       * Clean the contents of a directory.
    1.66       */
    1.67 -    static boolean clean(File dir) {
    1.68 -        boolean ok = true;
    1.69 -        for (File f: dir.listFiles()) {
    1.70 -            if (f.isDirectory())
    1.71 -                ok &= clean(f);
    1.72 -            ok &= f.delete();
    1.73 -        }
    1.74 -        return ok;
    1.75 +    static void clean(Path dir) throws IOException {
    1.76 +        Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
    1.77 +            @Override
    1.78 +            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
    1.79 +                Files.delete(file);
    1.80 +                return super.visitFile(file, attrs);
    1.81 +            }
    1.82 +
    1.83 +            @Override
    1.84 +            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
    1.85 +                if (exc == null) Files.delete(dir);
    1.86 +                return super.postVisitDirectory(dir, exc);
    1.87 +            }
    1.88 +        });
    1.89      }
    1.90  
    1.91      static abstract class Runner {

mercurial