jdk/test/sun/reflect/ReflectionFactory/NewConstructorForSerialization.java
changeset 41642 86bcd45aad5b
parent 41641 a628785b9dd9
parent 41626 bab2284261e6
child 41643 df0e03e3ca0e
equal deleted inserted replaced
41641:a628785b9dd9 41642:86bcd45aad5b
     1 /*
       
     2  * Copyright (c) 2016, 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 /*
       
    25  * @test
       
    26  * @bug 8137058
       
    27  * @summary Basic test for the unsupported newConstructorForSerialization
       
    28  * @modules jdk.unsupported
       
    29  */
       
    30 
       
    31 import java.lang.reflect.Constructor;
       
    32 import sun.reflect.ReflectionFactory;
       
    33 
       
    34 public class NewConstructorForSerialization {
       
    35 
       
    36     private static Constructor<?> getConstructor(Class<?> type)
       
    37         throws NoSuchMethodException
       
    38     {
       
    39         ReflectionFactory factory = ReflectionFactory.getReflectionFactory();
       
    40         Constructor<?> objectConstructor = type.getConstructor((Class[]) null);
       
    41 
       
    42         @SuppressWarnings("unchecked")
       
    43         Constructor<?> c = (Constructor<?>) factory
       
    44                 .newConstructorForSerialization(type, objectConstructor);
       
    45         return c;
       
    46     }
       
    47 
       
    48     public static void main(String[] args) throws Exception {
       
    49         System.out.println(getConstructor(Object.class).newInstance());
       
    50         System.out.println(getConstructor(Foo.class).newInstance());
       
    51         System.out.println(getConstructor(Bar.class).newInstance());
       
    52     }
       
    53 
       
    54     static class Foo {
       
    55         public Foo() { }
       
    56     }
       
    57 
       
    58     static class Bar extends Foo {
       
    59         public Bar() { }
       
    60     }
       
    61 }