2

What is a hidden class

A hidden class is a class that cannot be directly used by other classes. The main purpose of introducing hidden classes is for the framework to use, so that the framework can generate classes at runtime and use them indirectly through reflection. It may be a bit abstract, it doesn't matter, let's understand it intuitively through an example!

Hidden class case

Step 1: Create a normal Java class first

 public class JEP371HiddenClasses {

    public static String hello() {
        return "https://www.didispace.com";
    }

}

Step 2 : Compile it, or get the compiled class file. Then use Base64 to Encode the file content, you can use various tools, or you can use the following code to get it:

 String filePath = "JEP371HiddenClasses.class";
byte[] b = Files.readAllBytes(Paths.get(filePath));
log.info(Base64.getEncoder().encodeToString(b));

Execute it and get the following content:

 yv66vgAAAD0AFAoAAgADBwAEDAAFAAYBABBqYXZhL2xhbmcvT2JqZWN0AQAGPGluaXQ+AQADKClWCAAIAQAZaHR0cHM6Ly93d3cuZGlkaXNwYWNlLmNvbQcACgEALmNvbS9kaWRpc3BhY2UvZGVidWcvamF2YTE1L0pFUDM3MUhpZGRlbkNsYXNzZXMBAARDb2RlAQAPTGluZU51bWJlclRhYmxlAQASTG9jYWxWYXJpYWJsZVRhYmxlAQAEdGhpcwEAMExjb20vZGlkaXNwYWNlL2RlYnVnL2phdmExNS9KRVAzNzFIaWRkZW5DbGFzc2VzOwEABWhlbGxvAQAUKClMamF2YS9sYW5nL1N0cmluZzsBAApTb3VyY2VGaWxlAQAYSkVQMzcxSGlkZGVuQ2xhc3Nlcy5qYXZhACEACQACAAAAAAACAAEABQAGAAEACwAAAC8AAQABAAAABSq3AAGxAAAAAgAMAAAABgABAAAAAwANAAAADAABAAAABQAOAA8AAAAJABAAEQABAAsAAAAbAAEAAAAAAAMSB7AAAAABAAwAAAAGAAEAAAAGAAEAEgAAAAIAEw==

This content is the class written in the first step.

Step 3 : Load the class generated above through reflection, and call the hello function in the hidden class. The code is as follows:

 /**
 * 程序猿DD
 * <a href="https://www.didispace.com/java-features/">Java新特性解读</a>
 */
@Test
void testHiddenClasses() throws Throwable {
  // 1. 加载encode之后的隐藏类
  String CLASS_INFO = "yv66vgAAAD0AFAoAAgADBwAEDAAFAAYBABBqYXZhL2xhbmcvT2JqZWN0AQAGPGluaXQ+AQADKClWCAAIAQAZaHR0cHM6Ly93d3cuZGlkaXNwYWNlLmNvbQcACgEALmNvbS9kaWRpc3BhY2UvZGVidWcvamF2YTE1L0pFUDM3MUhpZGRlbkNsYXNzZXMBAARDb2RlAQAPTGluZU51bWJlclRhYmxlAQASTG9jYWxWYXJpYWJsZVRhYmxlAQAEdGhpcwEAMExjb20vZGlkaXNwYWNlL2RlYnVnL2phdmExNS9KRVAzNzFIaWRkZW5DbGFzc2VzOwEABWhlbGxvAQAUKClMamF2YS9sYW5nL1N0cmluZzsBAApTb3VyY2VGaWxlAQAYSkVQMzcxSGlkZGVuQ2xhc3Nlcy5qYXZhACEACQACAAAAAAACAAEABQAGAAEACwAAAC8AAQABAAAABSq3AAGxAAAAAgAMAAAABgABAAAAAwANAAAADAABAAAABQAOAA8AAAAJABAAEQABAAsAAAAbAAEAAAAAAAMSB7AAAAABAAwAAAAGAAEAAAAGAAEAEgAAAAIAEw==";
  byte[] classInBytes = getDecoder().decode(CLASS_INFO);
  Class<?> proxy = MethodHandles.lookup()
    .defineHiddenClass(classInBytes, true, MethodHandles.Lookup.ClassOption.NESTMATE)
    .lookupClass();

  // 输出类名
  log.info(proxy.getName());
  // 输出类有哪些函数
  for(Method method : proxy.getDeclaredMethods()) {
    log.info(method.getName());
  }
  // 2. 调用hello函数
  MethodHandle mh = MethodHandles.lookup().findStatic(proxy, "hello", MethodType.methodType(String.class));
  String result = (String) mh.invokeExact();
  log.info(result);
}

The specific execution logic is not verbose, and readers can understand it according to the comments. Finally, executing this test content can get the following output:

 17:20:50.360 [main] INFO com.didispace.debug.java15.JEP371Test - com.didispace.debug.java15.JEP371HiddenClasses/0x0000000800cb0c00
17:20:50.361 [main] INFO com.didispace.debug.java15.JEP371Test - hello
17:20:50.361 [main] INFO com.didispace.debug.java15.JEP371Test - https://www.didispace.com
  • The first line: output the class name of this hidden class
  • The second line: output the method name under this hidden class
  • The third line: the return content obtained by calling the hello method under the hidden class

Is it quite simple?

If you usually participate in the development of some basic frameworks like me, you must think this function is quite good!

This article was first published: Java 15 New Features: Hidden Classes
This video: What's New in Java 15: Hidden Classes

Well, today's sharing is here! If you encounter difficulties in the learning process? You can join our high-quality technical exchange group , participate in exchanges and discussions, and learn and progress better!

Welcome to my public account: Programmer DD. Learn about cutting-edge industry news for the first time, share in-depth technical dry goods, and obtain high-quality learning resources

程序猿DD
2.2k 声望2.8k 粉丝

作品:《Spring Cloud微服务实战》、SpringForAll社区、OpenWrite、Youtube中文配音