On 4/26/19 11:08 AM, Peter Levart wrote:
I wonder if it is even possible to create a test that would do something like the following:

        String s = ...
        String s2 = s + "const1" + "const2" + s;

...since javac concatenates consecutive constants into a single constant. So this actually becomes:

        String s2 = s + "const1const2" + s;

...in bytecode.

Well, you don't really have to assemble the appropriate bytecodes. You could just invoke the bootstrap method directly in the test, like:

        CallSite cs = StringConcatFactory.makeConcatWithConstants(
            MethodHandles.lookup(),
            "concat",
            MethodType.methodType(String.class, String.class, String.class),
            "\u0001\u0002\u0002\u0001",
            "C", "D"
        );

        MethodHandle mh = cs.dynamicInvoker();

        String res = (String) mh.invokeExact("A", "B");

        assert "ACDB".equals(res);


Regards, Peter

Reply via email to