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