KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > javadoc > AutoCommenterConstructor


1 package org.netbeans.modules.tasklist.javadoc;
2
3 import java.util.ArrayList JavaDoc;
4 import org.openide.src.ConstructorElement;
5 import org.openide.src.ElementFormat;
6 import org.openide.src.Identifier;
7 import org.openide.src.JavaDoc;
8 import org.openide.src.JavaDocSupport;
9 import org.openide.src.JavaDocTag;
10 import org.openide.src.MethodParameter;
11 import org.openide.src.SourceException;
12 import org.openide.util.NbBundle;
13 import org.netbeans.modules.tasklist.javadoc.ext.JavaTagNames;
14
15 public class AutoCommenterConstructor extends AutoCommenterElement {
16     private static final String JavaDoc[] NOT_PERMITTED_TAGS = {
17         JavaTagNames.TAG_AUTHOR,
18         JavaTagNames.TAG_SERIAL,
19         JavaTagNames.TAG_SERIALFIELD,
20         JavaTagNames.TAG_VERSION,
21         JavaTagNames.TAG_RETURN
22     };
23
24     private static final ElementFormat nameFormat = new ElementFormat( "{m} {n} ( {p} )" ); // NOI18N
25

26     public AutoCommenterConstructor( ConstructorElement element ) {
27         super( element );
28     }
29
30     JavaDoc getJavaDoc() {
31         return ((ConstructorElement)srcElement).getJavaDoc();
32     }
33
34     String JavaDoc[] getNotPermittedTags() {
35         return NOT_PERMITTED_TAGS;
36     }
37
38     String JavaDoc typeToString() {
39         return "constructor"; // NOI18N
40
}
41
42     boolean elementTagsOk() {
43         return elementTagsOk( null, false );
44     }
45
46
47     boolean elementTagsOk( ArrayList JavaDoc correctedTags, boolean checkOnly ) {
48
49         boolean error = false;
50
51         // Check param tags
52

53         JavaDocTag.Param[] ptags = ((ConstructorElement)srcElement).getJavaDoc().getParamTags();
54         boolean[] ptags_found = new boolean[ ptags.length ];
55         MethodParameter[] params = ((ConstructorElement)srcElement).getParameters();
56         //boolean[] params_ok = new boolean[ params.length ];
57

58         // Check if all parameters for the method are in the javadoc
59
// and if there are any parameter tag duplicates.
60
for (int j = 0 ; j < params.length ; j++) {
61             boolean tagFound = false;
62             boolean duplicateTagAlreadyFound = false;
63             for (int i = 0 ; i < ptags.length ; i++) {
64                 if ( ptags[i].parameterName() != null && ptags[i].parameterName().equals( params[j].getName() ) ) {
65                     ptags_found[i] = true;
66                     if (!tagFound) {
67                         tagFound = true;
68                     } else if (! duplicateTagAlreadyFound) {
69                         if ( checkOnly ) {
70                             return false;
71                         }
72                         else if ( correctedTags == null ) {
73                             errorList.add(NbBundle.getMessage(AutoCommenter.class, "ERR_DuplicatedParamTag", params[j].getName()));
74                             resolutionList.add(NbBundle.getMessage(AutoCommenter.class, "FIX_DuplicatedParamTag", params[j].getName()));
75 // XXX Do a better job than simply picking the first one! List them here and let user pick!
76
}
77                         error = true;
78                         duplicateTagAlreadyFound = true;
79                     }
80                 }
81             }
82
83             if (! tagFound ) {
84                 if ( checkOnly ) {
85                     return false;
86                 } else if ( correctedTags == null ) {
87                     error = true;
88                     errorList.add(NbBundle.getMessage(AutoCommenter.class, "ERR_NoTagForParam", params[j].getName()));
89                     resolutionList.add(NbBundle.getMessage(AutoCommenter.class, "FIX_NoTagForParam", params[j].getName()));
90                 }
91                 else {
92                     correctedTags.add( JavaDocSupport.createParamTag( JavaTagNames.TAG_PARAM, params[j].getName() ) );
93                 }
94             }
95         }
96
97         for( int i = 0; i < ptags.length; i++ ) {
98             if ( !ptags_found[i] ) {
99                 if ( checkOnly ) {
100                     return false;
101                 }
102                 else if ( correctedTags == null ) {
103                     errorList.add( NbBundle.getMessage(AutoCommenter.class,"ERR_NoSuchParam", ptags[i].name(), ptags[i].parameterName()));
104                     resolutionList.add(NbBundle.getMessage(AutoCommenter.class, "FIX_NoSuchParam", ptags[i].name(), ptags[i].parameterName()));
105                 }
106                 error = true;
107             }
108             else if ( correctedTags != null ) {
109                 correctedTags.add( ptags[i] );
110             }
111
112
113         }
114         // Check throws tags
115

116
117         JavaDocTag.Throws[] ttags = ((ConstructorElement)srcElement).getJavaDoc().getThrowsTags();
118         boolean[] ttags_found = new boolean[ ttags.length ];
119         Identifier[] excs = ((ConstructorElement)srcElement).getExceptions();
120         //boolean[] excs_ok = new boolean[ excs.length ];
121

122         // Check if all exceptions for the method are in the javadoc
123
// and if there are any exception tag duplicates.
124
for (int j = 0 ; j < excs.length ; j++) {
125             boolean tagFound = false;
126             boolean duplicateTagAlreadyFound = false;
127             for (int i = 0 ; i < ttags.length ; i++) {
128                 // if ( ttags[i].exceptionName() != null && ttags[i].exceptionName().equals( excs[j].getName() ) ) {
129
Identifier tagExId = Identifier.create(ttags[i].exceptionName() );
130                 if ( tagExId != null && ( tagExId.compareTo( excs[j], false ) ||
131                           tagExId.getName().equals(excs[j].getName() ) ) ) {
132                     ttags_found[i] = true;
133                     if (!tagFound) {
134                         tagFound = true;
135                     } else if (! duplicateTagAlreadyFound){
136                         if ( checkOnly ) {
137                             return false;
138                         }
139                         else if ( correctedTags == null ) {
140                             errorList.add(NbBundle.getMessage(AutoCommenter.class, "ERR_DuplicatedExceptionTag", excs[j].getName()));
141                             resolutionList.add(NbBundle.getMessage(AutoCommenter.class, "FIX_DuplicatedExceptionTag", excs[j].getName()));
142                         }
143                         error = true;
144                         duplicateTagAlreadyFound = true;
145                     }
146                 }
147             }
148
149             if (! tagFound ) {
150                 if ( checkOnly ) {
151                     return false;
152                 } else if ( correctedTags == null ) {
153                     error = true;
154                     errorList.add( NbBundle.getMessage(AutoCommenter.class, "ERR_NoTagForException", excs[j].getName() ));
155                     resolutionList.add(NbBundle.getMessage(AutoCommenter.class, "FIX_NoTagForException", excs[j].getName()));
156                 }
157                 else {
158                     correctedTags.add( JavaDocSupport.createThrowsTag( JavaTagNames.TAG_THROWS, excs[j].getName() ) );
159                 }
160             }
161         }
162
163         for( int i = 0; i < ttags.length; i++ ) {
164             if ( !ttags_found[i] ) {
165                 if ( checkOnly ) {
166                     return false;
167                 }
168                 else if ( correctedTags == null ) {
169                     errorList.add( NbBundle.getMessage(AutoCommenter.class, "ERR_NoSuchException", ttags[i].name(), ttags[i].exceptionName()));
170                     resolutionList.add(NbBundle.getMessage(AutoCommenter.class, "FIX_NoSuchException", ttags[i].name(), ttags[i].exceptionName()));
171                 }
172                 error = true;
173             }
174             else if ( correctedTags != null ) {
175                 correctedTags.add( ttags[i] );
176             }
177
178
179         }
180
181         return !error;
182     }
183
184     boolean isCorrectable () {
185         if ( super.isCorrectable() )
186             return true;
187
188         return !elementTagsOk( null, true );
189     }
190
191     void autoCorrect() throws SourceException {
192         autoCorrect( getJavaDoc() );
193     }
194
195     void autoCorrect( JavaDoc jDoc ) throws SourceException {
196         JavaDoc.Method jdTemp = JavaDocSupport.createMethodJavaDoc(getJavaDoc().getRawText());
197
198         // create comment without throws and params
199

200         JavaDocTag tags[] = jdTemp.getTags();
201         ArrayList JavaDoc stdTags = new ArrayList JavaDoc( tags.length );
202
203         for( int i = 0; i < tags.length; i++ ) {
204             if ( !( tags[i] instanceof JavaDocTag.Param ) &&
205                     !( tags[i] instanceof JavaDocTag.Throws ) ) {
206                 stdTags.add( tags[i] );
207             }
208         }
209
210         // jdTemp.setRawText( generateRawText( jdTemp.getText(), stdTags ) );
211
jdTemp.changeTags( (JavaDocTag[])stdTags.toArray( new JavaDocTag[ stdTags.size() ] ), JavaDoc.SET );
212
213         super.autoCorrect( jdTemp );
214
215         ArrayList JavaDoc correctedTags = new ArrayList JavaDoc();
216         elementTagsOk( correctedTags, false );
217
218         // Build all tags collection
219

220         ArrayList JavaDoc allTags = new ArrayList JavaDoc( correctedTags.size() + tags.length );
221         tags = jdTemp.getTags();
222         for( int i = 0; i < tags.length; i++ ) {
223             allTags.add( tags[i] );
224         }
225         allTags.addAll( correctedTags );
226
227         /*
228         Iterator it = allTags.iterator();
229         while( it.hasNext() ) {
230           System.out.println( (JavaDocTag)it.next() );
231     }
232         */

233         //getJavaDoc().setRawText( generateRawText( jdTemp.getText(), allTags ) );
234
jDoc.changeTags( (JavaDocTag[])allTags.toArray( new JavaDocTag[ allTags.size() ] ), JavaDoc.SET );
235     }
236
237     ElementFormat getNameFormat () {
238         return nameFormat;
239     }
240 }
241
Popular Tags