Path: blob/master/test/hotspot/jtreg/serviceability/dcmd/vm/ShowReflectionTargetTest.java
41153 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2018 SAP SE. All rights reserved.3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4*5* This code is free software; you can redistribute it and/or modify it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 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 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324import java.lang.reflect.Constructor;25import java.lang.reflect.Method;2627import org.testng.Assert;28import org.testng.annotations.Test;2930import jdk.test.lib.dcmd.CommandExecutor;31import jdk.test.lib.dcmd.JMXExecutor;32import jdk.test.lib.process.OutputAnalyzer;3334/*35* @test36* @summary Test that various diagnostic commands which can show core reflection37* invocation targets do so correctly (See: JDK-8203343).38* @library /test/lib39* @run testng/othervm -Dsun.reflect.noInflation=true ShowReflectionTargetTest40* @author stuefe41*/4243public class ShowReflectionTargetTest {4445@SuppressWarnings("unused")46private static class Dummy {47int _i;48public Dummy(int i) { _i = i; }49public int get_i() { return _i; }50}5152public void run(CommandExecutor executor) throws Exception {53// Do some reflection; since we set -Dsun.reflect.noInflation=true, this should54// immediately generate Generated{Method|Constructor}Accessor objects.55Class<?> c = Class.forName("ShowReflectionTargetTest$Dummy");56Constructor<?> ctor = c.getConstructor(int.class);57Method m = c.getMethod("get_i");5859Object o = ctor.newInstance(17);60int j = ((Integer)m.invoke(o)).intValue();61Assert.assertEquals(j, 17);6263// Now invoke VM.class_hierarchy and check its output.64// Should show reflection targets, e.g.:65// ....66// |--jdk.internal.reflect.MagicAccessorImpl/null67// | |--jdk.internal.reflect.FieldAccessorImpl/null68// | | |--jdk.internal.reflect.UnsafeFieldAccessorImpl/null69// | | | |--jdk.internal.reflect.UnsafeStaticFieldAccessorImpl/null70// | | | | |--jdk.internal.reflect.UnsafeQualifiedStaticFieldAccessorImpl/null71// | | | | | |--jdk.internal.reflect.UnsafeQualifiedStaticObjectFieldAccessorImpl/null72// | |--jdk.internal.reflect.ConstructorAccessorImpl/null73// | | |--jdk.internal.reflect.DelegatingConstructorAccessorImpl/null74// | | |--jdk.internal.reflect.NativeConstructorAccessorImpl/null75// > | | |--jdk.internal.reflect.GeneratedConstructorAccessor1/0x00007f75f04889b0 (invokes: java/lang/management/ManagementPermission::<init> (Ljava/lang/String;)V)76// > | | |--jdk.internal.reflect.GeneratedConstructorAccessor2/0x00007f75f0494990 (invokes: ShowReflectionTargetTest$Dummy::<init> (I)V)77// | | |--jdk.internal.reflect.BootstrapConstructorAccessorImpl/null78// | |--jdk.internal.reflect.MethodAccessorImpl/null79// > | | |--jdk.internal.reflect.GeneratedMethodAccessor1/0x00007f75f0494450 (invokes: ShowReflectionTargetTest$Dummy::get_i ()I)80// | | |--jdk.internal.reflect.DelegatingMethodAccessorImpl/null81// ...8283OutputAnalyzer output = executor.execute("VM.class_hierarchy");8485output.shouldMatch(".*jdk.internal.reflect.GeneratedConstructorAccessor.*invokes.*ShowReflectionTargetTest\\$Dummy::<init>.*");86output.shouldMatch(".*jdk.internal.reflect.GeneratedMethodAccessor.*invokes.*ShowReflectionTargetTest\\$Dummy::get_i.*");8788}8990@Test91public void jmx() throws Exception {92run(new JMXExecutor());93}9495}96979899100101