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