1/*
2 * Copyright (c) 2010, 2015, 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 7159016
27 * @summary Static import of member in processor-generated class fails in JDK 7
28 * @library lib
29 * @modules java.compiler
30 *          jdk.compiler
31 * @build JavacTestingAbstractProcessor
32 * @run main T7159016
33 * @author Jessie Glick
34 */
35
36import java.io.File;
37import java.io.FileWriter;
38import java.io.IOException;
39import java.io.Writer;
40import java.util.Collections;
41import java.util.Set;
42import javax.annotation.processing.AbstractProcessor;
43import javax.annotation.processing.RoundEnvironment;
44import javax.annotation.processing.SupportedAnnotationTypes;
45import javax.annotation.processing.SupportedSourceVersion;
46import javax.lang.model.SourceVersion;
47import javax.lang.model.element.TypeElement;
48import javax.tools.Diagnostic;
49import javax.tools.JavaCompiler;
50import javax.tools.StandardJavaFileManager;
51import javax.tools.ToolProvider;
52
53public class T7159016 {
54    public static void main(String[] args) throws Exception {
55        File src = new File("C.java");
56        Writer w = new FileWriter(src);
57        try {
58            w.write("import static p.Generated.m;\nclass C { {m(); } }\n");
59            w.flush();
60        } finally {
61            w.close();
62        }
63        JavaCompiler jc = ToolProvider.getSystemJavaCompiler();
64        try (StandardJavaFileManager fm = jc.getStandardFileManager(null, null, null)) {
65            JavaCompiler.CompilationTask task = jc.getTask(null, fm, null, null, null,
66                                                           fm.getJavaFileObjects(src));
67            task.setProcessors(Collections.singleton(new Proc()));
68            if (!task.call()) {
69                throw new Error("Test failed");
70            }
71        }
72    }
73
74    private static class Proc extends JavacTestingAbstractProcessor {
75        int written;
76        @Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
77            if (roundEnv.processingOver() || written++ > 0) {
78                return false;
79            }
80            messager.printMessage(Diagnostic.Kind.NOTE, "writing Generated.java");
81            try {
82                Writer w = processingEnv.getFiler().createSourceFile("p.Generated").openWriter();
83                try {
84                    w.write("package p; public class Generated { public static void m() { } }");
85                } finally {
86                    w.close();
87                }
88            } catch (IOException x) {
89                messager.printMessage(Diagnostic.Kind.ERROR, x.toString());
90            }
91            return true;
92        }
93    }
94}
95