1 /*
  2  * Copyright (c) 2001, 2024, 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.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package sun.reflect;
 27 
 28 import java.io.ObjectInputStream;
 29 import java.io.ObjectOutputStream;
 30 import java.io.ObjectStreamField;
 31 import java.io.OptionalDataException;
 32 import java.lang.invoke.MethodHandle;
 33 import java.lang.reflect.Constructor;
 34 import java.lang.reflect.InvocationTargetException;
 35 
 36 /**
 37  * ReflectionFactory supports custom serialization.
 38  * Its methods support the creation of uninitialized objects, invoking serialization
 39  * private methods for readObject, writeObject, readResolve, and writeReplace.
 40  */
 41 public class ReflectionFactory {
 42 
 43     private static final ReflectionFactory soleInstance = new ReflectionFactory();
 44     private static final jdk.internal.reflect.ReflectionFactory delegate =
 45             jdk.internal.reflect.ReflectionFactory.getReflectionFactory();
 46 
 47     private ReflectionFactory() {}
 48 
 49     /**
 50      * Provides the caller with the capability to instantiate reflective
 51      * objects.
 52      *
 53      * <p> The returned {@code ReflectionFactory} object should be carefully
 54      * guarded by the caller, since it can be used to read and write private
 55      * data and invoke private methods, as well as to load unverified bytecodes.
 56      * It must never be passed to untrusted code.
 57      *
 58      * @return the ReflectionFactory
 59      */
 60     public static ReflectionFactory getReflectionFactory() {
 61         return soleInstance;
 62     }
 63 
 64     /**
 65      * Returns an accessible constructor capable of creating instances
 66      * of the given class, initialized by the given constructor.
 67      *
 68      * @param cl the class to instantiate
 69      * @param constructorToCall the constructor to call
 70      * @return an accessible constructor





 71      */
 72     public Constructor<?> newConstructorForSerialization(Class<?> cl,
 73                                                          Constructor<?> constructorToCall)
 74     {
 75         return delegate.newConstructorForSerialization(cl,
 76                                                        constructorToCall);
 77     }
 78 
 79     /**
 80      * Returns an accessible no-arg constructor for a class.
 81      * The no-arg constructor is found searching the class and its supertypes.


 82      *
 83      * @param cl the class to instantiate
 84      * @return a no-arg constructor for the class or {@code null} if
 85      *     the class or supertypes do not have a suitable no-arg constructor

 86      */
 87     public final Constructor<?> newConstructorForSerialization(Class<?> cl)
 88     {
 89         return delegate.newConstructorForSerialization(cl);
 90     }
 91 
 92     /**
 93      * Returns an accessible no-arg constructor for an externalizable class to be
 94      * initialized using a public no-argument constructor.
 95      *
 96      * @param cl the class to instantiate
 97      * @return A no-arg constructor for the class; returns {@code null} if
 98      *     the class does not implement {@link java.io.Externalizable}
 99      */
100     public final Constructor<?> newConstructorForExternalization(Class<?> cl) {
101         return delegate.newConstructorForExternalization(cl);
102     }
103 
104     /**
105      * Returns a direct MethodHandle for the {@code readObject} method on
106      * a Serializable class.
107      * The first argument of {@link MethodHandle#invoke} is the serializable
108      * object and the second argument is the {@code ObjectInputStream} passed to
109      * {@code readObject}.
110      *
111      * @param cl a Serializable class
112      * @return  a direct MethodHandle for the {@code readObject} method of the class or
113      *          {@code null} if the class does not have a {@code readObject} method
114      */
115     public final MethodHandle readObjectForSerialization(Class<?> cl) {
116         return delegate.readObjectForSerialization(cl);
117     }
118 
119     /**
120      * Returns a direct MethodHandle for the {@code readObjectNoData} method on
121      * a Serializable class.
122      * The only argument of {@link MethodHandle#invoke} is the serializable
123      * object, which {@code readObjectNoData} is called on. No arguments are
124      * passed to the {@code readObjectNoData} method.
125      *
126      * @param cl a Serializable class
127      * @return  a direct MethodHandle for the {@code readObjectNoData} method
128      *          of the class or {@code null} if the class does not have a
129      *          {@code readObjectNoData} method
130      */
131     public final MethodHandle readObjectNoDataForSerialization(Class<?> cl) {
132         return delegate.readObjectNoDataForSerialization(cl);
133     }
134 
135     /**
136      * Generate and return a direct MethodHandle which implements
137      * the general default behavior for serializable class's {@code readObject}.
138      * The generated method behaves in accordance with the
139      * Java Serialization specification's rules for that method.
140      * <p>
141      * The generated method will invoke {@link ObjectInputStream#readFields()}
142      * to acquire the stream field values. The serialization fields of the class will
143      * then be populated from the stream values.
144      * <p>
145      * Only fields which are eligible for default serialization will be populated.
146      * This includes only fields which are not {@code transient} and not {@code static}
147      * (even if the field is {@code final} or {@code private}).
148      * <p>
149      * Requesting a default serialization method for a class in a disallowed
150      * category is not supported; {@code null} will be returned for such classes.
151      * The disallowed categories include (but are not limited to):
152      * <ul>
153      *     <li>Classes which do not implement {@code Serializable}</li>
154      *     <li>Classes which implement {@code Externalizable}</li>
155      *     <li>Classes which are specially handled by the Java Serialization specification,
156      *     including record classes, enum constant classes, {@code Class}, {@code String},
157      *     array classes, reflection proxy classes, and hidden classes</li>
158      *     <li>Classes which declare a valid {@code serialPersistentFields} value</li>
159      *     <li>Any special types which may possibly be added to the JDK or the Java language
160      *     in the future which in turn might require special handling by the
161      *     provisions of the corresponding future version of the Java Serialization
162      *     specification</li>
163      * </ul>
164      * <p>
165      * The generated method will accept the instance as its first argument
166      * and the {@code ObjectInputStream} as its second argument.
167      * The return type of the method is {@code void}.
168      *
169      * @param cl a Serializable class
170      * @return  a direct MethodHandle for the synthetic {@code readObject} method
171      *          or {@code null} if the class falls in a disallowed category
172      *
173      * @since 24
174      */
175     public final MethodHandle defaultReadObjectForSerialization(Class<?> cl) {
176         return delegate.defaultReadObjectForSerialization(cl);
177     }
178 
179     /**
180      * Returns a direct MethodHandle for the {@code writeObject} method on
181      * a Serializable class.
182      * The first argument of {@link MethodHandle#invoke} is the serializable
183      * object and the second argument is the {@code ObjectOutputStream} passed to
184      * {@code writeObject}.
185      *
186      * @param cl a Serializable class
187      * @return  a direct MethodHandle for the {@code writeObject} method of the class or
188      *          {@code null} if the class does not have a {@code writeObject} method
189      */
190     public final MethodHandle writeObjectForSerialization(Class<?> cl) {
191         return delegate.writeObjectForSerialization(cl);
192     }
193 
194     /**
195      * Generate and return a direct MethodHandle which implements
196      * the general default behavior for serializable class's {@code writeObject}.
197      * The generated method behaves in accordance with the
198      * Java Serialization specification's rules for that method.
199      * <p>
200      * The generated method will invoke {@link ObjectOutputStream#putFields}
201      * to acquire the buffer for the stream field values. The buffer will
202      * be populated from the serialization fields of the class. The buffer
203      * will then be flushed to the stream using the
204      * {@link ObjectOutputStream#writeFields()} method.
205      * <p>
206      * Only fields which are eligible for default serialization will be written
207      * to the buffer.
208      * This includes only fields which are not {@code transient} and not {@code static}
209      * (even if the field is {@code final} or {@code private}).
210      * <p>
211      * Requesting a default serialization method for a class in a disallowed
212      * category is not supported; {@code null} will be returned for such classes.
213      * The disallowed categories include (but are not limited to):
214      * <ul>
215      *     <li>Classes which do not implement {@code Serializable}</li>
216      *     <li>Classes which implement {@code Externalizable}</li>
217      *     <li>Classes which are specially handled by the Java Serialization specification,
218      *     including record classes, enum constant classes, {@code Class}, {@code String},
219      *     array classes, reflection proxy classes, and hidden classes</li>
220      *     <li>Classes which declare a valid {@code serialPersistentFields} value</li>
221      *     <li>Any special types which may possibly be added to the JDK or the Java language
222      *     in the future which in turn might require special handling by the
223      *     provisions of the corresponding future version of the Java Serialization
224      *     specification</li>
225      * </ul>
226      * <p>
227      * The generated method will accept the instance as its first argument
228      * and the {@code ObjectOutputStream} as its second argument.
229      * The return type of the method is {@code void}.
230      *
231      * @param cl a Serializable class
232      * @return  a direct MethodHandle for the synthetic {@code writeObject} method
233      *          or {@code null} if the class falls in a disallowed category
234      *
235      * @since 24
236      */
237     public final MethodHandle defaultWriteObjectForSerialization(Class<?> cl) {
238         return delegate.defaultWriteObjectForSerialization(cl);
239     }
240 
241     /**
242      * Returns a direct MethodHandle for the {@code readResolve} method on
243      * a serializable class.
244      * The single argument of {@link MethodHandle#invoke} is the serializable
245      * object.
246      *
247      * @param cl the Serializable class
248      * @return  a direct MethodHandle for the {@code readResolve} method of the class or
249      *          {@code null} if the class does not have a {@code readResolve} method
250      */
251     public final MethodHandle readResolveForSerialization(Class<?> cl) {
252         return delegate.readResolveForSerialization(cl);
253     }
254 
255     /**
256      * Returns a direct MethodHandle for the {@code writeReplace} method on
257      * a serializable class.
258      * The single argument of {@link MethodHandle#invoke} is the serializable
259      * object.
260      *
261      * @param cl the Serializable class
262      * @return  a direct MethodHandle for the {@code writeReplace} method of the class or
263      *          {@code null} if the class does not have a {@code writeReplace} method
264      */
265     public final MethodHandle writeReplaceForSerialization(Class<?> cl) {
266         return delegate.writeReplaceForSerialization(cl);
267     }
268 
269     /**
270      * Returns true if the class has a static initializer.
271      * The presence of a static initializer is used to compute the serialVersionUID.
272      * @param cl a serializable class
273      * @return {@code true} if the class has a static initializer,
274      *          otherwise {@code false}
275      */
276     public final boolean hasStaticInitializerForSerialization(Class<?> cl) {
277         return delegate.hasStaticInitializerForSerialization(cl);
278     }
279 
280     /**
281      * Returns a new OptionalDataException with {@code eof} set to {@code true}
282      * or {@code false}.
283      * @param bool the value of {@code eof} in the created OptionalDataException
284      * @return  a new OptionalDataException
285      */
286     public final OptionalDataException newOptionalDataExceptionForSerialization(boolean bool) {
287         Constructor<OptionalDataException> cons = delegate.newOptionalDataExceptionForSerialization();
288         try {
289             return cons.newInstance(bool);
290         } catch (InstantiationException|IllegalAccessException|InvocationTargetException ex) {
291             throw new InternalError("unable to create OptionalDataException", ex);
292         }
293     }
294 
295     /**
296      * {@return the declared {@code serialPersistentFields} from a
297      * serializable class, or {@code null} if none is declared, the field
298      * is declared but not valid, or the class is not a valid serializable class}
299      * A class is a valid serializable class if it implements {@code Serializable}
300      * but not {@code Externalizable}. The {@code serialPersistentFields} field
301      * is valid if it meets the type and accessibility restrictions defined
302      * by the Java Serialization specification.
303      *
304      * @param cl a Serializable class
305      *
306      * @since 24
307      */
308     public final ObjectStreamField[] serialPersistentFields(Class<?> cl) {
309         return delegate.serialPersistentFields(cl);
310     }
311 }
--- EOF ---