Fri, 24 Apr 2020 03:58:51 +0100
Merge
jjg@952 | 1 | /* |
jjg@952 | 2 | * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. |
jjg@952 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@952 | 4 | * |
jjg@952 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@952 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jjg@952 | 7 | * published by the Free Software Foundation. |
jjg@952 | 8 | * |
jjg@952 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@952 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@952 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@952 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@952 | 13 | * accompanied this code). |
jjg@952 | 14 | * |
jjg@952 | 15 | * You should have received a copy of the GNU General Public License version |
jjg@952 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@952 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@952 | 18 | * |
jjg@952 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
jjg@952 | 20 | * or visit www.oracle.com if you need additional information or have any |
jjg@952 | 21 | * questions. |
jjg@952 | 22 | */ |
jjg@952 | 23 | |
jjg@952 | 24 | /* |
jjg@952 | 25 | * @test |
jjg@952 | 26 | * @bug 7031108 |
jjg@952 | 27 | * @summary NPE in javac.jvm.ClassReader.findMethod in PackageElement.enclosedElements from AP in incr build |
darcy@1466 | 28 | * @library /tools/javac/lib |
jjg@952 | 29 | * @build JavacTestingAbstractProcessor T7031108 |
jjg@952 | 30 | * @run main T7031108 |
jjg@952 | 31 | */ |
jjg@952 | 32 | |
jjg@952 | 33 | import java.io.*; |
jjg@952 | 34 | import java.net.*; |
jjg@952 | 35 | import java.util.*; |
jjg@952 | 36 | import javax.annotation.processing.*; |
jjg@952 | 37 | import javax.lang.model.element.*; |
jjg@952 | 38 | import javax.tools.*; |
jjg@952 | 39 | import javax.tools.JavaCompiler.CompilationTask; |
jjg@952 | 40 | |
jjg@952 | 41 | public class T7031108 extends JavacTestingAbstractProcessor { |
jjg@952 | 42 | public static void main(String... args) throws Exception { |
jjg@952 | 43 | new T7031108().run(); |
jjg@952 | 44 | } |
jjg@952 | 45 | |
jjg@952 | 46 | /* Class containing a local class definition; |
jjg@952 | 47 | * compiled class file will have an EnclosedMethod attribute. |
jjg@952 | 48 | */ |
jjg@952 | 49 | static final JavaSource pC = |
jjg@952 | 50 | new JavaSource("p/C.java", |
jjg@952 | 51 | "package p;\n" |
jjg@952 | 52 | + "class C {\n" |
jjg@952 | 53 | + " void m() {\n" |
jjg@952 | 54 | + " new Runnable() {\n" |
jjg@952 | 55 | + " public void run() {\n" |
jjg@952 | 56 | + " new Runnable() {\n" |
jjg@952 | 57 | + " public void run() { }\n" |
jjg@952 | 58 | + " };\n" |
jjg@952 | 59 | + " }\n" |
jjg@952 | 60 | + " };\n" |
jjg@952 | 61 | + " }\n" |
jjg@952 | 62 | + "}"); |
jjg@952 | 63 | |
jjg@952 | 64 | /* Dummy source file to compile while running anno processor. */ |
jjg@952 | 65 | static final JavaSource dummy = |
jjg@952 | 66 | new JavaSource("Dummy.java", |
jjg@952 | 67 | "class Dummy { }"); |
jjg@952 | 68 | |
jjg@952 | 69 | void run() throws Exception { |
jjg@952 | 70 | JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); |
jjg@952 | 71 | StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null); |
jjg@952 | 72 | |
jjg@952 | 73 | // step 1: compile test classes |
jjg@952 | 74 | File cwd = new File("."); |
jjg@952 | 75 | fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(cwd)); |
jjg@952 | 76 | compile(comp, fm, null, null, pC); |
jjg@952 | 77 | |
jjg@952 | 78 | // step 2: verify functioning of processor |
jjg@952 | 79 | fm.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH, |
jjg@952 | 80 | fm.getLocation(StandardLocation.CLASS_PATH)); |
jjg@952 | 81 | fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(cwd)); |
jjg@952 | 82 | compile(comp, fm, null, getClass().getName(), dummy); |
jjg@952 | 83 | |
jjg@952 | 84 | File pC_class = new File(new File("p"), "C.class"); |
jjg@952 | 85 | pC_class.delete(); |
jjg@952 | 86 | |
jjg@952 | 87 | DiagnosticCollector<JavaFileObject> dc = new DiagnosticCollector<JavaFileObject>(); |
jjg@952 | 88 | compile(comp, fm, dc, getClass().getName(), dummy); |
jjg@952 | 89 | List<Diagnostic<? extends JavaFileObject>> diags =dc.getDiagnostics(); |
jjg@952 | 90 | |
jjg@952 | 91 | System.err.println(diags); |
jjg@952 | 92 | switch (diags.size()) { |
jjg@952 | 93 | case 0: |
jjg@952 | 94 | throw new Exception("no diagnostics received"); |
jjg@952 | 95 | case 1: |
jjg@952 | 96 | String code = diags.get(0).getCode(); |
jjg@952 | 97 | String expect = "compiler.err.proc.cant.access.1"; |
jjg@952 | 98 | if (!expect.equals(code)) |
jjg@952 | 99 | throw new Exception("unexpected diag code: " + code |
jjg@952 | 100 | + ", expected: " + expect); |
jjg@952 | 101 | break; |
jjg@952 | 102 | default: |
jjg@952 | 103 | throw new Exception("unexpected diags received"); |
jjg@952 | 104 | } |
jjg@952 | 105 | } |
jjg@952 | 106 | |
jjg@952 | 107 | void compile(JavaCompiler comp, JavaFileManager fm, |
jjg@952 | 108 | DiagnosticListener<JavaFileObject> dl, |
jjg@952 | 109 | String processor, JavaFileObject... files) throws Exception { |
jjg@952 | 110 | System.err.println("compile processor:" + processor + ", files:" + Arrays.asList(files)); |
jjg@952 | 111 | List<String> opts = new ArrayList<String>(); |
jjg@952 | 112 | if (processor != null) { |
jjg@952 | 113 | // opts.add("-verbose"); |
jjg@952 | 114 | opts.addAll(Arrays.asList("-processor", processor)); |
jjg@952 | 115 | } |
jjg@952 | 116 | CompilationTask task = comp.getTask(null, fm, dl, opts, null, Arrays.asList(files)); |
jjg@952 | 117 | boolean ok = task.call(); |
jjg@952 | 118 | if (dl == null && !ok) |
jjg@952 | 119 | throw new Exception("compilation failed"); |
jjg@952 | 120 | } |
jjg@952 | 121 | |
jjg@952 | 122 | static class JavaSource extends SimpleJavaFileObject { |
jjg@952 | 123 | JavaSource(String name, String text) { |
jjg@952 | 124 | super(URI.create("js://" + name), JavaFileObject.Kind.SOURCE); |
jjg@952 | 125 | this.text = text; |
jjg@952 | 126 | } |
jjg@952 | 127 | @Override |
jjg@952 | 128 | public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
jjg@952 | 129 | return text; |
jjg@952 | 130 | } |
jjg@952 | 131 | final String text; |
jjg@952 | 132 | } |
jjg@952 | 133 | |
jjg@952 | 134 | // annotation processor method |
jjg@952 | 135 | |
jjg@952 | 136 | @Override |
jjg@952 | 137 | public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
jjg@952 | 138 | if (!roundEnv.processingOver()) { |
jjg@952 | 139 | PackageElement p = elements.getPackageElement("p"); |
jjg@952 | 140 | List<? extends Element> elems = p.getEnclosedElements(); |
jjg@952 | 141 | System.err.println("contents of package p: " + elems); |
jjg@952 | 142 | if (elems.size() != 1 || !elems.get(0).getSimpleName().contentEquals("C")) { |
jjg@952 | 143 | messager.printMessage(Diagnostic.Kind.ERROR, "unexpected package contents"); |
jjg@952 | 144 | } |
jjg@952 | 145 | } |
jjg@952 | 146 | return true; |
jjg@952 | 147 | } |
jjg@952 | 148 | } |
jjg@952 | 149 |