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