50 import java.util.HashSet; 51 import java.util.LinkedHashMap; 52 import java.util.List; 53 import java.util.Set; 54 import java.util.Map; 55 import java.util.HashMap; 56 import java.util.Objects; 57 import java.util.StringJoiner; 58 import sun.misc.Unsafe; 59 import sun.reflect.CallerSensitive; 60 import sun.reflect.ConstantPool; 61 import sun.reflect.Reflection; 62 import sun.reflect.ReflectionFactory; 63 import sun.reflect.generics.factory.CoreReflectionFactory; 64 import sun.reflect.generics.factory.GenericsFactory; 65 import sun.reflect.generics.repository.ClassRepository; 66 import sun.reflect.generics.repository.MethodRepository; 67 import sun.reflect.generics.repository.ConstructorRepository; 68 import sun.reflect.generics.scope.ClassScope; 69 import sun.security.util.SecurityConstants; 70 import java.lang.annotation.Annotation; 71 import java.lang.reflect.Proxy; 72 import sun.reflect.annotation.*; 73 import sun.reflect.misc.ReflectUtil; 74 75 /** 76 * Instances of the class {@code Class} represent classes and 77 * interfaces in a running Java application. An enum is a kind of 78 * class and an annotation is a kind of interface. Every array also 79 * belongs to a class that is reflected as a {@code Class} object 80 * that is shared by all arrays with the same element type and number 81 * of dimensions. The primitive Java types ({@code boolean}, 82 * {@code byte}, {@code char}, {@code short}, 83 * {@code int}, {@code long}, {@code float}, and 84 * {@code double}), and the keyword {@code void} are also 85 * represented as {@code Class} objects. 86 * 87 * <p> {@code Class} has no public constructor. Instead {@code Class} 88 * objects are constructed automatically by the Java Virtual Machine as classes 89 * are loaded and by calls to the {@code defineClass} method in the class 90 * loader. 91 * 2445 static <T> boolean casReflectionData(Class<?> clazz, 2446 SoftReference<ReflectionData<T>> oldData, 2447 SoftReference<ReflectionData<T>> newData) { 2448 return unsafe.compareAndSwapObject(clazz, reflectionDataOffset, oldData, newData); 2449 } 2450 2451 static <T> boolean casAnnotationType(Class<?> clazz, 2452 AnnotationType oldType, 2453 AnnotationType newType) { 2454 return unsafe.compareAndSwapObject(clazz, annotationTypeOffset, oldType, newType); 2455 } 2456 2457 static <T> boolean casAnnotationData(Class<?> clazz, 2458 AnnotationData oldData, 2459 AnnotationData newData) { 2460 return unsafe.compareAndSwapObject(clazz, annotationDataOffset, oldData, newData); 2461 } 2462 } 2463 2464 /** 2465 * Reflection support. 2466 */ 2467 2468 // Caches for certain reflective results 2469 private static boolean useCaches = true; 2470 2471 // reflection data that might get invalidated when JVM TI RedefineClasses() is called 2472 private static class ReflectionData<T> { 2473 volatile Field[] declaredFields; 2474 volatile Field[] publicFields; 2475 volatile Method[] declaredMethods; 2476 volatile Method[] publicMethods; 2477 volatile Constructor<T>[] declaredConstructors; 2478 volatile Constructor<T>[] publicConstructors; 2479 // Intermediate results for getFields and getMethods 2480 volatile Field[] declaredPublicFields; 2481 volatile Method[] declaredPublicMethods; 2482 volatile Class<?>[] interfaces; 2483 2484 // Value of classRedefinedCount when we created this ReflectionData instance | 50 import java.util.HashSet; 51 import java.util.LinkedHashMap; 52 import java.util.List; 53 import java.util.Set; 54 import java.util.Map; 55 import java.util.HashMap; 56 import java.util.Objects; 57 import java.util.StringJoiner; 58 import sun.misc.Unsafe; 59 import sun.reflect.CallerSensitive; 60 import sun.reflect.ConstantPool; 61 import sun.reflect.Reflection; 62 import sun.reflect.ReflectionFactory; 63 import sun.reflect.generics.factory.CoreReflectionFactory; 64 import sun.reflect.generics.factory.GenericsFactory; 65 import sun.reflect.generics.repository.ClassRepository; 66 import sun.reflect.generics.repository.MethodRepository; 67 import sun.reflect.generics.repository.ConstructorRepository; 68 import sun.reflect.generics.scope.ClassScope; 69 import sun.security.util.SecurityConstants; 70 71 import java.lang.annotation.Annotation; 72 import java.lang.reflect.Proxy; 73 74 import sun.reflect.annotation.*; 75 import sun.reflect.misc.ReflectUtil; 76 77 /** 78 * Instances of the class {@code Class} represent classes and 79 * interfaces in a running Java application. An enum is a kind of 80 * class and an annotation is a kind of interface. Every array also 81 * belongs to a class that is reflected as a {@code Class} object 82 * that is shared by all arrays with the same element type and number 83 * of dimensions. The primitive Java types ({@code boolean}, 84 * {@code byte}, {@code char}, {@code short}, 85 * {@code int}, {@code long}, {@code float}, and 86 * {@code double}), and the keyword {@code void} are also 87 * represented as {@code Class} objects. 88 * 89 * <p> {@code Class} has no public constructor. Instead {@code Class} 90 * objects are constructed automatically by the Java Virtual Machine as classes 91 * are loaded and by calls to the {@code defineClass} method in the class 92 * loader. 93 * 2447 static <T> boolean casReflectionData(Class<?> clazz, 2448 SoftReference<ReflectionData<T>> oldData, 2449 SoftReference<ReflectionData<T>> newData) { 2450 return unsafe.compareAndSwapObject(clazz, reflectionDataOffset, oldData, newData); 2451 } 2452 2453 static <T> boolean casAnnotationType(Class<?> clazz, 2454 AnnotationType oldType, 2455 AnnotationType newType) { 2456 return unsafe.compareAndSwapObject(clazz, annotationTypeOffset, oldType, newType); 2457 } 2458 2459 static <T> boolean casAnnotationData(Class<?> clazz, 2460 AnnotationData oldData, 2461 AnnotationData newData) { 2462 return unsafe.compareAndSwapObject(clazz, annotationDataOffset, oldData, newData); 2463 } 2464 } 2465 2466 /** 2467 * ClassData 2468 */ 2469 private static class ClassData<T> { 2470 /** 2471 * This needs to be a simple data structure because we need to access 2472 * and update its elements from the JVM. Note that the Java side controls 2473 * the allocation and order of elements in the array; the JVM modifies 2474 * fields of those elements during class redefinition. 2475 */ 2476 private volatile Comparable<?>[] elementData; 2477 private volatile int size; 2478 2479 /** 2480 * Interns a member name in the member name table. 2481 * Returns null if a race with the jvm occurred. Races are detected 2482 * by checking for changes in the class redefinition count that occur 2483 * before an intern is complete. 2484 * 2485 * @param klass class whose redefinition count is checked. 2486 * @param memberName member name to be interned 2487 * @param redefined_count the value of classRedefinedCount() observed before 2488 * creation of the MemberName that is being interned. 2489 * @return null if a race occurred, otherwise the interned MemberName. 2490 */ 2491 @SuppressWarnings({"unchecked","rawtypes"}) 2492 public <E extends Comparable<? super E>> E intern(Class<?> klass, E memberName, int redefined_count) { 2493 if (elementData == null) { 2494 synchronized (this) { 2495 if (elementData == null) { 2496 elementData = new Comparable[1]; 2497 } 2498 } 2499 } 2500 synchronized (elementData) { 2501 final int index = Arrays.binarySearch(elementData, 0, size, memberName); 2502 if (index >= 0) { 2503 return (E) elementData[index]; 2504 } 2505 // Not found, add carefully. 2506 return add(klass, ~index, memberName, redefined_count); 2507 } 2508 } 2509 2510 /** 2511 * Appends the specified element at the specified index. 2512 * 2513 * @param klass the klass for this ClassData. 2514 * @param index index at which insertion should occur 2515 * @param e element to be insert into this list 2516 * @param redefined_count value of klass.classRedefinedCount() before e was created. 2517 * @return null if insertion failed because of redefinition race, otherwise e. 2518 */ 2519 private <E extends Comparable<? super E>> E add(Class<?> klass, int index, E e, int redefined_count) { 2520 int oldCapacity = size; 2521 Comparable<?>[] element_data = elementData; 2522 if (oldCapacity + 1 > element_data.length ) { 2523 // Replacing array with a copy is safe; elements are identical. 2524 grow(oldCapacity + 1); 2525 element_data = elementData; 2526 } 2527 /* 2528 * Careful dance to insert an element. 2529 * 2530 * Wish to ensure that we do not hide an element of 2531 * the array; must also ensure that visible-to-jvm 2532 * portion of array never contains nulls; also ensure 2533 * that array remains sorted. 2534 * 2535 * Note that taking a safepoint is also a barrier. 2536 */ 2537 if (oldCapacity > 0) { 2538 element_data[oldCapacity] = element_data[oldCapacity - 1]; 2539 // all array elements are non-null and sorted, increase size. 2540 // if store to element_data above floats below 2541 // store to size on the next line, that will be 2542 // inconsistent to the VM if a safepoint occurs here. 2543 size += 1; 2544 for (int i = oldCapacity; i > index; i--) { 2545 // pre: element_data[i] is duplicated at [i+1] 2546 element_data[i] = element_data[i - 1]; 2547 // post: element_data[i-1] is duplicated at [i] 2548 } 2549 // element_data[index] is duplicated at [index+1] 2550 element_data[index] = (Comparable<?>) e; 2551 } else { 2552 element_data[index] = (Comparable<?>) e; 2553 // Don't want store to element_data[index] above 2554 // to float past store to size below, otherwise 2555 // VM might see inconsistent state. 2556 size += 1; 2557 } 2558 if (redefined_count == klass.classRedefinedCount()) { 2559 return e; 2560 } 2561 /* The race was lost, must undo insertion and retry the intern 2562 * with a new resolution. 2563 */ 2564 for (int i = index; i < oldCapacity; i++) { 2565 element_data[i] = element_data[i+1]; 2566 } 2567 size -= 1; 2568 return null; 2569 } 2570 2571 /** 2572 * Increases the capacity to ensure that it can hold at least the 2573 * number of elements specified by the minimum capacity argument. 2574 * 2575 * @param minCapacity the desired minimum capacity 2576 */ 2577 private void grow(int minCapacity) { 2578 // overflow-conscious code 2579 int oldCapacity = elementData.length; 2580 int newCapacity = oldCapacity + (oldCapacity >> 1); 2581 if (newCapacity - minCapacity < 0) 2582 newCapacity = minCapacity; 2583 // minCapacity is usually close to size, so this is a win: 2584 elementData = Arrays.copyOf(elementData, newCapacity); 2585 } 2586 } 2587 2588 private volatile transient ClassData<T> classData; 2589 2590 /** 2591 * Lazily create {@link ClassData}. 2592 */ 2593 private ClassData<T> classData() { 2594 if (this.classData != null) { 2595 return this.classData; 2596 } 2597 synchronized (this) { 2598 if (this.classData == null) { 2599 this.classData = new ClassData<>(); 2600 } 2601 } 2602 return this.classData; 2603 } 2604 2605 /* package */ <E extends Comparable<? super E>> E internMemberName(E memberName, int redefined_count) { 2606 return classData().intern(this, memberName, redefined_count); 2607 } 2608 2609 /* package */ int classRedefinedCount() { 2610 return classRedefinedCount; 2611 } 2612 2613 /** 2614 * Reflection support. 2615 */ 2616 2617 // Caches for certain reflective results 2618 private static boolean useCaches = true; 2619 2620 // reflection data that might get invalidated when JVM TI RedefineClasses() is called 2621 private static class ReflectionData<T> { 2622 volatile Field[] declaredFields; 2623 volatile Field[] publicFields; 2624 volatile Method[] declaredMethods; 2625 volatile Method[] publicMethods; 2626 volatile Constructor<T>[] declaredConstructors; 2627 volatile Constructor<T>[] publicConstructors; 2628 // Intermediate results for getFields and getMethods 2629 volatile Field[] declaredPublicFields; 2630 volatile Method[] declaredPublicMethods; 2631 volatile Class<?>[] interfaces; 2632 2633 // Value of classRedefinedCount when we created this ReflectionData instance |