Wed, 27 Apr 2016 01:34:52 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | */ |
aoqi@0 | 23 | |
aoqi@0 | 24 | /* |
aoqi@0 | 25 | * @test |
aoqi@0 | 26 | * @bug 7092965 |
aoqi@0 | 27 | * @summary javac should not close processorClassLoader before end of compilation |
aoqi@0 | 28 | */ |
aoqi@0 | 29 | |
aoqi@0 | 30 | import com.sun.source.util.JavacTask; |
aoqi@0 | 31 | import com.sun.source.util.TaskEvent; |
aoqi@0 | 32 | import com.sun.source.util.TaskListener; |
aoqi@0 | 33 | import com.sun.tools.javac.api.JavacTool; |
aoqi@0 | 34 | import com.sun.tools.javac.file.JavacFileManager; |
aoqi@0 | 35 | import com.sun.tools.javac.util.Context; |
aoqi@0 | 36 | import java.io.File; |
aoqi@0 | 37 | import java.io.IOException; |
aoqi@0 | 38 | import java.net.URL; |
aoqi@0 | 39 | import java.net.URLClassLoader; |
aoqi@0 | 40 | import java.util.Arrays; |
aoqi@0 | 41 | import java.util.Collections; |
aoqi@0 | 42 | import java.util.List; |
aoqi@0 | 43 | import java.util.Set; |
aoqi@0 | 44 | import javax.annotation.processing.AbstractProcessor; |
aoqi@0 | 45 | import javax.annotation.processing.Messager; |
aoqi@0 | 46 | import javax.annotation.processing.RoundEnvironment; |
aoqi@0 | 47 | import javax.annotation.processing.SupportedAnnotationTypes; |
aoqi@0 | 48 | import javax.lang.model.SourceVersion; |
aoqi@0 | 49 | import javax.lang.model.element.TypeElement; |
aoqi@0 | 50 | import javax.tools.Diagnostic; |
aoqi@0 | 51 | import javax.tools.JavaFileObject; |
aoqi@0 | 52 | import javax.tools.StandardJavaFileManager; |
aoqi@0 | 53 | import javax.tools.StandardLocation; |
aoqi@0 | 54 | import javax.tools.ToolProvider; |
aoqi@0 | 55 | |
aoqi@0 | 56 | @SupportedAnnotationTypes("*") |
aoqi@0 | 57 | public class TestClose2 extends AbstractProcessor implements TaskListener { |
aoqi@0 | 58 | |
aoqi@0 | 59 | public static void main(String... args) throws Exception { |
aoqi@0 | 60 | new TestClose2().run(); |
aoqi@0 | 61 | } |
aoqi@0 | 62 | |
aoqi@0 | 63 | void run() throws IOException { |
aoqi@0 | 64 | File testSrc = new File(System.getProperty("test.src")); |
aoqi@0 | 65 | File testClasses = new File(System.getProperty("test.classes")); |
aoqi@0 | 66 | |
aoqi@0 | 67 | JavacTool tool = (JavacTool) ToolProvider.getSystemJavaCompiler(); |
aoqi@0 | 68 | final ClassLoader cl = getClass().getClassLoader(); |
aoqi@0 | 69 | Context c = new Context(); |
aoqi@0 | 70 | StandardJavaFileManager fm = new JavacFileManager(c, true, null) { |
aoqi@0 | 71 | @Override |
aoqi@0 | 72 | protected ClassLoader getClassLoader(URL[] urls) { |
aoqi@0 | 73 | return new URLClassLoader(urls, cl) { |
aoqi@0 | 74 | @Override |
aoqi@0 | 75 | public void close() throws IOException { |
aoqi@0 | 76 | System.err.println(getClass().getName() + " closing"); |
aoqi@0 | 77 | TestClose2.this.closedCount++; |
aoqi@0 | 78 | TestClose2.this.closedIsLast = true; |
aoqi@0 | 79 | super.close(); |
aoqi@0 | 80 | } |
aoqi@0 | 81 | }; |
aoqi@0 | 82 | } |
aoqi@0 | 83 | }; |
aoqi@0 | 84 | |
aoqi@0 | 85 | fm.setLocation(StandardLocation.CLASS_OUTPUT, |
aoqi@0 | 86 | Collections.singleton(new File("."))); |
aoqi@0 | 87 | fm.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH, |
aoqi@0 | 88 | Collections.singleton(testClasses)); |
aoqi@0 | 89 | Iterable<? extends JavaFileObject> files = |
aoqi@0 | 90 | fm.getJavaFileObjects(new File(testSrc, TestClose2.class.getName() + ".java")); |
aoqi@0 | 91 | List<String> options = Arrays.asList( |
aoqi@0 | 92 | "-processor", TestClose2.class.getName()); |
aoqi@0 | 93 | |
aoqi@0 | 94 | JavacTask task = tool.getTask(null, fm, null, options, null, files); |
aoqi@0 | 95 | task.setTaskListener(this); |
aoqi@0 | 96 | |
aoqi@0 | 97 | if (!task.call()) |
aoqi@0 | 98 | throw new Error("compilation failed"); |
aoqi@0 | 99 | |
aoqi@0 | 100 | if (closedCount == 0) |
aoqi@0 | 101 | throw new Error("no closing message"); |
aoqi@0 | 102 | else if (closedCount > 1) |
aoqi@0 | 103 | throw new Error(closedCount + " closed messages"); |
aoqi@0 | 104 | |
aoqi@0 | 105 | if (!closedIsLast) |
aoqi@0 | 106 | throw new Error("closing message not last"); |
aoqi@0 | 107 | } |
aoqi@0 | 108 | |
aoqi@0 | 109 | // AbstractProcessor methods |
aoqi@0 | 110 | |
aoqi@0 | 111 | @Override |
aoqi@0 | 112 | public SourceVersion getSupportedSourceVersion() { |
aoqi@0 | 113 | return SourceVersion.latest(); |
aoqi@0 | 114 | } |
aoqi@0 | 115 | |
aoqi@0 | 116 | @Override |
aoqi@0 | 117 | public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
aoqi@0 | 118 | Messager messager = processingEnv.getMessager(); |
aoqi@0 | 119 | messager.printMessage(Diagnostic.Kind.NOTE, "processing"); |
aoqi@0 | 120 | return true; |
aoqi@0 | 121 | } |
aoqi@0 | 122 | |
aoqi@0 | 123 | // TaskListener methods |
aoqi@0 | 124 | |
aoqi@0 | 125 | @Override |
aoqi@0 | 126 | public void started(TaskEvent e) { |
aoqi@0 | 127 | System.err.println("Started: " + e); |
aoqi@0 | 128 | closedIsLast = false; |
aoqi@0 | 129 | } |
aoqi@0 | 130 | |
aoqi@0 | 131 | @Override |
aoqi@0 | 132 | public void finished(TaskEvent e) { |
aoqi@0 | 133 | System.err.println("Finished: " + e); |
aoqi@0 | 134 | closedIsLast = false; |
aoqi@0 | 135 | } |
aoqi@0 | 136 | |
aoqi@0 | 137 | // |
aoqi@0 | 138 | |
aoqi@0 | 139 | int closedCount = 0; |
aoqi@0 | 140 | boolean closedIsLast = false; |
aoqi@0 | 141 | } |