[ 
https://issues.apache.org/jira/browse/GROOVY-11301?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17820103#comment-17820103
 ] 

Jochen Theodorou commented on GROOVY-11301:
-------------------------------------------

This can also be reproduced using the compilestatic annotation. I compared the 
java generated output with what we create and found that there seems to be a 
difference in what we give the lambda factory. In Java we have this helper 
method:
{code:Java}
  private static java.lang.String lambda$main$0();
    descriptor: ()Ljava/lang/String;
    flags: (0x100a) ACC_PRIVATE, ACC_STATIC, ACC_SYNTHETIC
    Code:
      stack=1, locals=0, args_size=0
         0: invokestatic  #3                  // Method 
Test$A.m:()Ljava/lang/String;
         3: areturn
      LineNumberTable:
        line 8: 0
{code} and a bootstrap method like this {code:Java}
      #22 REF_invokeStatic 
java/lang/invoke/LambdaMetafactory.metafactory:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;
    Method arguments:
      #23 ()Ljava/lang/Object;
      #24 REF_invokeStatic Test.lambda$main$0:()Ljava/lang/String;
      #25 ()Ljava/lang/String;
{code}
as can be seen, the method handle reference to the local helper method is given 
to LambdaMetafactory.
What we do is:
{code:Java}
     #45 REF_invokeStatic 
java/lang/invoke/LambdaMetafactory.metafactory:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;
    Method arguments:
      #30 ()Ljava/lang/Object;
      #37 REF_invokeStatic Test$A.m:()Ljava/lang/String;
      #38 ()Ljava/lang/String;
{code}
which is trying to give a handle to Test.A#m() to the LambdaFactory. Since we 
use the general public  Lookup object access to there is forbidden. We need to 
either use a lookup created from the class itself (not sure if Test is enough 
or if Test.A is required) or the helper method.

> References to inaccesible methods with static compilation leads to runtime 
> error
> --------------------------------------------------------------------------------
>
>                 Key: GROOVY-11301
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11301
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static compilation
>            Reporter: Thodoris Sotiropoulos
>            Priority: Minor
>         Attachments: Test$A.class, Test.class
>
>
> I have the following ill-typed program
> {code}
> import java.util.function.Supplier;
> public class Test {
>   static class A {
>     private static String m() { return null; }
>   }
>   public static void main(String[] args) {
>     Supplier<String> x  = A::m;
>   }
> }
> {code}
> h3. Actual behaviour
> The compiler accepts the program, but I receive the following runtime error, 
> because I access a private method.
> {code}
> Exception in thread "main" java.lang.IllegalAccessError: class Test tried to 
> access private method 'java.lang.String Test$A.m()' (Test and Test$A are in 
> unnamed module of loader 'app')
>         at Test.main(groovy51.groovy:9)
> {code}
> h3. Expected behavior
> The program should have been rejected by the compiler.
> Tested against master (commit: 191231a832efd2e2fc49391c01f8d176944d68e3)



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to