@Test public void testGenerateConstructorWithParams() { AST ast = AST.newAST(AST.JLS3); TypeDeclaration td = ast.newTypeDeclaration(); td.setName(ast.newSimpleName("CompanyException")); when(clazz.getName()).thenReturn("CompanyException"); exceptionGenerator.generateConstructorWithParams(clazz, ast, td, new String[] { "String", "Throwable" }, new String[] { "message", "cause" }); assertEquals( "class CompanyException {\n" + " public CompanyException( String message, Throwable cause){\n" + " super(message,cause);\n }\n" + "}\n", td.toString()); }
@SuppressWarnings("unchecked") public void generateConstructorWithParams(Classifier clazz, AST ast, TypeDeclaration td, String[] varTypes, String[] varNames) { MethodDeclaration constructor = ast.newMethodDeclaration(); constructor.setConstructor(true); constructor.setName(ast.newSimpleName(clazz.getName())); constructor.modifiers().add( ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD)); for (int index = 0; index < varTypes.length; index++) { SingleVariableDeclaration variableDeclaration = ast .newSingleVariableDeclaration(); variableDeclaration.setType(ast.newSimpleType(ast .newSimpleName(varTypes[index]))); variableDeclaration.setName(ast.newSimpleName(varNames[index])); constructor.parameters().add(variableDeclaration); } Block block = ast.newBlock(); SuperConstructorInvocation constructorInvocation = ast .newSuperConstructorInvocation(); for (int index = 0; index < varNames.length; index++) { constructorInvocation.arguments().add( ast.newSimpleName(varNames[index])); } block.statements().add(constructorInvocation); constructor.setBody(block); td.bodyDeclarations().add(constructor); }
ExceptionGenerator { @SuppressWarnings("unchecked") public void generateConstructorWithParams(Classifier clazz, AST ast, TypeDeclaration td, String[] varTypes, String[] varNames) { MethodDeclaration constructor = ast.newMethodDeclaration(); constructor.setConstructor(true); constructor.setName(ast.newSimpleName(clazz.getName())); constructor.modifiers().add( ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD)); for (int index = 0; index < varTypes.length; index++) { SingleVariableDeclaration variableDeclaration = ast .newSingleVariableDeclaration(); variableDeclaration.setType(ast.newSimpleType(ast .newSimpleName(varTypes[index]))); variableDeclaration.setName(ast.newSimpleName(varNames[index])); constructor.parameters().add(variableDeclaration); } Block block = ast.newBlock(); SuperConstructorInvocation constructorInvocation = ast .newSuperConstructorInvocation(); for (int index = 0; index < varNames.length; index++) { constructorInvocation.arguments().add( ast.newSimpleName(varNames[index])); } block.statements().add(constructorInvocation); constructor.setBody(block); td.bodyDeclarations().add(constructor); } }
ExceptionGenerator { @SuppressWarnings("unchecked") public void generateConstructorWithParams(Classifier clazz, AST ast, TypeDeclaration td, String[] varTypes, String[] varNames) { MethodDeclaration constructor = ast.newMethodDeclaration(); constructor.setConstructor(true); constructor.setName(ast.newSimpleName(clazz.getName())); constructor.modifiers().add( ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD)); for (int index = 0; index < varTypes.length; index++) { SingleVariableDeclaration variableDeclaration = ast .newSingleVariableDeclaration(); variableDeclaration.setType(ast.newSimpleType(ast .newSimpleName(varTypes[index]))); variableDeclaration.setName(ast.newSimpleName(varNames[index])); constructor.parameters().add(variableDeclaration); } Block block = ast.newBlock(); SuperConstructorInvocation constructorInvocation = ast .newSuperConstructorInvocation(); for (int index = 0; index < varNames.length; index++) { constructorInvocation.arguments().add( ast.newSimpleName(varNames[index])); } block.statements().add(constructorInvocation); constructor.setBody(block); td.bodyDeclarations().add(constructor); }  }
ExceptionGenerator { @SuppressWarnings("unchecked") public void generateConstructorWithParams(Classifier clazz, AST ast, TypeDeclaration td, String[] varTypes, String[] varNames) { MethodDeclaration constructor = ast.newMethodDeclaration(); constructor.setConstructor(true); constructor.setName(ast.newSimpleName(clazz.getName())); constructor.modifiers().add( ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD)); for (int index = 0; index < varTypes.length; index++) { SingleVariableDeclaration variableDeclaration = ast .newSingleVariableDeclaration(); variableDeclaration.setType(ast.newSimpleType(ast .newSimpleName(varTypes[index]))); variableDeclaration.setName(ast.newSimpleName(varNames[index])); constructor.parameters().add(variableDeclaration); } Block block = ast.newBlock(); SuperConstructorInvocation constructorInvocation = ast .newSuperConstructorInvocation(); for (int index = 0; index < varNames.length; index++) { constructorInvocation.arguments().add( ast.newSimpleName(varNames[index])); } block.statements().add(constructorInvocation); constructor.setBody(block); td.bodyDeclarations().add(constructor); }  void setCheckedException(boolean isCheckedException); CompilationUnit generateCheckedException(Classifier clazz,
			String sourceDirectoryPackageName); CompilationUnit generateUncheckedException(Classifier clazz,
			String sourceDirectoryPackageName); @SuppressWarnings("unchecked") void generateSerialVersionUID(Classifier clazz, AST ast,
			TypeDeclaration td); @SuppressWarnings("unchecked") TypeDeclaration generateClass(Classifier clazz, AST ast,
			CompilationUnit cu); void generateConstructors(Classifier clazz, AST ast,
			TypeDeclaration td); @SuppressWarnings("unchecked") void generateConstructorWithParams(Classifier clazz, AST ast,
			TypeDeclaration td, String[] varTypes, String[] varNames); }
ExceptionGenerator { @SuppressWarnings("unchecked") public void generateConstructorWithParams(Classifier clazz, AST ast, TypeDeclaration td, String[] varTypes, String[] varNames) { MethodDeclaration constructor = ast.newMethodDeclaration(); constructor.setConstructor(true); constructor.setName(ast.newSimpleName(clazz.getName())); constructor.modifiers().add( ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD)); for (int index = 0; index < varTypes.length; index++) { SingleVariableDeclaration variableDeclaration = ast .newSingleVariableDeclaration(); variableDeclaration.setType(ast.newSimpleType(ast .newSimpleName(varTypes[index]))); variableDeclaration.setName(ast.newSimpleName(varNames[index])); constructor.parameters().add(variableDeclaration); } Block block = ast.newBlock(); SuperConstructorInvocation constructorInvocation = ast .newSuperConstructorInvocation(); for (int index = 0; index < varNames.length; index++) { constructorInvocation.arguments().add( ast.newSimpleName(varNames[index])); } block.statements().add(constructorInvocation); constructor.setBody(block); td.bodyDeclarations().add(constructor); }  void setCheckedException(boolean isCheckedException); CompilationUnit generateCheckedException(Classifier clazz,
			String sourceDirectoryPackageName); CompilationUnit generateUncheckedException(Classifier clazz,
			String sourceDirectoryPackageName); @SuppressWarnings("unchecked") void generateSerialVersionUID(Classifier clazz, AST ast,
			TypeDeclaration td); @SuppressWarnings("unchecked") TypeDeclaration generateClass(Classifier clazz, AST ast,
			CompilationUnit cu); void generateConstructors(Classifier clazz, AST ast,
			TypeDeclaration td); @SuppressWarnings("unchecked") void generateConstructorWithParams(Classifier clazz, AST ast,
			TypeDeclaration td, String[] varTypes, String[] varNames);  }