On Tue, 25 Oct 2022 17:34:15 GMT, Ioi Lam <ik...@openjdk.org> wrote:

>> I've added a `java.lang.invoke.MethodHandle.TRACE_CALLSITE` property to show 
>> how invokedynamic call sites are resolved.
>> 
>> For example:
>> 
>> 
>> public class StrConcat {
>>     static String hello = "Hello";
>>     static String world = "World";
>>     public static void main(String args[]) {
>>         System.out.println(hello + world);
>>         System.out.println(hello + "World");
>>     }
>> }
>> 
>> $ java -Djava.lang.invoke.MethodHandle.TRACE_CALLSITE=true -cp . StrConcat
>> ======== CallSite: StrConcat.main(StrConcat.java:5)
>> BSM = 
>> java.base/java.lang.invoke.StringConcatFactory.makeConcatWithConstants(StringConcatFactory.java:354)
>> target class = java.lang.invoke.BoundMethodHandle$Species_L
>> target = (String,String)String : BMH.reinvoke=Lambda(a0:L/SpeciesData[L => 
>> Species_L],a1:L,a2:L)=>{
>>     t3:L=Species_L.argL0(a0:L);
>>     t4:L=MethodHandle.invokeBasic(t3:L,a1:L,a2:L);t4:L}
>> & BMH=[
>>   0: MethodHandle = {(Object,Object)String : 
>> DMH.invokeStatic=Lambda(a0:L,a1:L,a2:L)=>{
>>         t3:L=DirectMethodHandle.internalMemberName(a0:L);
>>         t4:L=MethodHandle.linkToStatic(a1:L,a2:L,t3:L);t4:L}
>>     & 
>> DMH.MN=java.lang.StringConcatHelper.simpleConcat(Object,Object)String/invokeStatic
>>   }
>> ]
>> HelloWorld
>> ======== CallSite: StrConcat.main(StrConcat.java:6)
>> BSM = 
>> java.base/java.lang.invoke.StringConcatFactory.makeConcatWithConstants(StringConcatFactory.java:354)
>> target class = java.lang.invoke.BoundMethodHandle$Species_LL
>> target = (String)String : invoke=Lambda(a0:L/SpeciesData[LL => 
>> BoundMethodHandle$Species_LL],a1:L)=>{
>>     t2:L=BoundMethodHandle$Species_LL.argL1(a0:L);
>>     t3:L=BoundMethodHandle$Species_LL.argL0(a0:L);
>>     t4:L=MethodHandle.invokeBasic(t3:L,a1:L,t2:L);t4:L}
>> & BMH=[
>>   0: MethodHandle = {(Object,Object)String : 
>> DMH.invokeStatic=Lambda(a0:L,a1:L,a2:L)=>{
>>         t3:L=DirectMethodHandle.internalMemberName(a0:L);
>>         t4:L=MethodHandle.linkToStatic(a1:L,a2:L,t3:L);t4:L}
>>     & 
>> DMH.MN=java.lang.StringConcatHelper.simpleConcat(Object,Object)String/invokeStatic
>>   }
>>   1: ( World )
>> ]
>> HelloWorld
>> 
>> 
>> More complex examples are in the JBS bug report
>> - https://bugs.openjdk.org/secure/attachment/101170/eclipse-ide-log.txt
>> - https://bugs.openjdk.org/secure/attachment/101168/lambda-expression.txt
>> - 
>> https://bugs.openjdk.org/secure/attachment/101174/pattern-matching-switch.txt
>> - https://bugs.openjdk.org/secure/attachment/101169/str-concat.txt
>
> Ioi Lam has updated the pull request incrementally with one additional commit 
> since the last revision:
> 
>   @JornVernee comments

The changes look good, but I have some more comments.

src/java.base/share/classes/java/lang/invoke/MethodHandleNatives.java line 329:

> 327:                 return null;
> 328:             }
> 329:         });

Not sure about this condition. It seems like `callerInfo[0] == callerName` will 
always be true at first, and then it gets overwritten by the stack trace 
element toString, so in effect it only picks up the first 
non-MethodHandleNatives caller? I think it could be clearer to use `filter` + 
`findFirst` to get the caller's `StackFrame` (that has the additional benefit 
of short-circuiting), and then convert to a String from there.

Also, please move the code that gets the caller info out-of-line to a helper 
method, since it's quite wordy.

-------------

PR: https://git.openjdk.org/jdk/pull/10842

Reply via email to