Java Code Examples for java.lang.reflect.Constructor.getExceptionTypes()

The following are Jave code examples for showing how to use getExceptionTypes() of the java.lang.reflect.Constructor class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: incubator-netbeans   File: FactoryTestGenerator.java   View Source Code Vote up 4 votes
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;
}