< prev index next >

src/java.base/share/classes/java/lang/Class.java

Print this page




  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  *


2405             int index = baseName.lastIndexOf('.');
2406             if (index != -1) {
2407                 name = baseName.substring(0, index).replace('.', '/')
2408                     +"/"+name;
2409             }
2410         } else {
2411             name = name.substring(1);
2412         }
2413         return name;
2414     }
2415 
2416     /**
2417      * Atomic operations support.
2418      */
2419     private static class Atomic {
2420         // initialize Unsafe machinery here, since we need to call Class.class instance method
2421         // and have to avoid calling it in the static initializer of the Class class...
2422         private static final Unsafe unsafe = Unsafe.getUnsafe();
2423         // offset of Class.reflectionData instance field
2424         private static final long reflectionDataOffset;


2425         // offset of Class.annotationType instance field
2426         private static final long annotationTypeOffset;
2427         // offset of Class.annotationData instance field
2428         private static final long annotationDataOffset;
2429 
2430         static {
2431             Field[] fields = Class.class.getDeclaredFields0(false); // bypass caches
2432             reflectionDataOffset = objectFieldOffset(fields, "reflectionData");

2433             annotationTypeOffset = objectFieldOffset(fields, "annotationType");
2434             annotationDataOffset = objectFieldOffset(fields, "annotationData");
2435         }
2436 
2437         private static long objectFieldOffset(Field[] fields, String fieldName) {
2438             Field field = searchFields(fields, fieldName);
2439             if (field == null) {
2440                 throw new Error("No " + fieldName + " field found in java.lang.Class");
2441             }
2442             return unsafe.objectFieldOffset(field);
2443         }
2444 
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;




  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  *


2407             int index = baseName.lastIndexOf('.');
2408             if (index != -1) {
2409                 name = baseName.substring(0, index).replace('.', '/')
2410                     +"/"+name;
2411             }
2412         } else {
2413             name = name.substring(1);
2414         }
2415         return name;
2416     }
2417 
2418     /**
2419      * Atomic operations support.
2420      */
2421     private static class Atomic {
2422         // initialize Unsafe machinery here, since we need to call Class.class instance method
2423         // and have to avoid calling it in the static initializer of the Class class...
2424         private static final Unsafe unsafe = Unsafe.getUnsafe();
2425         // offset of Class.reflectionData instance field
2426         private static final long reflectionDataOffset;
2427         // offset of Class.classData instance field
2428         private static final long classDataOffset;
2429         // offset of Class.annotationType instance field
2430         private static final long annotationTypeOffset;
2431         // offset of Class.annotationData instance field
2432         private static final long annotationDataOffset;
2433 
2434         static {
2435             Field[] fields = Class.class.getDeclaredFields0(false); // bypass caches
2436             reflectionDataOffset = objectFieldOffset(fields, "reflectionData");
2437             classDataOffset = objectFieldOffset(fields, "classData");
2438             annotationTypeOffset = objectFieldOffset(fields, "annotationType");
2439             annotationDataOffset = objectFieldOffset(fields, "annotationData");
2440         }
2441 
2442         private static long objectFieldOffset(Field[] fields, String fieldName) {
2443             Field field = searchFields(fields, fieldName);
2444             if (field == null) {
2445                 throw new Error("No " + fieldName + " field found in java.lang.Class");
2446             }
2447             return unsafe.objectFieldOffset(field);
2448         }
2449 
2450         static <T> boolean casReflectionData(Class<?> clazz,
2451                                              SoftReference<ReflectionData<T>> oldData,
2452                                              SoftReference<ReflectionData<T>> newData) {
2453             return unsafe.compareAndSwapObject(clazz, reflectionDataOffset, oldData, newData);
2454         }
2455 
2456         static <T> boolean casClassData(Class<?> clazz,
2457                                         Object oldData,
2458                                         Object newData) {
2459             return unsafe.compareAndSwapObject(clazz, classDataOffset, oldData, newData);
2460         }
2461 
2462         static <T> boolean casAnnotationType(Class<?> clazz,
2463                                              AnnotationType oldType,
2464                                              AnnotationType newType) {
2465             return unsafe.compareAndSwapObject(clazz, annotationTypeOffset, oldType, newType);
2466         }
2467 
2468         static <T> boolean casAnnotationData(Class<?> clazz,
2469                                              AnnotationData oldData,
2470                                              AnnotationData newData) {
2471             return unsafe.compareAndSwapObject(clazz, annotationDataOffset, oldData, newData);
2472         }
2473     }
2474 
2475     private volatile transient Object classData;
2476 
2477     /* package */ Object classData() {
2478         return this.classData;
2479     }
2480 
2481     /* package */ boolean casClassData(Object oldData, Object newData) {
2482         return Atomic.casClassData(this, oldData, newData);
2483     }
2484 
2485     /* package */ int classRedefinedCount() {
2486         return classRedefinedCount;
2487     }
2488 
2489     /**
2490      * Reflection support.
2491      */
2492 
2493     // Caches for certain reflective results
2494     private static boolean useCaches = true;
2495 
2496     // reflection data that might get invalidated when JVM TI RedefineClasses() is called
2497     private static class ReflectionData<T> {
2498         volatile Field[] declaredFields;
2499         volatile Field[] publicFields;
2500         volatile Method[] declaredMethods;
2501         volatile Method[] publicMethods;
2502         volatile Constructor<T>[] declaredConstructors;
2503         volatile Constructor<T>[] publicConstructors;
2504         // Intermediate results for getFields and getMethods
2505         volatile Field[] declaredPublicFields;
2506         volatile Method[] declaredPublicMethods;


< prev index next >