當前位置: 首頁>>代碼示例>>Java>>正文


Java Constructor.getExceptionTypes方法代碼示例

本文整理匯總了Java中java.lang.reflect.Constructor.getExceptionTypes方法的典型用法代碼示例。如果您正苦於以下問題:Java Constructor.getExceptionTypes方法的具體用法?Java Constructor.getExceptionTypes怎麽用?Java Constructor.getExceptionTypes使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.lang.reflect.Constructor的用法示例。


在下文中一共展示了Constructor.getExceptionTypes方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: createMethods

import java.lang.reflect.Constructor; //導入方法依賴的package包/類
private static String createMethods(String clsName, Constructor constructor) {
    String sname = clsName.substring(4); // Cut "Tree"
    Class[] params = constructor.getParameterTypes();
    HashSet set = new HashSet();
    String header = "";
    String atrs = "";
    String res = "";
    
    // create header declaration an attr. list
    for (int i = 0;  i < params.length; i++) {
        String pType = params[i].getName();
        String pName;
        
        if (params[i].isPrimitive()) {
            pName = pType + "_val";
        } else {
            pName = pType.substring(pType.lastIndexOf('.') + 1).toLowerCase();
        }
        
        if (set.contains(pName)) {
            pName += i;
        }
        set.add(pName);
        
        header += pType + " " + pName;
        atrs += pName;
        if (i < params.length - 1) {
            header += ", ";
            atrs += ", ";
        }
    }
    
    // create() method
    res += "\n"
    + "    static Tree" + sname + " create" + sname + "(" + header + ", String view) throws Exception {\n"
    + "        Tree" + sname + " node = new Tree" + sname + "(" + atrs + ");\n"
    + "        \n"
    + "        assertEquals(node, view);\n"
    + "        cloneNodeTest(node, view);\n"
    + "        return node;\n"
    + "    }\n";
    
    Class[] exs = constructor.getExceptionTypes();
    
    // if constructor throw InvalidArgumentException create method createInvalid()
    for (int i = 0; i < exs.length; i++) {
        if (InvalidArgumentException.class.isAssignableFrom(exs[i])) {
            res += "\n"
            + "    static void create" + sname + "Invalid(" + header + ") throws Exception {\n"
            + "        try {\n"
            + "            new Tree" + sname + "(" + atrs + ");\n"
            + "            // Fail if previous line doesn't trhow exception.\n"
            + "            fail(NOT_EXCEPTION + \"from: new Tree" + sname +"(" + atrs + ")\");\n"
            + "        } catch (InvalidArgumentException e) {\n"
            + "            // OK\n"
            + "        }\n"
            + "    }\n";
            break;
        }
    }
    return res;
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:63,代碼來源:FactoryTestGenerator.java


注:本文中的java.lang.reflect.Constructor.getExceptionTypes方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。