1/*
2 * Copyright (c) 2011, 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 7031108
27 * @summary NPE in javac.jvm.ClassReader.findMethod in PackageElement.enclosedElements from AP in incr build
28 * @library /tools/javac/lib
29 * @modules java.compiler
30 *          jdk.compiler
31 * @build JavacTestingAbstractProcessor T7031108
32 * @run main T7031108
33 */
34
35import java.io.*;
36import java.net.*;
37import java.util.*;
38import javax.annotation.processing.*;
39import javax.lang.model.element.*;
40import javax.tools.*;
41import javax.tools.JavaCompiler.CompilationTask;
42
43public class T7031108 extends JavacTestingAbstractProcessor {
44    public static void main(String... args) throws Exception {
45        new T7031108().run();
46    }
47
48    /* Class containing a local class definition;
49     * compiled class file will have an EnclosedMethod attribute.
50     */
51    static final JavaSource pC =
52            new JavaSource("p/C.java",
53                  "package p;\n"
54                + "class C {\n"
55                + "    void m() {\n"
56                + "        new Runnable() {\n"
57                + "            public void run() {\n"
58                + "                new Runnable() {\n"
59                + "                    public void run() { }\n"
60                + "                };\n"
61                + "            }\n"
62                + "        };\n"
63                + "    }\n"
64                + "}");
65
66    /* Dummy source file to compile while running anno processor. */
67    static final JavaSource dummy =
68            new JavaSource("Dummy.java",
69                "class Dummy { }");
70
71    void run() throws Exception {
72        JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
73        try (StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null)) {
74
75            // step 1: compile test classes
76            File cwd = new File(".");
77            fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(cwd));
78            compile(comp, fm, null, null, pC);
79
80            // step 2: verify functioning of processor
81            fm.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH,
82                    fm.getLocation(StandardLocation.CLASS_PATH));
83            fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(cwd));
84            compile(comp, fm, null, getClass().getName(), dummy);
85
86            File pC_class = new File(new File("p"), "C.class");
87            pC_class.delete();
88
89            DiagnosticCollector<JavaFileObject> dc = new DiagnosticCollector<JavaFileObject>();
90            compile(comp, fm, dc, getClass().getName(), dummy);
91            List<Diagnostic<? extends JavaFileObject>> diags =dc.getDiagnostics();
92
93            System.err.println(diags);
94            switch (diags.size()) {
95                case 0:
96                    throw new Exception("no diagnostics received");
97                case 1:
98                    String code = diags.get(0).getCode();
99                    String expect = "compiler.err.proc.cant.access.1";
100                    if (!expect.equals(code))
101                        throw new Exception("unexpected diag code: " + code
102                                + ", expected: " + expect);
103                    break;
104                default:
105                    throw new Exception("unexpected diags received");
106            }
107        }
108    }
109
110    void compile(JavaCompiler comp, JavaFileManager fm,
111            DiagnosticListener<JavaFileObject> dl,
112            String processor, JavaFileObject... files) throws Exception {
113        System.err.println("compile processor:" + processor + ", files:" + Arrays.asList(files));
114        List<String> opts = new ArrayList<String>();
115        if (processor != null) {
116            // opts.add("-verbose");
117            opts.addAll(Arrays.asList("-processor", processor));
118        }
119        CompilationTask task = comp.getTask(null, fm, dl, opts, null, Arrays.asList(files));
120        boolean ok = task.call();
121        if (dl == null && !ok)
122            throw new Exception("compilation failed");
123    }
124
125    static class JavaSource extends SimpleJavaFileObject {
126        JavaSource(String name, String text) {
127            super(URI.create("js://" + name), JavaFileObject.Kind.SOURCE);
128            this.text = text;
129        }
130        @Override
131        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
132            return text;
133        }
134        final String text;
135    }
136
137    // annotation processor method
138
139    @Override
140    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
141        if (!roundEnv.processingOver()) {
142            PackageElement p = elements.getPackageElement("p");
143            List<? extends Element> elems = p.getEnclosedElements();
144            System.err.println("contents of package p: " + elems);
145            if (elems.size() != 1 || !elems.get(0).getSimpleName().contentEquals("C")) {
146                messager.printMessage(Diagnostic.Kind.ERROR, "unexpected package contents");
147            }
148        }
149        return true;
150    }
151}
152
153