hotspot/test/serviceability/jvmti/TestLambdaFormRetransformation.java
author jwilhelm
Thu, 26 Feb 2015 21:00:03 +0100
changeset 29324 8672c9d7ae94
parent 28741 1f10b1bd612d
child 29678 dd2f3932c21e
permissions -rw-r--r--
8073883: serviceability/dcmd/gc/RunGCTest.java should not run with -XX:+ExplicitGCInvokesConcurrent Summary: Don't run test with -XX:+ExplicitGCInvokesConcurrent Reviewed-by: mgerdin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28741
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
     1
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
     2
/*
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
     3
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
     4
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
     5
 *
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
     6
 * This code is free software; you can redistribute it and/or modify it
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
     7
 * under the terms of the GNU General Public License version 2 only, as
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
     8
 * published by the Free Software Foundation.
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
     9
 *
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    14
 * accompanied this code).
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    15
 *
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    19
 *
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    22
 * questions.
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    23
 */
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    24
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    25
/**
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    26
 * @test
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    27
 * @bug 8008678
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    28
 * @summary JSR 292: constant pool reconstitution must support pseudo strings
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    29
 * @library /testlibrary
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    30
 * @compile -XDignore.symbol.file TestLambdaFormRetransformation.java
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    31
 * @run main TestLambdaFormRetransformation
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    32
 */
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    33
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    34
import java.io.IOException;
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    35
import java.lang.instrument.ClassFileTransformer;
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    36
import java.lang.instrument.IllegalClassFormatException;
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    37
import java.lang.instrument.Instrumentation;
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    38
import java.lang.instrument.UnmodifiableClassException;
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    39
import java.nio.file.Files;
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    40
import java.nio.file.Path;
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    41
import java.nio.file.Paths;
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    42
import java.security.ProtectionDomain;
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    43
import java.util.Arrays;
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    44
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    45
import com.oracle.java.testlibrary.ExitCode;
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    46
import com.oracle.java.testlibrary.OutputAnalyzer;
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    47
import com.oracle.java.testlibrary.ProcessTools;
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    48
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    49
public class TestLambdaFormRetransformation {
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    50
    private static String MANIFEST = String.format("Manifest-Version: 1.0\n" +
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    51
                                                   "Premain-Class: %s\n" +
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    52
                                                   "Can-Retransform-Classes: true\n",
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    53
                                                   Agent.class.getName());
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    54
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    55
    private static String CP = System.getProperty("test.classes");
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    56
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    57
    public static void main(String args[]) throws Throwable {
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    58
        Path agent = TestLambdaFormRetransformation.buildAgent();
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    59
        OutputAnalyzer oa = ProcessTools.executeTestJvm("-javaagent:" +
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    60
                                agent.toAbsolutePath().toString(), "-version");
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    61
        oa.shouldHaveExitValue(ExitCode.OK.value);
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    62
    }
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    63
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    64
    private static Path buildAgent() throws IOException {
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    65
        Path manifest = TestLambdaFormRetransformation.createManifest();
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    66
        Path jar = Files.createTempFile(Paths.get("."), null, ".jar");
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    67
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    68
        String[] args = new String[] {
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    69
            "-cfm",
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    70
            jar.toAbsolutePath().toString(),
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    71
            manifest.toAbsolutePath().toString(),
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    72
            "-C",
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    73
            TestLambdaFormRetransformation.CP,
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    74
            Agent.class.getName() + ".class"
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    75
        };
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    76
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    77
        sun.tools.jar.Main jarTool = new sun.tools.jar.Main(System.out, System.err, "jar");
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    78
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    79
        if (!jarTool.run(args)) {
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    80
            throw new Error("jar failed: args=" + Arrays.toString(args));
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    81
        }
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    82
        return jar;
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    83
    }
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    84
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    85
    private static Path createManifest() throws IOException {
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    86
        Path manifest = Files.createTempFile(Paths.get("."), null, ".mf");
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    87
        byte[] manifestBytes = TestLambdaFormRetransformation.MANIFEST.getBytes();
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    88
        Files.write(manifest, manifestBytes);
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    89
        return manifest;
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    90
    }
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    91
}
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    92
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    93
class Agent implements ClassFileTransformer {
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    94
    private static Runnable lambda = () -> {
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    95
        System.out.println("I'll crash you!");
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    96
    };
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    97
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    98
    public static void premain(String args, Instrumentation instrumentation) {
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
    99
        if (!instrumentation.isRetransformClassesSupported()) {
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   100
            System.out.println("Class retransformation is not supported.");
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   101
            return;
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   102
        }
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   103
        System.out.println("Calling lambda to ensure that lambda forms were created");
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   104
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   105
        Agent.lambda.run();
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   106
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   107
        System.out.println("Registering class file transformer");
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   108
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   109
        instrumentation.addTransformer(new Agent());
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   110
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   111
        for (Class c : instrumentation.getAllLoadedClasses()) {
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   112
            if (c.getName().contains("LambdaForm") &&
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   113
                instrumentation.isModifiableClass(c)) {
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   114
                System.out.format("We've found a modifiable lambda form: %s%n", c.getName());
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   115
                try {
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   116
                    instrumentation.retransformClasses(c);
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   117
                } catch (UnmodifiableClassException e) {
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   118
                    throw new AssertionError("Modification of modifiable class " +
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   119
                                             "caused UnmodifiableClassException", e);
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   120
                }
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   121
            }
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   122
        }
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   123
    }
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   124
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   125
    public static void main(String args[]) {
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   126
    }
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   127
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   128
    @Override
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   129
    public byte[] transform(ClassLoader loader,
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   130
                            String className,
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   131
                            Class<?> classBeingRedefined,
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   132
                            ProtectionDomain protectionDomain,
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   133
                            byte[] classfileBuffer
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   134
                           ) throws IllegalClassFormatException {
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   135
        System.out.println("Transforming " + className);
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   136
        return classfileBuffer.clone();
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   137
    }
1f10b1bd612d 8008678: JSR 292: constant pool reconstitution must support pseudo strings
sspitsyn
parents:
diff changeset
   138
}