Path: blob/master/test/jdk/java/lang/instrument/HiddenClass/HiddenClassAgent.java
41153 views
/*1* Copyright (c) 2020, 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*/2223/**24* @test25* @library /test/lib26* @modules java.instrument27* jdk.compiler28* @build jdk.test.lib.compiler.CompilerUtils29* jdk.test.lib.Utils *30* @run shell ../MakeJAR3.sh HiddenClassAgent 'Can-Retransform-Classes: true'31* @run main/othervm/native -javaagent:HiddenClassAgent.jar HiddenClassApp32*/3334import java.lang.instrument.*;3536/* Test that Instrumentation getAllLoadedClasses includes37* hidden classes but getInitiatedClasses does not.38* Check that all hidden classes are non-retransformable.39*/40public class HiddenClassAgent extends Thread {41private static volatile boolean completed = false;42private static volatile boolean failed = false;43private static volatile boolean hiddenClassLoaded = false;4445private static Instrumentation instr = null;46private static Object monitor = new Object();4748static void log(String str) { System.err.println(str); }49public static boolean failed() { return failed; }5051public static52boolean checkWaitForCompleteness() {53try {54synchronized (monitor) {55while (!completed) {56monitor.wait(100);57}58}59} catch (InterruptedException ex) {60ex.printStackTrace();61log("HiddenClassAgent: waitCheckForCompletness: Caught InterruptedException: " + ex);62}63return completed;64}6566public static67void setHiddenClassLoaded() {68synchronized (monitor) {69hiddenClassLoaded = true;70monitor.notifyAll();71}72}7374private static75void waitForHiddenClassLoad() {76try {77synchronized (monitor) {78while (!hiddenClassLoaded) {79monitor.wait(100);80}81}82} catch (InterruptedException ex) {83ex.printStackTrace();84log("HiddenClassAgent: waitForHiddenClassLoad: Caught InterruptedException: " + ex);85}86}8788private static89ClassLoader testGetAllLoadedClasses() {90boolean hiddenClassFound = false;91ClassLoader loader = null;92Class<?>[] classes = instr.getAllLoadedClasses();9394for (int i = 0; i < classes.length; i++) {95Class klass = classes[i];9697if (!klass.isHidden() || !klass.getName().contains("HiddenClass/")) {98continue;99}100log("HiddenClassAgent: getAllLoadedClasses returned hidden class: " + klass);101hiddenClassFound = true;102loader = klass.getClassLoader();103log("HiddenClassAgent: class loader of hidden class: " + loader);104105try {106instr.retransformClasses(klass);107log("HiddenClassAgent: FAIL: hidden class is retransformable: " + klass);108failed = true;109} catch (UnmodifiableClassException e) {110log("HiddenClassAgent: Got expected UnmodifiableClassException for class: " + klass);111} catch (Throwable e) {112log("HiddenClassAgent: FAIL: unexpected throwable in hidden class retransform: " + klass);113log("HiddenClassAgent: got Throwable" + e);114failed = true;115}116}117if (!hiddenClassFound) {118log("HiddenClassAgent: FAIL: a hidden class is not found in getAllLoadedClasses list");119failed = true;120}121return loader;122}123124private static125void testGetInitiatedClasses(ClassLoader loader) {126Class<?>[] classes = instr.getInitiatedClasses(loader);127for (int i = 0; i < classes.length; i++) {128Class klass = classes[i];129130if (klass.isHidden()) {131log("HiddenClassAgent: FAIL: getInitiatedClasses returned hidden class: " + klass);132failed = true;133return;134}135}136log("HiddenClassAgent: getInitiatedClasses returned no hidden classes as expected");137}138139public static void140premain(String agentArgs, Instrumentation instrumentation) {141instr = instrumentation;142Thread agentThread = new HiddenClassAgent();143agentThread.start();144}145146public void run () {147log("HiddenClassAgent: started");148waitForHiddenClassLoad();149150// Test getAllLoadedClasses151ClassLoader loader = testGetAllLoadedClasses();152153// Test getInitiatedClasses154testGetInitiatedClasses(null);155if (loader != null) {156testGetInitiatedClasses(loader);157}158159synchronized (monitor) {160completed = true;161monitor.notifyAll();162}163log("HiddenClassAgent: finished");164}165}166167168