1 /*
   2  * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2018, SAP SE. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 import java.lang.reflect.Constructor;
  26 import java.lang.reflect.Method;
  27 
  28 import org.testng.Assert;
  29 import org.testng.annotations.Test;
  30 
  31 import jdk.test.lib.dcmd.CommandExecutor;
  32 import jdk.test.lib.dcmd.JMXExecutor;
  33 import jdk.test.lib.process.OutputAnalyzer;
  34 
  35 /*
  36  * @test
  37  * @summary Test that various diagnostic commands which can show core reflection
  38  *          invocation targets do so correctly (See: JDK-8203343).
  39  * @library /test/lib
  40  * @run testng ShowReflectionTargetTest
  41  * @author stuefe
  42  */
  43 
  44 public class ShowReflectionTargetTest {
  45 
  46     @SuppressWarnings("unused")
  47     private static class Dummy {
  48         int _i;
  49         public Dummy(int i) { _i = i; }
  50         public int get_i() { return _i; }
  51     }
  52 
  53     public void run(CommandExecutor executor) throws Exception {
  54         // Do some reflection, enough times to hit the sun.reflect.inflationThreshold and force
  55         // generation of reflection accessor classes.
  56         Class<?> c = Class.forName("ShowReflectionTargetTest$Dummy");
  57         Constructor<?> ctor = c.getConstructor(int.class);
  58         Method m = c.getMethod("get_i");
  59         for (int i = 0; i < 100; i ++) {
  60             Object o = ctor.newInstance(i);
  61             int j = ((Integer)m.invoke(o)).intValue();
  62             Assert.assertEquals(j, i);
  63         }
  64 
  65         // Now invoke VM.class_hierarchy and check its output.
  66         // Should show reflection targets, e.g.:
  67         // ....
  68         //        |--jdk.internal.reflect.MagicAccessorImpl/null
  69         //        |  |--jdk.internal.reflect.FieldAccessorImpl/null
  70         //        |  |  |--jdk.internal.reflect.UnsafeFieldAccessorImpl/null
  71         //        |  |  |  |--jdk.internal.reflect.UnsafeStaticFieldAccessorImpl/null
  72         //        |  |  |  |  |--jdk.internal.reflect.UnsafeQualifiedStaticFieldAccessorImpl/null
  73         //        |  |  |  |  |  |--jdk.internal.reflect.UnsafeQualifiedStaticObjectFieldAccessorImpl/null
  74         //        |  |--jdk.internal.reflect.ConstructorAccessorImpl/null
  75         //        |  |  |--jdk.internal.reflect.DelegatingConstructorAccessorImpl/null
  76         //        |  |  |--jdk.internal.reflect.NativeConstructorAccessorImpl/null
  77         // >       |  |  |--jdk.internal.reflect.GeneratedConstructorAccessor1/0x00007f75f04889b0 (invokes: java/lang/management/ManagementPermission::<init> (Ljava/lang/String;)V)
  78         // >       |  |  |--jdk.internal.reflect.GeneratedConstructorAccessor2/0x00007f75f0494990 (invokes: ShowReflectionTargetTest$Dummy::<init> (I)V)
  79         //        |  |  |--jdk.internal.reflect.BootstrapConstructorAccessorImpl/null
  80         //        |  |--jdk.internal.reflect.MethodAccessorImpl/null
  81         // >       |  |  |--jdk.internal.reflect.GeneratedMethodAccessor1/0x00007f75f0494450 (invokes: ShowReflectionTargetTest$Dummy::get_i ()I)
  82         //        |  |  |--jdk.internal.reflect.DelegatingMethodAccessorImpl/null
  83         // ...
  84 
  85         OutputAnalyzer output = executor.execute("VM.class_hierarchy");
  86 
  87         output.shouldMatch(".*jdk.internal.reflect.GeneratedConstructorAccessor.*invokes.*ShowReflectionTargetTest\\$Dummy::<init>.*");
  88         output.shouldMatch(".*jdk.internal.reflect.GeneratedMethodAccessor.*invokes.*ShowReflectionTargetTest\\$Dummy::get_i.*");
  89 
  90     }
  91 
  92     @Test
  93     public void jmx() throws Exception {
  94         run(new JMXExecutor());
  95     }
  96 
  97 }
  98 
  99 
 100