1 /*
   2  * Copyright (c) 2014, 2015, Oracle and/or its affiliates. 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 it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 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 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test CodelistTest
  26  * @bug 8054889
  27  * @library /testlibrary
  28  * @modules java.base/sun.misc
  29  *          java.compiler
  30  *          java.management
  31  *          jdk.jvmstat/sun.jvmstat.monitor
  32  * @build jdk.test.lib.*
  33  * @build jdk.test.lib.dcmd.*
  34  * @build MethodIdentifierParser
  35  * @run testng CodelistTest
  36  * @summary Test of diagnostic command Compiler.codelist
  37  */
  38 
  39 import org.testng.annotations.Test;
  40 import org.testng.Assert;
  41 
  42 import jdk.test.lib.OutputAnalyzer;
  43 import jdk.test.lib.dcmd.CommandExecutor;
  44 import jdk.test.lib.dcmd.JMXExecutor;
  45 
  46 import java.lang.reflect.Method;
  47 
  48 public class CodelistTest {
  49 
  50     /**
  51      * This test calls Jcmd (diagnostic command tool) Compiler.codelist and then parses the output,
  52      * making sure that the first methods in the list is valid by reflection.
  53      *
  54      * Output example:
  55      *
  56      * 6 0 java.lang.System.arraycopy(Ljava/lang/Object;ILjava/lang/Object;II)V [0x00007f7b49200910, 0x00007f7b49200aa0 - 0x00007f7b49200d30]
  57      * 2 3 java.lang.String.indexOf(II)I [0x00007f7b49200d90, 0x00007f7b49200f60 - 0x00007f7b49201490]
  58      * 7 3 java.lang.Math.min(II)I [0x00007f7b4922f010, 0x00007f7b4922f180 - 0x00007f7b4922f338]
  59      * 8 3 java.lang.String.equals(Ljava/lang/Object;)Z [0x00007f7b4922fb10, 0x00007f7b4922fd40 - 0x00007f7b49230698]
  60      * 9 3 java.lang.AbstractStringBuilder.ensureCapacityInternal(I)V [0x00007f7b49232010, 0x00007f7b492321a0 - 0x00007f7b49232510]
  61      * 10 1 java.lang.Object.<init>()V [0x00007f7b49233e90, 0x00007f7b49233fe0 - 0x00007f7b49234118]
  62      *
  63      */
  64 
  65     public void run(CommandExecutor executor) {
  66         int ok   = 0;
  67         int fail = 0;
  68 
  69         // Get output from dcmd (diagnostic command)
  70         OutputAnalyzer output = executor.execute("Compiler.codelist");
  71 
  72         // Grab a method name from the output
  73         int count = 0;
  74 
  75         for (String line : output.asLines()) {
  76             count++;
  77 
  78             String[] parts = line.split(" ");
  79             // int compileID = Integer.parseInt(parts[0]);
  80             // int compileLevel = Integer.parseInt(parts[1]);
  81             String methodPrintedInLogFormat = parts[2];
  82 
  83             // skip inits, clinits and methodHandles - they can not be reflected
  84             if (methodPrintedInLogFormat.contains("<init>")) {
  85                 continue;
  86             }
  87             if (methodPrintedInLogFormat.contains("<clinit>")) {
  88                 continue;
  89             }
  90             if (methodPrintedInLogFormat.contains("MethodHandle")) {
  91                 continue;
  92             }
  93 
  94             MethodIdentifierParser mf = new MethodIdentifierParser(methodPrintedInLogFormat);
  95             Method m = null;
  96             try {
  97                 m = mf.getMethod();
  98             } catch (NoSuchMethodException e) {
  99                 m = null;
 100             } catch (ClassNotFoundException e) {
 101                 Assert.fail("Test error: Caught unexpected exception", e);
 102             }
 103             if (m == null) {
 104                 Assert.fail("Test failed on: " + methodPrintedInLogFormat);
 105             }
 106             if (count > 10) {
 107                 // Testing 10 entries is enough. Lets not waste time.
 108                 break;
 109             }
 110         }
 111     }
 112 
 113     @Test
 114     public void jmx() {
 115         run(new JMXExecutor());
 116     }
 117 }