< prev index next >

test/hotspot/jtreg/serviceability/jvmti/HiddenClass/P/Q/HiddenClassSigTest.java

Print this page
rev 58768 : 8238358: Implementation of JEP 371: Hidden Classes
Reviewed-by: alanb, cjplummer, coleenp, dholmes, dlong, forax, jlahoda, psandoz, plevart, vromero
Contributed-by: mandy.chung@oracle.com, lois.foltan@oracle.com, david.holmes@oracle.com, harold.seigel@oracle.com, serguei.spitsyn@oracle.com, alex.buckley@oracle.com, jamsheed.c.m@oracle.com, jan.lahoda@oracle.com, amy.lu@oracle.com
rev 58769 : [mq]: type-descriptor-name

@@ -60,13 +60,10 @@
     private static void log(String str) { System.out.println(str); }
 
     private static final String HCName = "P/Q/HiddenClassSig.class";
     private static final String DIR = Utils.TEST_CLASSES;
     private static final String LOG_PREFIX = "HiddenClassSigTest: ";
-    private static final String ARR_PREFIX = "[[L";
-    private static final String ARR_POSTFIX = "[][]";
-    private static final String CLASS_PREFIX = "class ";
 
     static native void checkHiddenClass(Class klass, String sig);
     static native void checkHiddenClassArray(Class array, String sig);
     static native boolean checkFailed();
 

@@ -109,119 +106,121 @@
         log(LOG_PREFIX + "toString:  " + klass.toString());
         log(LOG_PREFIX + "toGenStr:  " + klass.toGenericString());
         log(LOG_PREFIX + "elem type: " + klass.componentType());
     }
 
-    static boolean checkName(String name, String expName, String msgPart) {
+    private static final String HC_NAME = "P.Q.HiddenClassSig";
+    private static final String HC_SUFFIX_REGEX = "0x[0-9a-f]+";
+    static boolean checkName(Class<?> klass, String name, String toString) {
         boolean failed = false;
-        if (!name.equals(expName)) {
-            log("Test FAIL: result of " + msgPart + " does not match expectation");
+        String regex = "";
+        Class<?> c = klass;
+        while (c.isArray()) {
+            regex = "\\[" + regex;
+            c = c.componentType();
+        }
+        if (klass.isArray()) {
+            regex += "L" + HC_NAME + "/" + HC_SUFFIX_REGEX + ";";
+        } else {
+            regex = HC_NAME + "/" + HC_SUFFIX_REGEX;
+        }
+        if (!name.matches(regex)) {
+            log("Test FAIL: result of Class::getName" + " \"" + name + "\" does not match " + regex);
+            failed = true;
+        }
+        if (!toString.matches("class " + regex)) {
+            log("Test FAIL: result of Class::toString" + " \"" + name + "\" does not match " + regex);
             failed = true;
         }
         return failed;
     }
 
-    static boolean checkNameHas(String name, String expNamePart, String msgPart) {
+    static boolean checkTypeName(Class<?> klass, String name) {
         boolean failed = false;
-        if (name.indexOf(expNamePart) < 0) {
-            log("Test FAIL: result of " + msgPart + " does not match expectation");
+        String regex = HC_NAME + "/" + HC_SUFFIX_REGEX;
+        Class<?> c = klass;
+        while (c.isArray()) {
+            c = c.componentType();
+            regex = regex + "\\[\\]";
+        }
+        if (!name.matches(regex)) {
+            log("Test FAIL: result of Class::getTypeName" + " \"" + name + "\" does not match " + regex);
             failed = true;
         }
         return failed;
     }
 
-    static boolean checkArray(Class<?> arrClass) {
+    static boolean checkGenericString(Class<?> klass, String name) {
         boolean failed = false;
-        Class<?> elemClass = arrClass.componentType();
-
-        String arrName = arrClass.getName();
-        String arrType = arrClass.getTypeName();
-        String arrStr = arrClass.toString().substring(CLASS_PREFIX.length());
-        String arrGen = arrClass.toGenericString();
-
-        String elemName = elemClass.getName();
-        String elemType = elemClass.getTypeName();
-        String elemStr = elemClass.toString().substring(CLASS_PREFIX.length());
-        String elemGen = elemClass.toGenericString();
-
-        if (elemClass.isHidden()) {
-            elemGen = elemGen.substring(CLASS_PREFIX.length());
-        }
-        failed |= checkNameHas(arrName, elemName, "klass.getName()");
-        failed |= checkNameHas(arrStr, elemStr, "klass.toString()");
-        failed |= checkNameHas(arrType, elemType, "klass.getTypeName()");
-        failed |= checkNameHas(arrGen, elemGen, "klass.getGenericString()");
+        Class<?> c = klass;
+        String regex = HC_NAME + "/" + HC_SUFFIX_REGEX + "<T>";
+        if (!klass.isArray()) {
+            regex = "class " + regex;
+        }
+        while (c.isArray()) {
+            c = c.componentType();
+            regex = regex + "\\[\\]";
+        }
+        if (!name.matches(regex)) {
+            log("Test FAIL: result of Class::toGenericString" + " \"" + name + "\" does not match " + regex);
+            failed = true;
+        }
         return failed;
     }
 
-    static boolean testClass(Class<?> klass, int arrLevel, String baseName) {
+    static boolean checkDescriptorString(Class<?> klass, String name) {
         boolean failed = false;
-        boolean isHidden = (arrLevel == 0);
-        String prefix = (arrLevel == 0) ? "" : ARR_PREFIX.substring(2 - arrLevel);
-        String postfix = (arrLevel == 0) ? "" : ";";
+        String regex = "L" + HC_NAME.replace('.', '/') + "." + HC_SUFFIX_REGEX + ";";
+        Class<?> c = klass;
+        while (c.isArray()) {
+            regex = "\\[" + regex;
+            c = c.componentType();
+        }
+        if (!name.matches(regex)) {
+            log("Test FAIL: result of Class::descriptorString" + " \"" + name + "\" does not match " + regex);
+            failed = true;
+        }
+        return failed;
+    }
 
+    static boolean testClass(Class<?> klass) {
+        boolean failed = false;
         logClassInfo(klass);
 
-        String expName = ("" + klass).substring(CLASS_PREFIX.length());
-        String expType = baseName;
-        String expStr = CLASS_PREFIX + prefix + baseName + postfix;
-        String expGen = baseName + "<T>" + ARR_POSTFIX.substring(0, 2*arrLevel);
+        failed |= checkName(klass, klass.getName(), klass.toString());
+        failed |= checkTypeName(klass, klass.getTypeName());
+        failed |= checkGenericString(klass, klass.toGenericString());
+        failed |= checkDescriptorString(klass, klass.descriptorString());
 
-        if (arrLevel > 0) {
-            expType = expType + ARR_POSTFIX.substring(0, 2*arrLevel); 
-        } else {
-            expGen = CLASS_PREFIX + expGen; 
-        }
-        failed |= checkName(klass.getName(), expName, "klass.getName()");
-        failed |= checkName(klass.getTypeName(), expType, "klass.getTypeName()");
-        failed |= checkName(klass.toString(), expStr, "klass.toString()");
-        failed |= checkName(klass.toGenericString(), expGen, "klass.toGenericString()");
-
-        if (klass.isHidden() != isHidden) {
-            log("Test FAIL: result of klass.isHidden() does not match expectation");
+        if (klass.isArray() && klass.isHidden()) {
+            log("Test FAIL: an array class is never hidden");
             failed = true;
         }
-        String sig = hcSignature(klass);
-        if (arrLevel == 0) {
-            checkHiddenClass(klass, sig);
+        if (klass.isArray()) {
+            checkHiddenClassArray(klass, klass.descriptorString());
         } else {
-            failed |= checkArray(klass);
-            checkHiddenClassArray(klass, sig);
+            checkHiddenClass(klass, klass.descriptorString());
         }
         return failed;
     }
 
-    static String hcSignature(Class<?> klass) {
-        boolean isArray = klass.isArray();
-        String sig = klass.getName();
-        String prefix  = isArray ? "" : "L";
-        String postfix = isArray ? "" : ";";
-        int idx = sig.indexOf("/");
-
-        sig = prefix + sig.substring(0, idx).replace('.', '/')
-                      + "."
-                      + sig.substring(idx + 1, sig.length())
-                      + postfix;
-        return sig;
-    }
-
     public static void main(String args[]) throws Exception {
         log(LOG_PREFIX + "started");
         Class<?> hc = defineHiddenClass(HCName);
         String baseName = ("" + hc).substring("class ".length());
 
         Test<String> t = (Test<String>)hc.newInstance();
         String str = t.test("Test generic hidden class");
         log(LOG_PREFIX + "hc.test() returned string: " + str);
 
-        boolean failed = testClass(hc, 0, baseName);
+        boolean failed = testClass(hc);
 
         Class<?> hcArr = hc.arrayType();
-        failed |= testClass(hcArr, 1, baseName);
+        failed |= testClass(hcArr);
 
         Class<?> hcArrArr = hcArr.arrayType();
-        failed |= testClass(hcArrArr, 2, baseName);
+        failed |= testClass(hcArrArr);
 
         if (failed) {
           throw new RuntimeException("FAIL: failed status from java part");
         }
         if (checkFailed()) {
< prev index next >