MessageFormat.format(Object obj)方法主要用途为拼接message信息
用法:
Object[] testArgs = {new String("张三"),new String("大傻子")}; MessageFormat form = new MessageFormat("{0}是个{1}"); String format = form.format(testArgs); System.out.println(format);
输出结果:
张三是个大傻子
疑问一:format(testArgs);这里传的参数为Object[]类型
源码为:
/** * Formats an object to produce a string. This is equivalent to * <blockquote> * {@link #format(Object, StringBuffer, FieldPosition) format}<code>(obj, * new StringBuffer(), new FieldPosition(0)).toString();</code> * </blockquote> * * @param obj The object to format * @return Formatted string. * @exception IllegalArgumentException if the Format cannot format the given * object */ public final String format (Object obj) { return format(obj, new StringBuffer(), new FieldPosition(0)).toString(); }
这里要求参数为Object类型,尝试传Object类型,代码如下:
public static void main(String[] args) { //Object[] testArgs = {new String("张三"),new String("大傻子")}; Object testArgs1 = new String("张三"); MessageFormat form = new MessageFormat("{0}是个大傻子"); String format = form.format(testArgs1); System.out.println(format); }
报错<java.lang.ClassCastException>信息如下
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.Object; at java.text.MessageFormat.format(MessageFormat.java:865) at java.text.Format.format(Format.java:157) at lijian.test.Test000.main(Test000.java:14)
深入源码分析:
Format中的format (Object obj)实际return的为
format(obj, new StringBuffer(), new FieldPosition(0)).toString();
找到这个方法
/** * Formats an object and appends the resulting text to a given string * buffer. * If the <code>pos</code> argument identifies a field used by the format, * then its indices are set to the beginning and end of the first such * field encountered. * * @param obj The object to format * @param toAppendTo where the text is to be appended * @param pos A <code>FieldPosition</code> identifying a field * in the formatted text * @return the string buffer passed in as <code>toAppendTo</code>, * with formatted text appended * @exception NullPointerException if <code>toAppendTo</code> or * <code>pos</code> is null * @exception IllegalArgumentException if the Format cannot format the given * object */ public abstract StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos);
是一个抽象方法,由于是MessageFormat调用的,所以在MessageFormat类中找其实现方法
// Overrides /** * Formats an array of objects and appends the <code>MessageFormat</code>'s * pattern, with format elements replaced by the formatted objects, to the * provided <code>StringBuffer</code>. * This is equivalent to * <blockquote> * <code>{@link #format(java.lang.Object[], java.lang.StringBuffer, java.text.FieldPosition) format}((Object[]) arguments, result, pos)</code> * </blockquote> * * @param arguments an array of objects to be formatted and substituted. * @param result where text is appended. * @param pos On input: an alignment field, if desired. * On output: the offsets of the alignment field. * @exception IllegalArgumentException if an argument in the * <code>arguments</code> array is not of the type * expected by the format element(s) that use it. */ public final StringBuffer format(Object arguments, StringBuffer result, FieldPosition pos) { return subformat((Object[]) arguments, result, pos, null); }
这里对arguments参数做了一个强转...强转为了Object[]方法,怪不得报错了
由于业务需要,这里必须传Object,而不能传数组,这如何解决?
掩耳盗铃方案:
public static void main(String[] args) { Object[] testArgs = {new String("张三"),new String("大傻子")}; Object testArgs1 = testArgs; MessageFormat form = new MessageFormat("{0}是个{1}"); String format = form.format(testArgs1); System.out.println(format); }
将Object[]数组转为Object类型,谁让它是根类呢,啥都能装
到MessageFormat类中的format方法时强转回Object[]数组,就不会报错了
运行结果:
你是个大傻子