26 package jdk.internal.reflect; 27 28 import java.io.Externalizable; 29 import java.io.ObjectInputStream; 30 import java.io.ObjectOutputStream; 31 import java.io.ObjectStreamClass; 32 import java.io.OptionalDataException; 33 import java.io.Serializable; 34 import java.lang.invoke.MethodHandle; 35 import java.lang.invoke.MethodHandles; 36 import java.lang.reflect.Field; 37 import java.lang.reflect.Executable; 38 import java.lang.reflect.InvocationTargetException; 39 import java.lang.reflect.Method; 40 import java.lang.reflect.Constructor; 41 import java.lang.reflect.Modifier; 42 import java.security.PrivilegedAction; 43 import java.util.Objects; 44 import java.util.Properties; 45 46 import jdk.internal.misc.VM; 47 import sun.reflect.misc.ReflectUtil; 48 import sun.security.action.GetPropertyAction; 49 import sun.security.util.SecurityConstants; 50 51 /** <P> The master factory for all reflective objects, both those in 52 java.lang.reflect (Fields, Methods, Constructors) as well as their 53 delegates (FieldAccessors, MethodAccessors, ConstructorAccessors). 54 </P> 55 56 <P> The methods in this class are extremely unsafe and can cause 57 subversion of both the language and the verifier. For this reason, 58 they are all instance methods, and access to the constructor of 59 this factory is guarded by a security check, in similar style to 60 {@link jdk.internal.misc.Unsafe}. </P> 61 */ 62 63 public class ReflectionFactory { 64 65 private static boolean initted = false; 66 private static final ReflectionFactory soleInstance = new ReflectionFactory(); 67 // Provides access to package-private mechanisms in java.lang.reflect 68 private static volatile LangReflectAccess langReflectAccess; 69 70 /* Method for static class initializer <clinit>, or null */ 71 private static volatile Method hasStaticInitializerMethod; 72 73 // 74 // "Inflation" mechanism. Loading bytecodes to implement 75 // Method.invoke() and Constructor.newInstance() currently costs 76 // 3-4x more than an invocation via native code for the first 77 // invocation (though subsequent invocations have been benchmarked 78 // to be over 20x faster). Unfortunately this cost increases 79 // startup time for certain applications that use reflection 80 // intensively (but only once per class) to bootstrap themselves. 81 // To avoid this penalty we reuse the existing JVM entry points 82 // for the first few invocations of Methods and Constructors and 83 // then switch to the bytecode-based implementations. 84 // 85 // Package-private to be accessible to NativeMethodAccessorImpl 86 // and NativeConstructorAccessorImpl 87 private static boolean noInflation = false; 88 private static int inflationThreshold = 15; 89 90 // true if deserialization constructor checking is disabled 91 private static boolean disableSerialConstructorChecks = false; 92 93 private ReflectionFactory() { 94 } 95 96 /** 97 * A convenience class for acquiring the capability to instantiate 98 * reflective objects. Use this instead of a raw call to {@link 99 * #getReflectionFactory} in order to avoid being limited by the 100 * permissions of your callers. 101 * 102 * <p>An instance of this class can be used as the argument of 103 * <code>AccessController.doPrivileged</code>. 104 */ 105 public static final class GetReflectionFactoryAction 106 implements PrivilegedAction<ReflectionFactory> { 107 public ReflectionFactory run() { 108 return getReflectionFactory(); 109 } 110 } 111 112 /** 113 * Provides the caller with the capability to instantiate reflective 143 * A trusted method can define an alternate implementation for a method `foo` 144 * by defining a method named "reflected$foo" that will be invoked 145 * reflectively. 146 */ 147 private static Method findMethodForReflection(Method method) { 148 String altName = "reflected$" + method.getName(); 149 try { 150 return method.getDeclaringClass() 151 .getDeclaredMethod(altName, method.getParameterTypes()); 152 } catch (NoSuchMethodException ex) { 153 return null; 154 } 155 } 156 157 //-------------------------------------------------------------------------- 158 // 159 // Routines used by java.lang.reflect 160 // 161 // 162 163 /** Called only by java.lang.reflect.Modifier's static initializer */ 164 public void setLangReflectAccess(LangReflectAccess access) { 165 langReflectAccess = access; 166 } 167 168 /** 169 * Note: this routine can cause the declaring class for the field 170 * be initialized and therefore must not be called until the 171 * first get/set of this field. 172 * @param field the field 173 * @param override true if caller has overridden accessibility 174 */ 175 public FieldAccessor newFieldAccessor(Field field, boolean override) { 176 checkInitted(); 177 178 Field root = langReflectAccess().getRoot(field); 179 if (root != null) { 180 // FieldAccessor will use the root unless the modifiers have 181 // been overrridden 182 if (root.getModifiers() == field.getModifiers() || !override) { 183 field = root; 184 } 185 } 186 return UnsafeFieldAccessorFactory.newFieldAccessor(field, override); 187 } 188 189 public MethodAccessor newMethodAccessor(Method method) { 190 checkInitted(); 191 192 if (Reflection.isCallerSensitive(method)) { 193 Method altMethod = findMethodForReflection(method); 194 if (altMethod != null) { 195 method = altMethod; 196 } 197 } 198 199 // use the root Method that will not cache caller class 200 Method root = langReflectAccess().getRoot(method); 201 if (root != null) { 202 method = root; 203 } 204 205 if (noInflation && !ReflectUtil.isVMAnonymousClass(method.getDeclaringClass())) { 206 return new MethodAccessorGenerator(). 207 generateMethod(method.getDeclaringClass(), 208 method.getName(), 209 method.getParameterTypes(), 210 method.getReturnType(), 211 method.getExceptionTypes(), 212 method.getModifiers()); 213 } else { 214 NativeMethodAccessorImpl acc = 215 new NativeMethodAccessorImpl(method); 216 DelegatingMethodAccessorImpl res = 217 new DelegatingMethodAccessorImpl(acc); 218 acc.setParent(res); 219 return res; 220 } 221 } 222 223 public ConstructorAccessor newConstructorAccessor(Constructor<?> c) { 224 checkInitted(); 225 226 Class<?> declaringClass = c.getDeclaringClass(); 227 if (Modifier.isAbstract(declaringClass.getModifiers())) { 228 return new InstantiationExceptionConstructorAccessorImpl(null); 229 } 230 if (declaringClass == Class.class) { 231 return new InstantiationExceptionConstructorAccessorImpl 232 ("Can not instantiate java.lang.Class"); 233 } 234 235 // use the root Constructor that will not cache caller class 236 Constructor<?> root = langReflectAccess().getRoot(c); 237 if (root != null) { 238 c = root; 239 } 240 241 // Bootstrapping issue: since we use Class.newInstance() in 242 // the ConstructorAccessor generation process, we have to 243 // break the cycle here. 244 if (Reflection.isSubclassOf(declaringClass, 245 ConstructorAccessorImpl.class)) { 246 return new BootstrapConstructorAccessorImpl(c); 247 } 248 249 if (noInflation && !ReflectUtil.isVMAnonymousClass(c.getDeclaringClass())) { 250 return new MethodAccessorGenerator(). 251 generateConstructor(c.getDeclaringClass(), 252 c.getParameterTypes(), 253 c.getExceptionTypes(), 254 c.getModifiers()); 255 } else { 256 NativeConstructorAccessorImpl acc = 257 new NativeConstructorAccessorImpl(c); 258 DelegatingConstructorAccessorImpl res = 259 new DelegatingConstructorAccessorImpl(acc); 260 acc.setParent(res); 261 return res; 262 } 263 } 264 265 //-------------------------------------------------------------------------- 266 // 267 // Routines used by java.lang 268 // 269 // 270 271 /** Creates a new java.lang.reflect.Field. Access checks as per 272 java.lang.reflect.AccessibleObject are not overridden. */ 273 public Field newField(Class<?> declaringClass, 274 String name, 275 Class<?> type, 276 int modifiers, 277 int slot, 278 String signature, 279 byte[] annotations) 280 { 281 return langReflectAccess().newField(declaringClass, 282 name, 283 type, 284 modifiers, 285 slot, 286 signature, 287 annotations); 288 } 289 290 /** Creates a new java.lang.reflect.Method. Access checks as per 291 java.lang.reflect.AccessibleObject are not overridden. */ 292 public Method newMethod(Class<?> declaringClass, 293 String name, 294 Class<?>[] parameterTypes, 295 Class<?> returnType, 296 Class<?>[] checkedExceptions, 297 int modifiers, 298 int slot, 299 String signature, 300 byte[] annotations, 301 byte[] parameterAnnotations, 302 byte[] annotationDefault) 303 { 304 return langReflectAccess().newMethod(declaringClass, 305 name, 306 parameterTypes, 307 returnType, 308 checkedExceptions, 309 modifiers, 310 slot, 311 signature, 312 annotations, 313 parameterAnnotations, 314 annotationDefault); 315 } 316 317 /** Creates a new java.lang.reflect.Constructor. Access checks as 318 per java.lang.reflect.AccessibleObject are not overridden. */ 319 public Constructor<?> newConstructor(Class<?> declaringClass, 320 Class<?>[] parameterTypes, 321 Class<?>[] checkedExceptions, 322 int modifiers, 323 int slot, 324 String signature, 325 byte[] annotations, 326 byte[] parameterAnnotations) 327 { 328 return langReflectAccess().newConstructor(declaringClass, 329 parameterTypes, 330 checkedExceptions, 331 modifiers, 332 slot, 333 signature, 334 annotations, 335 parameterAnnotations); 336 } 337 338 /** Gets the MethodAccessor object for a java.lang.reflect.Method */ 339 public MethodAccessor getMethodAccessor(Method m) { 340 return langReflectAccess().getMethodAccessor(m); 341 } 342 343 /** Sets the MethodAccessor object for a java.lang.reflect.Method */ 344 public void setMethodAccessor(Method m, MethodAccessor accessor) { 345 langReflectAccess().setMethodAccessor(m, accessor); 346 } 347 348 /** Gets the ConstructorAccessor object for a 349 java.lang.reflect.Constructor */ 350 public ConstructorAccessor getConstructorAccessor(Constructor<?> c) { 351 return langReflectAccess().getConstructorAccessor(c); 352 } 353 354 /** Sets the ConstructorAccessor object for a 355 java.lang.reflect.Constructor */ 356 public void setConstructorAccessor(Constructor<?> c, 357 ConstructorAccessor accessor) 358 { 359 langReflectAccess().setConstructorAccessor(c, accessor); 360 } 361 362 /** Makes a copy of the passed method. The returned method is a 363 "child" of the passed one; see the comments in Method.java for 364 details. */ 365 public Method copyMethod(Method arg) { 366 return langReflectAccess().copyMethod(arg); 367 } 368 369 /** Makes a copy of the passed method. The returned method is NOT 370 * a "child" but a "sibling" of the Method in arg. Should only be 371 * used on non-root methods. */ 372 public Method leafCopyMethod(Method arg) { 373 return langReflectAccess().leafCopyMethod(arg); 374 } 375 376 377 /** Makes a copy of the passed field. The returned field is a 378 "child" of the passed one; see the comments in Field.java for 379 details. */ 380 public Field copyField(Field arg) { 381 return langReflectAccess().copyField(arg); 382 } 383 384 /** Makes a copy of the passed constructor. The returned 385 constructor is a "child" of the passed one; see the comments 386 in Constructor.java for details. */ 387 public <T> Constructor<T> copyConstructor(Constructor<T> arg) { 388 return langReflectAccess().copyConstructor(arg); 389 } 390 391 /** Gets the byte[] that encodes TypeAnnotations on an executable. 392 */ 393 public byte[] getExecutableTypeAnnotationBytes(Executable ex) { 394 return langReflectAccess().getExecutableTypeAnnotationBytes(ex); 395 } 396 397 public Class<?>[] getExecutableSharedParameterTypes(Executable ex) { 398 return langReflectAccess().getExecutableSharedParameterTypes(ex); 399 } 400 401 public <T> T newInstance(Constructor<T> ctor, Object[] args, Class<?> caller) 402 throws IllegalAccessException, InstantiationException, InvocationTargetException 403 { 404 return langReflectAccess().newInstance(ctor, args, caller); 405 } 406 407 //-------------------------------------------------------------------------- 408 // 409 // Routines used by serialization 410 // 411 // 412 413 public final Constructor<?> newConstructorForExternalization(Class<?> cl) { 414 if (!Externalizable.class.isAssignableFrom(cl)) { 415 return null; 416 } 417 try { 418 Constructor<?> cons = cl.getConstructor(); 419 cons.setAccessible(true); 420 return cons; 421 } catch (NoSuchMethodException ex) { 422 return null; 423 } 424 } 509 } catch (NoSuchMethodException ex) { 510 return null; 511 } 512 return generateConstructor(cl, constructorToCall); 513 } 514 515 private final Constructor<?> generateConstructor(Class<?> cl, 516 Constructor<?> constructorToCall) { 517 518 519 ConstructorAccessor acc = new MethodAccessorGenerator(). 520 generateSerializationConstructor(cl, 521 constructorToCall.getParameterTypes(), 522 constructorToCall.getExceptionTypes(), 523 constructorToCall.getModifiers(), 524 constructorToCall.getDeclaringClass()); 525 Constructor<?> c = newConstructor(constructorToCall.getDeclaringClass(), 526 constructorToCall.getParameterTypes(), 527 constructorToCall.getExceptionTypes(), 528 constructorToCall.getModifiers(), 529 langReflectAccess(). 530 getConstructorSlot(constructorToCall), 531 langReflectAccess(). 532 getConstructorSignature(constructorToCall), 533 langReflectAccess(). 534 getConstructorAnnotations(constructorToCall), 535 langReflectAccess(). 536 getConstructorParameterAnnotations(constructorToCall)); 537 setConstructorAccessor(c, acc); 538 c.setAccessible(true); 539 return c; 540 } 541 542 public final MethodHandle readObjectForSerialization(Class<?> cl) { 543 return findReadWriteObjectForSerialization(cl, "readObject", ObjectInputStream.class); 544 } 545 546 public final MethodHandle readObjectNoDataForSerialization(Class<?> cl) { 547 return findReadWriteObjectForSerialization(cl, "readObjectNoData", ObjectInputStream.class); 548 } 549 550 public final MethodHandle writeObjectForSerialization(Class<?> cl) { 551 return findReadWriteObjectForSerialization(cl, "writeObject", ObjectOutputStream.class); 552 } 553 554 private final MethodHandle findReadWriteObjectForSerialization(Class<?> cl, 555 String methodName, 708 String val = props.getProperty("sun.reflect.noInflation"); 709 if (val != null && val.equals("true")) { 710 noInflation = true; 711 } 712 713 val = props.getProperty("sun.reflect.inflationThreshold"); 714 if (val != null) { 715 try { 716 inflationThreshold = Integer.parseInt(val); 717 } catch (NumberFormatException e) { 718 throw new RuntimeException("Unable to parse property sun.reflect.inflationThreshold", e); 719 } 720 } 721 722 disableSerialConstructorChecks = 723 "true".equals(props.getProperty("jdk.disableSerialConstructorChecks")); 724 725 initted = true; 726 } 727 728 private static LangReflectAccess langReflectAccess() { 729 if (langReflectAccess == null) { 730 // Call a static method to get class java.lang.reflect.Modifier 731 // initialized. Its static initializer will cause 732 // setLangReflectAccess() to be called from the context of the 733 // java.lang.reflect package. 734 Modifier.isPublic(Modifier.PUBLIC); 735 } 736 return langReflectAccess; 737 } 738 739 /** 740 * Returns true if classes are defined in the classloader and same package, false 741 * otherwise. 742 * @param cl1 a class 743 * @param cl2 another class 744 * @returns true if the two classes are in the same classloader and package 745 */ 746 private static boolean packageEquals(Class<?> cl1, Class<?> cl2) { 747 assert !cl1.isArray() && !cl2.isArray(); 748 749 if (cl1 == cl2) { 750 return true; 751 } 752 753 return cl1.getClassLoader() == cl2.getClassLoader() && 754 Objects.equals(cl1.getPackageName(), cl2.getPackageName()); 755 } 756 757 } | 26 package jdk.internal.reflect; 27 28 import java.io.Externalizable; 29 import java.io.ObjectInputStream; 30 import java.io.ObjectOutputStream; 31 import java.io.ObjectStreamClass; 32 import java.io.OptionalDataException; 33 import java.io.Serializable; 34 import java.lang.invoke.MethodHandle; 35 import java.lang.invoke.MethodHandles; 36 import java.lang.reflect.Field; 37 import java.lang.reflect.Executable; 38 import java.lang.reflect.InvocationTargetException; 39 import java.lang.reflect.Method; 40 import java.lang.reflect.Constructor; 41 import java.lang.reflect.Modifier; 42 import java.security.PrivilegedAction; 43 import java.util.Objects; 44 import java.util.Properties; 45 46 import jdk.internal.access.JavaLangReflectAccess; 47 import jdk.internal.access.SharedSecrets; 48 import jdk.internal.misc.VM; 49 import sun.reflect.misc.ReflectUtil; 50 import sun.security.action.GetPropertyAction; 51 import sun.security.util.SecurityConstants; 52 53 /** <P> The master factory for all reflective objects, both those in 54 java.lang.reflect (Fields, Methods, Constructors) as well as their 55 delegates (FieldAccessors, MethodAccessors, ConstructorAccessors). 56 </P> 57 58 <P> The methods in this class are extremely unsafe and can cause 59 subversion of both the language and the verifier. For this reason, 60 they are all instance methods, and access to the constructor of 61 this factory is guarded by a security check, in similar style to 62 {@link jdk.internal.misc.Unsafe}. </P> 63 */ 64 65 public class ReflectionFactory { 66 67 private static boolean initted = false; 68 private static final ReflectionFactory soleInstance = new ReflectionFactory(); 69 70 71 /* Method for static class initializer <clinit>, or null */ 72 private static volatile Method hasStaticInitializerMethod; 73 74 // 75 // "Inflation" mechanism. Loading bytecodes to implement 76 // Method.invoke() and Constructor.newInstance() currently costs 77 // 3-4x more than an invocation via native code for the first 78 // invocation (though subsequent invocations have been benchmarked 79 // to be over 20x faster). Unfortunately this cost increases 80 // startup time for certain applications that use reflection 81 // intensively (but only once per class) to bootstrap themselves. 82 // To avoid this penalty we reuse the existing JVM entry points 83 // for the first few invocations of Methods and Constructors and 84 // then switch to the bytecode-based implementations. 85 // 86 // Package-private to be accessible to NativeMethodAccessorImpl 87 // and NativeConstructorAccessorImpl 88 private static boolean noInflation = false; 89 private static int inflationThreshold = 15; 90 91 // true if deserialization constructor checking is disabled 92 private static boolean disableSerialConstructorChecks = false; 93 94 private final JavaLangReflectAccess langReflectAccess; 95 private ReflectionFactory() { 96 this.langReflectAccess = SharedSecrets.getJavaLangReflectAccess(); 97 } 98 99 /** 100 * A convenience class for acquiring the capability to instantiate 101 * reflective objects. Use this instead of a raw call to {@link 102 * #getReflectionFactory} in order to avoid being limited by the 103 * permissions of your callers. 104 * 105 * <p>An instance of this class can be used as the argument of 106 * <code>AccessController.doPrivileged</code>. 107 */ 108 public static final class GetReflectionFactoryAction 109 implements PrivilegedAction<ReflectionFactory> { 110 public ReflectionFactory run() { 111 return getReflectionFactory(); 112 } 113 } 114 115 /** 116 * Provides the caller with the capability to instantiate reflective 146 * A trusted method can define an alternate implementation for a method `foo` 147 * by defining a method named "reflected$foo" that will be invoked 148 * reflectively. 149 */ 150 private static Method findMethodForReflection(Method method) { 151 String altName = "reflected$" + method.getName(); 152 try { 153 return method.getDeclaringClass() 154 .getDeclaredMethod(altName, method.getParameterTypes()); 155 } catch (NoSuchMethodException ex) { 156 return null; 157 } 158 } 159 160 //-------------------------------------------------------------------------- 161 // 162 // Routines used by java.lang.reflect 163 // 164 // 165 166 /* 167 * Note: this routine can cause the declaring class for the field 168 * be initialized and therefore must not be called until the 169 * first get/set of this field. 170 * @param field the field 171 * @param override true if caller has overridden accessibility 172 */ 173 public FieldAccessor newFieldAccessor(Field field, boolean override) { 174 checkInitted(); 175 176 Field root = langReflectAccess.getRoot(field); 177 if (root != null) { 178 // FieldAccessor will use the root unless the modifiers have 179 // been overrridden 180 if (root.getModifiers() == field.getModifiers() || !override) { 181 field = root; 182 } 183 } 184 return UnsafeFieldAccessorFactory.newFieldAccessor(field, override); 185 } 186 187 public MethodAccessor newMethodAccessor(Method method) { 188 checkInitted(); 189 190 if (Reflection.isCallerSensitive(method)) { 191 Method altMethod = findMethodForReflection(method); 192 if (altMethod != null) { 193 method = altMethod; 194 } 195 } 196 197 // use the root Method that will not cache caller class 198 Method root = langReflectAccess.getRoot(method); 199 if (root != null) { 200 method = root; 201 } 202 203 if (noInflation && !ReflectUtil.isVMAnonymousClass(method.getDeclaringClass())) { 204 return new MethodAccessorGenerator(). 205 generateMethod(method.getDeclaringClass(), 206 method.getName(), 207 method.getParameterTypes(), 208 method.getReturnType(), 209 method.getExceptionTypes(), 210 method.getModifiers()); 211 } else { 212 NativeMethodAccessorImpl acc = 213 new NativeMethodAccessorImpl(method); 214 DelegatingMethodAccessorImpl res = 215 new DelegatingMethodAccessorImpl(acc); 216 acc.setParent(res); 217 return res; 218 } 219 } 220 221 public ConstructorAccessor newConstructorAccessor(Constructor<?> c) { 222 checkInitted(); 223 224 Class<?> declaringClass = c.getDeclaringClass(); 225 if (Modifier.isAbstract(declaringClass.getModifiers())) { 226 return new InstantiationExceptionConstructorAccessorImpl(null); 227 } 228 if (declaringClass == Class.class) { 229 return new InstantiationExceptionConstructorAccessorImpl 230 ("Can not instantiate java.lang.Class"); 231 } 232 233 // use the root Constructor that will not cache caller class 234 Constructor<?> root = langReflectAccess.getRoot(c); 235 if (root != null) { 236 c = root; 237 } 238 239 // Bootstrapping issue: since we use Class.newInstance() in 240 // the ConstructorAccessor generation process, we have to 241 // break the cycle here. 242 if (Reflection.isSubclassOf(declaringClass, 243 ConstructorAccessorImpl.class)) { 244 return new BootstrapConstructorAccessorImpl(c); 245 } 246 247 if (noInflation && !ReflectUtil.isVMAnonymousClass(c.getDeclaringClass())) { 248 return new MethodAccessorGenerator(). 249 generateConstructor(c.getDeclaringClass(), 250 c.getParameterTypes(), 251 c.getExceptionTypes(), 252 c.getModifiers()); 253 } else { 254 NativeConstructorAccessorImpl acc = 255 new NativeConstructorAccessorImpl(c); 256 DelegatingConstructorAccessorImpl res = 257 new DelegatingConstructorAccessorImpl(acc); 258 acc.setParent(res); 259 return res; 260 } 261 } 262 263 //-------------------------------------------------------------------------- 264 // 265 // Routines used by java.lang 266 // 267 // 268 269 /** Creates a new java.lang.reflect.Constructor. Access checks as 270 per java.lang.reflect.AccessibleObject are not overridden. */ 271 public Constructor<?> newConstructor(Class<?> declaringClass, 272 Class<?>[] parameterTypes, 273 Class<?>[] checkedExceptions, 274 int modifiers, 275 int slot, 276 String signature, 277 byte[] annotations, 278 byte[] parameterAnnotations) 279 { 280 return langReflectAccess.newConstructor(declaringClass, 281 parameterTypes, 282 checkedExceptions, 283 modifiers, 284 slot, 285 signature, 286 annotations, 287 parameterAnnotations); 288 } 289 290 /** Gets the ConstructorAccessor object for a 291 java.lang.reflect.Constructor */ 292 public ConstructorAccessor getConstructorAccessor(Constructor<?> c) { 293 return langReflectAccess.getConstructorAccessor(c); 294 } 295 296 /** Sets the ConstructorAccessor object for a 297 java.lang.reflect.Constructor */ 298 public void setConstructorAccessor(Constructor<?> c, 299 ConstructorAccessor accessor) 300 { 301 langReflectAccess.setConstructorAccessor(c, accessor); 302 } 303 304 /** Makes a copy of the passed method. The returned method is a 305 "child" of the passed one; see the comments in Method.java for 306 details. */ 307 public Method copyMethod(Method arg) { 308 return langReflectAccess.copyMethod(arg); 309 } 310 311 /** Makes a copy of the passed method. The returned method is NOT 312 * a "child" but a "sibling" of the Method in arg. Should only be 313 * used on non-root methods. */ 314 public Method leafCopyMethod(Method arg) { 315 return langReflectAccess.leafCopyMethod(arg); 316 } 317 318 319 /** Makes a copy of the passed field. The returned field is a 320 "child" of the passed one; see the comments in Field.java for 321 details. */ 322 public Field copyField(Field arg) { 323 return langReflectAccess.copyField(arg); 324 } 325 326 /** Makes a copy of the passed constructor. The returned 327 constructor is a "child" of the passed one; see the comments 328 in Constructor.java for details. */ 329 public <T> Constructor<T> copyConstructor(Constructor<T> arg) { 330 return langReflectAccess.copyConstructor(arg); 331 } 332 333 /** Gets the byte[] that encodes TypeAnnotations on an executable. 334 */ 335 public byte[] getExecutableTypeAnnotationBytes(Executable ex) { 336 return langReflectAccess.getExecutableTypeAnnotationBytes(ex); 337 } 338 339 public Class<?>[] getExecutableSharedParameterTypes(Executable ex) { 340 return langReflectAccess.getExecutableSharedParameterTypes(ex); 341 } 342 343 public <T> T newInstance(Constructor<T> ctor, Object[] args, Class<?> caller) 344 throws IllegalAccessException, InstantiationException, InvocationTargetException 345 { 346 return langReflectAccess.newInstance(ctor, args, caller); 347 } 348 349 //-------------------------------------------------------------------------- 350 // 351 // Routines used by serialization 352 // 353 // 354 355 public final Constructor<?> newConstructorForExternalization(Class<?> cl) { 356 if (!Externalizable.class.isAssignableFrom(cl)) { 357 return null; 358 } 359 try { 360 Constructor<?> cons = cl.getConstructor(); 361 cons.setAccessible(true); 362 return cons; 363 } catch (NoSuchMethodException ex) { 364 return null; 365 } 366 } 451 } catch (NoSuchMethodException ex) { 452 return null; 453 } 454 return generateConstructor(cl, constructorToCall); 455 } 456 457 private final Constructor<?> generateConstructor(Class<?> cl, 458 Constructor<?> constructorToCall) { 459 460 461 ConstructorAccessor acc = new MethodAccessorGenerator(). 462 generateSerializationConstructor(cl, 463 constructorToCall.getParameterTypes(), 464 constructorToCall.getExceptionTypes(), 465 constructorToCall.getModifiers(), 466 constructorToCall.getDeclaringClass()); 467 Constructor<?> c = newConstructor(constructorToCall.getDeclaringClass(), 468 constructorToCall.getParameterTypes(), 469 constructorToCall.getExceptionTypes(), 470 constructorToCall.getModifiers(), 471 langReflectAccess. 472 getConstructorSlot(constructorToCall), 473 langReflectAccess. 474 getConstructorSignature(constructorToCall), 475 langReflectAccess. 476 getConstructorAnnotations(constructorToCall), 477 langReflectAccess. 478 getConstructorParameterAnnotations(constructorToCall)); 479 setConstructorAccessor(c, acc); 480 c.setAccessible(true); 481 return c; 482 } 483 484 public final MethodHandle readObjectForSerialization(Class<?> cl) { 485 return findReadWriteObjectForSerialization(cl, "readObject", ObjectInputStream.class); 486 } 487 488 public final MethodHandle readObjectNoDataForSerialization(Class<?> cl) { 489 return findReadWriteObjectForSerialization(cl, "readObjectNoData", ObjectInputStream.class); 490 } 491 492 public final MethodHandle writeObjectForSerialization(Class<?> cl) { 493 return findReadWriteObjectForSerialization(cl, "writeObject", ObjectOutputStream.class); 494 } 495 496 private final MethodHandle findReadWriteObjectForSerialization(Class<?> cl, 497 String methodName, 650 String val = props.getProperty("sun.reflect.noInflation"); 651 if (val != null && val.equals("true")) { 652 noInflation = true; 653 } 654 655 val = props.getProperty("sun.reflect.inflationThreshold"); 656 if (val != null) { 657 try { 658 inflationThreshold = Integer.parseInt(val); 659 } catch (NumberFormatException e) { 660 throw new RuntimeException("Unable to parse property sun.reflect.inflationThreshold", e); 661 } 662 } 663 664 disableSerialConstructorChecks = 665 "true".equals(props.getProperty("jdk.disableSerialConstructorChecks")); 666 667 initted = true; 668 } 669 670 /** 671 * Returns true if classes are defined in the classloader and same package, false 672 * otherwise. 673 * @param cl1 a class 674 * @param cl2 another class 675 * @returns true if the two classes are in the same classloader and package 676 */ 677 private static boolean packageEquals(Class<?> cl1, Class<?> cl2) { 678 assert !cl1.isArray() && !cl2.isArray(); 679 680 if (cl1 == cl2) { 681 return true; 682 } 683 684 return cl1.getClassLoader() == cl2.getClassLoader() && 685 Objects.equals(cl1.getPackageName(), cl2.getPackageName()); 686 } 687 688 } |