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