6668794: javac puts localized text in raw diagnostics

Wed, 12 Mar 2008 13:06:00 -0700

author
jjg
date
Wed, 12 Mar 2008 13:06:00 -0700
changeset 12
7366066839bb
parent 11
b66d15dfd001
child 13
6beca695cfae

6668794: javac puts localized text in raw diagnostics
6668796: bad diagnostic "bad class file" given for source files
Summary: Replace internal use of localized text with JCDiagnostic fragments; fix diagnostic for bad source file
Reviewed-by: mcimadamore

src/share/classes/com/sun/tools/javac/code/Symbol.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/comp/Check.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/jvm/ClassReader.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/main/JavaCompiler.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/6668794/badClass/A.java file | annotate | diff | comparison | revisions
test/tools/javac/6668794/badClass/B.java file | annotate | diff | comparison | revisions
test/tools/javac/6668794/badClass/Test.java file | annotate | diff | comparison | revisions
test/tools/javac/6668794/badSource/Test.java file | annotate | diff | comparison | revisions
test/tools/javac/6668794/badSource/Test.out file | annotate | diff | comparison | revisions
test/tools/javac/6668794/badSource/p/A.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/code/Symbol.java	Tue Mar 11 13:14:55 2008 -0700
     1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Symbol.java	Wed Mar 12 13:06:00 2008 -0700
     1.3 @@ -25,13 +25,9 @@
     1.4  
     1.5  package com.sun.tools.javac.code;
     1.6  
     1.7 -import java.util.ArrayList;
     1.8 -import java.util.Collections;
     1.9  import java.util.Set;
    1.10  import java.util.concurrent.Callable;
    1.11  import javax.lang.model.element.*;
    1.12 -import javax.lang.model.type.ReferenceType;
    1.13 -import javax.lang.model.type.TypeMirror;
    1.14  import javax.tools.JavaFileObject;
    1.15  
    1.16  import com.sun.tools.javac.util.*;
    1.17 @@ -1272,8 +1268,14 @@
    1.18          private static final long serialVersionUID = 0;
    1.19          public Symbol sym;
    1.20  
    1.21 +        /** A diagnostic object describing the failure
    1.22 +         */
    1.23 +        public JCDiagnostic diag;
    1.24 +
    1.25          /** A localized string describing the failure.
    1.26 +         * @deprecated Use {@code getDetail()} or {@code getMessage()}
    1.27           */
    1.28 +        @Deprecated
    1.29          public String errmsg;
    1.30  
    1.31          public CompletionFailure(Symbol sym, String errmsg) {
    1.32 @@ -1282,8 +1284,26 @@
    1.33  //          this.printStackTrace();//DEBUG
    1.34          }
    1.35  
    1.36 +        public CompletionFailure(Symbol sym, JCDiagnostic diag) {
    1.37 +            this.sym = sym;
    1.38 +            this.diag = diag;
    1.39 +//          this.printStackTrace();//DEBUG
    1.40 +        }
    1.41 +
    1.42 +        public JCDiagnostic getDiagnostic() {
    1.43 +            return diag;
    1.44 +        }
    1.45 +
    1.46 +        @Override
    1.47          public String getMessage() {
    1.48 -            return errmsg;
    1.49 +            if (diag != null)
    1.50 +                return diag.getMessage(null);
    1.51 +            else
    1.52 +                return errmsg;
    1.53 +        }
    1.54 +
    1.55 +        public Object getDetailValue() {
    1.56 +            return (diag != null ? diag : errmsg);
    1.57          }
    1.58  
    1.59          @Override
     2.1 --- a/src/share/classes/com/sun/tools/javac/comp/Check.java	Tue Mar 11 13:14:55 2008 -0700
     2.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Check.java	Wed Mar 12 13:06:00 2008 -0700
     2.3 @@ -173,7 +173,7 @@
     2.4       *  @param ex         The failure to report.
     2.5       */
     2.6      public Type completionError(DiagnosticPosition pos, CompletionFailure ex) {
     2.7 -        log.error(pos, "cant.access", ex.sym, ex.errmsg);
     2.8 +        log.error(pos, "cant.access", ex.sym, ex.getDetailValue());
     2.9          if (ex instanceof ClassReader.BadClassFile) throw new Abort();
    2.10          else return syms.errType;
    2.11      }
     3.1 --- a/src/share/classes/com/sun/tools/javac/jvm/ClassReader.java	Tue Mar 11 13:14:55 2008 -0700
     3.2 +++ b/src/share/classes/com/sun/tools/javac/jvm/ClassReader.java	Wed Mar 12 13:06:00 2008 -0700
     3.3 @@ -131,6 +131,10 @@
     3.4       */
     3.5      private final JavaFileManager fileManager;
     3.6  
     3.7 +    /** Factory for diagnostics
     3.8 +     */
     3.9 +    JCDiagnostic.Factory diagFactory;
    3.10 +
    3.11      /** Can be reassigned from outside:
    3.12       *  the completer to be used for ".java" files. If this remains unassigned
    3.13       *  ".java" files will not be loaded.
    3.14 @@ -221,6 +225,7 @@
    3.15          fileManager = context.get(JavaFileManager.class);
    3.16          if (fileManager == null)
    3.17              throw new AssertionError("FileManager initialization error");
    3.18 +        diagFactory = JCDiagnostic.Factory.instance(context);
    3.19  
    3.20          init(syms, definitive);
    3.21          log = Log.instance(context);
    3.22 @@ -256,23 +261,26 @@
    3.23   * Error Diagnoses
    3.24   ***********************************************************************/
    3.25  
    3.26 -    public static class BadClassFile extends CompletionFailure {
    3.27 +
    3.28 +    public class BadClassFile extends CompletionFailure {
    3.29          private static final long serialVersionUID = 0;
    3.30  
    3.31 -        /**
    3.32 -         * @param msg A localized message.
    3.33 -         */
    3.34 -        public BadClassFile(ClassSymbol c, Object cname, Object msg) {
    3.35 -            super(c, Log.getLocalizedString("bad.class.file.header",
    3.36 -                                            cname, msg));
    3.37 +        public BadClassFile(TypeSymbol sym, JavaFileObject file, JCDiagnostic diag) {
    3.38 +            super(sym, createBadClassFileDiagnostic(file, diag));
    3.39          }
    3.40      }
    3.41 +    // where
    3.42 +    private JCDiagnostic createBadClassFileDiagnostic(JavaFileObject file, JCDiagnostic diag) {
    3.43 +        String key = (file.getKind() == JavaFileObject.Kind.SOURCE
    3.44 +                    ? "bad.source.file.header" : "bad.class.file.header");
    3.45 +        return diagFactory.fragment(key, file, diag);
    3.46 +    }
    3.47  
    3.48      public BadClassFile badClassFile(String key, Object... args) {
    3.49          return new BadClassFile (
    3.50              currentOwner.enclClass(),
    3.51              currentClassFile,
    3.52 -            Log.getLocalizedString(key, args));
    3.53 +            diagFactory.fragment(key, args));
    3.54      }
    3.55  
    3.56  /************************************************************************
    3.57 @@ -1893,10 +1901,10 @@
    3.58                  currentClassFile = previousClassFile;
    3.59              }
    3.60          } else {
    3.61 +            JCDiagnostic diag =
    3.62 +                diagFactory.fragment("class.file.not.found", c.flatname);
    3.63              throw
    3.64 -                newCompletionFailure(c,
    3.65 -                                     Log.getLocalizedString("class.file.not.found",
    3.66 -                                                            c.flatname));
    3.67 +                newCompletionFailure(c, diag);
    3.68          }
    3.69      }
    3.70      // where
    3.71 @@ -1934,22 +1942,22 @@
    3.72           *  In practice, only one can be used at a time, so we share one
    3.73           *  to reduce the expense of allocating new exception objects.
    3.74           */
    3.75 -        private CompletionFailure newCompletionFailure(ClassSymbol c,
    3.76 -                                                       String localized) {
    3.77 +        private CompletionFailure newCompletionFailure(TypeSymbol c,
    3.78 +                                                       JCDiagnostic diag) {
    3.79              if (!cacheCompletionFailure) {
    3.80                  // log.warning("proc.messager",
    3.81                  //             Log.getLocalizedString("class.file.not.found", c.flatname));
    3.82                  // c.debug.printStackTrace();
    3.83 -                return new CompletionFailure(c, localized);
    3.84 +                return new CompletionFailure(c, diag);
    3.85              } else {
    3.86                  CompletionFailure result = cachedCompletionFailure;
    3.87                  result.sym = c;
    3.88 -                result.errmsg = localized;
    3.89 +                result.diag = diag;
    3.90                  return result;
    3.91              }
    3.92          }
    3.93          private CompletionFailure cachedCompletionFailure =
    3.94 -            new CompletionFailure(null, null);
    3.95 +            new CompletionFailure(null, (JCDiagnostic) null);
    3.96          {
    3.97              cachedCompletionFailure.setStackTrace(new StackTraceElement[0]);
    3.98          }
     4.1 --- a/src/share/classes/com/sun/tools/javac/main/JavaCompiler.java	Tue Mar 11 13:14:55 2008 -0700
     4.2 +++ b/src/share/classes/com/sun/tools/javac/main/JavaCompiler.java	Wed Mar 12 13:06:00 2008 -0700
     4.3 @@ -198,6 +198,10 @@
     4.4       */
     4.5      public Log log;
     4.6  
     4.7 +    /** Factory for creating diagnostic objects
     4.8 +     */
     4.9 +    JCDiagnostic.Factory diagFactory;
    4.10 +
    4.11      /** The tree factory module.
    4.12       */
    4.13      protected TreeMaker make;
    4.14 @@ -304,6 +308,7 @@
    4.15  
    4.16          names = Name.Table.instance(context);
    4.17          log = Log.instance(context);
    4.18 +        diagFactory = JCDiagnostic.Factory.instance(context);
    4.19          reader = ClassReader.instance(context);
    4.20          make = TreeMaker.instance(context);
    4.21          writer = ClassWriter.instance(context);
    4.22 @@ -318,7 +323,7 @@
    4.23              syms = Symtab.instance(context);
    4.24          } catch (CompletionFailure ex) {
    4.25              // inlined Check.completionError as it is not initialized yet
    4.26 -            log.error("cant.access", ex.sym, ex.errmsg);
    4.27 +            log.error("cant.access", ex.sym, ex.getDetailValue());
    4.28              if (ex instanceof ClassReader.BadClassFile)
    4.29                  throw new Abort();
    4.30          }
    4.31 @@ -683,16 +688,16 @@
    4.32                                                   JavaFileObject.Kind.SOURCE);
    4.33              if (isPkgInfo) {
    4.34                  if (enter.getEnv(tree.packge) == null) {
    4.35 -                    String msg
    4.36 -                        = log.getLocalizedString("file.does.not.contain.package",
    4.37 +                    JCDiagnostic diag =
    4.38 +                        diagFactory.fragment("file.does.not.contain.package",
    4.39                                                   c.location());
    4.40 -                    throw new ClassReader.BadClassFile(c, filename, msg);
    4.41 +                    throw reader.new BadClassFile(c, filename, diag);
    4.42                  }
    4.43              } else {
    4.44 -                throw new
    4.45 -                    ClassReader.BadClassFile(c, filename, log.
    4.46 -                                             getLocalizedString("file.doesnt.contain.class",
    4.47 -                                                                c.fullname));
    4.48 +                JCDiagnostic diag =
    4.49 +                        diagFactory.fragment("file.doesnt.contain.class",
    4.50 +                                            c.getQualifiedName());
    4.51 +                throw reader.new BadClassFile(c, filename, diag);
    4.52              }
    4.53          }
    4.54  
    4.55 @@ -997,7 +1002,7 @@
    4.56                  annotationProcessingOccurred = c.annotationProcessingOccurred = true;
    4.57              return c;
    4.58          } catch (CompletionFailure ex) {
    4.59 -            log.error("cant.access", ex.sym, ex.errmsg);
    4.60 +            log.error("cant.access", ex.sym, ex.getDetailValue());
    4.61              return this;
    4.62  
    4.63          }
     5.1 --- a/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java	Tue Mar 11 13:14:55 2008 -0700
     5.2 +++ b/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java	Wed Mar 12 13:06:00 2008 -0700
     5.3 @@ -734,7 +734,7 @@
     5.4          } catch (CompletionFailure ex) {
     5.5              StringWriter out = new StringWriter();
     5.6              ex.printStackTrace(new PrintWriter(out));
     5.7 -            log.error("proc.cant.access", ex.sym, ex.errmsg, out.toString());
     5.8 +            log.error("proc.cant.access", ex.sym, ex.getDetailValue(), out.toString());
     5.9              return false;
    5.10          } catch (Throwable t) {
    5.11              throw new AnnotationProcessingError(t);
     6.1 --- a/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Tue Mar 11 13:14:55 2008 -0700
     6.2 +++ b/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Wed Mar 12 13:06:00 2008 -0700
     6.3 @@ -827,6 +827,10 @@
     6.4  bad class file: {0}\n\
     6.5  {1}\n\
     6.6  Please remove or make sure it appears in the correct subdirectory of the classpath.
     6.7 +compiler.misc.bad.source.file.header=\
     6.8 +bad source file: {0}\n\
     6.9 +{1}\n\
    6.10 +Please remove or make sure it appears in the correct subdirectory of the sourcepath.
    6.11  
    6.12  ## The following are all possible strings for the second argument ({1}) of the
    6.13  ## above strings.
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/test/tools/javac/6668794/badClass/A.java	Wed Mar 12 13:06:00 2008 -0700
     7.3 @@ -0,0 +1,28 @@
     7.4 +/*
     7.5 + * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
     7.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     7.7 + *
     7.8 + * This code is free software; you can redistribute it and/or modify it
     7.9 + * under the terms of the GNU General Public License version 2 only, as
    7.10 + * published by the Free Software Foundation.  Sun designates this
    7.11 + * particular file as subject to the "Classpath" exception as provided
    7.12 + * by Sun in the LICENSE file that accompanied this code.
    7.13 + *
    7.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    7.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    7.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    7.17 + * version 2 for more details (a copy is included in the LICENSE file that
    7.18 + * accompanied this code).
    7.19 + *
    7.20 + * You should have received a copy of the GNU General Public License version
    7.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    7.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    7.23 + *
    7.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    7.25 + * CA 95054 USA or visit www.sun.com if you need additional information or
    7.26 + * have any questions.
    7.27 + */
    7.28 +
    7.29 +package q;
    7.30 +
    7.31 +class A { }
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/test/tools/javac/6668794/badClass/B.java	Wed Mar 12 13:06:00 2008 -0700
     8.3 @@ -0,0 +1,7 @@
     8.4 +/*
     8.5 + * /nodynamiccopyright/
     8.6 + */
     8.7 +
     8.8 +class B {
     8.9 +    p.A a;
    8.10 +}
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/test/tools/javac/6668794/badClass/Test.java	Wed Mar 12 13:06:00 2008 -0700
     9.3 @@ -0,0 +1,88 @@
     9.4 +/*
     9.5 + * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
     9.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     9.7 + *
     9.8 + * This code is free software; you can redistribute it and/or modify it
     9.9 + * under the terms of the GNU General Public License version 2 only, as
    9.10 + * published by the Free Software Foundation.  Sun designates this
    9.11 + * particular file as subject to the "Classpath" exception as provided
    9.12 + * by Sun in the LICENSE file that accompanied this code.
    9.13 + *
    9.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    9.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    9.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    9.17 + * version 2 for more details (a copy is included in the LICENSE file that
    9.18 + * accompanied this code).
    9.19 + *
    9.20 + * You should have received a copy of the GNU General Public License version
    9.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    9.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    9.23 + *
    9.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    9.25 + * CA 95054 USA or visit www.sun.com if you need additional information or
    9.26 + * have any questions.
    9.27 + */
    9.28 +
    9.29 +/*
    9.30 + * @test
    9.31 + * @bug 6668794 6668796
    9.32 + * @summary javac puts localized text in raw diagnostics
    9.33 + *      bad diagnostic "bad class file" given for source files
    9.34 + */
    9.35 +
    9.36 +import java.io.*;
    9.37 +import java.util.*;
    9.38 +import javax.tools.*;
    9.39 +
    9.40 +public class Test {
    9.41 +    public static void main(String[] args) throws Exception {
    9.42 +        new Test().run();
    9.43 +    }
    9.44 +
    9.45 +    void run() throws Exception {
    9.46 +
    9.47 +        // compile q.A then move it to p.A
    9.48 +        compile("A.java");
    9.49 +
    9.50 +        File p = new File("p");
    9.51 +        p.mkdirs();
    9.52 +        new File("q/A.class").renameTo(new File("p/A.class"));
    9.53 +
    9.54 +        // compile B against p.A
    9.55 +        String[] out = compile("B.java");
    9.56 +        if (out.length == 0)
    9.57 +            throw new Error("no diagnostics generated");
    9.58 +
    9.59 +        String expected = "B.java:6:6: compiler.err.cant.access: p.A, " +
    9.60 +            "(- compiler.misc.bad.class.file.header: A.class, " +
    9.61 +            "(- compiler.misc.class.file.wrong.class: q.A))";
    9.62 +
    9.63 +        if (!out[0].equals(expected)) {
    9.64 +            System.err.println("expected: " + expected);
    9.65 +            System.err.println("   found: " + out[0]);
    9.66 +            throw new Error("test failed");
    9.67 +        }
    9.68 +    }
    9.69 +
    9.70 +    String[] compile(String file) {
    9.71 +        String[] options = {
    9.72 +            "-XDrawDiagnostics",
    9.73 +            "-d", ".",
    9.74 +            "-classpath", ".",
    9.75 +            new File(testSrc, file).getPath()
    9.76 +        };
    9.77 +
    9.78 +        System.err.println("compile: " + Arrays.asList(options));
    9.79 +        StringWriter sw = new StringWriter();
    9.80 +        PrintWriter out = new PrintWriter(sw);
    9.81 +        int rc = com.sun.tools.javac.Main.compile(options, out);
    9.82 +        out.close();
    9.83 +
    9.84 +        String outText = sw.toString();
    9.85 +        System.err.println(outText);
    9.86 +
    9.87 +        return sw.toString().split("[\\r\\n]+");
    9.88 +    }
    9.89 +
    9.90 +    File testSrc = new File(System.getProperty("test.src", "."));
    9.91 +}
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/test/tools/javac/6668794/badSource/Test.java	Wed Mar 12 13:06:00 2008 -0700
    10.3 @@ -0,0 +1,11 @@
    10.4 +/*
    10.5 + * @test /nodynamiccopyight/
    10.6 + * @bug 6668794 6668796
    10.7 + * @summary javac puts localized text in raw diagnostics
    10.8 + *      bad diagnostic "bad class file" given for source files
    10.9 + * @compile/fail/ref=Test.out -XDrawDiagnostics Test.java
   10.10 + */
   10.11 +
   10.12 +class Test {
   10.13 +    p.A a;
   10.14 +}
    11.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.2 +++ b/test/tools/javac/6668794/badSource/Test.out	Wed Mar 12 13:06:00 2008 -0700
    11.3 @@ -0,0 +1,1 @@
    11.4 +Test.java:10:6: compiler.err.cant.access: p.A, (- compiler.misc.bad.source.file.header: A.java, (- compiler.misc.file.doesnt.contain.class: p.A))
    12.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.2 +++ b/test/tools/javac/6668794/badSource/p/A.java	Wed Mar 12 13:06:00 2008 -0700
    12.3 @@ -0,0 +1,28 @@
    12.4 +/*
    12.5 + * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
    12.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    12.7 + *
    12.8 + * This code is free software; you can redistribute it and/or modify it
    12.9 + * under the terms of the GNU General Public License version 2 only, as
   12.10 + * published by the Free Software Foundation.  Sun designates this
   12.11 + * particular file as subject to the "Classpath" exception as provided
   12.12 + * by Sun in the LICENSE file that accompanied this code.
   12.13 + *
   12.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
   12.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   12.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   12.17 + * version 2 for more details (a copy is included in the LICENSE file that
   12.18 + * accompanied this code).
   12.19 + *
   12.20 + * You should have received a copy of the GNU General Public License version
   12.21 + * 2 along with this work; if not, write to the Free Software Foundation,
   12.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   12.23 + *
   12.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
   12.25 + * CA 95054 USA or visit www.sun.com if you need additional information or
   12.26 + * have any questions.
   12.27 + */
   12.28 +
   12.29 +package q;
   12.30 +
   12.31 +class A { }

mercurial