test/tools/javac/processing/loader/testClose/TestClose2.java

changeset 1096
b0d5f00e69f7
parent 0
959103a6100f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/processing/loader/testClose/TestClose2.java	Wed Sep 21 21:56:53 2011 -0700
     1.3 @@ -0,0 +1,141 @@
     1.4 +/*
     1.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 7092965
    1.30 + * @summary javac should not close processorClassLoader before end of compilation
    1.31 + */
    1.32 +
    1.33 +import com.sun.source.util.JavacTask;
    1.34 +import com.sun.source.util.TaskEvent;
    1.35 +import com.sun.source.util.TaskListener;
    1.36 +import com.sun.tools.javac.api.JavacTool;
    1.37 +import com.sun.tools.javac.file.JavacFileManager;
    1.38 +import com.sun.tools.javac.util.Context;
    1.39 +import java.io.File;
    1.40 +import java.io.IOException;
    1.41 +import java.net.URL;
    1.42 +import java.net.URLClassLoader;
    1.43 +import java.util.Arrays;
    1.44 +import java.util.Collections;
    1.45 +import java.util.List;
    1.46 +import java.util.Set;
    1.47 +import javax.annotation.processing.AbstractProcessor;
    1.48 +import javax.annotation.processing.Messager;
    1.49 +import javax.annotation.processing.RoundEnvironment;
    1.50 +import javax.annotation.processing.SupportedAnnotationTypes;
    1.51 +import javax.lang.model.SourceVersion;
    1.52 +import javax.lang.model.element.TypeElement;
    1.53 +import javax.tools.Diagnostic;
    1.54 +import javax.tools.JavaFileObject;
    1.55 +import javax.tools.StandardJavaFileManager;
    1.56 +import javax.tools.StandardLocation;
    1.57 +import javax.tools.ToolProvider;
    1.58 +
    1.59 +@SupportedAnnotationTypes("*")
    1.60 +public class TestClose2 extends AbstractProcessor implements TaskListener {
    1.61 +
    1.62 +    public static void main(String... args) throws Exception {
    1.63 +        new TestClose2().run();
    1.64 +    }
    1.65 +
    1.66 +    void run() throws IOException {
    1.67 +        File testSrc = new File(System.getProperty("test.src"));
    1.68 +        File testClasses = new File(System.getProperty("test.classes"));
    1.69 +
    1.70 +        JavacTool tool = (JavacTool) ToolProvider.getSystemJavaCompiler();
    1.71 +        final ClassLoader cl = getClass().getClassLoader();
    1.72 +        Context c = new Context();
    1.73 +        StandardJavaFileManager fm = new JavacFileManager(c, true, null) {
    1.74 +            @Override
    1.75 +            protected ClassLoader getClassLoader(URL[] urls) {
    1.76 +                return new URLClassLoader(urls, cl) {
    1.77 +                    @Override
    1.78 +                    public void close() throws IOException {
    1.79 +                        System.err.println(getClass().getName() + " closing");
    1.80 +                        TestClose2.this.closedCount++;
    1.81 +                        TestClose2.this.closedIsLast = true;
    1.82 +                        super.close();
    1.83 +                    }
    1.84 +                };
    1.85 +            }
    1.86 +        };
    1.87 +
    1.88 +        fm.setLocation(StandardLocation.CLASS_OUTPUT,
    1.89 +                Collections.singleton(new File(".")));
    1.90 +        fm.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH,
    1.91 +                Collections.singleton(testClasses));
    1.92 +        Iterable<? extends JavaFileObject> files =
    1.93 +                fm.getJavaFileObjects(new File(testSrc, TestClose2.class.getName() + ".java"));
    1.94 +        List<String> options = Arrays.asList(
    1.95 +                "-processor", TestClose2.class.getName());
    1.96 +
    1.97 +        JavacTask task = tool.getTask(null, fm, null, options, null, files);
    1.98 +        task.setTaskListener(this);
    1.99 +
   1.100 +        if (!task.call())
   1.101 +            throw new Error("compilation failed");
   1.102 +
   1.103 +        if (closedCount == 0)
   1.104 +            throw new Error("no closing message");
   1.105 +        else if (closedCount > 1)
   1.106 +            throw new Error(closedCount + " closed messages");
   1.107 +
   1.108 +        if (!closedIsLast)
   1.109 +            throw new Error("closing message not last");
   1.110 +    }
   1.111 +
   1.112 +    // AbstractProcessor methods
   1.113 +
   1.114 +    @Override
   1.115 +    public SourceVersion getSupportedSourceVersion() {
   1.116 +        return SourceVersion.latest();
   1.117 +    }
   1.118 +
   1.119 +    @Override
   1.120 +    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
   1.121 +        Messager messager = processingEnv.getMessager();
   1.122 +        messager.printMessage(Diagnostic.Kind.NOTE, "processing");
   1.123 +        return true;
   1.124 +    }
   1.125 +
   1.126 +    // TaskListener methods
   1.127 +
   1.128 +    @Override
   1.129 +    public void started(TaskEvent e) {
   1.130 +        System.err.println("Started: " + e);
   1.131 +        closedIsLast = false;
   1.132 +    }
   1.133 +
   1.134 +    @Override
   1.135 +    public void finished(TaskEvent e) {
   1.136 +        System.err.println("Finished: " + e);
   1.137 +        closedIsLast = false;
   1.138 +    }
   1.139 +
   1.140 +    //
   1.141 +
   1.142 +    int closedCount = 0;
   1.143 +    boolean closedIsLast = false;
   1.144 +}

mercurial