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