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