hotspot/test/runtime/Throwable/TestThrowable.java
author alanb
Thu, 01 Dec 2016 08:56:41 +0000
changeset 42307 cefc81dc1d52
parent 40631 ed82623d7831
permissions -rw-r--r--
8169069: Module system implementation refresh (11/2016) Reviewed-by: lfoltan, acorn, ctornqvi, mchung Contributed-by: lois.foltan@oracle.com, harold.seigel@oracle.com, alan.bateman@oracle.com, mandy.chung@oracle.com, serguei.spitsyn@oracle.com, george.triantafillou@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37064
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
     1
/*
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
     4
 *
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
     7
 * published by the Free Software Foundation.
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
     8
 *
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    13
 * accompanied this code).
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    14
 *
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    18
 *
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    21
 * questions.
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    22
 */
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    23
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    24
/*
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    25
 * @test
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    26
 * @bug 8150778
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    27
 * @summary Test exception depths, and code to get stack traces
42307
cefc81dc1d52 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40631
diff changeset
    28
 * @modules java.base/jdk.internal.misc:open
cefc81dc1d52 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40631
diff changeset
    29
 * @modules java.base/java.lang:open
40631
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 39397
diff changeset
    30
 * @library /test/lib
37064
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    31
 * @run main/othervm -XX:MaxJavaStackTraceDepth=1024 TestThrowable
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    32
 */
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    33
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    34
import java.lang.reflect.Field;
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    35
import jdk.test.lib.Asserts;
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    36
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    37
public class TestThrowable {
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    38
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    39
  // Inner class that throws a lot of exceptions
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    40
  static class Thrower {
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    41
    static int MaxJavaStackTraceDepth = 1024; // as above
39397
ae63e0e7af71 8157592: StackTraceLogging fails with stack overflow on 32-bit Windows
gtriantafill
parents: 38152
diff changeset
    42
    int[] depths = {10, 34, 100, 1023, 1024, 1025};
37064
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    43
    int count = 0;
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    44
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    45
    int getDepth(Throwable t) throws Exception {
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    46
      Field f = Throwable.class.getDeclaredField("depth");
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    47
      f.setAccessible(true); // it's private
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    48
      return f.getInt(t);
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    49
    }
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    50
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    51
    void callThrow(int depth) {
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    52
      if (++count < depth) {
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    53
        callThrow(depth);
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    54
      } else {
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    55
        throw new RuntimeException("depth tested " + depth);
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    56
      }
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    57
    }
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    58
    void testThrow() throws Exception {
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    59
      for (int d : depths) {
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    60
        try {
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    61
          count = getDepth(new Throwable());
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    62
          callThrow(d);
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    63
        } catch(Exception e) {
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    64
          e.getStackTrace();
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    65
          System.out.println(e.getMessage());
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    66
          int throwableDepth = getDepth(e);
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    67
          Asserts.assertTrue(throwableDepth == d ||
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    68
                     (d > MaxJavaStackTraceDepth && throwableDepth == MaxJavaStackTraceDepth),
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    69
                     "depth should return the correct value: depth tested=" +
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    70
                     d + " throwableDepth=" + throwableDepth);
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    71
        }
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    72
      }
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    73
    }
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    74
  }
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    75
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    76
  public static void main(String... unused) throws Exception {
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    77
    Thrower t = new Thrower();
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    78
    t.testThrow();
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    79
  }
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    80
}