src/java.base/share/classes/java/lang/Class.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File jdk Cdiff src/java.base/share/classes/java/lang/Class.java

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

Print this page

        

*** 65,76 **** --- 65,78 ---- import sun.reflect.generics.repository.ClassRepository; import sun.reflect.generics.repository.MethodRepository; import sun.reflect.generics.repository.ConstructorRepository; import sun.reflect.generics.scope.ClassScope; import sun.security.util.SecurityConstants; + import java.lang.annotation.Annotation; import java.lang.reflect.Proxy; + import sun.reflect.annotation.*; import sun.reflect.misc.ReflectUtil; /** * Instances of the class {@code Class} represent classes and
*** 2460,2469 **** --- 2462,2587 ---- return unsafe.compareAndSwapObject(clazz, annotationDataOffset, oldData, newData); } } /** + * ClassData + */ + private static class ClassData<T> { + /** + * This needs to be a simple data structure because we need to access and update its elements from the JVM. + */ + private Comparable<?>[] elementData; + private int size; + + /** + * Interns a member name in the member name table. + * + * @param memberName member name to be interned + * @return + */ + @SuppressWarnings({"unchecked","rawtypes"}) + public <E extends Comparable<? super E>> E intern(E memberName) { + if (elementData == null) { + synchronized (this) { + if (elementData == null) { + elementData = new Comparable[1]; + } + } + } + synchronized (elementData) { + // for (int i = 0; i < size; i++) { + // Object element = elementData[i]; + // if (memberName.equals(element)) { + // return (E) element; + // } + // } + // add(memberName); + + final int index = Arrays.binarySearch(elementData, 0, size, memberName); + if (index >= 0) { + return (E) elementData[index]; + } + // Not found, add and sort. + add(memberName); + Arrays.parallelSort((E[]) elementData, 0, size); + } + return memberName; + } + + /* + * The following methods are taken from java.util.ArrayList to grow our inlined array. + */ + + /** + * Appends the specified element to the end of this list. + * + * @param e element to be appended to this list + * @return {@code true} (as specified by {@link Collection#add}) + */ + private void add(Object e) { + ensureCapacityInternal(size + 1); // Increments modCount!! + elementData[size++] = (Comparable<?>) e; + } + + /** + * Default initial capacity. + */ + private static final int DEFAULT_CAPACITY = 10; + + private void ensureCapacityInternal(int minCapacity) { + if (elementData == null) { + minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); + } + ensureExplicitCapacity(minCapacity); + } + + private void ensureExplicitCapacity(int minCapacity) { + // overflow-conscious code + if (minCapacity - elementData.length > 0) + grow(minCapacity); + } + + /** + * Increases the capacity to ensure that it can hold at least the + * number of elements specified by the minimum capacity argument. + * + * @param minCapacity the desired minimum capacity + */ + private void grow(int minCapacity) { + // overflow-conscious code + int oldCapacity = elementData.length; + int newCapacity = oldCapacity + (oldCapacity >> 1); + if (newCapacity - minCapacity < 0) + newCapacity = minCapacity; + // minCapacity is usually close to size, so this is a win: + elementData = Arrays.copyOf(elementData, newCapacity); + } + } + + private volatile transient ClassData<T> classData; + + /** + * Lazily create {@link ClassData}. + */ + private ClassData<T> classData() { + if (this.classData != null) { + return this.classData; + } + synchronized (this) { + if (this.classData == null) { + this.classData = new ClassData<T>(); + } + } + return this.classData; + } + + public <E extends Comparable<? super E>> E internMemberName(E memberName) { + return classData().intern(memberName); + } + + /** * Reflection support. */ // Caches for certain reflective results private static boolean useCaches = true;
src/java.base/share/classes/java/lang/Class.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File