test/tools/javac/diags/ArgTypeCompilerFactory.java

changeset 893
8f0dcb9499db
parent 842
3da26790ccb7
child 1097
497571d34112
     1.1 --- a/test/tools/javac/diags/ArgTypeCompilerFactory.java	Thu Feb 24 08:40:49 2011 -0800
     1.2 +++ b/test/tools/javac/diags/ArgTypeCompilerFactory.java	Fri Feb 25 12:09:33 2011 -0800
     1.3 @@ -34,6 +34,7 @@
     1.4  import com.sun.tools.javac.code.*;
     1.5  import com.sun.tools.javac.file.*;
     1.6  import com.sun.tools.javac.main.Main;
     1.7 +import com.sun.tools.javac.main.JavaCompiler;
     1.8  import com.sun.tools.javac.parser.Token;
     1.9  import com.sun.tools.javac.util.*;
    1.10  import com.sun.tools.javac.util.AbstractDiagnosticFormatter.SimpleConfiguration;
    1.11 @@ -107,8 +108,7 @@
    1.12              JavacTaskImpl t = (JavacTaskImpl) tool.getTask(out, fm, null, opts, null, fos);
    1.13              Context c = t.getContext();
    1.14              ArgTypeMessages.preRegister(c);
    1.15 -            Options options = Options.instance(c);
    1.16 -            Log.instance(c).setDiagnosticFormatter(new ArgTypeDiagnosticFormatter(options));
    1.17 +            ArgTypeJavaCompiler.preRegister(c);
    1.18              Boolean ok = t.call();
    1.19  
    1.20              return ok;
    1.21 @@ -144,7 +144,7 @@
    1.22                  }
    1.23              };
    1.24              JavacFileManager.preRegister(c); // can't create it until Log has been set up
    1.25 -            ArgTypeDiagnosticFormatter.preRegister(c);
    1.26 +            ArgTypeJavaCompiler.preRegister(c);
    1.27              ArgTypeMessages.preRegister(c);
    1.28              int result = main.compile(args.toArray(new String[args.size()]), c);
    1.29  
    1.30 @@ -170,7 +170,7 @@
    1.31  
    1.32              Context c = new Context();
    1.33              JavacFileManager.preRegister(c); // can't create it until Log has been set up
    1.34 -            ArgTypeDiagnosticFormatter.preRegister(c);
    1.35 +            ArgTypeJavaCompiler.preRegister(c);
    1.36              ArgTypeMessages.preRegister(c);
    1.37              com.sun.tools.javac.main.Main m = new com.sun.tools.javac.main.Main("javac", out);
    1.38              int rc = m.compile(args.toArray(new String[args.size()]), c);
    1.39 @@ -189,17 +189,6 @@
    1.40       * arg types.
    1.41       */
    1.42      static class ArgTypeDiagnosticFormatter extends AbstractDiagnosticFormatter {
    1.43 -        static void preRegister(final Context context) {
    1.44 -            context.put(Log.logKey, new Context.Factory<Log>() {
    1.45 -                public Log make() {
    1.46 -                    Log log = new Log(context) { };
    1.47 -                    Options options = Options.instance(context);
    1.48 -                    log.setDiagnosticFormatter(new ArgTypeDiagnosticFormatter(options));
    1.49 -                    return log;
    1.50 -                }
    1.51 -            });
    1.52 -
    1.53 -        }
    1.54  
    1.55          ArgTypeDiagnosticFormatter(Options options) {
    1.56              super(null, new SimpleConfiguration(options,
    1.57 @@ -246,14 +235,37 @@
    1.58      }
    1.59  
    1.60      /**
    1.61 +     * Trivial subtype of JavaCompiler to get access to the protected compilerKey field.
    1.62 +     * The factory is used to ensure that the log is initialized with an instance of
    1.63 +     * ArgTypeDiagnosticFormatter before we create the required JavaCompiler.
    1.64 +     */
    1.65 +    static class ArgTypeJavaCompiler extends JavaCompiler {
    1.66 +        static void preRegister(Context context) {
    1.67 +            context.put(compilerKey, new Context.Factory<JavaCompiler>() {
    1.68 +                public JavaCompiler make(Context c) {
    1.69 +                    Log log = Log.instance(c);
    1.70 +                    Options options = Options.instance(c);
    1.71 +                    log.setDiagnosticFormatter(new ArgTypeDiagnosticFormatter(options));
    1.72 +                    return new JavaCompiler(c);
    1.73 +                }
    1.74 +            });
    1.75 +        }
    1.76 +
    1.77 +        // not used
    1.78 +        private ArgTypeJavaCompiler() {
    1.79 +            super(null);
    1.80 +        }
    1.81 +    }
    1.82 +
    1.83 +    /**
    1.84       * Diagnostic formatter which "localizes" a message as a line
    1.85       * containing a key, and a possibly empty set of descriptive strings for the
    1.86       * arg types.
    1.87       */
    1.88      static class ArgTypeMessages extends JavacMessages {
    1.89 -        static void preRegister(final Context c) {
    1.90 -            c.put(JavacMessages.messagesKey, new Context.Factory<JavacMessages>() {
    1.91 -                public JavacMessages make() {
    1.92 +        static void preRegister(Context context) {
    1.93 +            context.put(JavacMessages.messagesKey, new Context.Factory<JavacMessages>() {
    1.94 +                public JavacMessages make(Context c) {
    1.95                      return new ArgTypeMessages(c) {
    1.96                          @Override
    1.97                          public String getLocalizedString(Locale l, String key, Object... args) {

mercurial