T7018098.java revision 3294:9adfb22ff08f
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
36import java.io.*;
37import java.util.*;
38import javax.annotation.processing.RoundEnvironment;
39import javax.annotation.processing.SupportedOptions;
40import javax.lang.model.element.TypeElement;
41import javax.tools.Diagnostic;
42
43import com.sun.tools.javac.file.FSInfo;
44import com.sun.tools.javac.processing.JavacProcessingEnvironment;
45import com.sun.tools.javac.util.Context;
46
47@SupportedOptions("expect")
48public 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:"
64                + "jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED,"
65                + "jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED,"
66                + "jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
67            "-XDaccessInternalAPI",
68            "-proc:only",
69            "-processor", myName,
70            "-Aexpect=false",
71            file.getPath());
72
73        testDir.mkdirs();
74        _assert(testDir.exists());
75
76        compile(
77            "-XaddExports:"
78                + "jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED,"
79                + "jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED,"
80                + "jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
81            "-XDaccessInternalAPI",
82            "-proc:only",
83            "-processor", myName,
84            "-Aexpect=true",
85            file.getPath());
86    }
87
88    void _assert(boolean cond) {
89        if (!cond)
90            throw new AssertionError();
91    }
92
93    void compile(String... args) throws Exception {
94        StringWriter sw = new StringWriter();
95        PrintWriter pw = new PrintWriter(sw);
96        int rc = com.sun.tools.javac.Main.compile(args, pw);
97        pw.close();
98        String out = sw.toString();
99        if (!out.isEmpty())
100            System.err.println(out);
101        if (rc != 0)
102            throw new Exception("compilation failed unexpectedly: rc=" + rc);
103    }
104
105    //---------------
106
107    @Override
108    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
109        Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
110        FSInfo fsInfo = context.get(FSInfo.class);
111
112        round++;
113        if (round == 1) {
114            boolean expect = Boolean.valueOf(options.get("expect"));
115            checkEqual("cache result", fsInfo.isDirectory(testDir.toPath()), expect);
116            initialFSInfo = fsInfo;
117        } else {
118            checkEqual("fsInfo", fsInfo, initialFSInfo);
119        }
120
121        return true;
122    }
123
124    <T> void checkEqual(String label, T actual, T expected) {
125        if (actual != expected)
126            messager.printMessage(Diagnostic.Kind.ERROR,
127                    "Unexpected value for " + label
128                    + "; expected: " + expected
129                    + "; found: " + actual);
130    }
131
132    int round = 0;
133    FSInfo initialFSInfo;
134}
135