@Override public void run(IProgressMonitor monitor) throws CoreException { if (monitor == null) monitor = new NullProgressMonitor(); try { monitor.beginTask("", 1); // $NON-NLS-1$ monitor.setTaskName(CodeGenerationMessages.GenerateToStringOperation_description); AbstractTypeDeclaration declaration = (AbstractTypeDeclaration) ASTNodes.findDeclaration(fContext.getTypeBinding(), fRewrite.getRoot()); ListRewrite rewriter = fRewrite .getASTRewrite() .getListRewrite(declaration, declaration.getBodyDeclarationsProperty()); if (fContext.getTypeBinding() != null && rewriter != null) { MethodDeclaration toStringMethod = fGenerator.generateToStringMethod(); List<BodyDeclaration> list = declaration.bodyDeclarations(); BodyDeclaration replace = findMethodToReplace(list, toStringMethod); if (replace == null || ((Boolean) toStringMethod.getProperty(AbstractToStringGenerator.OVERWRITE_METHOD_PROPERTY)) .booleanValue()) insertMethod(toStringMethod, rewriter, replace); List<MethodDeclaration> helperMethods = fGenerator.generateHelperMethods(); for (Iterator<MethodDeclaration> iterator = helperMethods.iterator(); iterator.hasNext(); ) { MethodDeclaration method = iterator.next(); replace = findMethodToReplace(list, method); if (replace == null || ((Boolean) method.getProperty(AbstractToStringGenerator.OVERWRITE_METHOD_PROPERTY)) .booleanValue()) { insertMethod(method, rewriter, replace); } } JavaModelUtil.applyEdit( (ICompilationUnit) fUnit.getJavaElement(), fRewrite.createChange(true).getEdit(), false, monitor); } } finally { monitor.done(); } }
/** * Creates new <code>GenerateToStringOperation</code>, using <code>settings.toStringStyle</code> * field to choose the right subclass. * * @param typeBinding binding for the type for which the toString() method will be created * @param selectedBindings bindings for the typetype's members to be used in created method * @param unit a compilation unit containing the type * @param elementPosition at this position in the compilation unit created method will be added * @param settings the settings for toString() generator * @return a ready to use <code>GenerateToStringOperation</code> object */ public static GenerateToStringOperation createOperation( ITypeBinding typeBinding, Object[] selectedBindings, CompilationUnit unit, IJavaElement elementPosition, ToStringGenerationSettings settings) { AbstractToStringGenerator generator = createToStringGenerator(settings.toStringStyle); ToStringTemplateParser parser = createTemplateParser(settings.toStringStyle); parser.parseTemplate(settings.stringFormatTemplate); CompilationUnitRewrite rewrite = new CompilationUnitRewrite((ICompilationUnit) unit.getTypeRoot(), unit); ToStringGenerationContext context = new ToStringGenerationContext(parser, selectedBindings, settings, typeBinding, rewrite); generator.setContext(context); return new GenerateToStringOperation(elementPosition, context, generator, unit, rewrite); }
/** @return RefactoringStatus with eventual errors and warnings */ public RefactoringStatus checkConditions() { return fGenerator.checkConditions(); }