Java 11 – Nested Based Access Control Methods(JEP 181)

Reading Time: 2 minutes

Before discussing what basically new methods introduced in Nested Based Access control we should know What is Nested Based Access Control. So for this you can refer to this blog .

From the above blog we can check that a new access control context nest is being added in Java 11, which has NestHosts and NestMembers. To check NestHosts and NestMembers of a class new methods are being added in java.lang.class. We will discuss those methods in this blog.

Let’s have a look at those methods in java.lang.class:

  • Class getNestHost()
  • Class[] getNestMembers()
  • boolean isNestmateOf(Class)

Let’s check how they are useful to us one by one.

For reference we can this example

public class Sample {
    public class Nest1 {
    }
    public class Nest2 {
        public class Nested1ClassA {
        }
        public class Nested1ClassB {
        }
    }
}

Class getNestHost()

As its name suggests it is basically used to know the Host of the Nest. In the compiled file we can see the NestHost as well.

    private static void getNestHost() {
        System.out.println("Nest Host:");
        System.out.println(((Class<?>) Sample.class).getNestHost().getSimpleName());
    }

    public static void main(String[] args) {
        getNestHost();
    }
Output:-
Nest Host:
Sample

Class[] getNestMembers()

As its name suggests it is basically used to know the Nest members, in a class every field or class will be the members of any nest. So this method helps to check the nest members of any class.

private static void getNestHost() {
        Class<?>[] nestMembers = Sample.class.getNestMembers();
        System.out.println("Nest Members:\n" +
                Arrays.stream(nestMembers).map(Class::getSimpleName)
                        .collect(Collectors.joining("\n")));
    }

public static void main(String[] args) {
        getNestHost();
    }
Output:-
Nest Members:
Sample
Nest2
Nested1ClassB
Nested1ClassA
Nest1

boolean isNestmateOf(Class)

This method returns the boolean value, it can be used to check whether two fields/classes/methods are Nest members of the same nest or the nest host is the same for both of the fields/classes/methods. It will return true if the fields/classes/methods are nest members or false if they are not.

private static void getIsNestmateOf(Class<?> cls1, Class<?> cls2) {
        System.out.printf("%s isNestmateOf %s = %s%n",
                cls1.getSimpleName(), cls2.getSimpleName(), cls1.isNestmateOf(cls2));
    }

public static void main(String[] args) {
        getIsNestmateOf(Sample.class, Nest1.class);
        getIsNestmateOf(Nest1.class, Nest2.Nested1ClassA.class);
    }
Output:-
Sample isNestmateOf Nest1 = true
Nest1 isNestmateOf Nested1ClassA = true

Hope this blog will help you.

Happy Coding !!!

References:-

Knoldus-blog-footer-image

Written by 

Sanjana Aggarwal is a Software Consultant at Knoldus Software LLP, having experience of more than 1.5 years. She has done MCA from Bharati Vidyapeeth Institute of Computer Application and Management, Paschim Vihar. She has a decent knowledge of Java, Scala, Lagom, Kafka, Cassandra, Cocuhbase, MongoDB, Akka and Akka HTTP. She loves reading books and cooking and traveling.

Leave a Reply