-
Notifications
You must be signed in to change notification settings - Fork 86
/
JavadocUtil.java
160 lines (140 loc) · 5.77 KB
/
JavadocUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*******************************************************************************
* Copyright (c) 2000, 2019 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
* Microsoft Corporation - copied to jdt.core.manipulation
*******************************************************************************/
package org.eclipse.jdt.internal.corext.refactoring.util;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import org.eclipse.text.edits.TextEditGroup;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.Javadoc;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.TagElement;
import org.eclipse.jdt.core.dom.TextElement;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
import org.eclipse.jdt.internal.core.manipulation.StubUtility;
import org.eclipse.jdt.internal.ui.text.correction.JavadocTagsSubProcessorCore;
public class JavadocUtil {
private static List<String> tagOrder= Arrays.asList(
TagElement.TAG_AUTHOR,
TagElement.TAG_VERSION,
TagElement.TAG_PARAM,
TagElement.TAG_RETURN,
TagElement.TAG_THROWS,
TagElement.TAG_EXCEPTION,
TagElement.TAG_SEE,
TagElement.TAG_SINCE,
TagElement.TAG_SERIAL,
TagElement.TAG_SERIALFIELD,
TagElement.TAG_SERIALDATA,
TagElement.TAG_DEPRECATED,
TagElement.TAG_VALUE
);
private JavadocUtil() {
// static-only
}
public static TagElement createParamTag(String parameterName, AST ast, IJavaProject javaProject) {
TagElement paramNode= ast.newTagElement();
paramNode.setTagName(TagElement.TAG_PARAM);
SimpleName simpleName= ast.newSimpleName(parameterName);
paramNode.fragments().add(simpleName);
TextElement textElement= ast.newTextElement();
String text= StubUtility.getTodoTaskTag(javaProject);
if (text != null)
textElement.setText(text); //TODO: use template with {@todo} ...
paramNode.fragments().add(textElement);
return paramNode;
}
/**
* Decide whether to add a "param" javadoc tag or not.
* @param methodDeclaration the method declaration
* @return method has javadoc && (method had no parameter before || there is already an @param tag)
*/
public static boolean shouldAddParamJavadoc(MethodDeclaration methodDeclaration) {
Javadoc javadoc= methodDeclaration.getJavadoc();
if (javadoc == null)
return false;
if (methodDeclaration.parameters().size() == 0)
return true;
List<TagElement> tags= javadoc.tags();
for (TagElement element : tags) {
if (TagElement.TAG_PARAM.equals(element.getTagName()))
return true;
}
return false;
}
/**
* Adds a "param" javadoc tag for a new last parameter if necessary.
*/
public static void addParamJavadoc(String parameterName, MethodDeclaration methodDeclaration,
ASTRewrite astRewrite, IJavaProject javaProject, TextEditGroup groupDescription) {
if (! shouldAddParamJavadoc(methodDeclaration))
return;
ListRewrite tagsRewrite= astRewrite.getListRewrite(methodDeclaration.getJavadoc(), Javadoc.TAGS_PROPERTY);
HashSet<String> leadingNames= new HashSet<>();
for (Iterator<SingleVariableDeclaration> iter= methodDeclaration.parameters().iterator(); iter.hasNext();) {
SingleVariableDeclaration curr= iter.next();
leadingNames.add(curr.getName().getIdentifier());
}
TagElement parameterTag= createParamTag(parameterName, astRewrite.getAST(), javaProject);
JavadocTagsSubProcessorCore.insertTag(tagsRewrite, parameterTag, leadingNames, groupDescription);
}
/**
* In a given list of tags, finds the one after which a first tag of type {@code tagName} should
* be inserted.
*
* @param tags existing tags
* @param tagName name of tag to add
* @return the <code>TagElement</code> just before the positions where a new
* <code>TagElement</code> with name <code>tagName</code> should be inserted, or
* <code>null</code> if it should be inserted as first.
*/
public static TagElement findTagElementToInsertAfter(List<TagElement> tags, String tagName) {
int goalOrdinal= tagOrder.indexOf(tagName);
if (goalOrdinal == -1) // unknown tag -> to end
return (tags.isEmpty()) ? null : (TagElement) tags.get(tags.size());
for (int i= 0; i < tags.size(); i++) {
int tagOrdinal= tagOrder.indexOf(tags.get(i).getTagName());
if (tagOrdinal >= goalOrdinal)
return (i == 0) ? null : (TagElement) tags.get(i - 1);
}
return (tags.isEmpty()) ? null : (TagElement) tags.get(tags.size() - 1);
}
/**
* In a given list of tags, finds the one before which a last tag of type {@code tagName} should
* be inserted.
*
* @param tags existing tags
* @param tagName name of tag to add
* @return the <code>TagElement</code> just after the positions where a new
* <code>TagElement</code> with name <code>tagName</code> should be inserted, or
* <code>null</code> if it should be inserted as last.
*/
public static TagElement findTagElementToInsertBefore(List<TagElement> tags, String tagName) {
int goalOrdinal= tagOrder.indexOf(tagName);
if (goalOrdinal == -1) // unknown tag -> to end
return (tags.isEmpty()) ? null : (TagElement) tags.get(tags.size());
for (int i= tags.size() - 1; i >= 0; i--) {
int tagOrdinal= tagOrder.indexOf(tags.get(i).getTagName());
if (tagOrdinal <= goalOrdinal)
return (i == tags.size() - 1) ? null : (TagElement) tags.get(i + 1);
}
return null;
}
}