T7031108.java revision 1465:b52a38d4536c
1111716Smckusick/* 2111716Smckusick * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 3111716Smckusick * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4111716Smckusick * 5111716Smckusick * This code is free software; you can redistribute it and/or modify it 6111716Smckusick * under the terms of the GNU General Public License version 2 only, as 7111716Smckusick * published by the Free Software Foundation. 8111716Smckusick * 9111716Smckusick * This code is distributed in the hope that it will be useful, but WITHOUT 10111716Smckusick * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11111716Smckusick * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12111716Smckusick * version 2 for more details (a copy is included in the LICENSE file that 13111716Smckusick * accompanied this code). 14111716Smckusick * 15111716Smckusick * You should have received a copy of the GNU General Public License version 16111716Smckusick * 2 along with this work; if not, write to the Free Software Foundation, 17111716Smckusick * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18111716Smckusick * 19111716Smckusick * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20111716Smckusick * or visit www.oracle.com if you need additional information or have any 21111716Smckusick * questions. 22111716Smckusick */ 23111716Smckusick 24111716Smckusick/* 25111716Smckusick * @test 26111716Smckusick * @bug 7031108 27111716Smckusick * @summary NPE in javac.jvm.ClassReader.findMethod in PackageElement.enclosedElements from AP in incr build 28111716Smckusick * @library /tools/javac/lib 29111716Smckusick * @build JavacTestingAbstractProcessor T7031108 30111716Smckusick * @run main T7031108 31111716Smckusick */ 32111716Smckusick 33111716Smckusickimport java.io.*; 34111716Smckusickimport java.net.*; 35111716Smckusickimport java.util.*; 36111716Smckusickimport javax.annotation.processing.*; 37218700Skeramidaimport javax.lang.model.element.*; 38111716Smckusickimport javax.tools.*; 39111716Smckusickimport javax.tools.JavaCompiler.CompilationTask; 40111716Smckusick 41111716Smckusickpublic class T7031108 extends JavacTestingAbstractProcessor { 42115288Sru public static void main(String... args) throws Exception { 43111716Smckusick new T7031108().run(); 44111716Smckusick } 45111716Smckusick 46111716Smckusick /* Class containing a local class definition; 47111716Smckusick * compiled class file will have an EnclosedMethod attribute. 48111716Smckusick */ 49115288Sru static final JavaSource pC = 50193051Spjd new JavaSource("p/C.java", 51111716Smckusick "package p;\n" 52111716Smckusick + "class C {\n" 53115288Sru + " void m() {\n" 54111716Smckusick + " new Runnable() {\n" 55115288Sru + " public void run() {\n" 56111716Smckusick + " new Runnable() {\n" 57111716Smckusick + " public void run() { }\n" 58115288Sru + " };\n" 59111716Smckusick + " }\n" 60193051Spjd + " };\n" 61193051Spjd + " }\n" 62193051Spjd + "}"); 63193051Spjd 64193051Spjd /* Dummy source file to compile while running anno processor. */ 65193051Spjd static final JavaSource dummy = 66193051Spjd new JavaSource("Dummy.java", 67193051Spjd "class Dummy { }"); 68193051Spjd 69111716Smckusick void run() throws Exception { 70111716Smckusick JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); 71111716Smckusick StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null); 72197275Sbrueffer 73119845Scharnier // step 1: compile test classes 74111716Smckusick File cwd = new File("."); 75115288Sru fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(cwd)); 76111716Smckusick compile(comp, fm, null, null, pC); 77115288Sru 78111716Smckusick // step 2: verify functioning of processor 79208027Suqs fm.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH, 80208027Suqs fm.getLocation(StandardLocation.CLASS_PATH)); 81208027Suqs fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(cwd)); 82218700Skeramida compile(comp, fm, null, getClass().getName(), dummy); 83218700Skeramida 84218700Skeramida File pC_class = new File(new File("p"), "C.class"); 85218700Skeramida pC_class.delete(); 86218700Skeramida 87218700Skeramida DiagnosticCollector<JavaFileObject> dc = new DiagnosticCollector<JavaFileObject>(); 88218700Skeramida compile(comp, fm, dc, getClass().getName(), dummy); 89218700Skeramida List<Diagnostic<? extends JavaFileObject>> diags =dc.getDiagnostics(); 90218700Skeramida 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 void compile(JavaCompiler comp, JavaFileManager fm, 108 DiagnosticListener<JavaFileObject> dl, 109 String processor, JavaFileObject... files) throws Exception { 110 System.err.println("compile processor:" + processor + ", files:" + Arrays.asList(files)); 111 List<String> opts = new ArrayList<String>(); 112 if (processor != null) { 113 // opts.add("-verbose"); 114 opts.addAll(Arrays.asList("-processor", processor)); 115 } 116 CompilationTask task = comp.getTask(null, fm, dl, opts, null, Arrays.asList(files)); 117 boolean ok = task.call(); 118 if (dl == null && !ok) 119 throw new Exception("compilation failed"); 120 } 121 122 static class JavaSource extends SimpleJavaFileObject { 123 JavaSource(String name, String text) { 124 super(URI.create("js://" + name), JavaFileObject.Kind.SOURCE); 125 this.text = text; 126 } 127 @Override 128 public CharSequence getCharContent(boolean ignoreEncodingErrors) { 129 return text; 130 } 131 final String text; 132 } 133 134 // annotation processor method 135 136 @Override 137 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 138 if (!roundEnv.processingOver()) { 139 PackageElement p = elements.getPackageElement("p"); 140 List<? extends Element> elems = p.getEnclosedElements(); 141 System.err.println("contents of package p: " + elems); 142 if (elems.size() != 1 || !elems.get(0).getSimpleName().contentEquals("C")) { 143 messager.printMessage(Diagnostic.Kind.ERROR, "unexpected package contents"); 144 } 145 } 146 return true; 147 } 148} 149 150