KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > util > JavadocUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.internal.corext.refactoring.util;
13
14 import java.util.HashSet JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.text.edits.TextEditGroup;
19
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;
30
31 import org.eclipse.jdt.internal.ui.text.correction.JavadocTagsSubProcessor;
32
33 import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility;
34
35
36 public class JavadocUtil {
37     
38     private JavadocUtil() {
39         // static-only
40
}
41     
42     //TODO: is a copy of ChangeSignatureRefactoring.DeclarationUpdate#createParamTag(..)
43
public static TagElement createParamTag(String JavaDoc parameterName, AST ast, IJavaProject javaProject) {
44         TagElement paramNode= ast.newTagElement();
45         paramNode.setTagName(TagElement.TAG_PARAM);
46     
47         SimpleName simpleName= ast.newSimpleName(parameterName);
48         paramNode.fragments().add(simpleName);
49     
50         TextElement textElement= ast.newTextElement();
51         String JavaDoc text= StubUtility.getTodoTaskTag(javaProject);
52         if (text != null)
53             textElement.setText(text); //TODO: use template with {@todo} ...
54
paramNode.fragments().add(textElement);
55         
56         return paramNode;
57     }
58
59     /**
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)
63      */

64     public static boolean shouldAddParamJavadoc(MethodDeclaration methodDeclaration) {
65         Javadoc javadoc= methodDeclaration.getJavadoc();
66         if (javadoc == null)
67             return false;
68         if (methodDeclaration.parameters().size() == 0)
69             return true;
70         List JavaDoc tags= javadoc.tags();
71         for (Iterator JavaDoc iter= tags.iterator(); iter.hasNext();) {
72             TagElement element= (TagElement) iter.next();
73             if (TagElement.TAG_PARAM.equals(element.getTagName()))
74                 return true;
75         }
76         return false;
77     }
78     
79     
80     /**
81      * Adds a "param" javadoc tag for a new last parameter if necessary.
82      * @param parameterName
83      * @param methodDeclaration
84      * @param astRewrite
85      * @param javaProject
86      * @param groupDescription
87      */

88     public static void addParamJavadoc(String JavaDoc parameterName, MethodDeclaration methodDeclaration,
89             ASTRewrite astRewrite, IJavaProject javaProject, TextEditGroup groupDescription) {
90         if (! shouldAddParamJavadoc(methodDeclaration))
91             return;
92         
93         ListRewrite tagsRewrite= astRewrite.getListRewrite(methodDeclaration.getJavadoc(), Javadoc.TAGS_PROPERTY);
94         HashSet JavaDoc leadingNames= new HashSet JavaDoc();
95         for (Iterator JavaDoc iter= methodDeclaration.parameters().iterator(); iter.hasNext();) {
96             SingleVariableDeclaration curr= (SingleVariableDeclaration) iter.next();
97             leadingNames.add(curr.getName().getIdentifier());
98         }
99         TagElement parameterTag= createParamTag(parameterName, astRewrite.getAST(), javaProject);
100         JavadocTagsSubProcessor.insertTag(tagsRewrite, parameterTag, leadingNames, groupDescription);
101     }
102
103 }
104
Popular Tags