8032779: Update code in java.lang to use newer language features
authorpsandoz
Mon, 27 Jan 2014 14:29:37 +0100
changeset 22581 e868cde95050
parent 22580 6068d5e6c746
child 22582 590b12d10fea
8032779: Update code in java.lang to use newer language features Reviewed-by: darcy, alanb
jdk/src/share/classes/java/lang/Class.java
jdk/src/share/classes/java/lang/ClassLoader.java
jdk/src/share/classes/java/lang/ConditionalSpecialCasing.java
jdk/src/share/classes/java/lang/Package.java
jdk/src/share/classes/java/lang/SecurityManager.java
jdk/src/share/classes/java/lang/StringCoding.java
jdk/src/share/classes/java/lang/ThreadLocal.java
jdk/src/share/classes/java/lang/Throwable.java
jdk/src/share/classes/java/lang/invoke/MethodHandleProxies.java
jdk/src/share/classes/java/lang/invoke/MutableCallSite.java
jdk/src/share/classes/java/lang/management/MemoryUsage.java
jdk/src/share/classes/java/lang/reflect/AccessibleObject.java
jdk/src/share/classes/java/lang/reflect/Parameter.java
jdk/src/share/classes/java/lang/reflect/Proxy.java
--- a/jdk/src/share/classes/java/lang/Class.java	Mon Jan 27 17:13:16 2014 +0400
+++ b/jdk/src/share/classes/java/lang/Class.java	Mon Jan 27 14:29:37 2014 +0100
@@ -1488,10 +1488,9 @@
                     List<Class<?>> list = new ArrayList<>();
                     Class<?> currentClass = Class.this;
                     while (currentClass != null) {
-                        Class<?>[] members = currentClass.getDeclaredClasses();
-                        for (int i = 0; i < members.length; i++) {
-                            if (Modifier.isPublic(members[i].getModifiers())) {
-                                list.add(members[i]);
+                        for (Class<?> m : currentClass.getDeclaredClasses()) {
+                            if (Modifier.isPublic(m.getModifiers())) {
+                                list.add(m);
                             }
                         }
                         currentClass = currentClass.getSuperclass();
@@ -2626,8 +2625,8 @@
     }
 
     private static void addAll(Collection<Field> c, Field[] o) {
-        for (int i = 0; i < o.length; i++) {
-            c.add(o[i]);
+        for (Field f : o) {
+            c.add(f);
         }
     }
 
@@ -2713,8 +2712,8 @@
         }
 
         void addAll(Method[] ma) {
-            for (int i = 0; i < ma.length; i++) {
-                add(ma[i]);
+            for (Method m : ma) {
+                add(m);
             }
         }
 
@@ -2819,9 +2818,8 @@
         // out concrete implementations inherited from superclasses at
         // the end.
         MethodArray inheritedMethods = new MethodArray();
-        Class<?>[] interfaces = getInterfaces();
-        for (int i = 0; i < interfaces.length; i++) {
-            inheritedMethods.addAllNonStatic(interfaces[i].privateGetPublicMethods());
+        for (Class<?> i : getInterfaces()) {
+            inheritedMethods.addAllNonStatic(i.privateGetPublicMethods());
         }
         if (!isInterface()) {
             Class<?> c = getSuperclass();
@@ -2864,9 +2862,9 @@
 
     private static Field searchFields(Field[] fields, String name) {
         String internedName = name.intern();
-        for (int i = 0; i < fields.length; i++) {
-            if (fields[i].getName() == internedName) {
-                return getReflectionFactory().copyField(fields[i]);
+        for (Field field : fields) {
+            if (field.getName() == internedName) {
+                return getReflectionFactory().copyField(field);
             }
         }
         return null;
@@ -2887,8 +2885,7 @@
         }
         // Direct superinterfaces, recursively
         Class<?>[] interfaces = getInterfaces();
-        for (int i = 0; i < interfaces.length; i++) {
-            Class<?> c = interfaces[i];
+        for (Class<?> c : interfaces) {
             if ((res = c.getField0(name)) != null) {
                 return res;
             }
@@ -2911,8 +2908,7 @@
     {
         Method res = null;
         String internedName = name.intern();
-        for (int i = 0; i < methods.length; i++) {
-            Method m = methods[i];
+        for (Method m : methods) {
             if (m.getName() == internedName
                 && arrayContentsEq(parameterTypes, m.getParameterTypes())
                 && (res == null
--- a/jdk/src/share/classes/java/lang/ClassLoader.java	Mon Jan 27 17:13:16 2014 +0400
+++ b/jdk/src/share/classes/java/lang/ClassLoader.java	Mon Jan 27 14:29:37 2014 +0100
@@ -199,8 +199,7 @@
 
         // the set of parallel capable loader types
         private static final Set<Class<? extends ClassLoader>> loaderTypes =
-            Collections.newSetFromMap(
-                new WeakHashMap<Class<? extends ClassLoader>, Boolean>());
+            Collections.newSetFromMap(new WeakHashMap<>());
         static {
             synchronized (loaderTypes) { loaderTypes.add(ClassLoader.class); }
         }
@@ -285,8 +284,7 @@
         if (ParallelLoaders.isRegistered(this.getClass())) {
             parallelLockMap = new ConcurrentHashMap<>();
             package2certs = new ConcurrentHashMap<>();
-            domains =
-                Collections.synchronizedSet(new HashSet<ProtectionDomain>());
+            domains = Collections.synchronizedSet(new HashSet<>());
             assertionLock = new Object();
         } else {
             // no finer-grained lock; lock on the classloader instance
@@ -916,10 +914,10 @@
         // go through and make sure all the certs in one array
         // are in the other and vice-versa.
         boolean match;
-        for (int i = 0; i < certs.length; i++) {
+        for (Certificate cert : certs) {
             match = false;
-            for (int j = 0; j < pcerts.length; j++) {
-                if (certs[i].equals(pcerts[j])) {
+            for (Certificate pcert : pcerts) {
+                if (cert.equals(pcert)) {
                     match = true;
                     break;
                 }
@@ -928,10 +926,10 @@
         }
 
         // now do the same for pcerts
-        for (int i = 0; i < pcerts.length; i++) {
+        for (Certificate pcert : pcerts) {
             match = false;
-            for (int j = 0; j < certs.length; j++) {
-                if (pcerts[i].equals(certs[j])) {
+            for (Certificate cert : certs) {
+                if (pcert.equals(cert)) {
                     match = true;
                     break;
                 }
@@ -1648,10 +1646,10 @@
             pkgs = Package.getSystemPackages();
         }
         if (pkgs != null) {
-            for (int i = 0; i < pkgs.length; i++) {
-                String pkgName = pkgs[i].getName();
+            for (Package pkg : pkgs) {
+                String pkgName = pkg.getName();
                 if (map.get(pkgName) == null) {
-                    map.put(pkgName, pkgs[i]);
+                    map.put(pkgName, pkg);
                 }
             }
         }
@@ -1830,8 +1828,8 @@
                 throw new UnsatisfiedLinkError("Can't load " + libfilename);
             }
         }
-        for (int i = 0 ; i < sys_paths.length ; i++) {
-            File libfile = new File(sys_paths[i], System.mapLibraryName(name));
+        for (String sys_path : sys_paths) {
+            File libfile = new File(sys_path, System.mapLibraryName(name));
             if (loadLibrary0(fromClass, libfile)) {
                 return;
             }
@@ -1841,9 +1839,8 @@
             }
         }
         if (loader != null) {
-            for (int i = 0 ; i < usr_paths.length ; i++) {
-                File libfile = new File(usr_paths[i],
-                                        System.mapLibraryName(name));
+            for (String usr_path : usr_paths) {
+                File libfile = new File(usr_path, System.mapLibraryName(name));
                 if (loadLibrary0(fromClass, libfile)) {
                     return;
                 }
--- a/jdk/src/share/classes/java/lang/ConditionalSpecialCasing.java	Mon Jan 27 17:13:16 2014 +0400
+++ b/jdk/src/share/classes/java/lang/ConditionalSpecialCasing.java	Mon Jan 27 14:29:37 2014 +0100
@@ -91,15 +91,14 @@
     static Hashtable<Integer, HashSet<Entry>> entryTable = new Hashtable<>();
     static {
         // create hashtable from the entry
-        for (int i = 0; i < entry.length; i ++) {
-            Entry cur = entry[i];
-            Integer cp = new Integer(cur.getCodePoint());
+        for (Entry cur : entry) {
+            Integer cp = cur.getCodePoint();
             HashSet<Entry> set = entryTable.get(cp);
             if (set == null) {
-                set = new HashSet<Entry>();
+                set = new HashSet<>();
+                entryTable.put(cp, set);
             }
             set.add(cur);
-            entryTable.put(cp, set);
         }
     }
 
--- a/jdk/src/share/classes/java/lang/Package.java	Mon Jan 27 17:13:16 2014 +0400
+++ b/jdk/src/share/classes/java/lang/Package.java	Mon Jan 27 14:29:37 2014 +0100
@@ -557,8 +557,8 @@
         // First, update the system package map with new package names
         String[] names = getSystemPackages0();
         synchronized (pkgs) {
-            for (int i = 0; i < names.length; i++) {
-                defineSystemPackage(names[i], getSystemPackage0(names[i]));
+            for (String name : names) {
+                defineSystemPackage(name, getSystemPackage0(name));
             }
             return pkgs.values().toArray(new Package[pkgs.size()]);
         }
--- a/jdk/src/share/classes/java/lang/SecurityManager.java	Mon Jan 27 17:13:16 2014 +0400
+++ b/jdk/src/share/classes/java/lang/SecurityManager.java	Mon Jan 27 14:29:37 2014 +0100
@@ -1476,10 +1476,10 @@
         /*
          * Traverse the list of packages, check for any matches.
          */
-        for (int i = 0; i < pkgs.length; i++) {
-            if (pkg.startsWith(pkgs[i]) || pkgs[i].equals(pkg + ".")) {
+        for (String restrictedPkg : pkgs) {
+            if (pkg.startsWith(restrictedPkg) || restrictedPkg.equals(pkg + ".")) {
                 checkPermission(
-                    new RuntimePermission("accessClassInPackage."+pkg));
+                    new RuntimePermission("accessClassInPackage." + pkg));
                 break;  // No need to continue; only need to check this once
             }
         }
@@ -1544,10 +1544,10 @@
         /*
          * Traverse the list of packages, check for any matches.
          */
-        for (int i = 0; i < pkgs.length; i++) {
-            if (pkg.startsWith(pkgs[i]) || pkgs[i].equals(pkg + ".")) {
+        for (String restrictedPkg : pkgs) {
+            if (pkg.startsWith(restrictedPkg) || restrictedPkg.equals(pkg + ".")) {
                 checkPermission(
-                    new RuntimePermission("defineClassInPackage."+pkg));
+                    new RuntimePermission("defineClassInPackage." + pkg));
                 break; // No need to continue; only need to check this once
             }
         }
--- a/jdk/src/share/classes/java/lang/StringCoding.java	Mon Jan 27 17:13:16 2014 +0400
+++ b/jdk/src/share/classes/java/lang/StringCoding.java	Mon Jan 27 14:29:37 2014 +0100
@@ -67,7 +67,7 @@
     }
 
     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
--- a/jdk/src/share/classes/java/lang/ThreadLocal.java	Mon Jan 27 17:13:16 2014 +0400
+++ b/jdk/src/share/classes/java/lang/ThreadLocal.java	Mon Jan 27 14:29:37 2014 +0100
@@ -382,8 +382,7 @@
             setThreshold(len);
             table = new Entry[len];
 
-            for (int j = 0; j < len; j++) {
-                Entry e = parentTable[j];
+            for (Entry e : parentTable) {
                 if (e != null) {
                     @SuppressWarnings("unchecked")
                     ThreadLocal<Object> key = (ThreadLocal<Object>) e.get();
@@ -685,8 +684,7 @@
             Entry[] newTab = new Entry[newLen];
             int count = 0;
 
-            for (int j = 0; j < oldLen; ++j) {
-                Entry e = oldTab[j];
+            for (Entry e : oldTab) {
                 if (e != null) {
                     ThreadLocal<?> k = e.get();
                     if (k == null) {
--- a/jdk/src/share/classes/java/lang/Throwable.java	Mon Jan 27 17:13:16 2014 +0400
+++ b/jdk/src/share/classes/java/lang/Throwable.java	Mon Jan 27 14:29:37 2014 +0100
@@ -646,8 +646,7 @@
     private void printStackTrace(PrintStreamOrWriter s) {
         // Guard against malicious overrides of Throwable.equals by
         // using a Set with identity equality semantics.
-        Set<Throwable> dejaVu =
-            Collections.newSetFromMap(new IdentityHashMap<Throwable, Boolean>());
+        Set<Throwable> dejaVu = Collections.newSetFromMap(new IdentityHashMap<>());
         dejaVu.add(this);
 
         synchronized (s.lock()) {
--- a/jdk/src/share/classes/java/lang/invoke/MethodHandleProxies.java	Mon Jan 27 17:13:16 2014 +0400
+++ b/jdk/src/share/classes/java/lang/invoke/MethodHandleProxies.java	Mon Jan 27 14:29:37 2014 +0100
@@ -303,7 +303,7 @@
 
     private static
     Method[] getSingleNameMethods(Class<?> intfc) {
-        ArrayList<Method> methods = new ArrayList<Method>();
+        ArrayList<Method> methods = new ArrayList<>();
         String uniqueName = null;
         for (Method m : intfc.getMethods()) {
             if (isObjectMethod(m))  continue;
--- a/jdk/src/share/classes/java/lang/invoke/MutableCallSite.java	Mon Jan 27 17:13:16 2014 +0400
+++ b/jdk/src/share/classes/java/lang/invoke/MutableCallSite.java	Mon Jan 27 14:29:37 2014 +0100
@@ -274,8 +274,8 @@
     public static void syncAll(MutableCallSite[] sites) {
         if (sites.length == 0)  return;
         STORE_BARRIER.lazySet(0);
-        for (int i = 0; i < sites.length; i++) {
-            sites[i].getClass();  // trigger NPE on first null
+        for (MutableCallSite site : sites) {
+            site.getClass();  // trigger NPE on first null
         }
         // FIXME: NYI
     }
--- a/jdk/src/share/classes/java/lang/management/MemoryUsage.java	Mon Jan 27 17:13:16 2014 +0400
+++ b/jdk/src/share/classes/java/lang/management/MemoryUsage.java	Mon Jan 27 14:29:37 2014 +0100
@@ -237,7 +237,7 @@
      * Returns a descriptive representation of this memory usage.
      */
     public String toString() {
-        StringBuffer buf = new StringBuffer();
+        StringBuilder buf = new StringBuilder();
         buf.append("init = " + init + "(" + (init >> 10) + "K) ");
         buf.append("used = " + used + "(" + (used >> 10) + "K) ");
         buf.append("committed = " + committed + "(" +
--- a/jdk/src/share/classes/java/lang/reflect/AccessibleObject.java	Mon Jan 27 17:13:16 2014 +0400
+++ b/jdk/src/share/classes/java/lang/reflect/AccessibleObject.java	Mon Jan 27 14:29:37 2014 +0100
@@ -93,8 +93,8 @@
         throws SecurityException {
         SecurityManager sm = System.getSecurityManager();
         if (sm != null) sm.checkPermission(ACCESS_PERMISSION);
-        for (int i = 0; i < array.length; i++) {
-            setAccessible0(array[i], flag);
+        for (AccessibleObject ao : array) {
+            setAccessible0(ao, flag);
         }
     }
 
--- a/jdk/src/share/classes/java/lang/reflect/Parameter.java	Mon Jan 27 17:13:16 2014 +0400
+++ b/jdk/src/share/classes/java/lang/reflect/Parameter.java	Mon Jan 27 14:29:37 2014 +0100
@@ -337,11 +337,9 @@
 
     private synchronized Map<Class<? extends Annotation>, Annotation> declaredAnnotations() {
         if(null == declaredAnnotations) {
-            declaredAnnotations =
-                new HashMap<Class<? extends Annotation>, Annotation>();
-            Annotation[] ann = getDeclaredAnnotations();
-            for(int i = 0; i < ann.length; i++)
-                declaredAnnotations.put(ann[i].annotationType(), ann[i]);
+            declaredAnnotations = new HashMap<>();
+            for (Annotation a : getDeclaredAnnotations())
+                declaredAnnotations.put(a.annotationType(), a);
         }
         return declaredAnnotations;
    }
--- a/jdk/src/share/classes/java/lang/reflect/Proxy.java	Mon Jan 27 17:13:16 2014 +0400
+++ b/jdk/src/share/classes/java/lang/reflect/Proxy.java	Mon Jan 27 14:29:37 2014 +0100
@@ -465,7 +465,7 @@
         Key2(Class<?> intf1, Class<?> intf2) {
             super(intf1);
             hash = 31 * intf1.hashCode() + intf2.hashCode();
-            ref2 = new WeakReference<Class<?>>(intf2);
+            ref2 = new WeakReference<>(intf2);
         }
 
         @Override
@@ -725,7 +725,6 @@
             }
 
             final Constructor<?> cons = cl.getConstructor(constructorParams);
-            final InvocationHandler ih = h;
             if (!Modifier.isPublic(cl.getModifiers())) {
                 AccessController.doPrivileged(new PrivilegedAction<Void>() {
                     public Void run() {
@@ -735,7 +734,7 @@
                 });
             }
             return cons.newInstance(new Object[]{h});
-        } catch (IllegalAccessException|InstantiationException e) {
+        } catch (IllegalAccessException | InstantiationException | NoSuchMethodException e) {
             throw new InternalError(e.toString(), e);
         } catch (InvocationTargetException e) {
             Throwable t = e.getCause();
@@ -744,8 +743,6 @@
             } else {
                 throw new InternalError(t.toString(), t);
             }
-        } catch (NoSuchMethodException e) {
-            throw new InternalError(e.toString(), e);
         }
     }