7031108: NPE in javac.jvm.ClassReader.findMethod in PackageElement.enclosedElements from AP in incr build

Wed, 30 Mar 2011 18:18:11 -0700

author
jjg
date
Wed, 30 Mar 2011 18:18:11 -0700
changeset 952
02ba4ff98742
parent 951
de1c65ecfec2
child 953
c55928005af4

7031108: NPE in javac.jvm.ClassReader.findMethod in PackageElement.enclosedElements from AP in incr build
Reviewed-by: darcy, mcimadamore

src/share/classes/com/sun/tools/javac/jvm/ClassReader.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/resources/compiler.properties file | annotate | diff | comparison | revisions
test/tools/javac/classreader/T7031108.java file | annotate | diff | comparison | revisions
test/tools/javac/diags/examples.not-yet.txt file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/jvm/ClassReader.java	Tue Mar 29 16:41:18 2011 +0100
     1.2 +++ b/src/share/classes/com/sun/tools/javac/jvm/ClassReader.java	Wed Mar 30 18:18:11 2011 -0700
     1.3 @@ -1162,6 +1162,9 @@
     1.4          ClassSymbol c = readClassSymbol(nextChar());
     1.5          NameAndType nt = (NameAndType)readPool(nextChar());
     1.6  
     1.7 +        if (c.members_field == null)
     1.8 +            throw badClassFile("bad.enclosing.class", self, c);
     1.9 +
    1.10          MethodSymbol m = findMethod(nt, c.members_field, self.flags());
    1.11          if (nt != null && m == null)
    1.12              throw badClassFile("bad.enclosing.method", self);
     2.1 --- a/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java	Tue Mar 29 16:41:18 2011 +0100
     2.2 +++ b/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java	Wed Mar 30 18:18:11 2011 -0700
     2.3 @@ -58,6 +58,7 @@
     2.4  import com.sun.tools.javac.file.FSInfo;
     2.5  import com.sun.tools.javac.file.JavacFileManager;
     2.6  import com.sun.tools.javac.jvm.*;
     2.7 +import com.sun.tools.javac.jvm.ClassReader.BadClassFile;
     2.8  import com.sun.tools.javac.main.JavaCompiler;
     2.9  import com.sun.tools.javac.main.JavaCompiler.CompileState;
    2.10  import com.sun.tools.javac.model.JavacElements;
    2.11 @@ -790,6 +791,9 @@
    2.12                                           RoundEnvironment renv) {
    2.13          try {
    2.14              return proc.process(tes, renv);
    2.15 +        } catch (BadClassFile ex) {
    2.16 +            log.error("proc.cant.access.1", ex.sym, ex.getDetailValue());
    2.17 +            return false;
    2.18          } catch (CompletionFailure ex) {
    2.19              StringWriter out = new StringWriter();
    2.20              ex.printStackTrace(new PrintWriter(out));
     3.1 --- a/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Tue Mar 29 16:41:18 2011 +0100
     3.2 +++ b/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Wed Mar 30 18:18:11 2011 -0700
     3.3 @@ -611,12 +611,18 @@
     3.4  
     3.5  # Errors related to annotation processing
     3.6  
     3.7 +# 0: symbol, 1: string, 2: stack-trace
     3.8  compiler.err.proc.cant.access=\
     3.9      cannot access {0}\n\
    3.10      {1}\n\
    3.11      Consult the following stack trace for details.\n\
    3.12      {2}
    3.13  
    3.14 +# 0: symbol, 1: string
    3.15 +compiler.err.proc.cant.access.1=\
    3.16 +    cannot access {0}\n\
    3.17 +    {1}
    3.18 +
    3.19  # 0: string
    3.20  compiler.err.proc.cant.find.class=\
    3.21      Could not find class file for ''{0}''.
    3.22 @@ -1424,8 +1430,13 @@
    3.23  compiler.misc.bad.class.signature=\
    3.24      bad class signature: {0}
    3.25  
    3.26 +#0: symbol, 1: symbol
    3.27 +compiler.misc.bad.enclosing.class=\
    3.28 +    bad enclosing class for {0}: {1}
    3.29 +
    3.30 +# 0: symbol
    3.31  compiler.misc.bad.enclosing.method=\
    3.32 -    bad enclosing method attribute: {0}
    3.33 +    bad enclosing method attribute for class {0}
    3.34  
    3.35  compiler.misc.bad.runtime.invisible.param.annotations=\
    3.36      bad RuntimeInvisibleParameterAnnotations attribute: {0}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test/tools/javac/classreader/T7031108.java	Wed Mar 30 18:18:11 2011 -0700
     4.3 @@ -0,0 +1,149 @@
     4.4 +/*
     4.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
     4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4.7 + *
     4.8 + * This code is free software; you can redistribute it and/or modify it
     4.9 + * under the terms of the GNU General Public License version 2 only, as
    4.10 + * published by the Free Software Foundation.
    4.11 + *
    4.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    4.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    4.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    4.15 + * version 2 for more details (a copy is included in the LICENSE file that
    4.16 + * accompanied this code).
    4.17 + *
    4.18 + * You should have received a copy of the GNU General Public License version
    4.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    4.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    4.21 + *
    4.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    4.23 + * or visit www.oracle.com if you need additional information or have any
    4.24 + * questions.
    4.25 + */
    4.26 +
    4.27 +/*
    4.28 + * @test
    4.29 + * @bug 7031108
    4.30 + * @summary NPE in javac.jvm.ClassReader.findMethod in PackageElement.enclosedElements from AP in incr build
    4.31 + * @library ../lib
    4.32 + * @build JavacTestingAbstractProcessor T7031108
    4.33 + * @run main T7031108
    4.34 + */
    4.35 +
    4.36 +import java.io.*;
    4.37 +import java.net.*;
    4.38 +import java.util.*;
    4.39 +import javax.annotation.processing.*;
    4.40 +import javax.lang.model.element.*;
    4.41 +import javax.tools.*;
    4.42 +import javax.tools.JavaCompiler.CompilationTask;
    4.43 +
    4.44 +public class T7031108 extends JavacTestingAbstractProcessor {
    4.45 +    public static void main(String... args) throws Exception {
    4.46 +        new T7031108().run();
    4.47 +    }
    4.48 +
    4.49 +    /* Class containing a local class definition;
    4.50 +     * compiled class file will have an EnclosedMethod attribute.
    4.51 +     */
    4.52 +    static final JavaSource pC =
    4.53 +            new JavaSource("p/C.java",
    4.54 +                  "package p;\n"
    4.55 +                + "class C {\n"
    4.56 +                + "    void m() {\n"
    4.57 +                + "        new Runnable() {\n"
    4.58 +                + "            public void run() {\n"
    4.59 +                + "                new Runnable() {\n"
    4.60 +                + "                    public void run() { }\n"
    4.61 +                + "                };\n"
    4.62 +                + "            }\n"
    4.63 +                + "        };\n"
    4.64 +                + "    }\n"
    4.65 +                + "}");
    4.66 +
    4.67 +    /* Dummy source file to compile while running anno processor. */
    4.68 +    static final JavaSource dummy =
    4.69 +            new JavaSource("Dummy.java",
    4.70 +                "class Dummy { }");
    4.71 +
    4.72 +    void run() throws Exception {
    4.73 +        JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
    4.74 +        StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
    4.75 +
    4.76 +        // step 1: compile test classes
    4.77 +        File cwd = new File(".");
    4.78 +        fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(cwd));
    4.79 +        compile(comp, fm, null, null, pC);
    4.80 +
    4.81 +        // step 2: verify functioning of processor
    4.82 +        fm.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH,
    4.83 +                fm.getLocation(StandardLocation.CLASS_PATH));
    4.84 +        fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(cwd));
    4.85 +        compile(comp, fm, null, getClass().getName(), dummy);
    4.86 +
    4.87 +        File pC_class = new File(new File("p"), "C.class");
    4.88 +        pC_class.delete();
    4.89 +
    4.90 +        DiagnosticCollector<JavaFileObject> dc = new DiagnosticCollector<JavaFileObject>();
    4.91 +        compile(comp, fm, dc, getClass().getName(), dummy);
    4.92 +        List<Diagnostic<? extends JavaFileObject>> diags =dc.getDiagnostics();
    4.93 +
    4.94 +        System.err.println(diags);
    4.95 +        switch (diags.size()) {
    4.96 +            case 0:
    4.97 +                throw new Exception("no diagnostics received");
    4.98 +            case 1:
    4.99 +                String code = diags.get(0).getCode();
   4.100 +                String expect = "compiler.err.proc.cant.access.1";
   4.101 +                if (!expect.equals(code))
   4.102 +                    throw new Exception("unexpected diag code: " + code
   4.103 +                            + ", expected: " + expect);
   4.104 +                break;
   4.105 +            default:
   4.106 +                throw new Exception("unexpected diags received");
   4.107 +        }
   4.108 +    }
   4.109 +
   4.110 +    void compile(JavaCompiler comp, JavaFileManager fm,
   4.111 +            DiagnosticListener<JavaFileObject> dl,
   4.112 +            String processor, JavaFileObject... files) throws Exception {
   4.113 +        System.err.println("compile processor:" + processor + ", files:" + Arrays.asList(files));
   4.114 +        List<String> opts = new ArrayList<String>();
   4.115 +        if (processor != null) {
   4.116 +            // opts.add("-verbose");
   4.117 +            opts.addAll(Arrays.asList("-processor", processor));
   4.118 +        }
   4.119 +        CompilationTask task = comp.getTask(null, fm, dl, opts, null, Arrays.asList(files));
   4.120 +        boolean ok = task.call();
   4.121 +        if (dl == null && !ok)
   4.122 +            throw new Exception("compilation failed");
   4.123 +    }
   4.124 +
   4.125 +    static class JavaSource extends SimpleJavaFileObject {
   4.126 +        JavaSource(String name, String text) {
   4.127 +            super(URI.create("js://" + name), JavaFileObject.Kind.SOURCE);
   4.128 +            this.text = text;
   4.129 +        }
   4.130 +        @Override
   4.131 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   4.132 +            return text;
   4.133 +        }
   4.134 +        final String text;
   4.135 +    }
   4.136 +
   4.137 +    // annotation processor method
   4.138 +
   4.139 +    @Override
   4.140 +    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
   4.141 +        if (!roundEnv.processingOver()) {
   4.142 +            PackageElement p = elements.getPackageElement("p");
   4.143 +            List<? extends Element> elems = p.getEnclosedElements();
   4.144 +            System.err.println("contents of package p: " + elems);
   4.145 +            if (elems.size() != 1 || !elems.get(0).getSimpleName().contentEquals("C")) {
   4.146 +                messager.printMessage(Diagnostic.Kind.ERROR, "unexpected package contents");
   4.147 +            }
   4.148 +        }
   4.149 +        return true;
   4.150 +    }
   4.151 +}
   4.152 +
     5.1 --- a/test/tools/javac/diags/examples.not-yet.txt	Tue Mar 29 16:41:18 2011 +0100
     5.2 +++ b/test/tools/javac/diags/examples.not-yet.txt	Wed Mar 30 18:18:11 2011 -0700
     5.3 @@ -27,6 +27,7 @@
     5.4  compiler.err.prob.found.req.1                           # Check: DEAD, in unused method
     5.5  compiler.err.proc.bad.config.file                       # JavacProcessingEnvironment
     5.6  compiler.err.proc.cant.access                           # completion failure
     5.7 +compiler.err.proc.cant.access.1                         # completion failure, no stack trace
     5.8  compiler.err.proc.cant.create.loader                    # security exception from service loader
     5.9  compiler.err.proc.no.service                            # JavacProcessingEnvironment: no service loader available
    5.10  compiler.err.proc.processor.bad.option.name             # cannot happen? masked by javac.err.invalid.A.key
    5.11 @@ -49,6 +50,7 @@
    5.12  compiler.misc.bad.class.signature                       # bad class file
    5.13  compiler.misc.bad.const.pool.tag                        # bad class file
    5.14  compiler.misc.bad.const.pool.tag.at                     # bad class file
    5.15 +compiler.misc.bad.enclosing.class                       # bad class file
    5.16  compiler.misc.bad.enclosing.method                      # bad class file
    5.17  compiler.misc.bad.runtime.invisible.param.annotations   # bad class file
    5.18  compiler.misc.bad.signature                             # bad class file

mercurial