jdk/test/java/lang/instrument/RedefineMethodInBacktraceApp.java
author stefank
Tue, 22 Jan 2013 13:53:53 +0100
changeset 16021 471eee27e770
child 21310 4595a9405051
permissions -rw-r--r--
8006506: Add test for redefining methods in backtraces to java/lang/instrument tests Reviewed-by: sspitsyn, coleenp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16021
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
     1
/*
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
     4
 *
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
     7
 * published by the Free Software Foundation.
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
     8
 *
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    13
 * accompanied this code).
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    14
 *
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    18
 *
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    21
 * questions.
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    22
 */
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    23
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    24
import java.io.File;
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    25
import java.io.FileInputStream;
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    26
import java.io.InputStream;
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    27
import java.lang.instrument.ClassDefinition;
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    28
import java.lang.reflect.InvocationTargetException;
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    29
import java.lang.reflect.Method;
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    30
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    31
/**
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    32
 * When an exception is thrown, the JVM collects just enough information
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    33
 * about the stack trace to be able to create a full fledged stack trace
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    34
 * (StackTraceElement[]). The backtrace contains this information and the
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    35
 * JVM  must make sure that the data in the backtrace is still usable after
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    36
 * a class redefinition.
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    37
 *
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    38
 * After the PermGen removal there was a bug when the last reference to a Method
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    39
 * was in the backtrace. The class of the method was kept alive, because of the
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    40
 * mirror stored in the backtrace, but the old versions of the redefined method
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    41
 * could be freed, since class redefinition didn't know about the backtraces.
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    42
 */
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    43
public class RedefineMethodInBacktraceApp {
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    44
    public static void main(String args[]) throws Exception {
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    45
        System.out.println("Hello from RedefineMethodInBacktraceApp!");
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    46
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    47
        new RedefineMethodInBacktraceApp().doTest();
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    48
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    49
        System.exit(0);
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    50
    }
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    51
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    52
    private void doTest() throws Exception {
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    53
        doMethodInBacktraceTest();
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    54
    }
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    55
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    56
    private void doMethodInBacktraceTest() throws Exception {
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    57
        Throwable t = getThrowableFromMethodToRedefine();
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    58
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    59
        doRedefine(RedefineMethodInBacktraceTarget.class);
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    60
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    61
        doClassUnloading();
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    62
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    63
        touchRedefinedMethodInBacktrace(t);
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    64
    }
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    65
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    66
    private static Throwable getThrowableFromMethodToRedefine() throws Exception {
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    67
        Class<RedefineMethodInBacktraceTarget> c =
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    68
                RedefineMethodInBacktraceTarget.class;
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    69
        Method method = c.getMethod("methodToRedefine");
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    70
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    71
        Throwable thrownFromMethodToRedefine = null;
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    72
        try {
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    73
            method.invoke(null);
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    74
        } catch (InvocationTargetException e) {
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    75
            thrownFromMethodToRedefine = e.getCause();
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    76
            if (!(thrownFromMethodToRedefine instanceof RuntimeException)) {
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    77
                throw e;
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    78
            }
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    79
        }
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    80
        method = null;
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    81
        c = null;
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    82
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    83
        return thrownFromMethodToRedefine;
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    84
    }
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    85
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    86
    private static void doClassUnloading() {
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    87
        // This will clean out old, unused redefined methods.
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    88
        System.gc();
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    89
    }
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    90
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    91
    private static void touchRedefinedMethodInBacktrace(Throwable throwable) {
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    92
        // Make sure that we can convert the backtrace, which is referring to
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    93
        // the redefined method, to a  StrackTraceElement[] without crashing.
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    94
        throwable.getStackTrace();
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    95
    }
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    96
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    97
    private static void doRedefine(Class<?> clazz) throws Exception {
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    98
        // Load the second version of this class.
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
    99
        File f = new File(clazz.getName() + ".class");
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
   100
        System.out.println("Reading test class from " + f.getAbsolutePath());
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
   101
        InputStream redefineStream = new FileInputStream(f);
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
   102
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
   103
        byte[] redefineBuffer = NamedBuffer.loadBufferFromStream(redefineStream);
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
   104
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
   105
        ClassDefinition redefineParamBlock = new ClassDefinition(
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
   106
                clazz, redefineBuffer);
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
   107
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
   108
        RedefineMethodInBacktraceAgent.getInstrumentation().redefineClasses(
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
   109
                new ClassDefinition[] {redefineParamBlock});
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
   110
    }
471eee27e770 8006506: Add test for redefining methods in backtraces to java/lang/instrument tests
stefank
parents:
diff changeset
   111
}