1 /*
   2  * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 7018098
  27  * @summary CacheFSInfo persists too long
  28  * @library /tools/javac/lib
  29  * @modules jdk.compiler/com.sun.tools.javac.file
  30  *          jdk.compiler/com.sun.tools.javac.processing
  31  *          jdk.compiler/com.sun.tools.javac.util
  32  * @build JavacTestingAbstractProcessor T7018098
  33  * @run main T7018098
  34  */
  35 
  36 import java.io.*;
  37 import java.util.*;
  38 import javax.annotation.processing.RoundEnvironment;
  39 import javax.annotation.processing.SupportedOptions;
  40 import javax.lang.model.element.TypeElement;
  41 import javax.tools.Diagnostic;
  42 
  43 import com.sun.tools.javac.file.FSInfo;
  44 import com.sun.tools.javac.processing.JavacProcessingEnvironment;
  45 import com.sun.tools.javac.util.Context;
  46 
  47 @SupportedOptions("expect")
  48 public class T7018098 extends JavacTestingAbstractProcessor {
  49     public static void main(String... args) throws Exception {
  50         new T7018098().run();
  51     }
  52 
  53     static File testDir = new File("T7018098.dir");
  54 
  55     void run() throws Exception {
  56         String myName = T7018098.class.getSimpleName();
  57         File testSrc = new File(System.getProperty("test.src"));
  58         File file = new File(testSrc, myName + ".java");
  59 
  60         _assert(!testDir.exists());
  61 
  62         compile(
  63             "-XaddExports:jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
  64             "-XaddExports:jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED",
  65             "-XaddExports:jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
  66             "-XDaccessInternalAPI",
  67             "-proc:only",
  68             "-processor", myName,
  69             "-Aexpect=false",
  70             file.getPath());
  71 
  72         testDir.mkdirs();
  73         _assert(testDir.exists());
  74 
  75         compile(
  76             "-XaddExports:jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
  77             "-XaddExports:jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED",
  78             "-XaddExports:jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
  79             "-XDaccessInternalAPI",
  80             "-proc:only",
  81             "-processor", myName,
  82             "-Aexpect=true",
  83             file.getPath());
  84     }
  85 
  86     void _assert(boolean cond) {
  87         if (!cond)
  88             throw new AssertionError();
  89     }
  90 
  91     void compile(String... args) throws Exception {
  92         StringWriter sw = new StringWriter();
  93         PrintWriter pw = new PrintWriter(sw);
  94         int rc = com.sun.tools.javac.Main.compile(args, pw);
  95         pw.close();
  96         String out = sw.toString();
  97         if (!out.isEmpty())
  98             System.err.println(out);
  99         if (rc != 0)
 100             throw new Exception("compilation failed unexpectedly: rc=" + rc);
 101     }
 102 
 103     //---------------
 104 
 105     @Override
 106     public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
 107         Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
 108         FSInfo fsInfo = context.get(FSInfo.class);
 109 
 110         round++;
 111         if (round == 1) {
 112             boolean expect = Boolean.valueOf(options.get("expect"));
 113             checkEqual("cache result", fsInfo.isDirectory(testDir.toPath()), expect);
 114             initialFSInfo = fsInfo;
 115         } else {
 116             checkEqual("fsInfo", fsInfo, initialFSInfo);
 117         }
 118 
 119         return true;
 120     }
 121 
 122     <T> void checkEqual(String label, T actual, T expected) {
 123         if (actual != expected)
 124             messager.printMessage(Diagnostic.Kind.ERROR,
 125                     "Unexpected value for " + label
 126                     + "; expected: " + expected
 127                     + "; found: " + actual);
 128     }
 129 
 130     int round = 0;
 131     FSInfo initialFSInfo;
 132 }