1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package org.eclipse.jdt.internal.corext.refactoring.util;
14 import java.util.HashSet;
15 import java.util.Iterator;
16 import java.util.List;
18 import org.eclipse.text.edits.TextEditGroup;
20 import org.eclipse.jdt.core.IJavaProject;
21 import org.eclipse.jdt.core.dom.AST;
22 import org.eclipse.jdt.core.dom.Javadoc;
23 import org.eclipse.jdt.core.dom.MethodDeclaration;
24 import org.eclipse.jdt.core.dom.SimpleName;
25 import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
26 import org.eclipse.jdt.core.dom.TagElement;
27 import org.eclipse.jdt.core.dom.TextElement;
28 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
29 import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
31 import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility;
33 import org.eclipse.jdt.internal.ui.text.correction.JavadocTagsSubProcessor;
36 public class JavadocUtil {
38 private JavadocUtil() {
42 //TODO: is a copy of ChangeSignatureRefactoring.DeclarationUpdate#createParamTag(..)
43 public static TagElement createParamTag(String parameterName, AST ast, IJavaProject javaProject) {
44 TagElement paramNode= ast.newTagElement();
45 paramNode.setTagName(TagElement.TAG_PARAM);
47 SimpleName simpleName= ast.newSimpleName(parameterName);
48 paramNode.fragments().add(simpleName);
50 TextElement textElement= ast.newTextElement();
51 String text= StubUtility.getTodoTaskTag(javaProject);
53 textElement.setText(text); //TODO: use template with {@todo} ...
54 paramNode.fragments().add(textElement);
60 * Decide whether to add a "param" javadoc tag or not.
61 * @param methodDeclaration the method declaration
62 * @return method has javadoc && (method had no parameter before || there is already an @param tag)
64 public static boolean shouldAddParamJavadoc(MethodDeclaration methodDeclaration) {
65 Javadoc javadoc= methodDeclaration.getJavadoc();
68 if (methodDeclaration.parameters().size() == 0)
70 List<TagElement> tags= javadoc.tags();
71 for (Iterator<TagElement> iter= tags.iterator(); iter.hasNext();) {
72 TagElement element= iter.next();
73 if (TagElement.TAG_PARAM.equals(element.getTagName()))
81 * Adds a "param" javadoc tag for a new last parameter if necessary.
82 * @param parameterName
83 * @param methodDeclaration
86 * @param groupDescription
88 public static void addParamJavadoc(String parameterName, MethodDeclaration methodDeclaration,
89 ASTRewrite astRewrite, IJavaProject javaProject, TextEditGroup groupDescription) {
90 if (! shouldAddParamJavadoc(methodDeclaration))
93 ListRewrite tagsRewrite= astRewrite.getListRewrite(methodDeclaration.getJavadoc(), Javadoc.TAGS_PROPERTY);
94 HashSet<String> leadingNames= new HashSet<String>();
95 for (Iterator<SingleVariableDeclaration> iter= methodDeclaration.parameters().iterator(); iter.hasNext();) {
96 SingleVariableDeclaration curr= iter.next();
97 leadingNames.add(curr.getName().getIdentifier());
99 TagElement parameterTag= createParamTag(parameterName, astRewrite.getAST(), javaProject);
100 JavadocTagsSubProcessor.insertTag(tagsRewrite, parameterTag, leadingNames, groupDescription);