Close

Java 11 - java.lang.Class Changes

[Updated: Oct 3, 2018, Created: Oct 2, 2018]

Java 11 introduces following methods in java.lang.Class which are related to Nested-Based Access Control Change - JEP 181.

public Class<?> getNestHost()

Returns the nested host, in other words the outer class of this nested class.

public Class<?>[] getNestMembers()

Returns all members of the nest this class belongs to.

public boolean isNestmateOf(Class<?> c)

Determines if the given Class is a nestmate of the class or interface represented by this Class object.

Examples

package com.logicbig.example;

import java.util.Arrays;

public class Test {
    public class NestedTestA {
    }

    public class NestedTestB {
    }

    public static void main(String[] args) {
        findNests(Test.class);
        findNests(NestedTestA.class);
        testNestMates(NestedTestA.class, NestedTestB.class);
        testNestMates(NestedTestA.class, Test.class);
        testNestMates(Test.class, NestedTestA.class);
        testNestMates(Test.class, Test.class);
    }

    private static void findNests(Class<?> klass) {
        System.out.printf("-- finding Nests for class=%s --%n", klass.getSimpleName());
        System.out.println("nested members:");
        Class<?>[] nestMembers = klass.getNestMembers();
        Arrays.stream(nestMembers)
              .map(Class::getSimpleName)
              .forEach(System.out::println);

        System.out.println("nested host:");
        Class<?> nestHost = klass.getNestHost();
        System.out.println(nestHost.getSimpleName());
    }

    private static void testNestMates(Class<?> klass1, Class<?> klass2) {
        boolean b = klass1.isNestmateOf(klass2);
        System.out.printf("%s isNestmateOf %s = %s%n", klass1.getSimpleName(), klass2.getSimpleName(), b);

    }
}
-- finding Nests for class=Test --
nested members:
Test
NestedTestB
NestedTestA
nested host:
Test
-- finding Nests for class=NestedTestA --
nested members:
Test
NestedTestB
NestedTestA
nested host:
Test
NestedTestA isNestmateOf NestedTestB = true
NestedTestA isNestmateOf Test = true
Test isNestmateOf NestedTestA = true
Test isNestmateOf Test = true
package com.logicbig.example;

import java.util.Arrays;
import java.util.concurrent.Flow;

public class Test2 {
  public static void main(String[] args) {
     findNests(String.class);
     findNests(Flow.Subscriber.class);
  }

  private static void findNests(Class<?> klass) {
      System.out.printf("-- finding Nests for class=%s --%n", klass.getSimpleName());
      System.out.println("nested members:");
      Class<?>[] nestMembers = klass.getNestMembers();
      Arrays.stream(nestMembers)
            .map(Class::getSimpleName)
            .forEach(System.out::println);

      System.out.println("nested host:");
      Class<?> nestHost = klass.getNestHost();
      System.out.println(nestHost.getSimpleName());
  }
}
-- finding Nests for class=String --
nested members:
String
CaseInsensitiveComparator
nested host:
String
-- finding Nests for class=Subscriber --
nested members:
Flow
Processor
Subscription
Subscriber
Publisher
nested host:
Flow

Example Project

Dependencies and Technologies Used:

  • JDK 11
Java 11 - java.lang.Class Changes Select All Download
  • java-11-class-changes
    • src
      • com
        • logicbig
          • example
            • Test.java

    See Also