test/tools/javac/classreader/T7031108.java

Wed, 14 Nov 2018 10:18:25 -0800

author
diazhou
date
Wed, 14 Nov 2018 10:18:25 -0800
changeset 3762
7909abb85562
parent 1466
b52a38d4536c
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag jdk8u201-b04 for changeset a7f48b9dfb82

     1 /*
     2  * Copyright (c) 2011, 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  */
    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  * @build JavacTestingAbstractProcessor T7031108
    30  * @run main T7031108
    31  */
    33 import java.io.*;
    34 import java.net.*;
    35 import java.util.*;
    36 import javax.annotation.processing.*;
    37 import javax.lang.model.element.*;
    38 import javax.tools.*;
    39 import javax.tools.JavaCompiler.CompilationTask;
    41 public class T7031108 extends JavacTestingAbstractProcessor {
    42     public static void main(String... args) throws Exception {
    43         new T7031108().run();
    44     }
    46     /* Class containing a local class definition;
    47      * compiled class file will have an EnclosedMethod attribute.
    48      */
    49     static final JavaSource pC =
    50             new JavaSource("p/C.java",
    51                   "package p;\n"
    52                 + "class C {\n"
    53                 + "    void m() {\n"
    54                 + "        new Runnable() {\n"
    55                 + "            public void run() {\n"
    56                 + "                new Runnable() {\n"
    57                 + "                    public void run() { }\n"
    58                 + "                };\n"
    59                 + "            }\n"
    60                 + "        };\n"
    61                 + "    }\n"
    62                 + "}");
    64     /* Dummy source file to compile while running anno processor. */
    65     static final JavaSource dummy =
    66             new JavaSource("Dummy.java",
    67                 "class Dummy { }");
    69     void run() throws Exception {
    70         JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
    71         StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
    73         // step 1: compile test classes
    74         File cwd = new File(".");
    75         fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(cwd));
    76         compile(comp, fm, null, null, pC);
    78         // step 2: verify functioning of processor
    79         fm.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH,
    80                 fm.getLocation(StandardLocation.CLASS_PATH));
    81         fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(cwd));
    82         compile(comp, fm, null, getClass().getName(), dummy);
    84         File pC_class = new File(new File("p"), "C.class");
    85         pC_class.delete();
    87         DiagnosticCollector<JavaFileObject> dc = new DiagnosticCollector<JavaFileObject>();
    88         compile(comp, fm, dc, getClass().getName(), dummy);
    89         List<Diagnostic<? extends JavaFileObject>> diags =dc.getDiagnostics();
    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     }
   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     }
   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     }
   134     // annotation processor method
   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 }

mercurial