< prev index next >

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

Print this page
rev 49922 : 8187123: (reflect) Class#getCanonicalName and Class#getSimpleName is a part of performance issue
Reviewed-by: psandoz

*** 1522,1538 **** * * @return the simple name of the underlying class * @since 1.5 */ public String getSimpleName() { ! if (isArray()) ! return getComponentType().getSimpleName()+"[]"; String simpleName = getSimpleBinaryName(); if (simpleName == null) { // top level class simpleName = getName(); ! return simpleName.substring(simpleName.lastIndexOf('.')+1); // strip the package name } return simpleName; } /** --- 1522,1547 ---- * * @return the simple name of the underlying class * @since 1.5 */ public String getSimpleName() { ! ReflectionData<T> rd = reflectionData(); ! String simpleName = rd.simpleName; ! if (simpleName == null) { ! rd.simpleName = simpleName = getSimpleName0(); ! } ! return simpleName; ! } + private String getSimpleName0() { + if (isArray()) { + return getComponentType().getSimpleName() + "[]"; + } String simpleName = getSimpleBinaryName(); if (simpleName == null) { // top level class simpleName = getName(); ! simpleName = simpleName.substring(simpleName.lastIndexOf('.') + 1); // strip the package name } return simpleName; } /**
*** 1544,1557 **** public String getTypeName() { if (isArray()) { try { Class<?> cl = this; int dimensions = 0; ! while (cl.isArray()) { dimensions++; cl = cl.getComponentType(); ! } StringBuilder sb = new StringBuilder(); sb.append(cl.getName()); for (int i = 0; i < dimensions; i++) { sb.append("[]"); } --- 1553,1566 ---- public String getTypeName() { if (isArray()) { try { Class<?> cl = this; int dimensions = 0; ! do { dimensions++; cl = cl.getComponentType(); ! } while (cl.isArray()); StringBuilder sb = new StringBuilder(); sb.append(cl.getName()); for (int i = 0; i < dimensions; i++) { sb.append("[]"); }
*** 1570,1595 **** * @return the canonical name of the underlying class if it exists, and * {@code null} otherwise. * @since 1.5 */ public String getCanonicalName() { if (isArray()) { String canonicalName = getComponentType().getCanonicalName(); if (canonicalName != null) return canonicalName + "[]"; else ! return null; } if (isLocalOrAnonymousClass()) ! return null; Class<?> enclosingClass = getEnclosingClass(); if (enclosingClass == null) { // top level class return getName(); } else { String enclosingName = enclosingClass.getCanonicalName(); if (enclosingName == null) ! return null; return enclosingName + "." + getSimpleName(); } } /** --- 1579,1613 ---- * @return the canonical name of the underlying class if it exists, and * {@code null} otherwise. * @since 1.5 */ public String getCanonicalName() { + ReflectionData<T> rd = reflectionData(); + String canonicalName = rd.canonicalName; + if (canonicalName == null) { + rd.canonicalName = canonicalName = getCanonicalName0(); + } + return canonicalName == ReflectionData.NULL_SENTINEL? null : canonicalName; + } + + private String getCanonicalName0() { if (isArray()) { String canonicalName = getComponentType().getCanonicalName(); if (canonicalName != null) return canonicalName + "[]"; else ! return ReflectionData.NULL_SENTINEL; } if (isLocalOrAnonymousClass()) ! return ReflectionData.NULL_SENTINEL; Class<?> enclosingClass = getEnclosingClass(); if (enclosingClass == null) { // top level class return getName(); } else { String enclosingName = enclosingClass.getCanonicalName(); if (enclosingName == null) ! return ReflectionData.NULL_SENTINEL; return enclosingName + "." + getSimpleName(); } } /**
*** 2906,2915 **** --- 2924,2938 ---- // Intermediate results for getFields and getMethods volatile Field[] declaredPublicFields; volatile Method[] declaredPublicMethods; volatile Class<?>[] interfaces; + // Cached names + volatile String simpleName; + volatile String canonicalName; + static final String NULL_SENTINEL = new String(); + // Value of classRedefinedCount when we created this ReflectionData instance final int redefinedCount; ReflectionData(int redefinedCount) { this.redefinedCount = redefinedCount;
< prev index next >