test/tools/javac/diags/ArgTypeCompilerFactory.java

changeset 893
8f0dcb9499db
parent 842
3da26790ccb7
child 1097
497571d34112
equal deleted inserted replaced
892:3e30c95da3c6 893:8f0dcb9499db
32 import com.sun.tools.javac.code.Flags.Flag; 32 import com.sun.tools.javac.code.Flags.Flag;
33 import com.sun.tools.javac.code.Kinds.KindName; 33 import com.sun.tools.javac.code.Kinds.KindName;
34 import com.sun.tools.javac.code.*; 34 import com.sun.tools.javac.code.*;
35 import com.sun.tools.javac.file.*; 35 import com.sun.tools.javac.file.*;
36 import com.sun.tools.javac.main.Main; 36 import com.sun.tools.javac.main.Main;
37 import com.sun.tools.javac.main.JavaCompiler;
37 import com.sun.tools.javac.parser.Token; 38 import com.sun.tools.javac.parser.Token;
38 import com.sun.tools.javac.util.*; 39 import com.sun.tools.javac.util.*;
39 import com.sun.tools.javac.util.AbstractDiagnosticFormatter.SimpleConfiguration; 40 import com.sun.tools.javac.util.AbstractDiagnosticFormatter.SimpleConfiguration;
40 import javax.lang.model.SourceVersion; 41 import javax.lang.model.SourceVersion;
41 42
105 Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files); 106 Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files);
106 107
107 JavacTaskImpl t = (JavacTaskImpl) tool.getTask(out, fm, null, opts, null, fos); 108 JavacTaskImpl t = (JavacTaskImpl) tool.getTask(out, fm, null, opts, null, fos);
108 Context c = t.getContext(); 109 Context c = t.getContext();
109 ArgTypeMessages.preRegister(c); 110 ArgTypeMessages.preRegister(c);
110 Options options = Options.instance(c); 111 ArgTypeJavaCompiler.preRegister(c);
111 Log.instance(c).setDiagnosticFormatter(new ArgTypeDiagnosticFormatter(options));
112 Boolean ok = t.call(); 112 Boolean ok = t.call();
113 113
114 return ok; 114 return ok;
115 } 115 }
116 } 116 }
142 ((JavacFileManager) get(JavaFileManager.class)).close(); 142 ((JavacFileManager) get(JavaFileManager.class)).close();
143 super.clear(); 143 super.clear();
144 } 144 }
145 }; 145 };
146 JavacFileManager.preRegister(c); // can't create it until Log has been set up 146 JavacFileManager.preRegister(c); // can't create it until Log has been set up
147 ArgTypeDiagnosticFormatter.preRegister(c); 147 ArgTypeJavaCompiler.preRegister(c);
148 ArgTypeMessages.preRegister(c); 148 ArgTypeMessages.preRegister(c);
149 int result = main.compile(args.toArray(new String[args.size()]), c); 149 int result = main.compile(args.toArray(new String[args.size()]), c);
150 150
151 return (result == 0); 151 return (result == 0);
152 } 152 }
168 for (File f: files) 168 for (File f: files)
169 args.add(f.getPath()); 169 args.add(f.getPath());
170 170
171 Context c = new Context(); 171 Context c = new Context();
172 JavacFileManager.preRegister(c); // can't create it until Log has been set up 172 JavacFileManager.preRegister(c); // can't create it until Log has been set up
173 ArgTypeDiagnosticFormatter.preRegister(c); 173 ArgTypeJavaCompiler.preRegister(c);
174 ArgTypeMessages.preRegister(c); 174 ArgTypeMessages.preRegister(c);
175 com.sun.tools.javac.main.Main m = new com.sun.tools.javac.main.Main("javac", out); 175 com.sun.tools.javac.main.Main m = new com.sun.tools.javac.main.Main("javac", out);
176 int rc = m.compile(args.toArray(new String[args.size()]), c); 176 int rc = m.compile(args.toArray(new String[args.size()]), c);
177 177
178 return (rc == 0); 178 return (rc == 0);
187 * Diagnostic formatter which reports formats a diag as a series of lines 187 * Diagnostic formatter which reports formats a diag as a series of lines
188 * containing a key, and a possibly empty set of descriptive strings for the 188 * containing a key, and a possibly empty set of descriptive strings for the
189 * arg types. 189 * arg types.
190 */ 190 */
191 static class ArgTypeDiagnosticFormatter extends AbstractDiagnosticFormatter { 191 static class ArgTypeDiagnosticFormatter extends AbstractDiagnosticFormatter {
192 static void preRegister(final Context context) {
193 context.put(Log.logKey, new Context.Factory<Log>() {
194 public Log make() {
195 Log log = new Log(context) { };
196 Options options = Options.instance(context);
197 log.setDiagnosticFormatter(new ArgTypeDiagnosticFormatter(options));
198 return log;
199 }
200 });
201
202 }
203 192
204 ArgTypeDiagnosticFormatter(Options options) { 193 ArgTypeDiagnosticFormatter(Options options) {
205 super(null, new SimpleConfiguration(options, 194 super(null, new SimpleConfiguration(options,
206 EnumSet.of(DiagnosticPart.SUMMARY, 195 EnumSet.of(DiagnosticPart.SUMMARY,
207 DiagnosticPart.DETAILS, 196 DiagnosticPart.DETAILS,
244 return true; 233 return true;
245 } 234 }
246 } 235 }
247 236
248 /** 237 /**
238 * Trivial subtype of JavaCompiler to get access to the protected compilerKey field.
239 * The factory is used to ensure that the log is initialized with an instance of
240 * ArgTypeDiagnosticFormatter before we create the required JavaCompiler.
241 */
242 static class ArgTypeJavaCompiler extends JavaCompiler {
243 static void preRegister(Context context) {
244 context.put(compilerKey, new Context.Factory<JavaCompiler>() {
245 public JavaCompiler make(Context c) {
246 Log log = Log.instance(c);
247 Options options = Options.instance(c);
248 log.setDiagnosticFormatter(new ArgTypeDiagnosticFormatter(options));
249 return new JavaCompiler(c);
250 }
251 });
252 }
253
254 // not used
255 private ArgTypeJavaCompiler() {
256 super(null);
257 }
258 }
259
260 /**
249 * Diagnostic formatter which "localizes" a message as a line 261 * Diagnostic formatter which "localizes" a message as a line
250 * containing a key, and a possibly empty set of descriptive strings for the 262 * containing a key, and a possibly empty set of descriptive strings for the
251 * arg types. 263 * arg types.
252 */ 264 */
253 static class ArgTypeMessages extends JavacMessages { 265 static class ArgTypeMessages extends JavacMessages {
254 static void preRegister(final Context c) { 266 static void preRegister(Context context) {
255 c.put(JavacMessages.messagesKey, new Context.Factory<JavacMessages>() { 267 context.put(JavacMessages.messagesKey, new Context.Factory<JavacMessages>() {
256 public JavacMessages make() { 268 public JavacMessages make(Context c) {
257 return new ArgTypeMessages(c) { 269 return new ArgTypeMessages(c) {
258 @Override 270 @Override
259 public String getLocalizedString(Locale l, String key, Object... args) { 271 public String getLocalizedString(Locale l, String key, Object... args) {
260 return getKeyArgsString(key, args); 272 return getKeyArgsString(key, args);
261 } 273 }

mercurial