src/jdk.unsupported/share/classes/sun/reflect/ReflectionFactory.java
changeset 51882 f6e15aa9c16e
parent 47400 f1721aa42d2c
equal deleted inserted replaced
51881:69faed47bf35 51882:f6e15aa9c16e
     1 /*
     1 /*
     2  * Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    27 
    27 
    28 import java.io.OptionalDataException;
    28 import java.io.OptionalDataException;
    29 import java.lang.invoke.MethodHandle;
    29 import java.lang.invoke.MethodHandle;
    30 import java.lang.reflect.Constructor;
    30 import java.lang.reflect.Constructor;
    31 import java.lang.reflect.InvocationTargetException;
    31 import java.lang.reflect.InvocationTargetException;
    32 import java.lang.reflect.UndeclaredThrowableException;
       
    33 import java.security.AccessControlContext;
       
    34 import java.security.AccessController;
    32 import java.security.AccessController;
    35 import java.security.Permission;
    33 import java.security.Permission;
    36 import java.security.ProtectionDomain;
       
    37 import java.security.PrivilegedAction;
    34 import java.security.PrivilegedAction;
    38 import jdk.internal.misc.SharedSecrets;
       
    39 import jdk.internal.misc.JavaSecurityAccess;
       
    40 
    35 
    41 /**
    36 /**
    42  * ReflectionFactory supports custom serialization.
    37  * ReflectionFactory supports custom serialization.
    43  * Its methods support the creation of uninitialized objects, invoking serialization
    38  * Its methods support the creation of uninitialized objects, invoking serialization
    44  * private methods for readObject, writeObject, readResolve, and writeReplace.
    39  * private methods for readObject, writeObject, readResolve, and writeReplace.
   143     public final MethodHandle readObjectForSerialization(Class<?> cl) {
   138     public final MethodHandle readObjectForSerialization(Class<?> cl) {
   144         return delegate.readObjectForSerialization(cl);
   139         return delegate.readObjectForSerialization(cl);
   145     }
   140     }
   146 
   141 
   147     /**
   142     /**
   148      * Invokes the supplied constructor, adding the provided protection domains
       
   149      * to the invocation stack before invoking {@code Constructor::newInstance}.
       
   150      * If no {@linkplain System#getSecurityManager() security manager} is present,
       
   151      * or no domains are provided, then this method simply calls
       
   152      * {@code cons.newInstance()}. Otherwise, it invokes the provided constructor
       
   153      * with privileges at the intersection of the current context and the provided
       
   154      * protection domains.
       
   155      *
       
   156      * @param cons A constructor obtained from {@code
       
   157      *        newConstructorForSerialization} or {@code
       
   158      *        newConstructorForExternalization}.
       
   159      * @param domains An array of protection domains that limit the privileges
       
   160      *        with which the constructor is invoked. Can be {@code null}
       
   161      *        or empty, in which case privileges are only limited by the
       
   162      *        {@linkplain AccessController#getContext() current context}.
       
   163      *
       
   164      * @return A new object built from the provided constructor.
       
   165      *
       
   166      * @throws NullPointerException if {@code cons} is {@code null}.
       
   167      * @throws InstantiationException if thrown by {@code cons.newInstance()}.
       
   168      * @throws InvocationTargetException if thrown by {@code cons.newInstance()}.
       
   169      * @throws IllegalAccessException if thrown by {@code cons.newInstance()}.
       
   170      */
       
   171     public final Object newInstanceForSerialization(Constructor<?> cons,
       
   172                                                     ProtectionDomain[] domains)
       
   173         throws InstantiationException, InvocationTargetException, IllegalAccessException
       
   174     {
       
   175         SecurityManager sm = System.getSecurityManager();
       
   176         if (sm == null || domains == null || domains.length == 0) {
       
   177             return cons.newInstance();
       
   178         } else {
       
   179             JavaSecurityAccess jsa = SharedSecrets.getJavaSecurityAccess();
       
   180             PrivilegedAction<?> pea = () -> {
       
   181                 try {
       
   182                     return cons.newInstance();
       
   183                 } catch (InstantiationException
       
   184                          | InvocationTargetException
       
   185                          | IllegalAccessException x) {
       
   186                     throw new UndeclaredThrowableException(x);
       
   187                 }
       
   188             }; // Can't use PrivilegedExceptionAction with jsa
       
   189             try {
       
   190                 return jsa.doIntersectionPrivilege(pea,
       
   191                            AccessController.getContext(),
       
   192                            new AccessControlContext(domains));
       
   193             } catch (UndeclaredThrowableException x) {
       
   194                 Throwable cause = x.getCause();
       
   195                  if (cause instanceof InstantiationException)
       
   196                     throw (InstantiationException) cause;
       
   197                 if (cause instanceof InvocationTargetException)
       
   198                     throw (InvocationTargetException) cause;
       
   199                 if (cause instanceof IllegalAccessException)
       
   200                     throw (IllegalAccessException) cause;
       
   201                 // not supposed to happen
       
   202                 throw x;
       
   203             }
       
   204         }
       
   205     }
       
   206 
       
   207     /**
       
   208      * Returns a direct MethodHandle for the {@code readObjectNoData} method on
   143      * Returns a direct MethodHandle for the {@code readObjectNoData} method on
   209      * a Serializable class.
   144      * a Serializable class.
   210      * The first argument of {@link MethodHandle#invoke} is the serializable
   145      * The first argument of {@link MethodHandle#invoke} is the serializable
   211      * object and the second argument is the {@code ObjectInputStream} passed to
   146      * object and the second argument is the {@code ObjectInputStream} passed to
   212      * {@code readObjectNoData}.
   147      * {@code readObjectNoData}.