hotspot/test/runtime/Throwable/TestThrowable.java
author amurillo
Fri, 06 May 2016 06:23:29 -0700
changeset 38208 ff63d43b0480
parent 38152 80e5da81fb2c
child 39397 ae63e0e7af71
permissions -rw-r--r--
Merge
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
38152
80e5da81fb2c 8154258: [TESTBUG] Various serviceability tests fail compilation
dsamersoff
parents: 37064
diff changeset
    28
 * @modules java.base/jdk.internal.misc
37064
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    29
 * @library /testlibrary
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    30
 * @run main/othervm -XX:MaxJavaStackTraceDepth=1024 TestThrowable
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    31
 */
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
import java.lang.reflect.Field;
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    34
import jdk.test.lib.Asserts;
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    35
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    36
public class TestThrowable {
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    37
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    38
  // Inner class that throws a lot of exceptions
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    39
  static class Thrower {
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    40
    static int MaxJavaStackTraceDepth = 1024; // as above
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    41
    int[] depths = {10, 34, 100, 1024, 2042};
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    42
    int count = 0;
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    43
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    44
    int getDepth(Throwable t) throws Exception {
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    45
      Field f = Throwable.class.getDeclaredField("depth");
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    46
      f.setAccessible(true); // it's private
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    47
      return f.getInt(t);
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    48
    }
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
    void callThrow(int depth) {
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    51
      if (++count < depth) {
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    52
        callThrow(depth);
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    53
      } else {
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    54
        throw new RuntimeException("depth tested " + depth);
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    55
      }
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
    void testThrow() throws Exception {
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    58
      for (int d : depths) {
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    59
        try {
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    60
          count = getDepth(new Throwable());
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    61
          callThrow(d);
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    62
        } catch(Exception e) {
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    63
          e.getStackTrace();
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    64
          System.out.println(e.getMessage());
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    65
          int throwableDepth = getDepth(e);
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    66
          Asserts.assertTrue(throwableDepth == d ||
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    67
                     (d > MaxJavaStackTraceDepth && throwableDepth == MaxJavaStackTraceDepth),
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    68
                     "depth should return the correct value: depth tested=" +
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    69
                     d + " throwableDepth=" + throwableDepth);
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    70
        }
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
  public static void main(String... unused) throws Exception {
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    76
    Thrower t = new Thrower();
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    77
    t.testThrow();
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    78
  }
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    79
}