Path: blob/master/test/hotspot/jtreg/serviceability/sa/TestDefaultMethods.java
41149 views
/*1* Copyright (c) 2015, 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*/2223import java.util.ArrayList;24import java.util.List;2526import sun.jvm.hotspot.HotSpotAgent;27import sun.jvm.hotspot.utilities.SystemDictionaryHelper;28import sun.jvm.hotspot.oops.InstanceKlass;29import sun.jvm.hotspot.debugger.*;30import sun.jvm.hotspot.oops.Method;31import sun.jvm.hotspot.utilities.MethodArray;3233import jdk.test.lib.apps.LingeredApp;34import jdk.test.lib.Asserts;35import jdk.test.lib.JDKToolLauncher;36import jdk.test.lib.JDKToolFinder;37import jdk.test.lib.Platform;38import jdk.test.lib.process.ProcessTools;39import jdk.test.lib.process.OutputAnalyzer;40import jdk.test.lib.SA.SATestUtils;41import jdk.test.lib.Utils;4243/**44* @test45* @library /test/lib46* @requires vm.hasSA47* @modules java.base/jdk.internal.misc48* jdk.hotspot.agent/sun.jvm.hotspot49* jdk.hotspot.agent/sun.jvm.hotspot.utilities50* jdk.hotspot.agent/sun.jvm.hotspot.oops51* jdk.hotspot.agent/sun.jvm.hotspot.debugger52* @run main TestDefaultMethods53*/5455public class TestDefaultMethods {5657private static LingeredAppWithDefaultMethods theApp = null;5859private static void printDefaultMethods(String pid,60String[] instanceKlassNames) {61HotSpotAgent agent = new HotSpotAgent();62try {63agent.attach(Integer.parseInt(pid));64}65catch (DebuggerException e) {66System.out.println(e.getMessage());67System.err.println("Unable to connect to process ID: " + pid);6869agent.detach();70e.printStackTrace();71}7273for (String instanceKlassName : instanceKlassNames) {74InstanceKlass iKlass = SystemDictionaryHelper.findInstanceKlass(instanceKlassName);75MethodArray methods = iKlass.getMethods();76MethodArray defaultMethods = iKlass.getDefaultMethods();77for (int i = 0; i < methods.length(); i++) {78Method m = methods.at(i);79System.out.println("Method: " + m.getName().asString() +80" in instance klass: " + instanceKlassName);81}82if (defaultMethods != null) {83for (int j = 0; j < defaultMethods.length(); j++) {84Method dm = defaultMethods.at(j);85System.out.println("Default method: " + dm.getName().asString() +86" in instance klass: " + instanceKlassName);87}88} else {89System.out.println("No default methods in " + instanceKlassName);90}9192}93agent.detach();94}9596private static void createAnotherToAttach(97String[] instanceKlassNames,98long lingeredAppPid) throws Exception {99// Start a new process to attach to the lingered app100ProcessBuilder processBuilder = ProcessTools.createJavaProcessBuilder(101"--add-modules=jdk.hotspot.agent",102"--add-exports=jdk.hotspot.agent/sun.jvm.hotspot=ALL-UNNAMED",103"--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.utilities=ALL-UNNAMED",104"--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.oops=ALL-UNNAMED",105"--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.debugger=ALL-UNNAMED",106"TestDefaultMethods",107Long.toString(lingeredAppPid));108SATestUtils.addPrivilegesIfNeeded(processBuilder);109OutputAnalyzer SAOutput = ProcessTools.executeProcess(processBuilder);110SAOutput.shouldHaveExitValue(0);111System.out.println(SAOutput.getOutput());112113SAOutput.shouldContain(114"Default method: hasScript in instance klass: " + instanceKlassNames[1]);115SAOutput.shouldContain(116"No default methods in " + instanceKlassNames[0]);117SAOutput.shouldContain(118"Method: hasScript in instance klass: " + instanceKlassNames[0]);119SAOutput.shouldContain(120"No default methods in " + instanceKlassNames[2]);121}122123public static void main (String... args) throws Exception {124SATestUtils.skipIfCannotAttach(); // throws SkippedException if attach not expected to work.125String[] instanceKlassNames = new String[] {126"Language",127"ParselTongue",128"SlytherinSpeak"129};130131if (args == null || args.length == 0) {132try {133theApp = new LingeredAppWithDefaultMethods();134LingeredApp.startApp(theApp, "-XX:+UsePerfData");135createAnotherToAttach(instanceKlassNames,136theApp.getPid());137} finally {138LingeredApp.stopApp(theApp);139}140} else {141printDefaultMethods(args[0], instanceKlassNames);142}143}144}145146147