日期:2014-05-20 浏览次数:20726 次
/**
* 重新设置String的值,不是将obj指向新的地址,而是直接修改obj.value的指向<br/>
* <strong>警告</strong>:obj必须这样申明: String obj = new String();<br/>
* 如果这样申明 String obj= "123";
* changeValue(obj,"abc"),将可能导致虚拟机中所有值为"123"的String变成"abc"
*
* @param obj
* @param newValue
* 新的值。
* @return obj
*/
public static String changeValue(String obj,String newValue)
{
char[] _value = newValue.toCharArray();
char[] charArray =Arrays.copyOf(_value, _value.length);
Field field=null;
try
{
field = String.class.getDeclaredField("value");
field.setAccessible(true);
field.set(obj, charArray);
field = String.class.getDeclaredField("offset");
field.setAccessible(true);
field.set(obj, 0);
field = String.class.getDeclaredField("count");
field.setAccessible(true);
field.set(obj, charArray.length);
field = String.class.getDeclaredField("hash");
field.setAccessible(true);
field.set(obj, 0);
}catch ( Exception e)
{
e.printStackTrace();
}
return obj;
}
public interface LegendConstants {
public static final String LEGEND_HOME = "legend.home";
public static final String LEGEND_I18N_ENCODING = "legend.i18n.encoding";
public static final String LEGEND_RESOURCE_ROOT = "legend.resource.root";