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 MethodMatcherTest
  26  * @library /testlibrary /test/lib
  27  * @build sun.hotspot.WhiteBox
  28  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  29  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  30  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI MethodMatcherTest
  31  * @summary Testing of compiler/MethodMatcher
  32  * @bug 8135068
  33  */
  34 
  35 import java.lang.reflect.Method;
  36 import java.util.ArrayList;
  37 
  38 import sun.hotspot.WhiteBox;
  39 
  40 public class MethodMatcherTest {
  41 
  42     /** Instance of WhiteBox */
  43     protected static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
  44 
  45     Method helper;
  46     Method getDate;
  47     Method inner;
  48     Method toString;
  49 
  50     static final int MATCH = 1;
  51     static final int NO_MATCH = 0;
  52     static final int PARSING_FAILURE = -1;
  53 
  54     public MethodMatcherTest() {
  55     }
  56 
  57     public void test() throws Exception {
  58         // instantiate before calling getMethod on innerHelper
  59         TestCases testCases = new TestCases();
  60 
  61         helper = getMethod(MethodMatcherTest.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, "pool/sub/Klass.method(I[Ljava/lang/String;Ljava/lang/Integer;[B[[D)V", NO_MATCH);
  67 
  68         // These should be improved to parsing failed in the future
  69         testCases.add(helper, "*Klass*,*$method*::", NO_MATCH);
  70         testCases.add(helper, "*Klass *+*", NO_MATCH);
  71         testCases.add(helper, "*Klass*::*method*", NO_MATCH);
  72 
  73         testCases.add(helper, "*,**", PARSING_FAILURE);
  74         testCases.add(helper, "*,*(I[Ljava/lang/String;Lj]ava/lang/Integer;[B[[D)V", PARSING_FAILURE);
  75         testCases.add(helper, "*,*)method*.", PARSING_FAILURE);
  76         testCases.add(helper, "{pool.subpack.Klass}* *", PARSING_FAILURE);
  77         testCases.add(helper, "*Klass met]hod/", PARSING_FAILURE);
  78         testCases.add(helper, "pool::su@%b::Klass* *)method.", PARSING_FAILURE);
  79         testCases.add(helper, "0pool/sub/Klass,*{method}*.(I[Ljava/lang/String;Lj]ava/lang/Integer;[B[[D)V", PARSING_FAILURE);
  80         testCases.add(helper, "*Klass nonexistent::)(I[Ljava/lang/String;Ljava/lang/Integer;[B[[D)V", PARSING_FAILURE);
  81         testCases.add(helper, "pool,su]b,Klass*,*)method*/", PARSING_FAILURE);
  82         testCases.add(helper, "_pool,sub,Klass*,met@%hod,(0)V", PARSING_FAILURE);
  83 
  84         testCases.add(helper, "*.*", MATCH);
  85         testCases.add(helper, "MethodMatcherTest.*", MATCH);
  86         testCases.add(helper, "MethodMatcherTest.helper", MATCH);
  87         testCases.add(helper, "MethodMatcherTest.helper()", MATCH);
  88         testCases.add(helper, "MethodMatcherTest.helper()V", MATCH);
  89         testCases.add(helper, "MethodMatcherTest.helper()V;", NO_MATCH);
  90         testCases.add(helper, "MethodMatcherTest.helper()I", NO_MATCH);
  91         testCases.add(helper, "MethodMatcherTest.helperX", NO_MATCH);
  92         testCases.add(helper, "MethodMatcherTestX.helper;", NO_MATCH);
  93         testCases.add(helper, "abc.*", NO_MATCH);
  94         testCases.add(helper, "*.abc", NO_MATCH);
  95 
  96         testCases.add(getDate, "*.*", MATCH);
  97         testCases.add(getDate, "*.getDate", MATCH);
  98         testCases.add(getDate, "java/util/Date.getDate", MATCH);
  99         testCases.add(getDate, "java/util/Date.*", MATCH);
 100 
 101         testCases.add(inner, "*.*", MATCH);
 102         testCases.add(inner, "MethodMatcherTest$TestCases.innerHelper", MATCH);
 103         testCases.add(inner, "MethodMatcherTest*.innerHelper", MATCH);
 104         testCases.add(inner, "MethodMatcherTest$*.innerHelper", MATCH);
 105         testCases.add(inner, "*$TestCases.innerHelper", MATCH);
 106         testCases.add(inner, "*TestCases.innerHelper", MATCH);
 107         testCases.add(inner, "TestCases.innerHelper", NO_MATCH);
 108         testCases.add(inner, "MethodMatcherTest.innerHelper", NO_MATCH);
 109 
 110         testCases.add(toString, "*.*", MATCH);
 111         testCases.add(toString, "java/lang/String.toString", MATCH);
 112         testCases.add(toString, "java.lang.String::toString", MATCH);
 113 
 114         testCases.add(toString, "java/lang/String::toString", PARSING_FAILURE);
 115         testCases.add(toString, "java.lang/String::toString", PARSING_FAILURE);
 116         testCases.add(toString, "java.lang/String.toString", PARSING_FAILURE);
 117         testCases.add(toString, "java::lang::String::toString", PARSING_FAILURE);
 118 
 119         testCases.add(toString, "java/lang/String.toString(*)", PARSING_FAILURE);
 120         testCases.add(toString, "java/lang/String.toString(L*", PARSING_FAILURE);
 121         testCases.add(toString, "java/lang/String.toString*(lsd)l", NO_MATCH);
 122         testCases.add(toString, "java/lang/String.toString(lsd)l", NO_MATCH);
 123         testCases.add(toString, "java/lang/String.toString (", MATCH);
 124         testCases.add(toString, "java/lang/String.toString ()", MATCH);
 125         testCases.add(toString, "java/lang/String.toString ()L", MATCH);
 126         testCases.add(toString, "java/lang/String.toString ()Lj", MATCH);
 127         testCases.add(toString, "java/lang/String.toString ()Ls", NO_MATCH);
 128         testCases.add(toString, "java/lang/String.toString*(", MATCH);
 129         testCases.add(toString, "java/lang/String.toString* (", MATCH);
 130         testCases.add(toString, "java/lang/String.toString*(;", NO_MATCH);
 131         testCases.add(toString, "java/lang/String.toString*();sf", NO_MATCH);
 132         testCases.add(toString, "java/lang/String.toString*()Ljava/lang/String;", MATCH);
 133         testCases.add(toString, "java/lang/String.toString()Ljava/lang/String;", MATCH);
 134         testCases.add(toString, "java/lang/String.toString ()Ljava/lang/String;", MATCH);
 135         testCases.add(toString, "java/lang/String.toString ()Ljava/lang/String", MATCH);
 136         testCases.add(toString, "java/lang/String.toString ()L", MATCH);
 137         testCases.add(toString, "java/lang/String.toString ()I;", NO_MATCH);
 138 
 139         testCases.add(toString, "*Internal.*", NO_MATCH);
 140         testCases.add(toString, "*Internal.**", PARSING_FAILURE);
 141         testCases.add(toString, "*Internal.***", PARSING_FAILURE);
 142         testCases.add(toString, "*Internal.*a**", PARSING_FAILURE);
 143         testCases.add(toString, "*Internal.**a*", PARSING_FAILURE);
 144 
 145         testCases.add(toString, "java.lang.String::<init>(Ljava/lang/String;)V", NO_MATCH);
 146         testCases.add(toString, "java.lang.String::<clinit>(Ljava/lang/String;)V", NO_MATCH);
 147         testCases.add(toString, "java.lang.String::<init(Ljava/lang/String;)V", PARSING_FAILURE);
 148         testCases.add(toString, "java.lang.String::init>(Ljava/lang/String;)V", PARSING_FAILURE);
 149 
 150         testCases.add(toString, "java/lang/String.toString()Ljava/lang/String;", MATCH);
 151         testCases.add(toString, "java/lang/Str<ing.toString()Ljava/lang/String;", PARSING_FAILURE);
 152         testCases.add(toString, "java/lang/Str>ing.toString()Ljava/lang/String;", PARSING_FAILURE);
 153         testCases.add(toString, "java/lang/<init>.toString()Ljava/lang/String;", PARSING_FAILURE);
 154         testCases.add(toString, "java/lang/<clinit>.toString()Ljava/lang/String;", PARSING_FAILURE);
 155 
 156         int failures = 0;
 157         for (TestCase t : testCases) {
 158             System.out.println("Test case: " + t.pattern);
 159             if (!t.test()) {
 160                 failures++;
 161                 System.out.println(" * FAILED");
 162             }
 163         }
 164         if (failures != 0) {
 165             throw new Exception("There where " + failures + " failures in this test");
 166         }
 167     }
 168 
 169     public static void main(String... args) throws Exception {
 170         MethodMatcherTest test = new MethodMatcherTest();
 171         test.test();
 172     }
 173 
 174     public void helper() {
 175 
 176     }
 177 
 178     private static Method getMethod(Class klass, String name, Class<?>... parameterTypes) {
 179         try {
 180             return klass.getDeclaredMethod(name, parameterTypes);
 181         } catch (NoSuchMethodException | SecurityException e) {
 182             throw new RuntimeException("exception on getting method Helper." + name, e);
 183         }
 184     }
 185 
 186     class TestCase {
 187         String pattern;
 188         Method testTarget;
 189         int expectedResult;
 190 
 191         public TestCase(Method testTarget, String pattern, int expectedResult) {
 192             this.testTarget = testTarget;
 193             this.pattern = pattern;
 194             this.expectedResult = expectedResult;
 195         }
 196 
 197         public String resultAsStr(int errorCode) {
 198             switch (errorCode) {
 199             case PARSING_FAILURE:
 200                 return "Parsing failed";
 201             case NO_MATCH:
 202                 return "No match";
 203             case MATCH:
 204                 return "Match";
 205             default:
 206                 return "Unknown error";
 207             }
 208         }
 209 
 210         boolean test() {
 211             int result = WHITE_BOX.matchesMethod(testTarget, pattern);
 212             if (result != expectedResult) {
 213                 System.out
 214                         .println("FAIL Wrong result, Got: " + resultAsStr(result) + "\n TestCase: " + this.toString());
 215                 return false;
 216             }
 217             return true;
 218         }
 219 
 220         @Override
 221         public String toString() {
 222             return "Method: '" + testTarget.toString() + "' Pattern: '" + pattern + "' Expected: "
 223                     + resultAsStr(expectedResult);
 224         }
 225 
 226         public void innerHelper() {
 227         }
 228     }
 229 
 230     class TestCases extends ArrayList<TestCase> {
 231         private static final long serialVersionUID = 1L;
 232 
 233         public boolean add(Method testTarget, String pattern, int expectedResult) {
 234             return super.add(new TestCase(testTarget, pattern, expectedResult));
 235         }
 236 
 237         public void innerHelper() {
 238         }
 239     }
 240 }