Re: Example for ConstantDynamic

Remi Forax <[email protected]> Wed, 30 Oct 2019 23:40:51 +0100 (CET)
Newsgroups gmane.comp.java.objectweb.asm
Message-ID <[email protected]>
This is a multi-part message in MIME format...

------------=_1572475671-30794-29
Content-Type: multipart/alternative; 
	boundary="=_a1fcc6f6-973c-4690-89d7-409daaf12099"

--=_a1fcc6f6-973c-4690-89d7-409daaf12099
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

Hi Gunnar,=20
with a LDC on a constant dynamic=20
mv.visitLdc( new ConstantDynamic ("name", "Ljava/lang/Object;", BSM, 2));=20

BSM is a Handle to a static method starting with a Lookup (a MethodHandles.=
Lookup), a String (here "name"), a Class corresponding to the field descrip=
tor (here Object.class) and some bootstrap constants (here 2), the return v=
alue is the value of the constant.=20
so Handle BSM =3D new Handle(Opcodes.H_INVOKESTATIC, "RT", "bsm", MethodTyp=
e.methodType(Object.class, Lookup.class, String.class, int.class).toMethodD=
escriptorString(), false);=20

so the method bsm inside the class RT can be=20
class RT {=20
public static Object bsm(Lookup lookup, String name, Class<?> type, int two=
) { ... }=20
}=20

note that the VM calls any boostrap method using MethodHandle.invoke so any=
 supertypes for the parameter types, any subtype for the return type, or an=
y boxing/varargs will also work, by example=20
class RT {=20
public static String bsm(Lookup lookup, Object name, Class<?> type, Object =
two) { ... }=20
}=20

R=C3=A9mi=20

> De: "Gunnar Morling" <[email protected]>
> =C3=80: "asm" <[email protected]>
> Envoy=C3=A9: Mercredi 30 Octobre 2019 23:11:52
> Objet: [asm] Example for ConstantDynamic

> Hi,
> Is there any simple yet complete example of producing a ConstantDynamic (=
JEP
> 309) reference (visitLdcInsn) using ASM? In particular I'm unsure about l=
inking
> it to the bootstrap method.

> Thanks for any pointers,

> --Gunnar

> --
> You receive this message as a subscriber of the [email protected] mailing list.
> To unsubscribe: mailto:[email protected]
> For general help: mailto:[email protected]?subject=3Dhelp
> OW2 mailing lists service home page: http://www.ow2.org/wws

--=_a1fcc6f6-973c-4690-89d7-409daaf12099
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

<html><body><div style=3D"font-family: arial, helvetica, sans-serif; font-s=
ize: 12pt; color: #000000"><div>Hi Gunnar,</div><div>with a LDC on a consta=
nt dynamic<br data-mce-bogus=3D"1"></div><div>&nbsp; mv.visitLdc(<span clas=
s=3D"pl-k">new</span> <span class=3D"pl-smi">ConstantDynamic</span>("name",=
 "Ljava/lang/Object;", BSM, 2));<br data-mce-bogus=3D"1"></div><div><br dat=
a-mce-bogus=3D"1"></div><div>BSM is a Handle to a static method starting wi=
th a Lookup (a MethodHandles.Lookup), a String (here "name"), a Class corre=
sponding to the field descriptor (here Object.class) and some bootstrap con=
stants (here 2), the return value is the value of the constant.<br data-mce=
-bogus=3D"1"></div><div>&nbsp; so <span class=3D"pl-c">Handle</span> BSM =
=3D <span class=3D"pl-c">new Handle(Opcodes.H_INVOKESTATIC, "RT", "bsm", Me=
thodType.methodType(Object.class, Lookup.class, String.class, int.class).to=
MethodDescriptorString(), false);</span><br data-mce-bogus=3D"1"></div><div=
><br data-mce-bogus=3D"1"></div><div>so the method bsm inside the class RT =
can be<br data-mce-bogus=3D"1"></div><div>class RT {<br data-mce-bogus=3D"1=
"></div><div>&nbsp; public static Object bsm(Lookup lookup, String name, Cl=
ass&lt;?&gt; type, int two) { ... }<br data-mce-bogus=3D"1"></div><div>}<br=
 data-mce-bogus=3D"1"></div><div><br data-mce-bogus=3D"1"></div><div>note t=
hat the VM calls any boostrap method using MethodHandle.invoke so any super=
types for the parameter types, any subtype for the return type, or any boxi=
ng/varargs will also work, by example<br data-mce-bogus=3D"1"></div><div><d=
iv>class RT {</div><div>&nbsp; public static String bsm(Lookup lookup, Obje=
ct name, Class&lt;?&gt; type, Object two) { ... }</div><div>}</div><div><br=
 data-mce-bogus=3D"1"></div></div><div>R=C3=A9mi<span class=3D"pl-c"></span=
></div><div>&nbsp; <br></div><hr id=3D"zwchr" data-marker=3D"__DIVIDER__"><=
div data-marker=3D"__HEADERS__"><blockquote style=3D"border-left:2px solid =
#1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font=
-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;f=
ont-size:12pt;"><b>De: </b>"Gunnar Morling" &lt;[email protected]&gt;<br=
><b>=C3=80: </b>"asm" &lt;[email protected]&gt;<br><b>Envoy=C3=A9: </b>Mercredi 3=
0 Octobre 2019 23:11:52<br><b>Objet: </b>[asm] Example for ConstantDynamic<=
br></blockquote></div><div data-marker=3D"__QUOTED_TEXT__"><blockquote styl=
e=3D"border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#=
000;font-weight:normal;font-style:normal;text-decoration:none;font-family:H=
elvetica,Arial,sans-serif;font-size:12pt;"><div dir=3D"ltr">Hi,<br><div>Is =
there any simple yet complete example of producing a ConstantDynamic (JEP 3=
09) reference (visitLdcInsn) using ASM? In particular I'm unsure about link=
ing it to the bootstrap method.</div><br><div>Thanks&nbsp;for any pointers,=
</div><br><div>--Gunnar</div><br></div>
<br><br>-- <br>You receive this message as a subscriber of the [email protected] =
mailing list.<br>To unsubscribe: mailto:[email protected]<br>For gene=
ral help: mailto:[email protected]?subject=3Dhelp<br>OW2 mailing lists service =
home page: http://www.ow2.org/wws<br></blockquote></div></div></body></html=
>=

--=_a1fcc6f6-973c-4690-89d7-409daaf12099--

------------=_1572475671-30794-29
Content-Type: text/plain; charset="UTF-8"
Content-Disposition: inline
Content-Transfer-Encoding: 8bit


-- 
You receive this message as a subscriber of the [email protected] mailing list.
To unsubscribe: mailto:[email protected]
For general help: mailto:[email protected]?subject=help
OW2 mailing lists service home page: http://www.ow2.org/wws

------------=_1572475671-30794-29--