langtools/test/tools/javac/processing/loader/testClose/TestClose.java
author jjg
Wed, 29 Oct 2014 17:25:23 -0700
changeset 27319 030080f03e4f
parent 12016 1990493b64db
child 30730 d3ce7619db2c
permissions -rw-r--r--
8062348: langtools tests should close file manager (group 1) Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10637
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
     1
/*
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
     2
 * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
10637
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
     4
 *
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
     8
 *
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    13
 * accompanied this code).
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    14
 *
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    18
 *
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    21
 * questions.
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    22
 */
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    23
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    24
/*
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    25
 * @test
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    26
 * @bug 7092965
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    27
 * @summary javac should not close processorClassLoader before end of compilation
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    28
 */
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    29
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    30
import com.sun.source.util.JavacTask;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    31
import com.sun.source.util.TaskEvent;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    32
import com.sun.source.util.TaskListener;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    33
import com.sun.tools.javac.api.ClientCodeWrapper.Trusted;
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 10637
diff changeset
    34
import com.sun.tools.javac.api.BasicJavacTask;
10637
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    35
import com.sun.tools.javac.api.JavacTool;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    36
import com.sun.tools.javac.processing.JavacProcessingEnvironment;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    37
import com.sun.tools.javac.util.Context;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    38
import java.io.ByteArrayOutputStream;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    39
import java.io.File;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    40
import java.io.IOException;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    41
import java.io.PrintStream;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    42
import java.lang.reflect.Field;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    43
import java.net.URI;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    44
import java.util.ArrayList;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    45
import java.util.Arrays;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    46
import java.util.Collections;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    47
import java.util.List;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    48
import javax.annotation.processing.ProcessingEnvironment;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    49
import javax.tools.JavaFileObject;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    50
import javax.tools.SimpleJavaFileObject;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    51
import javax.tools.StandardJavaFileManager;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    52
import javax.tools.StandardLocation;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    53
import javax.tools.ToolProvider;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    54
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    55
/*
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    56
 * The test compiles an annotation processor and a helper class into a
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    57
 * custom classes directory.
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    58
 *
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    59
 * It then uses them while compiling a dummy file, with the custom classes
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    60
 * directory on the processor path, thus guaranteeing that references to
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    61
 * these class are satisfied by the processor class loader.
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    62
 *
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    63
 * The annotation processor uses the javac TaskListener to run code
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    64
 * after annotation processing has completed, to verify that the classloader
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    65
 * is not closed until the end of the compilation.
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    66
 */
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    67
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    68
@Trusted // avoids use of ClientCodeWrapper
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    69
public class TestClose implements TaskListener {
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    70
    public static final String annoProc =
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    71
        "import java.util.*;\n" +
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    72
        "import javax.annotation.processing.*;\n" +
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    73
        "import javax.lang.model.*;\n" +
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    74
        "import javax.lang.model.element.*;\n" +
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    75
        "import com.sun.source.util.*;\n" +
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    76
        "import com.sun.tools.javac.processing.*;\n" +
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    77
        "import com.sun.tools.javac.util.*;\n" +
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    78
        "@SupportedAnnotationTypes(\"*\")\n" +
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    79
        "public class AnnoProc extends AbstractProcessor {\n" +
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    80
        "    @Override\n" +
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    81
        "    public SourceVersion getSupportedSourceVersion() {\n" +
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    82
        "        return SourceVersion.latest();\n" +
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    83
        "    }\n" +
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    84
        "    @Override\n" +
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    85
        "    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {\n" +
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    86
        "        System.out.println(\"in AnnoProc.process\");\n" +
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    87
        "        final ClassLoader cl = getClass().getClassLoader();\n" +
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    88
        "        if (roundEnv.processingOver()) {\n" +
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    89
        "            TestClose.add(processingEnv, new Runnable() {\n" +
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    90
        "                public void run() {\n" +
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    91
        "                    System.out.println(getClass().getName() + \": run()\");\n" +
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    92
        "                    try {\n" +
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 10637
diff changeset
    93
        "                        cl.loadClass(\"Callback\")\n" +
1990493b64db 7093891: support multiple task listeners
jjg
parents: 10637
diff changeset
    94
        "                            .asSubclass(Runnable.class)\n" +
1990493b64db 7093891: support multiple task listeners
jjg
parents: 10637
diff changeset
    95
        "                            .newInstance()\n" +
1990493b64db 7093891: support multiple task listeners
jjg
parents: 10637
diff changeset
    96
        "                            .run();\n" +
10637
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    97
        "                    } catch (ReflectiveOperationException e) {\n" +
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    98
        "                        throw new Error(e);\n" +
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    99
        "                    }\n" +
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   100
        "                }\n" +
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   101
        "            });\n" +
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   102
        "        }\n" +
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   103
        "        return true;\n" +
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   104
        "    }\n" +
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   105
        "}\n";
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   106
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   107
    public static final String callback =
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   108
        "public class Callback implements Runnable {\n" +
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   109
        "    public void run() {\n" +
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   110
        "        System.out.println(getClass().getName() + \": run()\");\n" +
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   111
        "    }\n" +
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   112
        "}";
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   113
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   114
    public static void main(String... args) throws Exception {
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   115
        new TestClose().run();
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   116
    }
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   117
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   118
    void run() throws IOException {
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   119
        JavacTool tool = (JavacTool) ToolProvider.getSystemJavaCompiler();
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   120
        try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
10637
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   121
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   122
            File classes = new File("classes");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   123
            classes.mkdirs();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   124
            File extraClasses = new File("extraClasses");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   125
            extraClasses.mkdirs();
10637
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   126
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   127
            System.out.println("compiling classes to extraClasses");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   128
            {   // setup class in extraClasses
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   129
                fm.setLocation(StandardLocation.CLASS_OUTPUT,
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   130
                        Collections.singleton(extraClasses));
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   131
                List<? extends JavaFileObject> files = Arrays.asList(
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   132
                        new MemFile("AnnoProc.java", annoProc),
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   133
                        new MemFile("Callback.java", callback));
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   134
                JavacTask task = tool.getTask(null, fm, null, null, null, files);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   135
                check(task.call());
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   136
            }
10637
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   137
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   138
            System.out.println("compiling dummy to classes with anno processor");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   139
            {   // use that class in a TaskListener after processing has completed
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   140
                PrintStream prev = System.out;
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   141
                String out;
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   142
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   143
                try (PrintStream ps = new PrintStream(baos)) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   144
                    System.setOut(ps);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   145
                    File testClasses = new File(System.getProperty("test.classes"));
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   146
                    fm.setLocation(StandardLocation.CLASS_OUTPUT,
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   147
                            Collections.singleton(classes));
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   148
                    fm.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH,
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   149
                            Arrays.asList(extraClasses, testClasses));
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   150
                    List<? extends JavaFileObject> files = Arrays.asList(
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   151
                            new MemFile("my://dummy", "class Dummy { }"));
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   152
                    List<String> options = Arrays.asList("-processor", "AnnoProc");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   153
                    JavacTask task = tool.getTask(null, fm, null, options, null, files);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   154
                    task.setTaskListener(this);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   155
                    check(task.call());
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   156
                } finally {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   157
                    System.setOut(prev);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   158
                    out = baos.toString();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   159
                    if (!out.isEmpty())
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   160
                        System.out.println(out);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   161
                }
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   162
                check(out.contains("AnnoProc$1: run()"));
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 12016
diff changeset
   163
                check(out.contains("Callback: run()"));
10637
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   164
            }
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   165
        }
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   166
    }
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   167
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   168
    @Override
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   169
    public void started(TaskEvent e) {
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   170
        System.out.println("Started: " + e);
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   171
    }
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   172
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   173
    @Override
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   174
    public void finished(TaskEvent e) {
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   175
        System.out.println("Finished: " + e);
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   176
        if (e.getKind() == TaskEvent.Kind.ANALYZE) {
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   177
            for (Runnable r: runnables) {
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   178
                System.out.println("running " + r);
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   179
                r.run();
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   180
            }
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   181
        }
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   182
    }
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   183
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   184
    void check(boolean b) {
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   185
        if (!b)
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   186
            throw new AssertionError();
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   187
    }
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   188
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   189
    public static void add(ProcessingEnvironment env, Runnable r) {
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   190
        try {
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 10637
diff changeset
   191
            JavacTask task = JavacTask.instance(env);
1990493b64db 7093891: support multiple task listeners
jjg
parents: 10637
diff changeset
   192
            TaskListener l = ((BasicJavacTask) task).getTaskListeners().iterator().next();
10637
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   193
            // The TaskListener is an instanceof TestClose, but when using the
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   194
            // default class loaders. the taskListener uses a different
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   195
            // instance of Class<TestClose> than the anno processor.
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   196
            // If you try to evaluate
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 10637
diff changeset
   197
            //      TestClose tc = (TestClose) (l).
10637
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   198
            // you get the following somewhat confusing error:
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   199
            //   java.lang.ClassCastException: TestClose cannot be cast to TestClose
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   200
            // The workaround is to access the fields of TestClose with reflection.
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 10637
diff changeset
   201
            Field f = l.getClass().getField("runnables");
10637
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   202
            @SuppressWarnings("unchecked")
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 10637
diff changeset
   203
            List<Runnable> runnables = (List<Runnable>) f.get(l);
10637
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   204
            runnables.add(r);
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   205
        } catch (Throwable t) {
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 10637
diff changeset
   206
            t.printStackTrace();
10637
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   207
        }
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   208
    }
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   209
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   210
    public List<Runnable> runnables = new ArrayList<>();
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   211
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   212
    class MemFile extends SimpleJavaFileObject {
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   213
        public final String text;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   214
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   215
        MemFile(String name, String text) {
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   216
            super(URI.create(name), JavaFileObject.Kind.SOURCE);
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   217
            this.text = text;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   218
        }
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   219
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   220
        @Override
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   221
        public String getName() {
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   222
            return uri.toString();
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   223
        }
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   224
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   225
        @Override
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   226
        public String getCharContent(boolean ignoreEncodingErrors) {
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   227
            return text;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   228
        }
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   229
    }
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   230
}