Path: blob/master/test/hotspot/jtreg/serviceability/dcmd/vm/ClassHierarchyTest.java
41153 views
/*1* Copyright (c) 2015, 2017, 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* @summary Test of diagnostic command VM.class_hierarchy26* @library /test/lib27* @modules java.base/jdk.internal.misc28* java.compiler29* java.management30* jdk.internal.jvmstat/sun.jvmstat.monitor31* @run testng ClassHierarchyTest32*/3334import org.testng.annotations.Test;35import org.testng.Assert;3637import jdk.test.lib.process.OutputAnalyzer;38import jdk.test.lib.dcmd.CommandExecutor;39import jdk.test.lib.dcmd.JMXExecutor;4041import java.io.File;42import java.io.FileInputStream;43import java.io.IOException;44import java.nio.ByteBuffer;45import java.nio.channels.FileChannel;46import java.util.Iterator;47import java.util.regex.Matcher;48import java.util.regex.Pattern;4950public class ClassHierarchyTest {5152// $> jcmd DcmdTestClass VM.class_hierarchy DcmdTestClass | grep DcmdTestClass\$\$Lambda53// |--DcmdTestClass$$Lambda$1/4081552/0xa529fbb05455// > VM.class_hierarchy DcmdBaseClass56// java.lang.Object/null57// |--DcmdBaseClass/0xa4abcd485859// > VM.class_hierarchy DcmdBaseClass -s60// java.lang.Object/null61// |--DcmdBaseClass/0xa4abcd4862// | |--DcmdTestClass/0xa4abcd486364// > VM.class_hierarchy DcmdBaseClass -i -s65// java.lang.Object/null66// |--DcmdBaseClass/0xa4abcd4867// | implements Intf2/0xa4abcd48 (declared intf)68// | implements Intf1/0xa4abcd48 (inherited intf)69// | |--DcmdTestClass/0xa4abcd4870// | | implements Intf1/0xa4abcd48 (inherited intf)71// | | implements Intf2/0xa4abcd48 (inherited intf)7273static Pattern expected_lambda_line =74Pattern.compile("\\|--DcmdTestClass\\$\\$Lambda.*");7576static Pattern expected_lines[] = {77Pattern.compile("java.lang.Object/null"),78Pattern.compile("\\|--DcmdBaseClass/0x(\\p{XDigit}*)"),79Pattern.compile("\\| implements Intf2/0x(\\p{XDigit}*) \\(declared intf\\)"),80Pattern.compile("\\| implements Intf1/0x(\\p{XDigit}*) \\(inherited intf\\)"),81Pattern.compile("\\| \\|--DcmdTestClass/0x(\\p{XDigit}*)"),82Pattern.compile("\\| \\| implements Intf1/0x(\\p{XDigit}*) \\(inherited intf\\)"),83Pattern.compile("\\| \\| implements Intf2/0x(\\p{XDigit}*) \\(inherited intf\\)")84};8586public void run(CommandExecutor executor) throws ClassNotFoundException {87OutputAnalyzer output;88Iterator<String> lines;89int i;9091// Load our test class whose hierarchy we will print.92Class<?> c = Class.forName("DcmdTestClass");9394// Verify the presence of the lamba anonymous class95output = executor.execute("VM.class_hierarchy");96lines = output.asLines().iterator();97Boolean foundMatch = false;98while (lines.hasNext()) {99String line = lines.next();100Matcher m = expected_lambda_line.matcher(line);101if (m.matches()) {102foundMatch = true;103break;104}105}106if (!foundMatch) {107Assert.fail("Failed to find lamda class");108}109110// Verify the output for the simple hierachry of just DcmdBaseClass.111output = executor.execute("VM.class_hierarchy DcmdBaseClass");112lines = output.asLines().iterator();113i = 0;114while (lines.hasNext()) {115String line = lines.next();116Matcher m = expected_lines[i].matcher(line);117i++;118if (!m.matches()) {119Assert.fail("Failed to match line #" + i + ": " + line);120}121// Should only be two lines of output in this form.122if (i == 2) break;123}124if (lines.hasNext()) {125String line = lines.next();126Assert.fail("Unexpected dcmd output: " + line);127}128129// Verify the output for the full hierarchy of DcmdBaseClass, but without interfaces.130output = executor.execute("VM.class_hierarchy DcmdBaseClass -s");131lines = output.asLines().iterator();132i = 0;133while (lines.hasNext()) {134String line = lines.next();135Matcher m = expected_lines[i].matcher(line);136i++;137if (!m.matches()) {138Assert.fail("Failed to match line #" + i + ": " + line);139}140// "implements" lines should not be in this output.141if (i == 2 || i == 4) i += 2;142}143if (lines.hasNext()) {144String line = lines.next();145Assert.fail("Unexpected dcmd output: " + line);146}147148// Verify the output for the full hierarchy of DcmdBaseClass, including interfaces.149output = executor.execute("VM.class_hierarchy DcmdBaseClass -i -s");150lines = output.asLines().iterator();151i = 0;152String classLoaderAddr = null;153while (lines.hasNext()) {154String line = lines.next();155Matcher m = expected_lines[i].matcher(line);156i++;157if (!m.matches()) {158Assert.fail("Failed to match line #" + i + ": " + line);159}160if (i == 2) {161// Fetch the ClassLoader address, which should be the same in162// subsequent lines.163classLoaderAddr = m.group(1);164System.out.println(classLoaderAddr);165} else if (i > 2) {166if (!classLoaderAddr.equals(m.group(1))) {167Assert.fail("Classloader address didn't match on line #"168+ i + ": " + line);169}170}171if (i == expected_lines.length) break;172}173if (lines.hasNext()) {174String line = lines.next();175Assert.fail("Unexpected dcmd output: " + line);176}177}178179@Test180public void jmx() throws ClassNotFoundException {181run(new JMXExecutor());182}183}184185interface Intf1 {186}187188interface Intf2 extends Intf1 {189}190191class DcmdBaseClass implements Intf2 {192}193194class DcmdTestClass extends DcmdBaseClass {195static {196// Force creation of anonymous class (for the lambdaform).197Runnable r = () -> System.out.println("Hello");198r.run();199}200}201202203