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