View Javadoc
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.netty5.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  
26  @TargetClass(NetUtil.class)
27  final class NetUtilSubstitutions {
28      private NetUtilSubstitutions() {
29      }
30  
31      @Alias
32      @InjectAccessors(NetUtilLocalhost4Accessor.class)
33      public static Inet4Address LOCALHOST4;
34  
35      @Alias
36      @InjectAccessors(NetUtilLocalhost6Accessor.class)
37      public static Inet6Address LOCALHOST6;
38  
39      @Alias
40      @InjectAccessors(NetUtilLocalhostAccessor.class)
41      public static InetAddress LOCALHOST;
42  
43      private static final class NetUtilLocalhost4Accessor {
44          static Inet4Address get() {
45              // using https://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom
46              return NetUtilLocalhost4LazyHolder.LOCALHOST4;
47          }
48  
49          static void set(Inet4Address ignored) {
50              // a no-op setter to avoid exceptions when NetUtil is initialized at run-time
51          }
52      }
53  
54      private static final class NetUtilLocalhost4LazyHolder {
55          private static final Inet4Address LOCALHOST4 = NetUtilInitializations.createLocalhost4();
56      }
57  
58      private static final class NetUtilLocalhost6Accessor {
59          static Inet6Address get() {
60              // using https://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom
61              return NetUtilLocalhost6LazyHolder.LOCALHOST6;
62          }
63  
64          static void set(Inet6Address ignored) {
65              // a no-op setter to avoid exceptions when NetUtil is initialized at run-time
66          }
67      }
68  
69      private static final class NetUtilLocalhost6LazyHolder {
70          private static final Inet6Address LOCALHOST6 = NetUtilInitializations.createLocalhost6();
71      }
72  
73      private static final class NetUtilLocalhostAccessor {
74          static InetAddress get() {
75              // using https://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom
76              return NetUtilLocalhostLazyHolder.LOCALHOST;
77          }
78  
79          static void set(InetAddress ignored) {
80              // a no-op setter to avoid exceptions when NetUtil is initialized at run-time
81          }
82      }
83  
84      private static final class NetUtilLocalhostLazyHolder {
85          private static final InetAddress LOCALHOST = NetUtilInitializations
86                  .determineLoopback(NetUtilLocalhost4LazyHolder.LOCALHOST4, NetUtilLocalhost6LazyHolder.LOCALHOST6)
87                  .address();
88      }
89  }
90