8032779: Update code in java.lang to use newer language features

Reviewed-by: darcy, alanb
This commit is contained in:
Paul Sandoz 2014-01-27 14:29:37 +01:00
parent 2489c7b220
commit 51efd87812
14 changed files with 56 additions and 72 deletions

View File

@ -1488,10 +1488,9 @@ public final class Class<T> implements java.io.Serializable,
List<Class<?>> list = new ArrayList<>(); List<Class<?>> list = new ArrayList<>();
Class<?> currentClass = Class.this; Class<?> currentClass = Class.this;
while (currentClass != null) { while (currentClass != null) {
Class<?>[] members = currentClass.getDeclaredClasses(); for (Class<?> m : currentClass.getDeclaredClasses()) {
for (int i = 0; i < members.length; i++) { if (Modifier.isPublic(m.getModifiers())) {
if (Modifier.isPublic(members[i].getModifiers())) { list.add(m);
list.add(members[i]);
} }
} }
currentClass = currentClass.getSuperclass(); currentClass = currentClass.getSuperclass();
@ -2626,8 +2625,8 @@ public final class Class<T> implements java.io.Serializable,
} }
private static void addAll(Collection<Field> c, Field[] o) { private static void addAll(Collection<Field> c, Field[] o) {
for (int i = 0; i < o.length; i++) { for (Field f : o) {
c.add(o[i]); c.add(f);
} }
} }
@ -2713,8 +2712,8 @@ public final class Class<T> implements java.io.Serializable,
} }
void addAll(Method[] ma) { void addAll(Method[] ma) {
for (int i = 0; i < ma.length; i++) { for (Method m : ma) {
add(ma[i]); add(m);
} }
} }
@ -2819,9 +2818,8 @@ public final class Class<T> implements java.io.Serializable,
// out concrete implementations inherited from superclasses at // out concrete implementations inherited from superclasses at
// the end. // the end.
MethodArray inheritedMethods = new MethodArray(); MethodArray inheritedMethods = new MethodArray();
Class<?>[] interfaces = getInterfaces(); for (Class<?> i : getInterfaces()) {
for (int i = 0; i < interfaces.length; i++) { inheritedMethods.addAllNonStatic(i.privateGetPublicMethods());
inheritedMethods.addAllNonStatic(interfaces[i].privateGetPublicMethods());
} }
if (!isInterface()) { if (!isInterface()) {
Class<?> c = getSuperclass(); Class<?> c = getSuperclass();
@ -2864,9 +2862,9 @@ public final class Class<T> implements java.io.Serializable,
private static Field searchFields(Field[] fields, String name) { private static Field searchFields(Field[] fields, String name) {
String internedName = name.intern(); String internedName = name.intern();
for (int i = 0; i < fields.length; i++) { for (Field field : fields) {
if (fields[i].getName() == internedName) { if (field.getName() == internedName) {
return getReflectionFactory().copyField(fields[i]); return getReflectionFactory().copyField(field);
} }
} }
return null; return null;
@ -2887,8 +2885,7 @@ public final class Class<T> implements java.io.Serializable,
} }
// Direct superinterfaces, recursively // Direct superinterfaces, recursively
Class<?>[] interfaces = getInterfaces(); Class<?>[] interfaces = getInterfaces();
for (int i = 0; i < interfaces.length; i++) { for (Class<?> c : interfaces) {
Class<?> c = interfaces[i];
if ((res = c.getField0(name)) != null) { if ((res = c.getField0(name)) != null) {
return res; return res;
} }
@ -2911,8 +2908,7 @@ public final class Class<T> implements java.io.Serializable,
{ {
Method res = null; Method res = null;
String internedName = name.intern(); String internedName = name.intern();
for (int i = 0; i < methods.length; i++) { for (Method m : methods) {
Method m = methods[i];
if (m.getName() == internedName if (m.getName() == internedName
&& arrayContentsEq(parameterTypes, m.getParameterTypes()) && arrayContentsEq(parameterTypes, m.getParameterTypes())
&& (res == null && (res == null

View File

@ -199,8 +199,7 @@ public abstract class ClassLoader {
// the set of parallel capable loader types // the set of parallel capable loader types
private static final Set<Class<? extends ClassLoader>> loaderTypes = private static final Set<Class<? extends ClassLoader>> loaderTypes =
Collections.newSetFromMap( Collections.newSetFromMap(new WeakHashMap<>());
new WeakHashMap<Class<? extends ClassLoader>, Boolean>());
static { static {
synchronized (loaderTypes) { loaderTypes.add(ClassLoader.class); } synchronized (loaderTypes) { loaderTypes.add(ClassLoader.class); }
} }
@ -285,8 +284,7 @@ public abstract class ClassLoader {
if (ParallelLoaders.isRegistered(this.getClass())) { if (ParallelLoaders.isRegistered(this.getClass())) {
parallelLockMap = new ConcurrentHashMap<>(); parallelLockMap = new ConcurrentHashMap<>();
package2certs = new ConcurrentHashMap<>(); package2certs = new ConcurrentHashMap<>();
domains = domains = Collections.synchronizedSet(new HashSet<>());
Collections.synchronizedSet(new HashSet<ProtectionDomain>());
assertionLock = new Object(); assertionLock = new Object();
} else { } else {
// no finer-grained lock; lock on the classloader instance // no finer-grained lock; lock on the classloader instance
@ -916,10 +914,10 @@ public abstract class ClassLoader {
// go through and make sure all the certs in one array // go through and make sure all the certs in one array
// are in the other and vice-versa. // are in the other and vice-versa.
boolean match; boolean match;
for (int i = 0; i < certs.length; i++) { for (Certificate cert : certs) {
match = false; match = false;
for (int j = 0; j < pcerts.length; j++) { for (Certificate pcert : pcerts) {
if (certs[i].equals(pcerts[j])) { if (cert.equals(pcert)) {
match = true; match = true;
break; break;
} }
@ -928,10 +926,10 @@ public abstract class ClassLoader {
} }
// now do the same for pcerts // now do the same for pcerts
for (int i = 0; i < pcerts.length; i++) { for (Certificate pcert : pcerts) {
match = false; match = false;
for (int j = 0; j < certs.length; j++) { for (Certificate cert : certs) {
if (pcerts[i].equals(certs[j])) { if (pcert.equals(cert)) {
match = true; match = true;
break; break;
} }
@ -1648,10 +1646,10 @@ public abstract class ClassLoader {
pkgs = Package.getSystemPackages(); pkgs = Package.getSystemPackages();
} }
if (pkgs != null) { if (pkgs != null) {
for (int i = 0; i < pkgs.length; i++) { for (Package pkg : pkgs) {
String pkgName = pkgs[i].getName(); String pkgName = pkg.getName();
if (map.get(pkgName) == null) { if (map.get(pkgName) == null) {
map.put(pkgName, pkgs[i]); map.put(pkgName, pkg);
} }
} }
} }
@ -1830,8 +1828,8 @@ public abstract class ClassLoader {
throw new UnsatisfiedLinkError("Can't load " + libfilename); throw new UnsatisfiedLinkError("Can't load " + libfilename);
} }
} }
for (int i = 0 ; i < sys_paths.length ; i++) { for (String sys_path : sys_paths) {
File libfile = new File(sys_paths[i], System.mapLibraryName(name)); File libfile = new File(sys_path, System.mapLibraryName(name));
if (loadLibrary0(fromClass, libfile)) { if (loadLibrary0(fromClass, libfile)) {
return; return;
} }
@ -1841,9 +1839,8 @@ public abstract class ClassLoader {
} }
} }
if (loader != null) { if (loader != null) {
for (int i = 0 ; i < usr_paths.length ; i++) { for (String usr_path : usr_paths) {
File libfile = new File(usr_paths[i], File libfile = new File(usr_path, System.mapLibraryName(name));
System.mapLibraryName(name));
if (loadLibrary0(fromClass, libfile)) { if (loadLibrary0(fromClass, libfile)) {
return; return;
} }

View File

@ -91,15 +91,14 @@ final class ConditionalSpecialCasing {
static Hashtable<Integer, HashSet<Entry>> entryTable = new Hashtable<>(); static Hashtable<Integer, HashSet<Entry>> entryTable = new Hashtable<>();
static { static {
// create hashtable from the entry // create hashtable from the entry
for (int i = 0; i < entry.length; i ++) { for (Entry cur : entry) {
Entry cur = entry[i]; Integer cp = cur.getCodePoint();
Integer cp = new Integer(cur.getCodePoint());
HashSet<Entry> set = entryTable.get(cp); HashSet<Entry> set = entryTable.get(cp);
if (set == null) { if (set == null) {
set = new HashSet<Entry>(); set = new HashSet<>();
entryTable.put(cp, set);
} }
set.add(cur); set.add(cur);
entryTable.put(cp, set);
} }
} }

View File

@ -557,8 +557,8 @@ public class Package implements java.lang.reflect.AnnotatedElement {
// First, update the system package map with new package names // First, update the system package map with new package names
String[] names = getSystemPackages0(); String[] names = getSystemPackages0();
synchronized (pkgs) { synchronized (pkgs) {
for (int i = 0; i < names.length; i++) { for (String name : names) {
defineSystemPackage(names[i], getSystemPackage0(names[i])); defineSystemPackage(name, getSystemPackage0(name));
} }
return pkgs.values().toArray(new Package[pkgs.size()]); return pkgs.values().toArray(new Package[pkgs.size()]);
} }

View File

@ -1476,10 +1476,10 @@ class SecurityManager {
/* /*
* Traverse the list of packages, check for any matches. * Traverse the list of packages, check for any matches.
*/ */
for (int i = 0; i < pkgs.length; i++) { for (String restrictedPkg : pkgs) {
if (pkg.startsWith(pkgs[i]) || pkgs[i].equals(pkg + ".")) { if (pkg.startsWith(restrictedPkg) || restrictedPkg.equals(pkg + ".")) {
checkPermission( checkPermission(
new RuntimePermission("accessClassInPackage."+pkg)); new RuntimePermission("accessClassInPackage." + pkg));
break; // No need to continue; only need to check this once break; // No need to continue; only need to check this once
} }
} }
@ -1544,10 +1544,10 @@ class SecurityManager {
/* /*
* Traverse the list of packages, check for any matches. * Traverse the list of packages, check for any matches.
*/ */
for (int i = 0; i < pkgs.length; i++) { for (String restrictedPkg : pkgs) {
if (pkg.startsWith(pkgs[i]) || pkgs[i].equals(pkg + ".")) { if (pkg.startsWith(restrictedPkg) || restrictedPkg.equals(pkg + ".")) {
checkPermission( checkPermission(
new RuntimePermission("defineClassInPackage."+pkg)); new RuntimePermission("defineClassInPackage." + pkg));
break; // No need to continue; only need to check this once break; // No need to continue; only need to check this once
} }
} }

View File

@ -67,7 +67,7 @@ class StringCoding {
} }
private static <T> void set(ThreadLocal<SoftReference<T>> tl, T ob) { private static <T> void set(ThreadLocal<SoftReference<T>> tl, T ob) {
tl.set(new SoftReference<T>(ob)); tl.set(new SoftReference<>(ob));
} }
// Trim the given byte array to the given length // Trim the given byte array to the given length

View File

@ -382,8 +382,7 @@ public class ThreadLocal<T> {
setThreshold(len); setThreshold(len);
table = new Entry[len]; table = new Entry[len];
for (int j = 0; j < len; j++) { for (Entry e : parentTable) {
Entry e = parentTable[j];
if (e != null) { if (e != null) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
ThreadLocal<Object> key = (ThreadLocal<Object>) e.get(); ThreadLocal<Object> key = (ThreadLocal<Object>) e.get();
@ -685,8 +684,7 @@ public class ThreadLocal<T> {
Entry[] newTab = new Entry[newLen]; Entry[] newTab = new Entry[newLen];
int count = 0; int count = 0;
for (int j = 0; j < oldLen; ++j) { for (Entry e : oldTab) {
Entry e = oldTab[j];
if (e != null) { if (e != null) {
ThreadLocal<?> k = e.get(); ThreadLocal<?> k = e.get();
if (k == null) { if (k == null) {

View File

@ -646,8 +646,7 @@ public class Throwable implements Serializable {
private void printStackTrace(PrintStreamOrWriter s) { private void printStackTrace(PrintStreamOrWriter s) {
// Guard against malicious overrides of Throwable.equals by // Guard against malicious overrides of Throwable.equals by
// using a Set with identity equality semantics. // using a Set with identity equality semantics.
Set<Throwable> dejaVu = Set<Throwable> dejaVu = Collections.newSetFromMap(new IdentityHashMap<>());
Collections.newSetFromMap(new IdentityHashMap<Throwable, Boolean>());
dejaVu.add(this); dejaVu.add(this);
synchronized (s.lock()) { synchronized (s.lock()) {

View File

@ -303,7 +303,7 @@ public class MethodHandleProxies {
private static private static
Method[] getSingleNameMethods(Class<?> intfc) { Method[] getSingleNameMethods(Class<?> intfc) {
ArrayList<Method> methods = new ArrayList<Method>(); ArrayList<Method> methods = new ArrayList<>();
String uniqueName = null; String uniqueName = null;
for (Method m : intfc.getMethods()) { for (Method m : intfc.getMethods()) {
if (isObjectMethod(m)) continue; if (isObjectMethod(m)) continue;

View File

@ -274,8 +274,8 @@ public class MutableCallSite extends CallSite {
public static void syncAll(MutableCallSite[] sites) { public static void syncAll(MutableCallSite[] sites) {
if (sites.length == 0) return; if (sites.length == 0) return;
STORE_BARRIER.lazySet(0); STORE_BARRIER.lazySet(0);
for (int i = 0; i < sites.length; i++) { for (MutableCallSite site : sites) {
sites[i].getClass(); // trigger NPE on first null site.getClass(); // trigger NPE on first null
} }
// FIXME: NYI // FIXME: NYI
} }

View File

@ -237,7 +237,7 @@ public class MemoryUsage {
* Returns a descriptive representation of this memory usage. * Returns a descriptive representation of this memory usage.
*/ */
public String toString() { public String toString() {
StringBuffer buf = new StringBuffer(); StringBuilder buf = new StringBuilder();
buf.append("init = " + init + "(" + (init >> 10) + "K) "); buf.append("init = " + init + "(" + (init >> 10) + "K) ");
buf.append("used = " + used + "(" + (used >> 10) + "K) "); buf.append("used = " + used + "(" + (used >> 10) + "K) ");
buf.append("committed = " + committed + "(" + buf.append("committed = " + committed + "(" +

View File

@ -93,8 +93,8 @@ public class AccessibleObject implements AnnotatedElement {
throws SecurityException { throws SecurityException {
SecurityManager sm = System.getSecurityManager(); SecurityManager sm = System.getSecurityManager();
if (sm != null) sm.checkPermission(ACCESS_PERMISSION); if (sm != null) sm.checkPermission(ACCESS_PERMISSION);
for (int i = 0; i < array.length; i++) { for (AccessibleObject ao : array) {
setAccessible0(array[i], flag); setAccessible0(ao, flag);
} }
} }

View File

@ -337,11 +337,9 @@ public final class Parameter implements AnnotatedElement {
private synchronized Map<Class<? extends Annotation>, Annotation> declaredAnnotations() { private synchronized Map<Class<? extends Annotation>, Annotation> declaredAnnotations() {
if(null == declaredAnnotations) { if(null == declaredAnnotations) {
declaredAnnotations = declaredAnnotations = new HashMap<>();
new HashMap<Class<? extends Annotation>, Annotation>(); for (Annotation a : getDeclaredAnnotations())
Annotation[] ann = getDeclaredAnnotations(); declaredAnnotations.put(a.annotationType(), a);
for(int i = 0; i < ann.length; i++)
declaredAnnotations.put(ann[i].annotationType(), ann[i]);
} }
return declaredAnnotations; return declaredAnnotations;
} }

View File

@ -465,7 +465,7 @@ public class Proxy implements java.io.Serializable {
Key2(Class<?> intf1, Class<?> intf2) { Key2(Class<?> intf1, Class<?> intf2) {
super(intf1); super(intf1);
hash = 31 * intf1.hashCode() + intf2.hashCode(); hash = 31 * intf1.hashCode() + intf2.hashCode();
ref2 = new WeakReference<Class<?>>(intf2); ref2 = new WeakReference<>(intf2);
} }
@Override @Override
@ -725,7 +725,6 @@ public class Proxy implements java.io.Serializable {
} }
final Constructor<?> cons = cl.getConstructor(constructorParams); final Constructor<?> cons = cl.getConstructor(constructorParams);
final InvocationHandler ih = h;
if (!Modifier.isPublic(cl.getModifiers())) { if (!Modifier.isPublic(cl.getModifiers())) {
AccessController.doPrivileged(new PrivilegedAction<Void>() { AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() { public Void run() {
@ -735,7 +734,7 @@ public class Proxy implements java.io.Serializable {
}); });
} }
return cons.newInstance(new Object[]{h}); return cons.newInstance(new Object[]{h});
} catch (IllegalAccessException|InstantiationException e) { } catch (IllegalAccessException | InstantiationException | NoSuchMethodException e) {
throw new InternalError(e.toString(), e); throw new InternalError(e.toString(), e);
} catch (InvocationTargetException e) { } catch (InvocationTargetException e) {
Throwable t = e.getCause(); Throwable t = e.getCause();
@ -744,8 +743,6 @@ public class Proxy implements java.io.Serializable {
} else { } else {
throw new InternalError(t.toString(), t); throw new InternalError(t.toString(), t);
} }
} catch (NoSuchMethodException e) {
throw new InternalError(e.toString(), e);
} }
} }