Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8173203

MethodHandles.privateLookupIn() always succeeds when target is in unnamed module

    Details

      Description

      MethodHandles.privateLookupIn() is specified to throw IllegalAccessException if lookup class' module doesn't read target's module. However, IllegalAccessException is not thrown if target's module is unnamed.

      Example:
      =============================
      Sources:

      // m/module-info.java
      module m {
          exports pkg;
      }

      // m/pkg/Named.java
      package pkg;

      import java.lang.invoke.MethodHandles;

      public class Named {
          public static MethodHandles.Lookup lookup() {
              return MethodHandles.lookup();
          }
      }

      // classes/Unnamed.java
      import pkg.Named;

      import java.lang.invoke.MethodHandles;

      public class Unnamed {
          public static void main(String[] args) throws IllegalAccessException {
              MethodHandles.Lookup startingLookup = Named.lookup();
              Class<?> targetClass = Unnamed.class;

              System.out.println("Lookup class module: " + startingLookup.lookupClass().getModule().getName());
              System.out.println("Target class module: " + targetClass.getModule().getName());
              System.out.println("Lookup class module reads target class module: " + startingLookup.lookupClass().getModule().canRead(targetClass.getModule()));

              MethodHandles.Lookup lookup = MethodHandles.privateLookupIn(targetClass, startingLookup);

              System.out.println("Resulting lookup: " + lookup);
              System.out.println("Resulting lookup class: " + lookup.lookupClass());
              System.out.println("Resulting lookup modes: " + lookup.lookupModes());
          }
      }

      =============================
      Output:

      Lookup class module: m
      Target class module: null
      Lookup class module reads target class module: false
      Resulting lookup: Unnamed
      Resulting lookup class: class Unnamed
      Resulting lookup modes: 31

        Attachments

          Activity

            People

            • Assignee:
              alanb Alan Bateman
              Reporter:
              slukyanov Stanislav Lukyanov (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: