1 /*
   2  * Copyright (c) 2014, 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 ..
  28  * @build DcmdUtil MethodIdentifierParser CodelistTest
  29  * @run main CodelistTest
  30  * @summary Test of diagnostic command Compiler.codelist
  31  */
  32 
  33 import java.io.BufferedReader;
  34 import java.io.StringReader;
  35 import java.lang.reflect.Method;
  36 
  37 public class CodelistTest {
  38 
  39     /**
  40      * This test calls Jcmd (diagnostic command tool) Compiler.codelist and then parses the output,
  41      * making sure that the first methods in the list is valid by reflection.
  42      *
  43      * Output example:
  44      *
  45      * 6 0 java.lang.System.arraycopy(Ljava/lang/Object;ILjava/lang/Object;II)V [0x00007f7b49200910, 0x00007f7b49200aa0 - 0x00007f7b49200d30]
  46      * 2 3 java.lang.String.indexOf(II)I [0x00007f7b49200d90, 0x00007f7b49200f60 - 0x00007f7b49201490]
  47      * 7 3 java.lang.Math.min(II)I [0x00007f7b4922f010, 0x00007f7b4922f180 - 0x00007f7b4922f338]
  48      * 8 3 java.lang.String.equals(Ljava/lang/Object;)Z [0x00007f7b4922fb10, 0x00007f7b4922fd40 - 0x00007f7b49230698]
  49      * 9 3 java.lang.AbstractStringBuilder.ensureCapacityInternal(I)V [0x00007f7b49232010, 0x00007f7b492321a0 - 0x00007f7b49232510]
  50      * 10 1 java.lang.Object.<init>()V [0x00007f7b49233e90, 0x00007f7b49233fe0 - 0x00007f7b49234118]
  51      *
  52      */
  53 
  54     public static void main(String arg[]) throws Exception {
  55         int ok   = 0;
  56         int fail = 0;
  57 
  58         // Get output from dcmd (diagnostic command)
  59         String result = DcmdUtil.executeDcmd("Compiler.codelist");
  60         BufferedReader r = new BufferedReader(new StringReader(result));
  61 
  62         // Grab a method name from the output
  63         String line;
  64         int count = 0;
  65 
  66         while((line = r.readLine()) != null) {
  67             count++;
  68 
  69             String[] parts = line.split(" ");
  70             // int compileID = Integer.parseInt(parts[0]);
  71             // int compileLevel = Integer.parseInt(parts[1]);
  72             String methodPrintedInLogFormat = parts[2];
  73 
  74             // skip inits and clinits - they can not be reflected
  75             if (methodPrintedInLogFormat.contains("<init>")) {
  76                 continue;
  77             }
  78             if (methodPrintedInLogFormat.contains("<clinit>")) {
  79                 continue;
  80             }
  81             if (methodPrintedInLogFormat.contains("MethodHandle")) {
  82                 continue;
  83             }
  84 
  85             MethodIdentifierParser mf = new MethodIdentifierParser(methodPrintedInLogFormat);
  86             Method m;
  87             try {
  88                 m = mf.getMethod();
  89             } catch (NoSuchMethodException e) {
  90                 m = null;
  91             }
  92             if (m == null) {
  93                 throw new Exception("Test failed on: " + methodPrintedInLogFormat);
  94             }
  95             if (count > 10) {
  96                 // Testing 10 entries is enough. Lets not waste time.
  97                 break;
  98             }
  99         }
 100     }
 101 }