aoqi@0: /* aoqi@0: * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 7031108 aoqi@0: * @summary NPE in javac.jvm.ClassReader.findMethod in PackageElement.enclosedElements from AP in incr build aoqi@0: * @library /tools/javac/lib aoqi@0: * @build JavacTestingAbstractProcessor T7031108 aoqi@0: * @run main T7031108 aoqi@0: */ aoqi@0: aoqi@0: import java.io.*; aoqi@0: import java.net.*; aoqi@0: import java.util.*; aoqi@0: import javax.annotation.processing.*; aoqi@0: import javax.lang.model.element.*; aoqi@0: import javax.tools.*; aoqi@0: import javax.tools.JavaCompiler.CompilationTask; aoqi@0: aoqi@0: public class T7031108 extends JavacTestingAbstractProcessor { aoqi@0: public static void main(String... args) throws Exception { aoqi@0: new T7031108().run(); aoqi@0: } aoqi@0: aoqi@0: /* Class containing a local class definition; aoqi@0: * compiled class file will have an EnclosedMethod attribute. aoqi@0: */ aoqi@0: static final JavaSource pC = aoqi@0: new JavaSource("p/C.java", aoqi@0: "package p;\n" aoqi@0: + "class C {\n" aoqi@0: + " void m() {\n" aoqi@0: + " new Runnable() {\n" aoqi@0: + " public void run() {\n" aoqi@0: + " new Runnable() {\n" aoqi@0: + " public void run() { }\n" aoqi@0: + " };\n" aoqi@0: + " }\n" aoqi@0: + " };\n" aoqi@0: + " }\n" aoqi@0: + "}"); aoqi@0: aoqi@0: /* Dummy source file to compile while running anno processor. */ aoqi@0: static final JavaSource dummy = aoqi@0: new JavaSource("Dummy.java", aoqi@0: "class Dummy { }"); aoqi@0: aoqi@0: void run() throws Exception { aoqi@0: JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); aoqi@0: StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null); aoqi@0: aoqi@0: // step 1: compile test classes aoqi@0: File cwd = new File("."); aoqi@0: fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(cwd)); aoqi@0: compile(comp, fm, null, null, pC); aoqi@0: aoqi@0: // step 2: verify functioning of processor aoqi@0: fm.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH, aoqi@0: fm.getLocation(StandardLocation.CLASS_PATH)); aoqi@0: fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(cwd)); aoqi@0: compile(comp, fm, null, getClass().getName(), dummy); aoqi@0: aoqi@0: File pC_class = new File(new File("p"), "C.class"); aoqi@0: pC_class.delete(); aoqi@0: aoqi@0: DiagnosticCollector dc = new DiagnosticCollector(); aoqi@0: compile(comp, fm, dc, getClass().getName(), dummy); aoqi@0: List> diags =dc.getDiagnostics(); aoqi@0: aoqi@0: System.err.println(diags); aoqi@0: switch (diags.size()) { aoqi@0: case 0: aoqi@0: throw new Exception("no diagnostics received"); aoqi@0: case 1: aoqi@0: String code = diags.get(0).getCode(); aoqi@0: String expect = "compiler.err.proc.cant.access.1"; aoqi@0: if (!expect.equals(code)) aoqi@0: throw new Exception("unexpected diag code: " + code aoqi@0: + ", expected: " + expect); aoqi@0: break; aoqi@0: default: aoqi@0: throw new Exception("unexpected diags received"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void compile(JavaCompiler comp, JavaFileManager fm, aoqi@0: DiagnosticListener dl, aoqi@0: String processor, JavaFileObject... files) throws Exception { aoqi@0: System.err.println("compile processor:" + processor + ", files:" + Arrays.asList(files)); aoqi@0: List opts = new ArrayList(); aoqi@0: if (processor != null) { aoqi@0: // opts.add("-verbose"); aoqi@0: opts.addAll(Arrays.asList("-processor", processor)); aoqi@0: } aoqi@0: CompilationTask task = comp.getTask(null, fm, dl, opts, null, Arrays.asList(files)); aoqi@0: boolean ok = task.call(); aoqi@0: if (dl == null && !ok) aoqi@0: throw new Exception("compilation failed"); aoqi@0: } aoqi@0: aoqi@0: static class JavaSource extends SimpleJavaFileObject { aoqi@0: JavaSource(String name, String text) { aoqi@0: super(URI.create("js://" + name), JavaFileObject.Kind.SOURCE); aoqi@0: this.text = text; aoqi@0: } aoqi@0: @Override aoqi@0: public CharSequence getCharContent(boolean ignoreEncodingErrors) { aoqi@0: return text; aoqi@0: } aoqi@0: final String text; aoqi@0: } aoqi@0: aoqi@0: // annotation processor method aoqi@0: aoqi@0: @Override aoqi@0: public boolean process(Set annotations, RoundEnvironment roundEnv) { aoqi@0: if (!roundEnv.processingOver()) { aoqi@0: PackageElement p = elements.getPackageElement("p"); aoqi@0: List elems = p.getEnclosedElements(); aoqi@0: System.err.println("contents of package p: " + elems); aoqi@0: if (elems.size() != 1 || !elems.get(0).getSimpleName().contentEquals("C")) { aoqi@0: messager.printMessage(Diagnostic.Kind.ERROR, "unexpected package contents"); aoqi@0: } aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: } aoqi@0: