JDK 10 RFR of JDK-8062385: Remove @SuppressWarnings("cast") and casts for NIO related usages when JDK 9 becomes the bootstrap JDK

joe darcy joe.darcy at oracle.com
Thu Oct 19 21:32:39 UTC 2017


Hello,

With the bootstrap of JDK 10 updated to JDK 9, please review the small 
patch below to address

     JDK-8062385: Remove @SuppressWarnings("cast") and casts for NIO 
related usages when JDK 9 becomes the bootstrap JDK

For background, quoting the description of JDK-8062385:

> The fix for JDK-4774077 introduced covariant return types in the NIO 
> buffer hierarchy.
>
> As a consequence that fix introduced redundant casts (and because of 
> -Werror a build failure) in langtools when compiling with JDK 9, but 
> the casts are still required when bootstrapping with JDK 8.
>
> The fix for JDK-8062376 added @SuppressWarnings("cast") annotations to 
> ensure no build failure.
>
> When JDK 9 becomes the default bootstrap JDK the 
> @SuppressWarnings("cast") and casts can be removed. 

Thanks,

-Joe

diff -r 92f08900cb3c 
src/jdk.compiler/share/classes/com/sun/tools/javac/file/BaseFileManager.java
--- 
a/src/jdk.compiler/share/classes/com/sun/tools/javac/file/BaseFileManager.java 
Thu Oct 19 17:47:04 2017 +0200
+++ 
b/src/jdk.compiler/share/classes/com/sun/tools/javac/file/BaseFileManager.java 
Thu Oct 19 14:28:21 2017 -0700
@@ -304,7 +304,6 @@
          return (encodingName != null) ? encodingName : 
getDefaultEncodingName();
      }

-    @SuppressWarnings("cast")
      public CharBuffer decode(ByteBuffer inbuf, boolean 
ignoreEncodingErrors) {
          String encName = getEncodingName();
          CharsetDecoder decoder;
@@ -312,7 +311,7 @@
              decoder = getDecoder(encName, ignoreEncodingErrors);
          } catch (IllegalCharsetNameException | 
UnsupportedCharsetException e) {
              log.error(Errors.UnsupportedEncoding(encName));
-            return (CharBuffer)CharBuffer.allocate(1).flip();
+            return CharBuffer.allocate(1).flip();
          }

          // slightly overestimate the buffer size to avoid reallocation.
@@ -389,7 +388,6 @@
       * @return a byte buffer containing the contents of the stream
       * @throws IOException if an error occurred while reading the stream
       */
-    @SuppressWarnings("cast")
      public ByteBuffer makeByteBuffer(InputStream in)
          throws IOException {
          int limit = in.available();
@@ -401,14 +399,14 @@
                  // expand buffer
                  result = ByteBuffer.
                      allocate(limit <<= 1).
-                    put((ByteBuffer)result.flip());
+                    put(result.flip());
              int count = in.read(result.array(),
                  position,
                  limit - position);
              if (count < 0) break;
              result.position(position += count);
          }
-        return (ByteBuffer)result.flip();
+        return result.flip();
      }

      public void recycleByteBuffer(ByteBuffer bb) {
@@ -418,14 +416,13 @@
      /**
       * A single-element cache of direct byte buffers.
       */
-    @SuppressWarnings("cast")
      private static class ByteBufferCache {
          private ByteBuffer cached;
          ByteBuffer get(int capacity) {
              if (capacity < 20480) capacity = 20480;
              ByteBuffer result =
                  (cached != null && cached.capacity() >= capacity)
-                ? (ByteBuffer)cached.clear()
+                ? cached.clear()
                  : ByteBuffer.allocate(capacity + capacity>>1);
              cached = null;
              return result;
diff -r 92f08900cb3c 
src/jdk.compiler/share/classes/com/sun/tools/javac/file/JavacFileManager.java
--- 
a/src/jdk.compiler/share/classes/com/sun/tools/javac/file/JavacFileManager.java 
Thu Oct 19 17:47:04 2017 +0200
+++ 
b/src/jdk.compiler/share/classes/com/sun/tools/javac/file/JavacFileManager.java 
Thu Oct 19 14:28:21 2017 -0700
@@ -98,10 +98,9 @@
   */
  public class JavacFileManager extends BaseFileManager implements 
StandardJavaFileManager {

-    @SuppressWarnings("cast")
      public static char[] toArray(CharBuffer buffer) {
          if (buffer.hasArray())
-            return ((CharBuffer)buffer.compact().flip()).array();
+            return buffer.compact().flip().array();
          else
              return buffer.toString().toCharArray();
      }



More information about the compiler-dev mailing list