【问题标题】:Altering Boolean string values using reflection does not work使用反射更改布尔字符串值不起作用
【发布时间】:2012-01-04 14:53:15
【问题描述】:

我正在试验 Java 反射和内联字符串,并得出了令我感到困惑的结果。

import java.lang.reflect.Field;

public class HappyDebugging {

    public static void main(String[] args) throws Exception {
        defineTrueFalse();

        System.out.println("true is " + true); // why is it "true is true"?
        System.out.println("false is " + false);
        System.out.println(true);
        System.out.println(false);
        System.out.println("true");
        System.out.println("false");
        System.out.println("true is " + Boolean.valueOf(true));
        System.out.println("false is " + Boolean.valueOf(false));
        System.out.println("true is " + Boolean.valueOf("true"));
        System.out.println("false is " + Boolean.valueOf("false"));
    }

    static void defineTrueFalse() throws Exception{
        Field field = String.class.getDeclaredField("value");
        field.setAccessible(true);
        field.set("true", new char[] {'f', 'a', 'l', 's', 'e'});
        field.set("false", new char[] {'t', 'r', 'u', 'e'});

        field = String.class.getDeclaredField("offset");
        field.setAccessible(true);
        field.setInt("true", 0);
        field.setInt("false", 0);

        field = String.class.getDeclaredField("count");
        field.setAccessible(true);
        field.setInt("true", 5);
        field.setInt("false", 4);
    }
}

为什么输出的前两行是

true is true
false is false

我希望他们是

true is false
false is true

请注意输出在不同平台上有所不同。

【问题讨论】:

  • 不确定这个字段是什么,但我认为你没有覆盖任何东西,因为函数 (defineTrueFalse()) 是无效的,所以它仍然将 true 打印为 true(并且 false 打印为 false)跨度>
  • 似乎您所做的只是通过破坏String.class 内部结构来调用未定义的行为。
  • @Cemre:他没有压倒任何东西。他正在摆弄由文字"true""false" 表示的String 对象中包含的字段值(valueoffsetcount)。
  • 啊我明白了。很抱歉误解了这个问题。请忽略我的评论。感谢您的回复。

标签: java string reflection boolean


【解决方案1】:

这似乎有效....

String.valueOf(BooleanValue)

【讨论】:

    【解决方案2】:

    在我的编译器中,这两行代码被编译为使用实际的字符串 "true is true""false is false"(也就是说,不会发生运行时连接),所以你的反射邪恶来得太晚了。你说输出取决于平台,所以我猜有些编译器一定不会进行这个优化。

    【讨论】:

    • 即使编译器不将其视为编译时常量,布尔值也不会受到反射邪恶的影响
    • 那个和AbstractStringBuilder.append(boolean b)的实现
    【解决方案3】:

    defineTrueFalse 没有效果,所以"true is " + true 被视为"true is " + Boolean.toString(true) 这就是为什么它给你的结果为true is true

    【讨论】:

      【解决方案4】:

      答案很简单。这两行

       System.out.println("true is " + true);
       System.out.println("false is " + false);
      

      执行以下操作。 1. boolean "true" 类型的简单值被转换为字符串,得到字符串 "true"。 “假”也一样,就是这样

      【讨论】:

        【解决方案5】:

        因为你弄乱了 intern String 文字的内容(在某种程度上,我可能会补充说,这会让你的灵魂在地狱的第九圈中遭受永恒的痛苦),但你的前两行连接的是布尔文字,而不是 String文字。 defineTrueFalse() 中的任何内容都不会影响布尔值 truefalse(与字符串文字 "true""false" 不同)。

        请注意输出在不同平台上有所不同。

        但不是前两行,我敢打赌。对于可能与字符串相关的东西,因为行为取决于字符串文字的实习,我认为规范不能保证这一点(因此,地狱的第九圈)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-04-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-06-09
          • 2020-07-03
          • 1970-01-01
          相关资源
          最近更新 更多