|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.netbeans.jdk.fallback.lang; |
| 20 | + |
| 21 | +import java.lang.invoke.MethodHandle; |
| 22 | +import java.lang.invoke.MethodHandles; |
| 23 | +import java.lang.invoke.MethodType; |
| 24 | +import java.util.Objects; |
| 25 | +import java.util.function.Supplier; |
| 26 | +import java.util.logging.Level; |
| 27 | +import java.util.logging.Logger; |
| 28 | + |
| 29 | +/** |
| 30 | + * Delegates to JDK's LazyConstant and provides a fallback implementation if not available. |
| 31 | + * |
| 32 | + * Internal API, may be removed when no longer needed. |
| 33 | + * |
| 34 | + * @author mbien |
| 35 | + */ |
| 36 | +public final class NBLazyConstant { |
| 37 | + |
| 38 | + private static final MethodHandle lazyConstantFactory; |
| 39 | + |
| 40 | + static { |
| 41 | + Logger log = Logger.getLogger(NBLazyConstant.class.getName()); |
| 42 | + MethodHandle mh = null; |
| 43 | + try { |
| 44 | + if (Boolean.getBoolean("nb.jdk.LazyConstant.usefallback")) { |
| 45 | + mh = null; |
| 46 | + log.log(Level.INFO, "using fallback"); |
| 47 | + } else if (Runtime.version().feature() >= 26) { |
| 48 | + Class<?> entryPoint = Class.forName("java.lang.LazyConstant"); |
| 49 | + mh = MethodHandles.lookup().findStatic(entryPoint, "of", MethodType.methodType(entryPoint, Supplier.class)) |
| 50 | + .asType(MethodType.methodType(Supplier.class, Supplier.class)); |
| 51 | + } else if (Runtime.version().feature() == 25) { |
| 52 | + Class<?> entryPoint = Class.forName("java.lang.StableValue"); |
| 53 | + mh = MethodHandles.lookup().findStatic(entryPoint, "supplier", MethodType.methodType(Supplier.class, Supplier.class)); |
| 54 | + } |
| 55 | + // dryrun - just to be sure |
| 56 | + if (mh != null) { |
| 57 | + Supplier<?> probe = () -> true; |
| 58 | + ((Supplier<?>)mh.invokeExact(probe)).get(); |
| 59 | + } |
| 60 | + } catch (Throwable ex) { |
| 61 | + mh = null; |
| 62 | + log.log(Level.FINE, "using fallback", ex); |
| 63 | + } |
| 64 | + lazyConstantFactory = mh; |
| 65 | + log.log(Level.FINE, () -> "impl=" + String.valueOf(lazyConstantFactory)); |
| 66 | + } |
| 67 | + |
| 68 | + private NBLazyConstant() {} |
| 69 | + |
| 70 | + /** |
| 71 | + * Create a {@link Supplier} for a lazily initializing constant. |
| 72 | + * @param computingFunction Factory to create the constant, only called once. |
| 73 | + * @return Returns the constant, never null. |
| 74 | + */ |
| 75 | + @SuppressWarnings("unchecked") |
| 76 | + public static <T> Supplier<T> of(Supplier<? extends T> computingFunction) { |
| 77 | + Objects.requireNonNull(computingFunction); |
| 78 | + if (computingFunction instanceof DoubleCheckedFallback<? extends T> lc) { |
| 79 | + return (Supplier<T>) lc; |
| 80 | + } |
| 81 | + if (lazyConstantFactory != null) { |
| 82 | + try { |
| 83 | + return (Supplier<T>) lazyConstantFactory.invokeExact(computingFunction); |
| 84 | + } catch (RuntimeException ex) { |
| 85 | + throw ex; |
| 86 | + } catch (Throwable ex) { |
| 87 | + throw new RuntimeException(ex); |
| 88 | + } |
| 89 | + } else { |
| 90 | + return new DoubleCheckedFallback<>(computingFunction); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private static class DoubleCheckedFallback<T> implements Supplier<T> { |
| 95 | + |
| 96 | + private volatile T constant; |
| 97 | + private Supplier<? extends T> factory; |
| 98 | + |
| 99 | + private DoubleCheckedFallback(Supplier<? extends T> factory) { |
| 100 | + this.factory = factory; |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public T get() { |
| 105 | + T c = constant; |
| 106 | + if (c == null) { |
| 107 | + synchronized (this) { |
| 108 | + c = constant; |
| 109 | + if (c == null) { |
| 110 | + c = factory.get(); |
| 111 | + Objects.requireNonNull(c); |
| 112 | + constant = c; |
| 113 | + factory = null; |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + return c; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public String toString() { |
| 122 | + return getClass().getSimpleName() + "{factory=" + factory + ", constant=" + constant + '}'; |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments