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