langtools/test/tools/javac/processing/loader/testClose/TestClose2.java
author akulyakh
Thu, 21 May 2015 11:41:04 -0700
changeset 30730 d3ce7619db2c
parent 10637 2ea5fbb913ac
child 36526 3b41f1c69604
permissions -rw-r--r--
8076543: Add @modules as needed to the langtools tests Reviewed-by: jjg, shurailine
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
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 10637
diff changeset
     2
 * Copyright (c) 2011, 2015, 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
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 10637
diff changeset
    28
 * @modules jdk.compiler/com.sun.tools.javac.api
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 10637
diff changeset
    29
 *          jdk.compiler/com.sun.tools.javac.file
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 10637
diff changeset
    30
 *          jdk.compiler/com.sun.tools.javac.util
10637
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    31
 */
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    32
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    33
import com.sun.source.util.JavacTask;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    34
import com.sun.source.util.TaskEvent;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    35
import com.sun.source.util.TaskListener;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    36
import com.sun.tools.javac.api.JavacTool;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    37
import com.sun.tools.javac.file.JavacFileManager;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    38
import com.sun.tools.javac.util.Context;
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.net.URL;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    42
import java.net.URLClassLoader;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    43
import java.util.Arrays;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    44
import java.util.Collections;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    45
import java.util.List;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    46
import java.util.Set;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    47
import javax.annotation.processing.AbstractProcessor;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    48
import javax.annotation.processing.Messager;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    49
import javax.annotation.processing.RoundEnvironment;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    50
import javax.annotation.processing.SupportedAnnotationTypes;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    51
import javax.lang.model.SourceVersion;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    52
import javax.lang.model.element.TypeElement;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    53
import javax.tools.Diagnostic;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    54
import javax.tools.JavaFileObject;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    55
import javax.tools.StandardJavaFileManager;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    56
import javax.tools.StandardLocation;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    57
import javax.tools.ToolProvider;
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
@SupportedAnnotationTypes("*")
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    60
public class TestClose2 extends AbstractProcessor implements TaskListener {
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    61
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    62
    public static void main(String... args) throws Exception {
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    63
        new TestClose2().run();
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    64
    }
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    65
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    66
    void run() throws IOException {
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    67
        File testSrc = new File(System.getProperty("test.src"));
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    68
        File testClasses = new File(System.getProperty("test.classes"));
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    69
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    70
        JavacTool tool = (JavacTool) ToolProvider.getSystemJavaCompiler();
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    71
        final ClassLoader cl = getClass().getClassLoader();
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    72
        Context c = new Context();
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    73
        StandardJavaFileManager fm = new JavacFileManager(c, true, null) {
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    74
            @Override
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    75
            protected ClassLoader getClassLoader(URL[] urls) {
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    76
                return new URLClassLoader(urls, cl) {
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    77
                    @Override
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    78
                    public void close() throws IOException {
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    79
                        System.err.println(getClass().getName() + " closing");
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    80
                        TestClose2.this.closedCount++;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    81
                        TestClose2.this.closedIsLast = true;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    82
                        super.close();
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    83
                    }
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    84
                };
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    85
            }
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    86
        };
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    87
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    88
        fm.setLocation(StandardLocation.CLASS_OUTPUT,
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    89
                Collections.singleton(new File(".")));
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    90
        fm.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH,
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    91
                Collections.singleton(testClasses));
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    92
        Iterable<? extends JavaFileObject> files =
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    93
                fm.getJavaFileObjects(new File(testSrc, TestClose2.class.getName() + ".java"));
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    94
        List<String> options = Arrays.asList(
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    95
                "-processor", TestClose2.class.getName());
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    96
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    97
        JavacTask task = tool.getTask(null, fm, null, options, null, files);
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    98
        task.setTaskListener(this);
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
    99
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   100
        if (!task.call())
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   101
            throw new Error("compilation failed");
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   102
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   103
        if (closedCount == 0)
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   104
            throw new Error("no closing message");
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   105
        else if (closedCount > 1)
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   106
            throw new Error(closedCount + " closed messages");
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   107
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   108
        if (!closedIsLast)
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   109
            throw new Error("closing message not last");
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   110
    }
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   111
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   112
    // AbstractProcessor methods
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
    @Override
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   115
    public SourceVersion getSupportedSourceVersion() {
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   116
        return SourceVersion.latest();
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
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   119
    @Override
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   120
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   121
        Messager messager = processingEnv.getMessager();
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   122
        messager.printMessage(Diagnostic.Kind.NOTE, "processing");
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   123
        return true;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   124
    }
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   125
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   126
    // TaskListener methods
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   127
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   128
    @Override
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   129
    public void started(TaskEvent e) {
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   130
        System.err.println("Started: " + e);
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   131
        closedIsLast = false;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   132
    }
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   133
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   134
    @Override
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   135
    public void finished(TaskEvent e) {
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   136
        System.err.println("Finished: " + e);
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   137
        closedIsLast = false;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   138
    }
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   139
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   140
    //
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   141
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   142
    int closedCount = 0;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   143
    boolean closedIsLast = false;
2ea5fbb913ac 7092965: javac should not close processorClassLoader before end of compilation
jjg
parents:
diff changeset
   144
}