changeset 10033:a69d209ab181

Merge
author naoto
date Thu, 18 Sep 2014 08:22:02 -0700
parents 59dc07537a78 b2bd6792a38b
children 4df174a954be
files
diffstat 7 files changed, 120 insertions(+), 160 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/io/ByteArrayOutputStream.java	Wed Sep 17 15:06:37 2014 -0700
+++ b/src/share/classes/java/io/ByteArrayOutputStream.java	Thu Sep 18 08:22:02 2014 -0700
@@ -94,6 +94,14 @@
     }
 
     /**
+     * The maximum size of array to allocate.
+     * Some VMs reserve some header words in an array.
+     * Attempts to allocate larger arrays may result in
+     * OutOfMemoryError: Requested array size exceeds VM limit
+     */
+    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
+
+    /**
      * Increases the capacity to ensure that it can hold at least the
      * number of elements specified by the minimum capacity argument.
      *
@@ -105,14 +113,19 @@
         int newCapacity = oldCapacity << 1;
         if (newCapacity - minCapacity < 0)
             newCapacity = minCapacity;
-        if (newCapacity < 0) {
-            if (minCapacity < 0) // overflow
-                throw new OutOfMemoryError();
-            newCapacity = Integer.MAX_VALUE;
-        }
+        if (newCapacity - MAX_ARRAY_SIZE > 0)
+            newCapacity = hugeCapacity(minCapacity);
         buf = Arrays.copyOf(buf, newCapacity);
     }
 
+    private static int hugeCapacity(int minCapacity) {
+        if (minCapacity < 0) // overflow
+            throw new OutOfMemoryError();
+        return (minCapacity > MAX_ARRAY_SIZE) ?
+            Integer.MAX_VALUE :
+            MAX_ARRAY_SIZE;
+    }
+
     /**
      * Writes the specified byte to this byte array output stream.
      *
--- a/src/share/classes/java/lang/reflect/Constructor.java	Wed Sep 17 15:06:37 2014 -0700
+++ b/src/share/classes/java/lang/reflect/Constructor.java	Thu Sep 18 08:22:02 2014 -0700
@@ -557,33 +557,15 @@
      */
     @Override
     public AnnotatedType getAnnotatedReceiverType() {
-        Class<?> thisDeclClass = getDeclaringClass();
-        Class<?> enclosingClass = thisDeclClass.getEnclosingClass();
+        if (getDeclaringClass().getEnclosingClass() == null)
+            return super.getAnnotatedReceiverType();
 
-        if (enclosingClass == null) {
-            // A Constructor for a top-level class
-            return null;
-        }
-
-        Class<?> outerDeclaringClass = thisDeclClass.getDeclaringClass();
-        if (outerDeclaringClass == null) {
-            // A constructor for a local or anonymous class
-            return null;
-        }
-
-        // Either static nested or inner class
-        if (Modifier.isStatic(thisDeclClass.getModifiers())) {
-            // static nested
-            return null;
-        }
-
-        // A Constructor for an inner class
         return TypeAnnotationParser.buildAnnotatedType(getTypeAnnotationBytes0(),
                 sun.misc.SharedSecrets.getJavaLangAccess().
-                    getConstantPool(thisDeclClass),
+                        getConstantPool(getDeclaringClass()),
                 this,
-                thisDeclClass,
-                enclosingClass,
+                getDeclaringClass(),
+                getDeclaringClass().getEnclosingClass(),
                 TypeAnnotation.TypeAnnotationTarget.METHOD_RECEIVER);
     }
 }
--- a/src/share/classes/java/lang/reflect/Executable.java	Wed Sep 17 15:06:37 2014 -0700
+++ b/src/share/classes/java/lang/reflect/Executable.java	Thu Sep 18 08:22:02 2014 -0700
@@ -602,24 +602,21 @@
     /**
      * Returns an {@code AnnotatedType} object that represents the use of a
      * type to specify the receiver type of the method/constructor represented
-     * by this {@code Executable} object.
+     * by this Executable object. The receiver type of a method/constructor is
+     * available only if the method/constructor has a <em>receiver
+     * parameter</em> (JLS 8.4.1).
      *
-     * The receiver type of a method/constructor is available only if the
-     * method/constructor has a receiver parameter (JLS 8.4.1). If this {@code
-     * Executable} object <em>represents an instance method or represents a
-     * constructor of an inner member class</em>, and the
-     * method/constructor <em>either</em> has no receiver parameter or has a
-     * receiver parameter with no annotations on its type, then the return
-     * value is an {@code AnnotatedType} object representing an element with no
+     * If this {@code Executable} object represents a constructor or instance
+     * method that does not have a receiver parameter, or has a receiver
+     * parameter with no annotations on its type, then the return value is an
+     * {@code AnnotatedType} object representing an element with no
      * annotations.
      *
-     * If this {@code Executable} object represents a static method or
-     * represents a constructor of a top level, static member, local, or
-     * anoymous class, then the return value is null.
+     * If this {@code Executable} object represents a static method, then the
+     * return value is null.
      *
      * @return an object representing the receiver type of the method or
-     * constructor represented by this {@code Executable} or {@code null} if
-     * this {@code Executable} can not have a receiver parameter
+     * constructor represented by this {@code Executable}
      *
      * @since 1.8
      */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/io/ByteArrayOutputStream/MaxCapacity.java	Thu Sep 18 08:22:02 2014 -0700
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2014 Google Inc. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @ignore This test has huge memory requirements
+ * @run main/timeout=1800/othervm -Xmx8g MaxCapacity
+ * @bug 8055949
+ * @summary Check that we can write (almost) Integer.MAX_VALUE bytes
+ *          to a ByteArrayOutputStream.
+ * @author Martin Buchholz
+ */
+import java.io.ByteArrayOutputStream;
+
+public class MaxCapacity {
+    public static void main(String[] args) {
+        long maxHeap = Runtime.getRuntime().maxMemory();
+        if (maxHeap < 3L * Integer.MAX_VALUE) {
+            System.out.printf("Skipping test; max memory %sM too small%n",
+                              maxHeap/(1024*1024));
+            return;
+        }
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        for (long n = 0; ; n++) {
+            try {
+                baos.write((byte)'x');
+            } catch (Throwable t) {
+                // check data integrity while we're here
+                byte[] bytes = baos.toByteArray();
+                if (bytes.length != n)
+                    throw new AssertionError("wrong length");
+                if (bytes[0] != 'x' ||
+                    bytes[bytes.length - 1] != 'x')
+                    throw new AssertionError("wrong contents");
+
+                long gap = Integer.MAX_VALUE - n;
+                System.out.printf("gap=%dM %d%n", gap/(1024*1024), gap);
+                if (gap > 1024)
+                    throw t;
+                // t.printStackTrace();
+                break;
+            }
+        }
+    }
+}
--- a/test/java/lang/annotation/typeAnnotations/ConstructorReceiverTest.java	Wed Sep 17 15:06:37 2014 -0700
+++ b/test/java/lang/annotation/typeAnnotations/ConstructorReceiverTest.java	Thu Sep 18 08:22:02 2014 -0700
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 8023651 8044629
+ * @bug 8023651
  * @summary Test that the receiver annotations and the return annotations of
  *          constructors behave correctly.
  * @run testng ConstructorReceiverTest
@@ -38,16 +38,11 @@
 import static org.testng.Assert.*;
 
 public class ConstructorReceiverTest {
-    public static final Integer EMPTY_ANNOTATED_TYPE =  Integer.valueOf(-1);
-
     // Format is {
     //   { Class to get ctor for,
     //       ctor param class,
     //       value of anno of return type,
-    //       value of anno for receiver,
-    //              or null if there should be no receiver,
-    //              or EMPTY_ANNOTATED_TYPE of there should be a receiver but
-    //              no annotation
+    //       value of anno for receiver or null if there should be no receiver anno
     //    },
     //    ...
     // }
@@ -56,15 +51,13 @@
         { ConstructorReceiverTest.Middle.class, ConstructorReceiverTest.class, Integer.valueOf(10), Integer.valueOf(15) },
         { ConstructorReceiverTest.Middle.Inner.class, ConstructorReceiverTest.Middle.class, Integer.valueOf(100), Integer.valueOf(150) },
         { ConstructorReceiverTest.Middle.Inner.Innermost.class, ConstructorReceiverTest.Middle.Inner.class, Integer.valueOf(1000), Integer.valueOf(1500) },
-        { ConstructorReceiverTest.Middle.InnerNoReceiver.class, ConstructorReceiverTest.Middle.class, Integer.valueOf(300), EMPTY_ANNOTATED_TYPE },
+        { ConstructorReceiverTest.Middle.InnerNoReceiver.class, ConstructorReceiverTest.Middle.class, Integer.valueOf(300), null },
         { ConstructorReceiverTest.Nested.class, null, Integer.valueOf(20), null },
         { ConstructorReceiverTest.Nested.NestedMiddle.class, ConstructorReceiverTest.Nested.class, Integer.valueOf(200), Integer.valueOf(250)},
         { ConstructorReceiverTest.Nested.NestedMiddle.NestedInner.class, ConstructorReceiverTest.Nested.NestedMiddle.class, Integer.valueOf(2000), Integer.valueOf(2500)},
-        { ConstructorReceiverTest.Nested.NestedMiddle.NestedInnerNoReceiver.class, ConstructorReceiverTest.Nested.NestedMiddle.class, Integer.valueOf(4000), EMPTY_ANNOTATED_TYPE},
-        { ConstructorReceiverTest.Nested.NestedMiddle.SecondNestedInnerNoReceiver.class, ConstructorReceiverTest.Nested.NestedMiddle.class, Integer.valueOf(5000), EMPTY_ANNOTATED_TYPE},
+        { ConstructorReceiverTest.Nested.NestedMiddle.NestedInnerNoReceiver.class, ConstructorReceiverTest.Nested.NestedMiddle.class, Integer.valueOf(4000), null},
     };
 
-
     @DataProvider
     public Object[][] data() { return TESTS; }
 
@@ -78,27 +71,14 @@
             c = toTest.getDeclaredConstructor(ctorParamType);
 
         AnnotatedType annotatedReceiverType = c.getAnnotatedReceiverType();
+        Annotation[] receiverAnnotations = annotatedReceiverType.getAnnotations();
 
-        // Some Constructors doesn't conceptually have a receiver, they should return null
         if (receiverVal == null) {
-            assertNull(annotatedReceiverType, "getAnnotatedReciverType  should return null for Constructor: " + c);
+            assertEquals(receiverAnnotations.length, 0, Arrays.asList(receiverAnnotations).toString() +
+                    " should be empty. Looking at 'length': ");
             return;
         }
 
-        // check that getType() matches the receiver
-        assertEquals(annotatedReceiverType.getType(),
-                ctorParamType,
-                "getType() doesn't match receiver type: " + ctorParamType);
-
-        Annotation[] receiverAnnotations = annotatedReceiverType.getAnnotations();
-
-        // Some Constructors have no annotations on but in theory can have a receiver
-        if (receiverVal.equals(EMPTY_ANNOTATED_TYPE)) {
-            assertEquals(receiverAnnotations.length, 0, "expecting an empty annotated type for: " + c);
-            return;
-        }
-
-        // The rest should have annotations
         assertEquals(receiverAnnotations.length, 1, "expecting a 1 element array. Looking at 'length': ");
         assertEquals(((Annot)receiverAnnotations[0]).value(), receiverVal.intValue(), " wrong annotation found. Found " +
                 receiverAnnotations[0] +
@@ -156,10 +136,6 @@
             class NestedInnerNoReceiver {
                 @Annot(4000) public NestedInnerNoReceiver() {}
             }
-
-            class SecondNestedInnerNoReceiver {
-                @Annot(5000) public SecondNestedInnerNoReceiver(NestedMiddle NestedMiddle.this) {}
-            }
         }
     }
 
--- a/test/java/lang/annotation/typeAnnotations/GetAnnotatedReceiverType.java	Wed Sep 17 15:06:37 2014 -0700
+++ b/test/java/lang/annotation/typeAnnotations/GetAnnotatedReceiverType.java	Thu Sep 18 08:22:02 2014 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, 2014 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -23,11 +23,10 @@
 
 /*
  * @test
- * @bug 8024915 8044629
+ * @bug 8024915
  */
 
 import java.lang.reflect.AnnotatedType;
-import java.lang.reflect.Executable;
 import java.util.Arrays;
 
 public class GetAnnotatedReceiverType {
@@ -43,115 +42,41 @@
         public Inner1(GetAnnotatedReceiverType GetAnnotatedReceiverType.this) {}
     }
 
-    public static class Nested {
-        public Nested() {}
-
-        public class NestedInner {
-            public NestedInner() { }
-
-            public Class<?> getLocalClass () {
-                class NestedInnerLocal { public NestedInnerLocal() {} }
-                return NestedInnerLocal.class;
-            }
-
-            public Class<?> getAnonymousClass() {
-                return new Object() {}.getClass();
-            }
-        }
-    }
-
-    public class Inner2 {
-        public Inner2() { }
-
-        public class Inner3 {
-            public Inner3() { }
-
-            public Class<?> getLocalClass () {
-                class InnerLocal { public InnerLocal() {} }
-                return InnerLocal.class;
-            }
-
-            public Class<?> getAnonymousClass() {
-                return new Object() {}.getClass();
-            }
-        }
-
-        public Class<?> getLocalClass () {
-            class InnerLocal { public InnerLocal() {} }
-                return InnerLocal.class;
-        }
-
-        public Class<?> getAnonymousClass() {
-            return new Object() {}.getClass();
-        }
-    }
-
     private static int failures = 0;
     private static int tests = 0;
 
     public static void main(String[] args) throws NoSuchMethodException {
-        checkEmptyAT(GetAnnotatedReceiverType.class.getMethod("method"),
+        checkEmptyAT(GetAnnotatedReceiverType.class.getMethod("method").getAnnotatedReceiverType(),
                 "getAnnotatedReceiverType for \"method\" should return an empty AnnotatedType");
-        checkEmptyAT(Inner0.class.getConstructor(GetAnnotatedReceiverType.class),
+        checkEmptyAT(Inner0.class.getConstructor(GetAnnotatedReceiverType.class).getAnnotatedReceiverType(),
                 "getAnnotatedReceiverType for a ctor without a \"this\" should return an empty AnnotatedType");
 
-        checkEmptyAT(GetAnnotatedReceiverType.class.getMethod("method0"),
+        checkEmptyAT(GetAnnotatedReceiverType.class.getMethod("method0").getAnnotatedReceiverType(),
                 "getAnnotatedReceiverType for \"method0\" should return an empty AnnotatedType");
-        checkEmptyAT(Inner1.class.getConstructor(GetAnnotatedReceiverType.class),
+        checkEmptyAT(Inner1.class.getConstructor(GetAnnotatedReceiverType.class).getAnnotatedReceiverType(),
                 "getAnnotatedReceiverType for a ctor with a \"this\" should return an empty AnnotatedType");
 
-        checkNull(GetAnnotatedReceiverType.class.getMethod("method4"),
+        checkNull(GetAnnotatedReceiverType.class.getMethod("method4").getAnnotatedReceiverType(),
                 "getAnnotatedReceiverType() on a static method should return null");
 
-        // More nested, inner, local and anonymous classes
-        Nested nested = new Nested();
-        Nested.NestedInner instance = nested.new NestedInner();
-        checkNull(nested.getClass().getConstructors()[0],
-                "getAnnotatedReceiverType() on a constructor for a static class should return null");
-        checkEmptyAT(instance.getClass().getConstructors()[0],
-                "getAnnotatedReceiverType for a ctor without a \"this\" should return an empty AnnotatedType");
-        checkNull(instance.getLocalClass().getConstructors()[0],
-                "getAnnotatedReceiverType() on a constructor for a local class should return null");
-        checkNull(instance.getAnonymousClass().getDeclaredConstructors()[0],
-                "getAnnotatedReceiverType() on a constructor for an anonymous class should return null");
-
-        GetAnnotatedReceiverType outer = new GetAnnotatedReceiverType();
-        Inner2 instance2 = outer.new Inner2();
-        checkEmptyAT(instance2.getClass().getConstructors()[0],
-                "getAnnotatedReceiverType for a ctor without a \"this\" should return an empty AnnotatedType");
-        checkNull(instance2.getLocalClass().getConstructors()[0],
-                "getAnnotatedReceiverType() on a constructor for a local class should return null");
-        checkNull(instance2.getAnonymousClass().getDeclaredConstructors()[0],
-                "getAnnotatedReceiverType() on a constructor for an anonymous class should return null");
-
-        Inner2.Inner3 instance3 = instance2.new Inner3();
-        checkEmptyAT(instance3.getClass().getConstructors()[0],
-                "getAnnotatedReceiverType for a ctor without a \"this\" should return an empty AnnotatedType");
-        checkNull(instance3.getLocalClass().getConstructors()[0],
-                "getAnnotatedReceiverType() on a constructor for a local class should return null");
-        checkNull(instance3.getAnonymousClass().getDeclaredConstructors()[0],
-                "getAnnotatedReceiverType() on a constructor for an anonymous class should return null");
-
         if (failures != 0)
             throw new RuntimeException("Test failed, see log for details");
-        else if (tests != 15)
+        else if (tests != 5)
             throw new RuntimeException("Not all cases ran, failing");
     }
 
-    private static void checkNull(Executable e, String msg) {
-        AnnotatedType a = e.getAnnotatedReceiverType();
-        if (a != null) {
+    private static void checkNull(Object o, String msg) {
+        if (o != null) {
             failures++;
-            System.err.println(msg + ": " + e);
+            System.err.println(msg);
         }
         tests++;
     }
 
-    private static void checkEmptyAT(Executable e, String msg) {
-        AnnotatedType a = e.getAnnotatedReceiverType();
+    private static void checkEmptyAT(AnnotatedType a, String msg) {
         if (a.getAnnotations().length != 0) {
             failures++;
-            System.err.print(msg + ": " + e);
+            System.err.print(msg);
         }
         tests++;
     }
--- a/test/java/lang/annotation/typeAnnotations/TestExecutableGetAnnotatedType.java	Wed Sep 17 15:06:37 2014 -0700
+++ b/test/java/lang/annotation/typeAnnotations/TestExecutableGetAnnotatedType.java	Thu Sep 18 08:22:02 2014 -0700
@@ -73,11 +73,13 @@
         testParameters(e.getParameters());
     }
 
+    // should test constructors as well, see JDK-8044629
     @Test(dataProvider = "genericMethodData")
     public void testGenericReceiverType(Executable e) throws Exception {
         testReceiverType0(e);
     }
 
+    // should test constructors as well, see JDK-8044629
     @Test(dataProvider = "methodData")
     public void testReceiverType(Executable e) throws Exception {
         testReceiverType0(e);