Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/instrument/ParallelTransformerLoaderAgent.java
38813 views
/*1* Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.lang.instrument.*;24import java.net.*;25import java.io.*;26import java.security.*;2728/**29* Test Java Agent30*31* @author Daryl Puryear32* @copyright 1999-2004 Wily Technology, Inc. All rights reserved.33*/34public class ParallelTransformerLoaderAgent35{36private static URL sURL;37private static ClassLoader sClassLoader;3839public static synchronized ClassLoader40getClassLoader()41{42return sClassLoader;43}4445public static synchronized void46generateNewClassLoader()47{48sClassLoader = new URLClassLoader(new URL[] {sURL});49}5051public static void52premain( String agentArgs,53Instrumentation instrumentation)54throws Exception55{56if (agentArgs == null || agentArgs == "")57{58System.err.println("Error: No jar file name provided, test will not run.");59return;60}6162sURL = (new File(agentArgs)).toURL();63System.out.println("Using jar file: " + sURL);64generateNewClassLoader();6566instrumentation.addTransformer(new TestTransformer());67}6869private static class TestTransformer70implements ClassFileTransformer71{72public byte[]73transform( ClassLoader loader,74String className,75Class classBeingRedefined,76ProtectionDomain protectionDomain,77byte[] classfileBuffer)78throws IllegalClassFormatException79{80String tName = Thread.currentThread().getName();81// In 160_03 and older, transform() is called82// with the "system_loader_lock" held and that83// prevents the bootstrap class loaded from84// running in parallel. If we add a slight sleep85// delay here when the transform() call is not86// main or TestThread, then the deadlock in87// 160_03 and older is much more reproducible.88if (!tName.equals("main") && !tName.equals("TestThread")) {89System.out.println("Thread '" + tName +90"' has called transform()");91try {92Thread.sleep(500);93} catch (InterruptedException ie) {94}95}9697// load additional classes when called from other threads98if (!tName.equals("main"))99{100loadClasses(3);101}102return null;103}104105public static void106loadClasses( int index)107{108ClassLoader loader = ParallelTransformerLoaderAgent.getClassLoader();109try110{111Class.forName("TestClass" + index, true, loader);112}113catch (Exception e)114{115e.printStackTrace();116}117}118}119}120121122