1 /*
  2  * Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 import java.io.Serializable;
 25 import java.lang.invoke.MethodHandles;
 26 
 27 import jdk.internal.value.ValueClass;
 28 import jdk.test.lib.helpers.StrictInit;
 29 import org.junit.jupiter.api.BeforeAll;
 30 import org.junit.jupiter.api.Test;
 31 import sun.reflect.ReflectionFactory;
 32 
 33 import static org.junit.jupiter.api.Assertions.*;
 34 
 35 /*
 36  * @test
 37  * @bug 8137058 8164908 8168980 8275137 8333796
 38  * @summary Basic test for the unsupported ReflectionFactory
 39  * @modules jdk.unsupported
 40  * @modules java.base/jdk.internal.value
 41  * @enablePreview
 42  * @library /test/lib
 43  * @build ${test.main.class}
 44  * @run driver jdk.test.lib.helpers.StrictProcessor
 45  *             ReflectionFactoryPreviewTest$StrictSerializableChild
 46  * @run junit ${test.main.class}
 47  */
 48 
 49 public class ReflectionFactoryPreviewTest {
 50 
 51     // Initialized by init()
 52     static ReflectionFactory factory;
 53 
 54     @BeforeAll
 55     static void init() {
 56         factory = ReflectionFactory.getReflectionFactory();
 57     }
 58 
 59     abstract static value class AvcSuperType {
 60         int bar;
 61 
 62         static {
 63             var f = assertDoesNotThrow(() -> AvcSuperType.class.getDeclaredField("bar"));
 64             assertTrue(f.isStrictInit());
 65         }
 66 
 67         AvcSuperType(int bar) {
 68             this.bar = bar;
 69         }
 70 
 71         AvcSuperType() {
 72             this(42);
 73         }
 74     }
 75 
 76     static class SerializableChild extends AvcSuperType implements Serializable {}
 77 
 78     static class StrictSerializableChild extends AvcSuperType implements Serializable {
 79         @StrictInit int baz;
 80 
 81         static {
 82             var f = assertDoesNotThrow(() -> StrictSerializableChild.class.getDeclaredField("baz"));
 83             assertTrue(f.isStrictInit());
 84         }
 85 
 86         StrictSerializableChild(int baz) {
 87             this.baz = baz;
 88             super();
 89         }
 90 
 91         StrictSerializableChild() {
 92             this(76);
 93         }
 94     }
 95 
 96     @Test
 97     void checkNewConstructor() throws Exception {
 98         MethodHandles.lookup().ensureInitialized(StrictSerializableChild.class);
 99         assertNull(factory.newConstructorForSerialization(Integer.class));
100         assertNull(factory.newConstructorForSerialization(AvcSuperType.class));
101         assertSame(AvcSuperType.class, factory.newConstructorForSerialization(SerializableChild.class).getDeclaringClass());
102         assertNull(factory.newConstructorForSerialization(StrictSerializableChild.class));
103 
104         assertThrows(UnsupportedOperationException.class, () ->
105                 factory.newConstructorForSerialization(Integer.class, Number.class.getDeclaredConstructor()));
106         assertTrue(ValueClass.hasStrictInstanceField(StrictSerializableChild.class));
107         var avcCtor = AvcSuperType.class.getDeclaredConstructor();
108         assertThrows(UnsupportedOperationException.class, () ->
109                 factory.newConstructorForSerialization(StrictSerializableChild.class, avcCtor));
110         assertSame(AvcSuperType.class, factory.newConstructorForSerialization(SerializableChild.class, avcCtor).getDeclaringClass());
111 
112         assertThrows(UnsupportedOperationException.class, () ->
113                 factory.newConstructorForSerialization(AvcSuperType.class, Object.class.getDeclaredConstructor()));
114     }
115 }