提交 afe66510 编写于 作者: M mcimadamore

6730423: Diagnostic formatter should be an instance field of JCDiagnostic

Summary: JCDiagnostic.fragment should be deprecated and the diagnostic factory should be used instead
Reviewed-by: jjg
上级 a9a136bb
...@@ -79,6 +79,7 @@ public class Attr extends JCTree.Visitor { ...@@ -79,6 +79,7 @@ public class Attr extends JCTree.Visitor {
final Enter enter; final Enter enter;
final Target target; final Target target;
final Types types; final Types types;
final JCDiagnostic.Factory diags;
final Annotate annotate; final Annotate annotate;
public static Attr instance(Context context) { public static Attr instance(Context context) {
...@@ -102,6 +103,7 @@ public class Attr extends JCTree.Visitor { ...@@ -102,6 +103,7 @@ public class Attr extends JCTree.Visitor {
cfolder = ConstFold.instance(context); cfolder = ConstFold.instance(context);
target = Target.instance(context); target = Target.instance(context);
types = Types.instance(context); types = Types.instance(context);
diags = JCDiagnostic.Factory.instance(context);
annotate = Annotate.instance(context); annotate = Annotate.instance(context);
Options options = Options.instance(context); Options options = Options.instance(context);
...@@ -2419,7 +2421,7 @@ public class Attr extends JCTree.Visitor { ...@@ -2419,7 +2421,7 @@ public class Attr extends JCTree.Visitor {
if (false) { if (false) {
// TODO: make assertConvertible work // TODO: make assertConvertible work
chk.typeError(tree.pos(), JCDiagnostic.fragment("incompatible.types"), actual, formal); chk.typeError(tree.pos(), diags.fragment("incompatible.types"), actual, formal);
throw new AssertionError("Tree: " + tree throw new AssertionError("Tree: " + tree
+ " actual:" + actual + " actual:" + actual
+ " formal: " + formal); + " formal: " + formal);
......
...@@ -63,6 +63,7 @@ public class Check { ...@@ -63,6 +63,7 @@ public class Check {
private final Target target; private final Target target;
private final Source source; private final Source source;
private final Types types; private final Types types;
private final JCDiagnostic.Factory diags;
private final boolean skipAnnotations; private final boolean skipAnnotations;
private final TreeInfo treeinfo; private final TreeInfo treeinfo;
...@@ -86,6 +87,7 @@ public class Check { ...@@ -86,6 +87,7 @@ public class Check {
syms = Symtab.instance(context); syms = Symtab.instance(context);
infer = Infer.instance(context); infer = Infer.instance(context);
this.types = Types.instance(context); this.types = Types.instance(context);
diags = JCDiagnostic.Factory.instance(context);
Options options = Options.instance(context); Options options = Options.instance(context);
target = Target.instance(context); target = Target.instance(context);
source = Source.instance(context); source = Source.instance(context);
...@@ -343,7 +345,7 @@ public class Check { ...@@ -343,7 +345,7 @@ public class Check {
if (types.isAssignable(found, req, convertWarner(pos, found, req))) if (types.isAssignable(found, req, convertWarner(pos, found, req)))
return found; return found;
if (found.tag <= DOUBLE && req.tag <= DOUBLE) if (found.tag <= DOUBLE && req.tag <= DOUBLE)
return typeError(pos, JCDiagnostic.fragment("possible.loss.of.precision"), found, req); return typeError(pos, diags.fragment("possible.loss.of.precision"), found, req);
if (found.isSuperBound()) { if (found.isSuperBound()) {
log.error(pos, "assignment.from.super-bound", found); log.error(pos, "assignment.from.super-bound", found);
return syms.errType; return syms.errType;
...@@ -352,7 +354,7 @@ public class Check { ...@@ -352,7 +354,7 @@ public class Check {
log.error(pos, "assignment.to.extends-bound", req); log.error(pos, "assignment.to.extends-bound", req);
return syms.errType; return syms.errType;
} }
return typeError(pos, JCDiagnostic.fragment("incompatible.types"), found, req); return typeError(pos, diags.fragment("incompatible.types"), found, req);
} }
/** Instantiate polymorphic type to some prototype, unless /** Instantiate polymorphic type to some prototype, unless
...@@ -380,7 +382,7 @@ public class Check { ...@@ -380,7 +382,7 @@ public class Check {
} else { } else {
JCDiagnostic d = ex.getDiagnostic(); JCDiagnostic d = ex.getDiagnostic();
return typeError(pos, return typeError(pos,
JCDiagnostic.fragment("incompatible.types" + (d!=null ? ".1" : ""), d), diags.fragment("incompatible.types" + (d!=null ? ".1" : ""), d),
t, pt); t, pt);
} }
} }
...@@ -401,7 +403,7 @@ public class Check { ...@@ -401,7 +403,7 @@ public class Check {
return req; return req;
} else { } else {
return typeError(pos, return typeError(pos,
JCDiagnostic.fragment("inconvertible.types"), diags.fragment("inconvertible.types"),
found, req); found, req);
} }
} }
...@@ -480,9 +482,9 @@ public class Check { ...@@ -480,9 +482,9 @@ public class Check {
Type checkClassType(DiagnosticPosition pos, Type t) { Type checkClassType(DiagnosticPosition pos, Type t) {
if (t.tag != CLASS && t.tag != ERROR) if (t.tag != CLASS && t.tag != ERROR)
return typeTagError(pos, return typeTagError(pos,
JCDiagnostic.fragment("type.req.class"), diags.fragment("type.req.class"),
(t.tag == TYPEVAR) (t.tag == TYPEVAR)
? JCDiagnostic.fragment("type.parameter", t) ? diags.fragment("type.parameter", t)
: t); : t);
else else
return t; return t;
...@@ -515,7 +517,7 @@ public class Check { ...@@ -515,7 +517,7 @@ public class Check {
Type checkReifiableReferenceType(DiagnosticPosition pos, Type t) { Type checkReifiableReferenceType(DiagnosticPosition pos, Type t) {
if (t.tag != CLASS && t.tag != ARRAY && t.tag != ERROR) { if (t.tag != CLASS && t.tag != ARRAY && t.tag != ERROR) {
return typeTagError(pos, return typeTagError(pos,
JCDiagnostic.fragment("type.req.class.array"), diags.fragment("type.req.class.array"),
t); t);
} else if (!types.isReifiable(t)) { } else if (!types.isReifiable(t)) {
log.error(pos, "illegal.generic.type.for.instof"); log.error(pos, "illegal.generic.type.for.instof");
...@@ -540,7 +542,7 @@ public class Check { ...@@ -540,7 +542,7 @@ public class Check {
return t; return t;
default: default:
return typeTagError(pos, return typeTagError(pos,
JCDiagnostic.fragment("type.req.ref"), diags.fragment("type.req.ref"),
t); t);
} }
} }
...@@ -560,7 +562,7 @@ public class Check { ...@@ -560,7 +562,7 @@ public class Check {
return t; return t;
default: default:
return typeTagError(pos, return typeTagError(pos,
JCDiagnostic.fragment("type.req.ref"), diags.fragment("type.req.ref"),
t); t);
} }
} }
...@@ -1028,7 +1030,7 @@ public class Check { ...@@ -1028,7 +1030,7 @@ public class Check {
* @param other The overridden method. * @param other The overridden method.
* @return An internationalized string. * @return An internationalized string.
*/ */
static Object cannotOverride(MethodSymbol m, MethodSymbol other) { Object cannotOverride(MethodSymbol m, MethodSymbol other) {
String key; String key;
if ((other.owner.flags() & INTERFACE) == 0) if ((other.owner.flags() & INTERFACE) == 0)
key = "cant.override"; key = "cant.override";
...@@ -1036,7 +1038,7 @@ public class Check { ...@@ -1036,7 +1038,7 @@ public class Check {
key = "cant.implement"; key = "cant.implement";
else else
key = "clashes.with"; key = "clashes.with";
return JCDiagnostic.fragment(key, m, m.location(), other, other.location()); return diags.fragment(key, m, m.location(), other, other.location());
} }
/** A customized "override" warning message. /** A customized "override" warning message.
...@@ -1044,7 +1046,7 @@ public class Check { ...@@ -1044,7 +1046,7 @@ public class Check {
* @param other The overridden method. * @param other The overridden method.
* @return An internationalized string. * @return An internationalized string.
*/ */
static Object uncheckedOverrides(MethodSymbol m, MethodSymbol other) { Object uncheckedOverrides(MethodSymbol m, MethodSymbol other) {
String key; String key;
if ((other.owner.flags() & INTERFACE) == 0) if ((other.owner.flags() & INTERFACE) == 0)
key = "unchecked.override"; key = "unchecked.override";
...@@ -1052,7 +1054,7 @@ public class Check { ...@@ -1052,7 +1054,7 @@ public class Check {
key = "unchecked.implement"; key = "unchecked.implement";
else else
key = "unchecked.clash.with"; key = "unchecked.clash.with";
return JCDiagnostic.fragment(key, m, m.location(), other, other.location()); return diags.fragment(key, m, m.location(), other, other.location());
} }
/** A customized "override" warning message. /** A customized "override" warning message.
...@@ -1060,7 +1062,7 @@ public class Check { ...@@ -1060,7 +1062,7 @@ public class Check {
* @param other The overridden method. * @param other The overridden method.
* @return An internationalized string. * @return An internationalized string.
*/ */
static Object varargsOverrides(MethodSymbol m, MethodSymbol other) { Object varargsOverrides(MethodSymbol m, MethodSymbol other) {
String key; String key;
if ((other.owner.flags() & INTERFACE) == 0) if ((other.owner.flags() & INTERFACE) == 0)
key = "varargs.override"; key = "varargs.override";
...@@ -1068,7 +1070,7 @@ public class Check { ...@@ -1068,7 +1070,7 @@ public class Check {
key = "varargs.implement"; key = "varargs.implement";
else else
key = "varargs.clash.with"; key = "varargs.clash.with";
return JCDiagnostic.fragment(key, m, m.location(), other, other.location()); return diags.fragment(key, m, m.location(), other, other.location());
} }
/** Check that this method conforms with overridden method 'other'. /** Check that this method conforms with overridden method 'other'.
...@@ -1157,7 +1159,7 @@ public class Check { ...@@ -1157,7 +1159,7 @@ public class Check {
// allow limited interoperability with covariant returns // allow limited interoperability with covariant returns
} else { } else {
typeError(TreeInfo.diagnosticPositionFor(m, tree), typeError(TreeInfo.diagnosticPositionFor(m, tree),
JCDiagnostic.fragment("override.incompatible.ret", diags.fragment("override.incompatible.ret",
cannotOverride(m, other)), cannotOverride(m, other)),
mtres, otres); mtres, otres);
return; return;
...@@ -1165,7 +1167,7 @@ public class Check { ...@@ -1165,7 +1167,7 @@ public class Check {
} else if (overrideWarner.warned) { } else if (overrideWarner.warned) {
warnUnchecked(TreeInfo.diagnosticPositionFor(m, tree), warnUnchecked(TreeInfo.diagnosticPositionFor(m, tree),
"prob.found.req", "prob.found.req",
JCDiagnostic.fragment("override.unchecked.ret", diags.fragment("override.unchecked.ret",
uncheckedOverrides(m, other)), uncheckedOverrides(m, other)),
mtres, otres); mtres, otres);
} }
...@@ -2170,7 +2172,7 @@ public class Check { ...@@ -2170,7 +2172,7 @@ public class Check {
boolean warned = this.warned; boolean warned = this.warned;
super.warnUnchecked(); super.warnUnchecked();
if (warned) return; // suppress redundant diagnostics if (warned) return; // suppress redundant diagnostics
Object problem = JCDiagnostic.fragment(key); Object problem = diags.fragment(key);
Check.this.warnUnchecked(pos(), "prob.found.req", problem, found, expected); Check.this.warnUnchecked(pos(), "prob.found.req", problem, found, expected);
} }
} }
......
...@@ -29,6 +29,7 @@ import com.sun.tools.javac.util.*; ...@@ -29,6 +29,7 @@ import com.sun.tools.javac.util.*;
import com.sun.tools.javac.util.List; import com.sun.tools.javac.util.List;
import com.sun.tools.javac.code.*; import com.sun.tools.javac.code.*;
import com.sun.tools.javac.code.Type.*; import com.sun.tools.javac.code.Type.*;
import com.sun.tools.javac.util.JCDiagnostic;
import static com.sun.tools.javac.code.Flags.*; import static com.sun.tools.javac.code.Flags.*;
import static com.sun.tools.javac.code.Kinds.*; import static com.sun.tools.javac.code.Kinds.*;
...@@ -50,6 +51,7 @@ public class Infer { ...@@ -50,6 +51,7 @@ public class Infer {
Symtab syms; Symtab syms;
Types types; Types types;
JCDiagnostic.Factory diags;
public static Infer instance(Context context) { public static Infer instance(Context context) {
Infer instance = context.get(inferKey); Infer instance = context.get(inferKey);
...@@ -62,6 +64,11 @@ public class Infer { ...@@ -62,6 +64,11 @@ public class Infer {
context.put(inferKey, this); context.put(inferKey, this);
syms = Symtab.instance(context); syms = Symtab.instance(context);
types = Types.instance(context); types = Types.instance(context);
diags = JCDiagnostic.Factory.instance(context);
ambiguousNoInstanceException =
new NoInstanceException(true, diags);
unambiguousNoInstanceException =
new NoInstanceException(false, diags);
} }
public static class NoInstanceException extends RuntimeException { public static class NoInstanceException extends RuntimeException {
...@@ -70,35 +77,35 @@ public class Infer { ...@@ -70,35 +77,35 @@ public class Infer {
boolean isAmbiguous; // exist several incomparable best instances? boolean isAmbiguous; // exist several incomparable best instances?
JCDiagnostic diagnostic; JCDiagnostic diagnostic;
JCDiagnostic.Factory diags;
NoInstanceException(boolean isAmbiguous) { NoInstanceException(boolean isAmbiguous, JCDiagnostic.Factory diags) {
this.diagnostic = null; this.diagnostic = null;
this.isAmbiguous = isAmbiguous; this.isAmbiguous = isAmbiguous;
this.diags = diags;
} }
NoInstanceException setMessage(String key) { NoInstanceException setMessage(String key) {
this.diagnostic = JCDiagnostic.fragment(key); this.diagnostic = diags.fragment(key);
return this; return this;
} }
NoInstanceException setMessage(String key, Object arg1) { NoInstanceException setMessage(String key, Object arg1) {
this.diagnostic = JCDiagnostic.fragment(key, arg1); this.diagnostic = diags.fragment(key, arg1);
return this; return this;
} }
NoInstanceException setMessage(String key, Object arg1, Object arg2) { NoInstanceException setMessage(String key, Object arg1, Object arg2) {
this.diagnostic = JCDiagnostic.fragment(key, arg1, arg2); this.diagnostic = diags.fragment(key, arg1, arg2);
return this; return this;
} }
NoInstanceException setMessage(String key, Object arg1, Object arg2, Object arg3) { NoInstanceException setMessage(String key, Object arg1, Object arg2, Object arg3) {
this.diagnostic = JCDiagnostic.fragment(key, arg1, arg2, arg3); this.diagnostic = diags.fragment(key, arg1, arg2, arg3);
return this; return this;
} }
public JCDiagnostic getDiagnostic() { public JCDiagnostic getDiagnostic() {
return diagnostic; return diagnostic;
} }
} }
private final NoInstanceException ambiguousNoInstanceException = private final NoInstanceException ambiguousNoInstanceException;
new NoInstanceException(true); private final NoInstanceException unambiguousNoInstanceException;
private final NoInstanceException unambiguousNoInstanceException =
new NoInstanceException(false);
/*************************************************************************** /***************************************************************************
* Auxiliary type values and classes * Auxiliary type values and classes
......
...@@ -72,6 +72,7 @@ public class MemberEnter extends JCTree.Visitor implements Completer { ...@@ -72,6 +72,7 @@ public class MemberEnter extends JCTree.Visitor implements Completer {
private final Todo todo; private final Todo todo;
private final Annotate annotate; private final Annotate annotate;
private final Types types; private final Types types;
private final JCDiagnostic.Factory diags;
private final Target target; private final Target target;
private final boolean skipAnnotations; private final boolean skipAnnotations;
...@@ -96,6 +97,7 @@ public class MemberEnter extends JCTree.Visitor implements Completer { ...@@ -96,6 +97,7 @@ public class MemberEnter extends JCTree.Visitor implements Completer {
todo = Todo.instance(context); todo = Todo.instance(context);
annotate = Annotate.instance(context); annotate = Annotate.instance(context);
types = Types.instance(context); types = Types.instance(context);
diags = JCDiagnostic.Factory.instance(context);
target = Target.instance(context); target = Target.instance(context);
skipAnnotations = skipAnnotations =
Options.instance(context).get("skipAnnotations") != null; Options.instance(context).get("skipAnnotations") != null;
...@@ -133,7 +135,7 @@ public class MemberEnter extends JCTree.Visitor implements Completer { ...@@ -133,7 +135,7 @@ public class MemberEnter extends JCTree.Visitor implements Completer {
if (tsym.kind == PCK && tsym.members().elems == null && !tsym.exists()) { if (tsym.kind == PCK && tsym.members().elems == null && !tsym.exists()) {
// If we can't find java.lang, exit immediately. // If we can't find java.lang, exit immediately.
if (((PackageSymbol)tsym).fullname.equals(names.java_lang)) { if (((PackageSymbol)tsym).fullname.equals(names.java_lang)) {
JCDiagnostic msg = JCDiagnostic.fragment("fatal.err.no.java.lang"); JCDiagnostic msg = diags.fragment("fatal.err.no.java.lang");
throw new FatalError(msg); throw new FatalError(msg);
} else { } else {
log.error(pos, "doesnt.exist", tsym); log.error(pos, "doesnt.exist", tsym);
...@@ -319,7 +321,7 @@ public class MemberEnter extends JCTree.Visitor implements Completer { ...@@ -319,7 +321,7 @@ public class MemberEnter extends JCTree.Visitor implements Completer {
log.error(pos, "cant.resolve.location", log.error(pos, "cant.resolve.location",
KindName.STATIC, KindName.STATIC,
name, List.<Type>nil(), List.<Type>nil(), name, List.<Type>nil(), List.<Type>nil(),
typeKindName(tsym.type), Kinds.typeKindName(tsym.type),
tsym.type); tsym.type);
} }
} finally { } finally {
......
...@@ -59,6 +59,7 @@ public class Resolve { ...@@ -59,6 +59,7 @@ public class Resolve {
ClassReader reader; ClassReader reader;
TreeInfo treeinfo; TreeInfo treeinfo;
Types types; Types types;
JCDiagnostic.Factory diags;
public final boolean boxingEnabled; // = source.allowBoxing(); public final boolean boxingEnabled; // = source.allowBoxing();
public final boolean varargsEnabled; // = source.allowVarargs(); public final boolean varargsEnabled; // = source.allowVarargs();
private final boolean debugResolve; private final boolean debugResolve;
...@@ -92,6 +93,7 @@ public class Resolve { ...@@ -92,6 +93,7 @@ public class Resolve {
reader = ClassReader.instance(context); reader = ClassReader.instance(context);
treeinfo = TreeInfo.instance(context); treeinfo = TreeInfo.instance(context);
types = Types.instance(context); types = Types.instance(context);
diags = JCDiagnostic.Factory.instance(context);
Source source = Source.instance(context); Source source = Source.instance(context);
boxingEnabled = source.allowBoxing(); boxingEnabled = source.allowBoxing();
varargsEnabled = source.allowVarargs(); varargsEnabled = source.allowVarargs();
...@@ -449,7 +451,7 @@ public class Resolve { ...@@ -449,7 +451,7 @@ public class Resolve {
Symbol sym = findField(env, site, name, site.tsym); Symbol sym = findField(env, site, name, site.tsym);
if (sym.kind == VAR) return (VarSymbol)sym; if (sym.kind == VAR) return (VarSymbol)sym;
else throw new FatalError( else throw new FatalError(
JCDiagnostic.fragment("fatal.err.cant.locate.field", diags.fragment("fatal.err.cant.locate.field",
name)); name));
} }
...@@ -1248,7 +1250,7 @@ public class Resolve { ...@@ -1248,7 +1250,7 @@ public class Resolve {
pos, env, site, name, argtypes, typeargtypes); pos, env, site, name, argtypes, typeargtypes);
if (sym.kind == MTH) return (MethodSymbol)sym; if (sym.kind == MTH) return (MethodSymbol)sym;
else throw new FatalError( else throw new FatalError(
JCDiagnostic.fragment("fatal.err.cant.locate.meth", diags.fragment("fatal.err.cant.locate.meth",
name)); name));
} }
...@@ -1320,7 +1322,7 @@ public class Resolve { ...@@ -1320,7 +1322,7 @@ public class Resolve {
pos, env, site, argtypes, typeargtypes); pos, env, site, argtypes, typeargtypes);
if (sym.kind == MTH) return (MethodSymbol)sym; if (sym.kind == MTH) return (MethodSymbol)sym;
else throw new FatalError( else throw new FatalError(
JCDiagnostic.fragment("fatal.err.cant.locate.ctor", site)); diags.fragment("fatal.err.cant.locate.ctor", site));
} }
/** Resolve operator. /** Resolve operator.
......
...@@ -59,20 +59,19 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> { ...@@ -59,20 +59,19 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> {
return instance; return instance;
} }
final Messages messages; DiagnosticFormatter<JCDiagnostic> formatter;
final String prefix; final String prefix;
/** Create a new diagnostic factory. */ /** Create a new diagnostic factory. */
protected Factory(Context context) { protected Factory(Context context) {
this(Messages.instance(context), "compiler");
context.put(diagnosticFactoryKey, this); context.put(diagnosticFactoryKey, this);
messages = Messages.instance(context);
prefix = "compiler";
} }
/** Create a new diagnostic factory. */ /** Create a new diagnostic factory. */
public Factory(Messages messages, String prefix) { public Factory(Messages messages, String prefix) {
this.messages = messages;
this.prefix = prefix; this.prefix = prefix;
this.formatter = new BasicDiagnosticFormatter(messages);
} }
/** /**
...@@ -84,7 +83,7 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> { ...@@ -84,7 +83,7 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> {
*/ */
public JCDiagnostic error( public JCDiagnostic error(
DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
return new JCDiagnostic(messages, ERROR, true, source, pos, qualify(ERROR, key), args); return new JCDiagnostic(formatter, ERROR, true, source, pos, qualify(ERROR, key), args);
} }
/** /**
...@@ -97,7 +96,7 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> { ...@@ -97,7 +96,7 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> {
*/ */
public JCDiagnostic mandatoryWarning( public JCDiagnostic mandatoryWarning(
DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
return new JCDiagnostic(messages, WARNING, true, source, pos, qualify(WARNING, key), args); return new JCDiagnostic(formatter, WARNING, true, source, pos, qualify(WARNING, key), args);
} }
/** /**
...@@ -109,7 +108,7 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> { ...@@ -109,7 +108,7 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> {
*/ */
public JCDiagnostic warning( public JCDiagnostic warning(
DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
return new JCDiagnostic(messages, WARNING, false, source, pos, qualify(WARNING, key), args); return new JCDiagnostic(formatter, WARNING, false, source, pos, qualify(WARNING, key), args);
} }
/** /**
...@@ -119,7 +118,7 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> { ...@@ -119,7 +118,7 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> {
* @see MandatoryWarningHandler * @see MandatoryWarningHandler
*/ */
public JCDiagnostic mandatoryNote(DiagnosticSource source, String key, Object... args) { public JCDiagnostic mandatoryNote(DiagnosticSource source, String key, Object... args) {
return new JCDiagnostic(messages, NOTE, true, source, null, qualify(NOTE, key), args); return new JCDiagnostic(formatter, NOTE, true, source, null, qualify(NOTE, key), args);
} }
/** /**
...@@ -140,7 +139,7 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> { ...@@ -140,7 +139,7 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> {
*/ */
public JCDiagnostic note( public JCDiagnostic note(
DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
return new JCDiagnostic(messages, NOTE, false, source, pos, qualify(NOTE, key), args); return new JCDiagnostic(formatter, NOTE, false, source, pos, qualify(NOTE, key), args);
} }
/** /**
...@@ -149,7 +148,7 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> { ...@@ -149,7 +148,7 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> {
* @param args Fields of the error message. * @param args Fields of the error message.
*/ */
public JCDiagnostic fragment(String key, Object... args) { public JCDiagnostic fragment(String key, Object... args) {
return new JCDiagnostic(messages, FRAGMENT, false, null, null, qualify(FRAGMENT, key), args); return new JCDiagnostic(formatter, FRAGMENT, false, null, null, qualify(FRAGMENT, key), args);
} }
protected String qualify(DiagnosticType t, String key) { protected String qualify(DiagnosticType t, String key) {
...@@ -163,10 +162,11 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> { ...@@ -163,10 +162,11 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> {
* Create a fragment diagnostic, for use as an argument in other diagnostics * Create a fragment diagnostic, for use as an argument in other diagnostics
* @param key The key for the localized error message. * @param key The key for the localized error message.
* @param args Fields of the error message. * @param args Fields of the error message.
*
*/ */
// should be deprecated @Deprecated
public static JCDiagnostic fragment(String key, Object... args) { public static JCDiagnostic fragment(String key, Object... args) {
return new JCDiagnostic(Messages.getDefaultMessages(), return new JCDiagnostic(getFragmentFormatter(),
FRAGMENT, FRAGMENT,
false, false,
null, null,
...@@ -174,6 +174,14 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> { ...@@ -174,6 +174,14 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> {
"compiler." + FRAGMENT.key + "." + key, "compiler." + FRAGMENT.key + "." + key,
args); args);
} }
//where
@Deprecated
public static DiagnosticFormatter<JCDiagnostic> getFragmentFormatter() {
if (fragmentFormatter == null) {
fragmentFormatter = new BasicDiagnosticFormatter(Messages.getDefaultMessages());
}
return fragmentFormatter;
}
/** /**
* A DiagnosticType defines the type of the diagnostic. * A DiagnosticType defines the type of the diagnostic.
...@@ -247,7 +255,6 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> { ...@@ -247,7 +255,6 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> {
private final int pos; private final int pos;
} }
private final Messages messages;
private final DiagnosticType type; private final DiagnosticType type;
private final DiagnosticSource source; private final DiagnosticSource source;
private final DiagnosticPosition position; private final DiagnosticPosition position;
...@@ -266,7 +273,7 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> { ...@@ -266,7 +273,7 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> {
* @param key a resource key to identify the text of the diagnostic * @param key a resource key to identify the text of the diagnostic
* @param args arguments to be included in the text of the diagnostic * @param args arguments to be included in the text of the diagnostic
*/ */
protected JCDiagnostic(Messages messages, protected JCDiagnostic(DiagnosticFormatter<JCDiagnostic> formatter,
DiagnosticType dt, DiagnosticType dt,
boolean mandatory, boolean mandatory,
DiagnosticSource source, DiagnosticSource source,
...@@ -276,7 +283,7 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> { ...@@ -276,7 +283,7 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> {
if (source == null && pos != null && pos.getPreferredPosition() != Position.NOPOS) if (source == null && pos != null && pos.getPreferredPosition() != Position.NOPOS)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
this.messages = messages; this.defaultFormatter = formatter;
this.type = dt; this.type = dt;
this.mandatory = mandatory; this.mandatory = mandatory;
this.source = source; this.source = source;
...@@ -398,25 +405,19 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> { ...@@ -398,25 +405,19 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> {
* @return the prefix string associated with a particular type of diagnostic * @return the prefix string associated with a particular type of diagnostic
*/ */
public String getPrefix(DiagnosticType dt) { public String getPrefix(DiagnosticType dt) {
return getFormatter().formatKind(this, Locale.getDefault()); return defaultFormatter.formatKind(this, Locale.getDefault());
}
private DiagnosticFormatter<JCDiagnostic> getFormatter() {
if (defaultFormatter == null) {
defaultFormatter = new BasicDiagnosticFormatter(messages);
}
return defaultFormatter;
} }
/** /**
* Return the standard presentation of this diagnostic. * Return the standard presentation of this diagnostic.
*/ */
public String toString() { public String toString() {
return getFormatter().format(this,Locale.getDefault()); return defaultFormatter.format(this,Locale.getDefault());
} }
private static DiagnosticFormatter<JCDiagnostic> defaultFormatter; private DiagnosticFormatter<JCDiagnostic> defaultFormatter;
@Deprecated
private static DiagnosticFormatter<JCDiagnostic> fragmentFormatter;
// Methods for javax.tools.Diagnostic // Methods for javax.tools.Diagnostic
...@@ -440,6 +441,6 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> { ...@@ -440,6 +441,6 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> {
public String getMessage(Locale locale) { public String getMessage(Locale locale) {
// RFE 6406133: JCDiagnostic.getMessage ignores locale argument // RFE 6406133: JCDiagnostic.getMessage ignores locale argument
return getFormatter().formatMessage(this, locale); return defaultFormatter.formatMessage(this, locale);
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册