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 35 import java.io.*; 36 import java.net.*; 37 import java.util.*; 38 import javax.annotation.processing.*; 39 import javax.lang.model.element.*; 40 import javax.tools.*; 41 import javax.tools.JavaCompiler.CompilationTask; 42 43 public 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 private static final String PACKAGE_CONTENT_ERROR = "package does not contain C"; 67 68 /* Dummy source file to compile while running anno processor. */ 69 static final JavaSource dummy = 70 new JavaSource("Dummy.java", 71 "class Dummy { }"); 72 73 void run() throws Exception { 74 JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); 75 try (StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null)) { 76 77 // step 1: compile test classes 78 File cwd = new File("."); 79 fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(cwd)); 80 compile(comp, fm, null, null, pC); 81 82 // step 2: verify functioning of processor 83 fm.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH, 84 fm.getLocation(StandardLocation.CLASS_PATH)); 85 fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(cwd)); 86 compile(comp, fm, null, getClass().getName(), dummy); 87 88 File pC_class = new File(new File("p"), "C.class"); 89 pC_class.delete(); 90 91 DiagnosticCollector<JavaFileObject> dc = new DiagnosticCollector<JavaFileObject>(); 92 compile(comp, fm, dc, getClass().getName(), dummy); 93 List<Diagnostic<? extends JavaFileObject>> diags =dc.getDiagnostics(); 94 95 System.err.println(diags); 96 switch (diags.size()) { 97 case 0: 98 throw new Exception("no diagnostics received"); 99 case 1: 100 String code = diags.get(0).getCode(); 101 String expect = "compiler.err.proc.messager"; 102 if (!expect.equals(code)) 103 throw new Exception("unexpected diag code: " + code 104 + ", expected: " + expect); 105 String message = diags.get(0).getMessage(null); 106 if (!PACKAGE_CONTENT_ERROR.equals(message)) { 107 throw new Exception("unexpected diag message: " + code 108 + ", expected: " + PACKAGE_CONTENT_ERROR); 109 } 110 break; 111 default: 112 throw new Exception("unexpected diags received"); 113 } 114 } 115 } 116 117 void compile(JavaCompiler comp, JavaFileManager fm, 118 DiagnosticListener<JavaFileObject> dl, 119 String processor, JavaFileObject... files) throws Exception { 120 System.err.println("compile processor:" + processor + ", files:" + Arrays.asList(files)); 121 List<String> opts = new ArrayList<String>(); 122 if (processor != null) { 123 // opts.add("-verbose"); 124 opts.addAll(Arrays.asList("-processor", processor)); 125 } 126 CompilationTask task = comp.getTask(null, fm, dl, opts, null, Arrays.asList(files)); 127 boolean ok = task.call(); 128 if (dl == null && !ok) 129 throw new Exception("compilation failed"); 130 } 131 132 static class JavaSource extends SimpleJavaFileObject { 133 JavaSource(String name, String text) { 134 super(URI.create("js://" + name), JavaFileObject.Kind.SOURCE); 135 this.text = text; 136 } 137 @Override 138 public CharSequence getCharContent(boolean ignoreEncodingErrors) { 139 return text; 140 } 141 final String text; 142 } 143 144 // annotation processor method 145 146 @Override 147 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 148 if (!roundEnv.processingOver()) { 149 PackageElement p = elements.getPackageElement("p"); 150 List<? extends Element> elems = p.getEnclosedElements(); 151 System.err.println("contents of package p: " + elems); 152 if (elems.size() != 1 || !elems.get(0).getSimpleName().contentEquals("C")) { 153 messager.printMessage(Diagnostic.Kind.ERROR, PACKAGE_CONTENT_ERROR); 154 } 155 } 156 return true; 157 } 158 } 159