1 /*
   2  * Copyright (c) 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 InlineMatcherTest
  26  * @library /testlibrary /../../test/lib
  27  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  28  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  29  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI InlineMatcherTest
  30  * @summary Testing of compiler/InlineMatcher
  31  * @bug 8074095
  32  */
  33 
  34 import java.lang.reflect.Method;
  35 import java.util.ArrayList;
  36 import sun.hotspot.WhiteBox;
  37 
  38 public class InlineMatcherTest {
  39 
  40     /** Instance of WhiteBox */
  41     protected static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
  42 
  43     Method helper;
  44     Method getDate;
  45     Method inner;
  46     Method toString;
  47 
  48     final public static int FORCE_INLINE = 2;
  49     final public static int DONT_INLINE = 1;
  50     final public static int NO_MATCH = 0;
  51     final public static int PARSING_FAILURE = -1;
  52 
  53     public InlineMatcherTest() {
  54 
  55     }
  56 
  57     public void test() throws Exception {
  58         // instantiate before calling getMethod on innerHelper
  59         TestCases testCases = new TestCases();
  60 
  61         helper  = getMethod(InlineMatcherTest.class, "helper");
  62                 getDate = getMethod(java.util.Date.class, "getDate");
  63                 inner   = getMethod(TestCases.class, "innerHelper");
  64                 toString = getMethod(String.class, "toString");
  65 
  66                 testCases.add(helper, "*.*",                         PARSING_FAILURE);
  67                 testCases.add(helper, "+*.*",                        FORCE_INLINE);
  68                 testCases.add(helper, "++*.*",                       NO_MATCH);    // + is a valid part of the class name
  69                 testCases.add(helper, "-*.*",                        DONT_INLINE);
  70                 testCases.add(helper, "--*.*",                       NO_MATCH);    // - is a valid part of the class name
  71 
  72                 testCases.add(helper, "+InlineMatcherTest.*",         FORCE_INLINE);
  73                 testCases.add(helper, "+InlineMatcherTest.helper",    FORCE_INLINE);
  74                 testCases.add(helper, "+InlineMatcherTest.helper()",  FORCE_INLINE);
  75                 testCases.add(helper, "+InlineMatcherTest.helper()V", FORCE_INLINE);
  76                 testCases.add(helper, "+InlineMatcherTest.helper(",   FORCE_INLINE);
  77 
  78                 testCases.add(helper, "-InlineMatcherTest.*",         DONT_INLINE);
  79                 testCases.add(helper, "-InlineMatcherTest.helper",    DONT_INLINE);
  80                 testCases.add(helper, "-InlineMatcherTest.helper()",  DONT_INLINE);
  81                 testCases.add(helper, "-InlineMatcherTest.helper()V", DONT_INLINE);
  82                 testCases.add(helper, "-InlineMatcherTest.helper(",   DONT_INLINE);
  83 
  84                 testCases.add(helper, "+abc.*",                       NO_MATCH);
  85                 testCases.add(helper, "+*.abc",                       NO_MATCH);
  86                 testCases.add(helper, "-abc.*",                       NO_MATCH);
  87                 testCases.add(helper, "-*.abcls ",                    NO_MATCH);
  88 
  89             int failures = 0;
  90 
  91             for(TestCase t : testCases) {
  92                 System.out.println("Test case: " + t.pattern);
  93                     if (!t.test()) {
  94                         failures++;
  95                         System.out.println(" * FAILED");
  96                     }
  97                 }
  98                 if (failures != 0) {
  99                         throw new Exception("There where " + failures + " failures in this test");
 100                 }
 101     }
 102 
 103         public static void main(String... args) throws Exception {
 104                 InlineMatcherTest test = new InlineMatcherTest();
 105                 test.test();
 106         }
 107 
 108         public void helper() {
 109 
 110         }
 111 
 112     private static Method getMethod(Class klass, String name, Class<?>... parameterTypes) {
 113         try {
 114             return klass.getDeclaredMethod(name, parameterTypes);
 115         } catch (NoSuchMethodException | SecurityException e) {
 116             throw new RuntimeException(
 117                     "exception on getting method Helper." + name, e);
 118         }
 119     }
 120 
 121     class TestCase {
 122         String pattern;
 123         Method testTarget;
 124         int expectedResult;
 125 
 126         public TestCase(Method testTarget, String pattern, int expectedResult) {
 127                 this.testTarget = testTarget;
 128                 this.pattern = pattern;
 129                 this.expectedResult = expectedResult;
 130         }
 131 
 132         public String resultAsStr(int errorCode) {
 133                 switch (errorCode) {
 134                         case PARSING_FAILURE: return "Parsing failed";
 135                         case NO_MATCH: return "No match";
 136                         case DONT_INLINE: return "Dont Inline";
 137                         case FORCE_INLINE: return "Force Inline";
 138                         default: return "Unknown error";
 139                 }
 140         }
 141 
 142         boolean test() {
 143                 int result = WHITE_BOX.matchesInlineMatcher(testTarget, pattern);
 144                 if (result != expectedResult) {
 145                         System.out.println("FAIL Wrong result, Got: " + resultAsStr(result) + "\n TestCase: " + this.toString());
 146                         return false;
 147                 }
 148                 return true;
 149         }
 150 
 151         @Override
 152         public String toString() {
 153                 return "Method: '" + testTarget.toString() + "' Pattern: '" + pattern + "' Expected: " + resultAsStr(expectedResult);
 154         }
 155 
 156         public void innerHelper() {
 157 
 158         }
 159     }
 160 
 161     class TestCases extends ArrayList<TestCase> {
 162                 private static final long serialVersionUID = 1L;
 163 
 164                 public boolean add(Method testTarget, String pattern, int expectedResult) {
 165                         return super.add(new TestCase(testTarget, pattern, expectedResult));
 166                 }
 167     }
 168 }
 169 
 170