{
try{
ICompilationUnit src = (ICompilationUnit)cu.getJavaElement();
IJavaProject javaProject = src.getJavaProject();
MultiTextEdit edit = new MultiTextEdit();
ImportRewrite imports = StubUtility.createImportRewrite(cu, true);
imports.addStaticImport("org.junit.Assert", "*", false);
imports.addImport(JUnitCorePlugin.JUNIT4_ANNOTATION_NAME);
edit.addChild(imports.rewriteImports(null));
AST ast = cu.getAST();
ASTRewrite astRewrite = ASTRewrite.create(ast);
ASTNode node = cu.findDeclaringNode(typeBinding);
ChildListPropertyDescriptor property = ((AbstractTypeDeclaration)node)
.getBodyDeclarationsProperty();
ListRewrite memberRewriter = astRewrite.getListRewrite(node, property);
HashSet<String> added = new HashSet<String>();
for (IMethodBinding binding : methodBindings){
String name = binding.getName();
if (added.contains(name)){
continue;
}
added.add(name);
MethodDeclaration stub = ast.newMethodDeclaration();
stub.setConstructor(false);
stub.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC));
Annotation marker = ast.newMarkerAnnotation();
marker.setTypeName(ast.newSimpleName("Test"));
astRewrite
.getListRewrite(stub, MethodDeclaration.MODIFIERS2_PROPERTY)
.insertFirst(marker, null);
stub.setName(ast.newSimpleName(name));
Block body = ast.newBlock();
stub.setBody(body);
String todoTask = "";
String todoTaskTag = JUnitStubUtility.getTodoTaskTag(javaProject);
if (todoTaskTag != null) {
todoTask = " // " + todoTaskTag;
}
String message = WizardMessages
.NewTestCaseWizardPageOne_not_yet_implemented_string;
body.statements().add(astRewrite.createStringPlaceholder(
todoTask,
ASTNode.RETURN_STATEMENT));
body.statements().add(astRewrite.createStringPlaceholder(
Messages.format("fail(\"{0}\");", message),
ASTNode.RETURN_STATEMENT));
memberRewriter.insertLast(stub, null);
}
edit.addChild(astRewrite.rewriteAST());
JavaModelUtil.applyEdit(src, edit, true, null);
}catch(Exception e){
throw new RuntimeException(e);
}