/
githubmirror
/
panama-vector
Обзор
Документация
Войти
/
githubmirror
/
panama-vector
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
test/jdk/java/lang/LazyConstant/LazyConstantTestUtil.java
213 строк
6 KB
Per Minborg
8376811: Implement JEP 531: Lazy Constants (Third Preview)
08 май 2026, 11:03
08 май 2026, 11:03
a6bd64b
Код
Авторство
О чём код?
/* * Copyright (c) 2025, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ import java.io.IOException; import java.lang.reflect.Field; import java.lang.reflect.Type; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.*; final class LazyConstantTestUtil { private LazyConstantTestUtil() { } public static final String UNINITIALIZED_TAG = ".uninitialized"; public static final class CountingSupplier<T> extends AbstractCounting<Supplier<T>> implements Supplier<T> { public CountingSupplier(Supplier<T> delegate) { super(delegate); } @Override public T get() { incrementCounter(); return delegate.get(); } } public static final class CountingIntFunction<R> extends AbstractCounting<IntFunction<R>> implements IntFunction<R> { public CountingIntFunction(IntFunction<R> delegate) { super(delegate); } @Override public R apply(int value) { incrementCounter(); return delegate.apply(value); } } public static final class CountingFunction<T, R> extends AbstractCounting<Function<T, R>> implements Function<T, R> { public CountingFunction(Function<T, R> delegate) { super(delegate); } @Override public R apply(T t) { incrementCounter(); return delegate.apply(t); } } public static final class CountingPredicate<T> extends AbstractCounting<Predicate<T>> implements Predicate<T> { public CountingPredicate(Predicate<T> delegate) { super(delegate); } @Override public boolean test(T t) { incrementCounter(); return delegate.test(t); } } public static final class CountingBiFunction<T, U, R> extends AbstractCounting<BiFunction<T, U, R>> implements BiFunction<T, U, R> { public CountingBiFunction(BiFunction<T, U, R> delegate) { super(delegate); } @Override public R apply(T t, U u) { incrementCounter(); return delegate.apply(t, u); } } abstract static class AbstractCounting<D> { private final AtomicInteger cnt = new AtomicInteger(); protected final D delegate; protected AbstractCounting(D delegate) { this.delegate = delegate; } protected final void incrementCounter() { cnt.incrementAndGet(); } public final int cnt() { return cnt.get(); } @Override public final String toString() { return cnt.toString(); } } static Object functionHolder(Object o) { try { final Field field = field(o.getClass(), "functionHolder"); field.setAccessible(true); return field.get(o); } catch (ReflectiveOperationException e) { throw new RuntimeException(e); } } static Object functionHolderFunction(Object o) { try { final Field field = field(o.getClass(), "function"); field.setAccessible(true); return field.get(o); } catch (ReflectiveOperationException e) { throw new RuntimeException(e); } } static int functionHolderCounter(Object o) { try { final Field field = field(o.getClass(), "counter"); field.setAccessible(true); return (int)field.get(o); } catch (ReflectiveOperationException e) { throw new RuntimeException(e); } } static Object computingFunction(LazyConstant<?> o) { try { final Field field = field(o.getClass(), "computingFunctionOrExceptionType"); field.setAccessible(true); return field.get(o); } catch (ReflectiveOperationException e) { throw new RuntimeException(e); } } static Field field(Class<?> clazz, String name) { if (clazz.equals(Object.class)) { throw new RuntimeException("No " + name); } try { return clazz.getDeclaredField(name); } catch (NoSuchFieldException e) { return field(clazz.getSuperclass(), name); } } static String expectedMessage(Class<? extends Throwable> throwableClass, Object input) { return "Unable to access the lazy collection because " + throwableClass.getName() + " was thrown at initial computation for input '" + input + "'"; } @SuppressWarnings("unchecked") static <E extends Throwable> void sneakyThrow(Throwable e) throws E { throw (E) e; } record Thrower(String message, Function<String, ? extends Throwable> factory) { public Supplier<? extends Throwable> supplier() { return () -> factory.apply(Thrower.this.message); } } static List<Thrower> throwers() { return List.of( new Thrower("Initial checked exception", IOException::new), new Thrower("Initial runtime exception", UnsupportedOperationException::new), new Thrower("Initial Error", InternalError::new) ); } }