public void testSetExceptions() throws Exception { CtClass cc = sloader.get("test2.SetExceptions"); CtMethod m = cc.getDeclaredMethod("f"); CtClass ex = m.getExceptionTypes()[0]; assertEquals("java.lang.Exception", ex.getName()); m.setExceptionTypes(null); assertEquals(0, m.getExceptionTypes().length); m.setExceptionTypes(new CtClass[0]); assertEquals(0, m.getExceptionTypes().length); m.setExceptionTypes(new CtClass[] {ex}); assertEquals(ex, m.getExceptionTypes()[0]); }
/** * Creates a public abstract method. * * @param returnType the type of the returned value * @param mname the method name * @param parameters a list of the parameter types * @param exceptions a list of the exception types * @param declaring the class to which the created method is added. * @see CtMethod#CtMethod(CtClass,String,CtClass[],CtClass) */ public static CtMethod abstractMethod( CtClass returnType, String mname, CtClass[] parameters, CtClass[] exceptions, CtClass declaring) throws NotFoundException { CtMethod cm = new CtMethod(returnType, mname, parameters, declaring); cm.setExceptionTypes(exceptions); return cm; }
/** * Creates a method. * * @param modifiers access modifiers. * @param returnType the type of the returned value. * @param mname the method name. * @param parameters a list of the parameter types. * @param exceptions a list of the exception types. * @param body the source text of the method body. It must be a block surrounded by <code>{} * </code>. If it is <code>null</code>, the created method does nothing except returning zero * or null. * @param declaring the class to which the created method is added. * @see Modifier */ public static CtMethod make( int modifiers, CtClass returnType, String mname, CtClass[] parameters, CtClass[] exceptions, String body, CtClass declaring) throws CannotCompileException { try { CtMethod cm = new CtMethod(returnType, mname, parameters, declaring); cm.setModifiers(modifiers); cm.setExceptionTypes(exceptions); cm.setBody(body); return cm; } catch (NotFoundException e) { throw new CannotCompileException(e); } }