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/othervm -Dsun.reflect.noInflation=true 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; since we set -Dsun.reflect.noInflation=true, this should
  55         // immediately generate Generated{Method|Constructor}Accessor objects.
  56         Class<?> c = Class.forName("ShowReflectionTargetTest$Dummy");
  57         Constructor<?> ctor = c.getConstructor(int.class);
  58         Method m = c.getMethod("get_i");
  59 
  60         Object o = ctor.newInstance(17);
  61         int j = ((Integer)m.invoke(o)).intValue();
  62         Assert.assertEquals(j, 17);
  63 
  64         // Now invoke VM.class_hierarchy and check its output.
  65         // Should show reflection targets, e.g.:
  66         // ....
  67         //        |--jdk.internal.reflect.MagicAccessorImpl/null
  68         //        |  |--jdk.internal.reflect.FieldAccessorImpl/null
  69         //        |  |  |--jdk.internal.reflect.UnsafeFieldAccessorImpl/null
  70         //        |  |  |  |--jdk.internal.reflect.UnsafeStaticFieldAccessorImpl/null
  71         //        |  |  |  |  |--jdk.internal.reflect.UnsafeQualifiedStaticFieldAccessorImpl/null
  72         //        |  |  |  |  |  |--jdk.internal.reflect.UnsafeQualifiedStaticObjectFieldAccessorImpl/null
  73         //        |  |--jdk.internal.reflect.ConstructorAccessorImpl/null
  74         //        |  |  |--jdk.internal.reflect.DelegatingConstructorAccessorImpl/null
  75         //        |  |  |--jdk.internal.reflect.NativeConstructorAccessorImpl/null
  76         // >       |  |  |--jdk.internal.reflect.GeneratedConstructorAccessor1/0x00007f75f04889b0 (invokes: java/lang/management/ManagementPermission::<init> (Ljava/lang/String;)V)
  77         // >       |  |  |--jdk.internal.reflect.GeneratedConstructorAccessor2/0x00007f75f0494990 (invokes: ShowReflectionTargetTest$Dummy::<init> (I)V)
  78         //        |  |  |--jdk.internal.reflect.BootstrapConstructorAccessorImpl/null
  79         //        |  |--jdk.internal.reflect.MethodAccessorImpl/null
  80         // >       |  |  |--jdk.internal.reflect.GeneratedMethodAccessor1/0x00007f75f0494450 (invokes: ShowReflectionTargetTest$Dummy::get_i ()I)
  81         //        |  |  |--jdk.internal.reflect.DelegatingMethodAccessorImpl/null
  82         // ...
  83 
  84         OutputAnalyzer output = executor.execute("VM.class_hierarchy");
  85 
  86         output.shouldMatch(".*jdk.internal.reflect.GeneratedConstructorAccessor.*invokes.*ShowReflectionTargetTest\\$Dummy::<init>.*");
  87         output.shouldMatch(".*jdk.internal.reflect.GeneratedMethodAccessor.*invokes.*ShowReflectionTargetTest\\$Dummy::get_i.*");
  88 
  89     }
  90 
  91     @Test
  92     public void jmx() throws Exception {
  93         run(new JMXExecutor());
  94     }
  95 
  96 }
  97 
  98 
  99