< prev index next >

test/compiler/profiling/spectrapredefineclass_classloaders/Agent.java

Print this page
rev 11557 : 8132919: use package in compiler tests
Reviewed-by: duke


   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.security.*;
  25 import java.lang.instrument.*;
  26 import java.lang.reflect.*;
  27 import java.lang.management.ManagementFactory;
  28 import com.sun.tools.attach.VirtualMachine;
  29 import java.lang.reflect.*;




  30 import java.net.MalformedURLException;
  31 import java.net.URL;
  32 import java.net.URLClassLoader;
  33 import java.nio.file.Paths;

  34 
  35 public class Agent implements ClassFileTransformer {
  36     public static ClassLoader newClassLoader() {
  37         try {
  38             return new URLClassLoader(new URL[] {
  39                     Paths.get(System.getProperty("test.classes",".")).toUri().toURL(),
  40             }, null);
  41         } catch (MalformedURLException e){
  42             throw new RuntimeException("Unexpected URL conversion failure", e);
  43         }
  44     }
  45 
  46     static public Class Test_class;
  47 
  48     static public void main(String[] args) throws Exception {
  49 
  50         // loader2 must be first on the list so loader 1 must be used first
  51         ClassLoader loader1 = newClassLoader();
  52         Class dummy = loader1.loadClass("Test");

  53 
  54         ClassLoader loader2 = newClassLoader();
  55 
  56         Test_class = loader2.loadClass("Test");
  57         Method m3 = Test_class.getMethod("m3", ClassLoader.class);
  58         // Add speculative trap in m2() (loaded by loader1) that
  59         // references m4() (loaded by loader2).
  60         m3.invoke(Test_class.newInstance(), loader1);
  61 
  62         String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName();
  63         int p = nameOfRunningVM.indexOf('@');
  64         String pid = nameOfRunningVM.substring(0, p);
  65 
  66         // Make the nmethod go away
  67         for (int i = 0; i < 10; i++) {
  68             System.gc();
  69         }
  70 
  71         // Redefine class Test loaded by loader2
  72         for (int i = 0; i < 2; i++) {
  73             try {
  74                 VirtualMachine vm = VirtualMachine.attach(pid);
  75                 vm.loadAgent(System.getProperty("test.classes",".") + "/agent.jar", "");
  76                 vm.detach();




   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package compiler.profiling.spectrapredefineclass_classloaders;
  25 


  26 import com.sun.tools.attach.VirtualMachine;
  27 
  28 import java.lang.instrument.ClassFileTransformer;
  29 import java.lang.instrument.Instrumentation;
  30 import java.lang.management.ManagementFactory;
  31 import java.lang.reflect.Method;
  32 import java.net.MalformedURLException;
  33 import java.net.URL;
  34 import java.net.URLClassLoader;
  35 import java.nio.file.Paths;
  36 import java.security.ProtectionDomain;
  37 
  38 public class Agent implements ClassFileTransformer {
  39     public static ClassLoader newClassLoader() {
  40         try {
  41             return new URLClassLoader(new URL[] {
  42                     Paths.get(System.getProperty("test.classes",".")).toUri().toURL(),
  43             }, null);
  44         } catch (MalformedURLException e){
  45             throw new RuntimeException("Unexpected URL conversion failure", e);
  46         }
  47     }
  48 
  49     static public Class Test_class;
  50 
  51     static public void main(String[] args) throws Exception {
  52 
  53         // loader2 must be first on the list so loader 1 must be used first
  54         ClassLoader loader1 = newClassLoader();
  55         String packageName = Agent.class.getPackage().getName();
  56         Class dummy = loader1.loadClass(packageName + ".Test");
  57 
  58         ClassLoader loader2 = newClassLoader();
  59 
  60         Test_class = loader2.loadClass(packageName + ".Test");
  61         Method m3 = Test_class.getMethod("m3", ClassLoader.class);
  62         // Add speculative trap in m2() (loaded by loader1) that
  63         // references m4() (loaded by loader2).
  64         m3.invoke(Test_class.newInstance(), loader1);
  65 
  66         String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName();
  67         int p = nameOfRunningVM.indexOf('@');
  68         String pid = nameOfRunningVM.substring(0, p);
  69 
  70         // Make the nmethod go away
  71         for (int i = 0; i < 10; i++) {
  72             System.gc();
  73         }
  74 
  75         // Redefine class Test loaded by loader2
  76         for (int i = 0; i < 2; i++) {
  77             try {
  78                 VirtualMachine vm = VirtualMachine.attach(pid);
  79                 vm.loadAgent(System.getProperty("test.classes",".") + "/agent.jar", "");
  80                 vm.detach();


< prev index next >