查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2020 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.util;
17  
18  import com.oracle.svm.core.annotate.Alias;
19  import com.oracle.svm.core.annotate.InjectAccessors;
20  import com.oracle.svm.core.annotate.TargetClass;
21  
22  import java.net.Inet4Address;
23  import java.net.Inet6Address;
24  import java.net.InetAddress;
25  import java.net.NetworkInterface;
26  import java.util.Collection;
27  
28  @TargetClass(NetUtil.class)
29  final class NetUtilSubstitutions {
30      private NetUtilSubstitutions() {
31      }
32  
33      @Alias
34      @InjectAccessors(NetUtilLocalhost4Accessor.class)
35      public static Inet4Address LOCALHOST4;
36  
37      @Alias
38      @InjectAccessors(NetUtilLocalhost6Accessor.class)
39      public static Inet6Address LOCALHOST6;
40  
41      @Alias
42      @InjectAccessors(NetUtilLocalhostAccessor.class)
43      public static InetAddress LOCALHOST;
44  
45      @Alias
46      @InjectAccessors(NetUtilNetworkInterfacesAccessor.class)
47      public static Collection<NetworkInterface> NETWORK_INTERFACES;
48  
49      private static final class NetUtilLocalhost4Accessor {
50          static Inet4Address get() {
51              // using https://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom
52              return NetUtilLocalhost4LazyHolder.LOCALHOST4;
53          }
54  
55          static void set(Inet4Address ignored) {
56              // a no-op setter to avoid exceptions when NetUtil is initialized at run-time
57          }
58      }
59  
60      private static final class NetUtilLocalhost4LazyHolder {
61          private static final Inet4Address LOCALHOST4 = NetUtilInitializations.createLocalhost4();
62      }
63  
64      private static final class NetUtilLocalhost6Accessor {
65          static Inet6Address get() {
66              // using https://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom
67              return NetUtilLocalhost6LazyHolder.LOCALHOST6;
68          }
69  
70          static void set(Inet6Address ignored) {
71              // a no-op setter to avoid exceptions when NetUtil is initialized at run-time
72          }
73      }
74  
75      private static final class NetUtilLocalhost6LazyHolder {
76          private static final Inet6Address LOCALHOST6 = NetUtilInitializations.createLocalhost6();
77      }
78  
79      private static final class NetUtilLocalhostAccessor {
80          static InetAddress get() {
81              // using https://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom
82              return NetUtilLocalhostLazyHolder.LOCALHOST;
83          }
84  
85          static void set(InetAddress ignored) {
86              // a no-op setter to avoid exceptions when NetUtil is initialized at run-time
87          }
88      }
89  
90      private static final class NetUtilLocalhostLazyHolder {
91          private static final InetAddress LOCALHOST = NetUtilInitializations
92                  .determineLoopback(NetUtilNetworkInterfacesLazyHolder.NETWORK_INTERFACES,
93                          NetUtilLocalhost4LazyHolder.LOCALHOST4, NetUtilLocalhost6LazyHolder.LOCALHOST6)
94                  .address();
95      }
96  
97      private static final class NetUtilNetworkInterfacesAccessor {
98          static Collection<NetworkInterface> get() {
99              // using https://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom
100             return NetUtilNetworkInterfacesLazyHolder.NETWORK_INTERFACES;
101         }
102 
103         static void set(Collection<NetworkInterface> ignored) {
104             // a no-op setter to avoid exceptions when NetUtil is initialized at run-time
105         }
106     }
107 
108     private static final class NetUtilNetworkInterfacesLazyHolder {
109         private static final Collection<NetworkInterface> NETWORK_INTERFACES =
110                 NetUtilInitializations.networkInterfaces();
111     }
112 }
113