Debian Codegraph @

src/com/google/javascript/rhino/JSDocInfo.java

raw symbols SCIP
1 /*
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is Rhino code, released
17 * May 6, 1999.
18 *
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 1997-1999
22 * the Initial Developer. All Rights Reserved.
23 *
24 * Contributor(s):
25 * Bob Jervis
26 * Google Inc.
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU General Public License Version 2 or later (the "GPL"), in which
30 * case the provisions of the GPL are applicable instead of those above. If
31 * you wish to allow use of your version of this file only under the terms of
32 * the GPL and not to allow others to use your version of this file under the
33 * MPL, indicate your decision by deleting the provisions above and replacing
34 * them with the notice and other provisions required by the GPL. If you do
35 * not delete the provisions above, a recipient may use your version of this
36 * file under either the MPL or the GPL.
37 *
38 * ***** END LICENSE BLOCK ***** */
39
40 package com.google.javascript.rhino;
41
42 import com.google.common.base.Preconditions;
43 import com.google.common.collect.ImmutableList;
44 import com.google.common.collect.ImmutableSet;
45 import com.google.common.collect.Lists;
46 import com.google.common.collect.Sets;
47
48 import java.io.Serializable;
49 import java.util.Collection;
50 import java.util.Collections;
51 import java.util.LinkedHashMap;
52 import java.util.List;
53 import java.util.Map;
54 import java.util.Set;
55
56 /**
57 * <p>JSDoc information describing JavaScript code. JSDoc is represented as a
58 * unified object with fields for each JSDoc annotation, even though some
59 * combinations are incorrect. For instance, if a JSDoc describes an enum,
60 * it cannot have information about a return type. This implementation
61 * takes advantage of such incompatibilities to reuse fields for multiple
62 * purposes, reducing memory consumption.</p>
63 *
64 * <p>Constructing {@link JSDocInfo} objects is simplified by
65 * {@link JSDocInfoBuilder} which provides early incompatibility detection.</p>
66 *
67 */
68 public class JSDocInfo implements Serializable {
69 private static final long serialVersionUID = 1L;
70
71 /**
72 * Visibility categories. The {@link Visibility#ordinal()} can be used as a
73 * numerical indicator of privacy, where 0 is the most private. This means
74 * that the {@link Visibility#compareTo} method can be used to
75 * determine if a visibility is more permissive than another.
76 */
77 public enum Visibility {
78 PRIVATE,
79 PROTECTED,
80 PUBLIC,
81
82 // If visibility is not specified, we just assume that visibility
83 // is inherited from the super class.
84 INHERITED
85 }
86
87 private static final class LazilyInitializedInfo implements Serializable {
88 private static final long serialVersionUID = 1L;
89
90 // Function information
91 JSTypeExpression baseType = null;
92 List<JSTypeExpression> extendedInterfaces = null;
93 List<JSTypeExpression> implementedInterfaces = null;
94 Map<String, JSTypeExpression> parameters = null;
95 List<JSTypeExpression> thrownTypes = null;
96 ImmutableList<String> templateTypeNames = null;
97 ImmutableList<String> classTemplateTypeNames = null;
98
99 // Other information
100 String description = null;
101 String meaning = null;
102 String deprecated = null;
103 String license = null;
104 Set<String> suppressions = null;
105 Set<String> modifies = null;
106 String lendsName = null;
107 boolean ngInject = false;
108 }
109
110 private static final class LazilyInitializedDocumentation {
111 String sourceComment = null;
112 List<Marker> markers = null;
113
114 Map<String, String> parameters = null;
115 Map<JSTypeExpression, String> throwsDescriptions = null;
116 String blockDescription = null;
117 String fileOverview = null;
118 String returnDescription = null;
119 String version = null;
120
121 List<String> authors = null;
122 List<String> sees = null;
123 }
124
125 /**
126 * A piece of information (found in a marker) which contains a position
127 * with a string.
128 */
129 public static class StringPosition extends SourcePosition<String> {
130 }
131
132 /**
133 * A piece of information (found in a marker) which contains a position
134 * with a string that has no leading or trailing whitespace.
135 */
136 static class TrimmedStringPosition extends StringPosition {
137 @Override public void setItem(String item) {
138 Preconditions.checkArgument(
139 item.charAt(0) != ' ' &&
140 item.charAt(item.length() - 1) != ' ',
141 "String has leading or trailing whitespace");
142 super.setItem(item);
143 }
144 }
145
146 /**
147 * A piece of information (found in a marker) which contains a position
148 * with a name node.
149 */
150 public static class NamePosition extends SourcePosition<Node> {}
151
152 /**
153 * A piece of information (found in a marker) which contains a position
154 * with a type expression syntax tree.
155 */
156 public static class TypePosition extends SourcePosition<Node> {
157 private boolean brackets = false;
158
159 /** Returns whether the type has curly braces around it. */
160 public boolean hasBrackets() {
161 return brackets;
162 }
163
164 void setHasBrackets(boolean newVal) {
165 brackets = newVal;
166 }
167 }
168
169 /**
170 * Defines a class for containing the parsing information
171 * for this JSDocInfo. For each annotation found in the
172 * JsDoc, a marker will be created indicating the annotation
173 * itself, the name of the annotation (if any; for example,
174 * a @param has a name, but a @return does not), the
175 * textual description found on that annotation and, if applicable,
176 * the type declaration. All this information is only collected
177 * if documentation collection is turned on.
178 */
179 public static final class Marker {
180 private TrimmedStringPosition annotation = null;
181 private TrimmedStringPosition name = null;
182 private SourcePosition<Node> nameNode = null;
183 private StringPosition description = null;
184 private TypePosition type = null;
185
186 /**
187 * Gets the position information for the annotation name. (e.g., "param")
188 */
189 public StringPosition getAnnotation() {
190 return annotation;
191 }
192
193 void setAnnotation(TrimmedStringPosition p) {
194 annotation = p;
195 }
196
197 /**
198 * Gets the position information for the name found
199 * in a @param tag.
200 * @deprecated Use #getNameNode
201 */
202 @Deprecated
203 public StringPosition getName() {
204 return name;
205 }
206
207 void setName(TrimmedStringPosition p) {
208 name = p;
209 }
210
211 /**
212 * Gets the position information for the name found
213 * in an @param tag.
214 */
215 public SourcePosition<Node> getNameNode() {
216 return nameNode;
217 }
218
219 void setNameNode(SourcePosition<Node> p) {
220 nameNode = p;
221 }
222
223 /**
224 * Gets the position information for the description found
225 * in a block tag.
226 */
227 public StringPosition getDescription() {
228 return description;
229 }
230
231 void setDescription(StringPosition p) {
232 description = p;
233 }
234
235 /**
236 * Gets the position information for the type expression found
237 * in some block tags, like "@param" and "@return".
238 */
239 public TypePosition getType() {
240 return type;
241 }
242
243 void setType(TypePosition p) {
244 type = p;
245 }
246 }
247
248 private LazilyInitializedInfo info = null;
249
250 private LazilyInitializedDocumentation documentation = null;
251
252 // The Node this JSDoc is associated with.
253 private Node associatedNode = null;
254
255 private Visibility visibility = null;
256
257 /**
258 * The {@link #isConstant()}, {@link #isConstructor()}, {@link #isInterface},
259 * {@link #isHidden()} and {@link #shouldPreserveTry()} flags as well as
260 * whether the {@link #type} field stores a value for {@link #getType()},
261 * {@link #getReturnType()} or {@link #getEnumParameterType()}.
262 *
263 * @see #setFlag(boolean, int)
264 * @see #getFlag(int)
265 * @see #setType(JSTypeExpression, int)
266 * @see #getType(int)
267 */
268 private int bitset = 0x00;
269
270 /**
271 * The type for {@link #getType()}, {@link #getReturnType()} or
272 * {@link #getEnumParameterType()}. The knowledge of which one is recorded is
273 * stored in the {@link #bitset} field.
274 *
275 * @see #setType(JSTypeExpression, int)
276 * @see #getType(int)
277 */
278 private JSTypeExpression type = null;
279
280 /**
281 * The type for {@link #getThisType()}.
282 */
283 private JSTypeExpression thisType = null;
284
285 /**
286 * Whether to include documentation.
287 *
288 * @see JSDocInfo.LazilyInitializedDocumentation
289 */
290 private boolean includeDocumentation = false;
291
292 // We use a bit map to represent whether or not the JSDoc contains
293 // one of the "boolean" annotation types (annotations like @constructor,
294 // for which the presence of the annotation alone is significant).
295
296 // Mask all the boolean annotation types
297 private static final int MASK_FLAGS = 0x3FFFFFFF;
298
299 private static final int MASK_CONSTANT = 0x00000001; // @const
300 private static final int MASK_CONSTRUCTOR = 0x00000002; // @constructor
301 private static final int MASK_DEFINE = 0x00000004; // @define
302 private static final int MASK_HIDDEN = 0x00000008; // @hidden
303 private static final int MASK_PRESERVETRY = 0x00000010; // @preserveTry
304 private static final int MASK_NOCHECK = 0x00000020; // @notypecheck
305 private static final int MASK_OVERRIDE = 0x00000040; // @override
306 private static final int MASK_NOALIAS = 0x00000080; // @noalias
307 private static final int MASK_DEPRECATED = 0x00000100; // @deprecated
308 private static final int MASK_INTERFACE = 0x00000200; // @interface
309 private static final int MASK_EXPORT = 0x00000400; // @export
310 private static final int MASK_NOSHADOW = 0x00000800; // @noshadow
311 private static final int MASK_FILEOVERVIEW = 0x00001000; // @fileoverview
312 private static final int MASK_IMPLICITCAST = 0x00002000; // @implicitCast
313 private static final int MASK_NOSIDEEFFECTS = 0x00004000; // @nosideeffects
314 private static final int MASK_EXTERNS = 0x00008000; // @externs
315 private static final int MASK_JAVADISPATCH = 0x00010000; // @javadispatch
316 private static final int MASK_NOCOMPILE = 0x00020000; // @nocompile
317 private static final int MASK_CONSISTIDGEN = 0x00040000; // @consistentIdGenerator
318 private static final int MASK_IDGEN = 0x00080000; // @idGenerator
319 private static final int MASK_EXPOSE = 0x00100000; // @expose
320 private static final int MASK_STRUCT = 0x00200000; // @struct
321 private static final int MASK_DICT = 0x00400000; // @dict
322 private static final int MASK_STALBEIDGEN = 0x00800000; // @stableIdGenerator
323
324 // 3 bit type field stored in the top 3 bits of the most significant
325 // nibble.
326 private static final int MASK_TYPEFIELD = 0xE0000000; // 1110...
327 private static final int TYPEFIELD_TYPE = 0x20000000; // 0010...
328 private static final int TYPEFIELD_RETURN = 0x40000000; // 0100...
329 private static final int TYPEFIELD_ENUM = 0x60000000; // 0110...
330 private static final int TYPEFIELD_TYPEDEF = 0x80000000; // 1000...
331
332 /**
333 * Creates a {@link JSDocInfo} object. This object should be created using
334 * a {@link JSDocInfoBuilder}.
335 */
336 JSDocInfo(boolean includeDocumentation) {
337 this.includeDocumentation = includeDocumentation;
338 }
339
340 // Visible for testing.
341 public JSDocInfo() {}
342
343 void setConsistentIdGenerator(boolean value) {
344 setFlag(value, MASK_CONSISTIDGEN);
345 }
346
347 void setStableIdGenerator(boolean value) {
348 setFlag(value, MASK_STALBEIDGEN);
349 }
350
351 void setConstant(boolean value) {
352 setFlag(value, MASK_CONSTANT);
353 }
354
355 void setConstructor(boolean value) {
356 setFlag(value, MASK_CONSTRUCTOR);
357 }
358
359 void setStruct() {
360 setFlag(true, MASK_STRUCT);
361 }
362
363 void setDict() {
364 setFlag(true, MASK_DICT);
365 }
366
367 void setDefine(boolean value) {
368 setFlag(value, MASK_DEFINE);
369 }
370
371 void setHidden(boolean value) {
372 setFlag(value, MASK_HIDDEN);
373 }
374
375 void setNoCheck(boolean value) {
376 setFlag(value, MASK_NOCHECK);
377 }
378
379 void setShouldPreserveTry(boolean value) {
380 setFlag(value, MASK_PRESERVETRY);
381 }
382
383 void setOverride(boolean value) {
384 setFlag(value, MASK_OVERRIDE);
385 }
386
387 void setNoAlias(boolean value) {
388 setFlag(value, MASK_NOALIAS);
389 }
390
391 // Visible for testing.
392 public void setDeprecated(boolean value) {
393 setFlag(value, MASK_DEPRECATED);
394 }
395
396 void setInterface(boolean value) {
397 setFlag(value, MASK_INTERFACE);
398 }
399
400 void setExport(boolean value) {
401 setFlag(value, MASK_EXPORT);
402 }
403
404 void setExpose(boolean value) {
405 setFlag(value, MASK_EXPOSE);
406 }
407
408 void setNoShadow(boolean value) {
409 setFlag(value, MASK_NOSHADOW);
410 }
411
412 void setIdGenerator(boolean value) {
413 setFlag(value, MASK_IDGEN);
414 }
415
416 void setImplicitCast(boolean value) {
417 setFlag(value, MASK_IMPLICITCAST);
418 }
419
420 void setNoSideEffects(boolean value) {
421 setFlag(value, MASK_NOSIDEEFFECTS);
422 }
423
424 void setExterns(boolean value) {
425 setFlag(value, MASK_EXTERNS);
426 }
427
428 void setJavaDispatch(boolean value) {
429 setFlag(value, MASK_JAVADISPATCH);
430 }
431
432 void setNoCompile(boolean value) {
433 setFlag(value, MASK_NOCOMPILE);
434 }
435
436 private void setFlag(boolean value, int mask) {
437 if (value) {
438 bitset |= mask;
439 } else {
440 bitset &= ~mask;
441 }
442 }
443
444 /**
445 * @return whether the {@code @consistentIdGenerator} is present on
446 * this {@link JSDocInfo}
447 */
448 public boolean isConsistentIdGenerator() {
449 return getFlag(MASK_CONSISTIDGEN);
450 }
451
452 /**
453 * @return whether the {@code @stableIdGenerator} is present on this {@link JSDocInfo}.
454 */
455 public boolean isStableIdGenerator() {
456 return getFlag(MASK_STALBEIDGEN);
457 }
458
459 /**
460 * Returns whether the {@code @const} annotation is present on this
461 * {@link JSDocInfo}.
462 */
463 public boolean isConstant() {
464 return getFlag(MASK_CONSTANT) || isDefine();
465 }
466
467 /**
468 * Returns whether the {@code @constructor} annotation is present on this
469 * {@link JSDocInfo}.
470 */
471 public boolean isConstructor() {
472 return getFlag(MASK_CONSTRUCTOR);
473 }
474
475 /**
476 * Returns whether the {@code @struct} annotation is present on this
477 * {@link JSDocInfo}.
478 */
479 public boolean makesStructs() {
480 return getFlag(MASK_STRUCT);
481 }
482
483 /**
484 * Returns whether the {@code @dict} annotation is present on this
485 * {@link JSDocInfo}.
486 */
487 public boolean makesDicts() {
488 return getFlag(MASK_DICT);
489 }
490
491 /**
492 * Returns whether the {@code @define} annotation is present on this
493 * {@link JSDocInfo}. If this annotation is present, then the
494 * {@link #getType()} method will retrieve the define type.
495 */
496 public boolean isDefine() {
497 return getFlag(MASK_DEFINE);
498 }
499
500 /**
501 * Returns whether the {@code @hidden} annotation is present on this
502 * {@link JSDocInfo}.
503 */
504 public boolean isHidden() {
505 return getFlag(MASK_HIDDEN);
506 }
507
508 /**
509 * Returns whether the {@code @nocheck} annotation is present on this
510 * {@link JSDocInfo}.
511 */
512 public boolean isNoTypeCheck() {
513 return getFlag(MASK_NOCHECK);
514 }
515
516 /**
517 * Returns whether the {@code @preserveTry} annotation is present on this
518 * {@link JSDocInfo}.
519 */
520 public boolean shouldPreserveTry() {
521 return getFlag(MASK_PRESERVETRY);
522 }
523
524 /**
525 * Returns whether the {@code @override} annotation is present on this
526 * {@link JSDocInfo}.
527 */
528 public boolean isOverride() {
529 return getFlag(MASK_OVERRIDE);
530 }
531
532 /**
533 * Returns whether the {@code @noalias} annotation is present on this
534 * {@link JSDocInfo}.
535 */
536 public boolean isNoAlias() {
537 return getFlag(MASK_NOALIAS);
538 }
539
540 /**
541 * Returns whether the {@code @deprecated} annotation is present on this
542 * {@link JSDocInfo}.
543 */
544 public boolean isDeprecated() {
545 return getFlag(MASK_DEPRECATED);
546 }
547
548 /**
549 * Returns whether the {@code @interface} annotation is present on this
550 * {@link JSDocInfo}.
551 */
552 public boolean isInterface() {
553 return getFlag(MASK_INTERFACE);
554 }
555
556 /**
557 * Returns whether the {@code @export} annotation is present on this
558 * {@link JSDocInfo}.
559 */
560 public boolean isExport() {
561 return getFlag(MASK_EXPORT);
562 }
563
564 /**
565 * Returns whether the {@code @expose} annotation is present on this
566 * {@link JSDocInfo}.
567 */
568 public boolean isExpose() {
569 return getFlag(MASK_EXPOSE);
570 }
571
572 /**
573 * Returns whether the {@code @noshadow} annotation is present on this
574 * {@link JSDocInfo}.
575 */
576 public boolean isNoShadow() {
577 return getFlag(MASK_NOSHADOW);
578 }
579
580 /**
581 * @return whether the {@code @idGenerator} is present on
582 * this {@link JSDocInfo}
583 */
584 public boolean isIdGenerator() {
585 return getFlag(MASK_IDGEN);
586 }
587
588 /**
589 * Returns whether the {@code @implicitCast} annotation is present on this
590 * {@link JSDocInfo}.
591 */
592 public boolean isImplicitCast() {
593 return getFlag(MASK_IMPLICITCAST);
594 }
595
596 /**
597 * Returns whether the {@code @nosideeffects} annotation is present on this
598 * {@link JSDocInfo}.
599 */
600 public boolean isNoSideEffects() {
601 return getFlag(MASK_NOSIDEEFFECTS);
602 }
603
604 /**
605 * Returns whether the {@code @externs} annotation is present on this
606 * {@link JSDocInfo}.
607 */
608 public boolean isExterns() {
609 return getFlag(MASK_EXTERNS);
610 }
611
612 /**
613 * Returns whether the {@code @javadispatch} annotation is present on this
614 * {@link JSDocInfo}.
615 */
616 public boolean isJavaDispatch() {
617 return getFlag(MASK_JAVADISPATCH);
618 }
619
620 /**
621 * Returns whether the {@code @nocompile} annotation is present on this
622 * {@link JSDocInfo}.
623 */
624 public boolean isNoCompile() {
625 return getFlag(MASK_NOCOMPILE);
626 }
627
628 /**
629 * @return Whether there is declaration present on this {@link JSDocInfo}.
630 */
631 public boolean containsDeclaration() {
632 return (hasType()
633 || hasReturnType()
634 || hasEnumParameterType()
635 || hasTypedefType()
636 || hasThisType()
637 || getParameterCount() > 0
638 || getFlag(MASK_CONSTANT
639 | MASK_CONSTRUCTOR
640 | MASK_DEFINE
641 | MASK_OVERRIDE
642 | MASK_NOALIAS
643 | MASK_DEPRECATED
644 | MASK_INTERFACE
645 | MASK_NOSHADOW
646 | MASK_IMPLICITCAST
647 | MASK_NOSIDEEFFECTS));
648 }
649
650 private boolean getFlag(int mask) {
651 return (bitset & mask) != 0x00;
652 }
653
654 // Visible for testing.
655 public void setVisibility(Visibility visibility) {
656 this.visibility = visibility;
657 }
658
659 private void lazyInitInfo() {
660 if (info == null) {
661 info = new LazilyInitializedInfo();
662 }
663 }
664
665 /**
666 * Lazily initializes the documentation information object, but only
667 * if the JSDocInfo was told to keep such information around.
668 */
669 private boolean lazyInitDocumentation() {
670 if (!includeDocumentation) {
671 return false;
672 }
673
674 if (documentation == null) {
675 documentation = new LazilyInitializedDocumentation();
676 }
677
678 return true;
679 }
680
681 /**
682 * Adds a marker to the documentation (if it exists) and
683 * returns the marker. Returns null otherwise.
684 */
685 Marker addMarker() {
686 if (!lazyInitDocumentation()) {
687 return null;
688 }
689
690 if (documentation.markers == null) {
691 documentation.markers = Lists.newArrayList();
692 }
693
694 Marker marker = new Marker();
695 documentation.markers.add(marker);
696 return marker;
697 }
698
699 /**
700 * Sets the deprecation reason.
701 *
702 * @param reason The deprecation reason
703 */
704 boolean setDeprecationReason(String reason) {
705 lazyInitInfo();
706
707 if (info.deprecated != null) {
708 return false;
709 }
710
711 info.deprecated = reason;
712 return true;
713 }
714
715 /**
716 * Add a suppressed warning.
717 */
718 public void addSuppression(String suppression) {
719 lazyInitInfo();
720
721 if (info.suppressions == null) {
722 info.suppressions = Sets.newHashSet();
723 }
724 info.suppressions.add(suppression);
725 }
726
727 /**
728 * Sets suppressed warnings.
729 * @param suppressions A list of suppressed warning types.
730 */
731 boolean setSuppressions(Set<String> suppressions) {
732 lazyInitInfo();
733
734 if (info.suppressions != null) {
735 return false;
736 }
737
738 info.suppressions = suppressions;
739 return true;
740 }
741
742 /**
743 * Add modifies values.
744 */
745 void addModifies(String modifies) {
746 lazyInitInfo();
747
748 if (info.modifies == null) {
749 info.modifies = Sets.newHashSet();
750 }
751 info.modifies.add(modifies);
752 }
753
754 /**
755 * Sets modifies values.
756 * @param modifies A list of modifies types.
757 */
758 boolean setModifies(Set<String> modifies) {
759 lazyInitInfo();
760
761 if (info.modifies != null) {
762 return false;
763 }
764
765 info.modifies = modifies;
766 return true;
767 }
768
769 /**
770 * Documents the version.
771 */
772 boolean documentVersion(String version) {
773 if (!lazyInitDocumentation()) {
774 return true;
775 }
776
777 if (documentation.version != null) {
778 return false;
779 }
780
781 documentation.version = version;
782 return true;
783 }
784
785 /**
786 * Documents a reference (i.e. adds a "see" reference to the list).
787 */
788 boolean documentReference(String reference) {
789 if (!lazyInitDocumentation()) {
790 return true;
791 }
792
793 if (documentation.sees == null) {
794 documentation.sees = Lists.newArrayList();
795 }
796
797 documentation.sees.add(reference);
798 return true;
799 }
800
801 /**
802 * Documents the author (i.e. adds it to the author list).
803 */
804 boolean documentAuthor(String author) {
805 if (!lazyInitDocumentation()) {
806 return true;
807 }
808
809 if (documentation.authors == null) {
810 documentation.authors = Lists.newArrayList();
811 }
812
813 documentation.authors.add(author);
814 return true;
815 }
816
817 /**
818 * Documents the throws (i.e. adds it to the throws list).
819 */
820 boolean documentThrows(JSTypeExpression type, String throwsDescription) {
821 if (!lazyInitDocumentation()) {
822 return true;
823 }
824
825 if (documentation.throwsDescriptions == null) {
826 documentation.throwsDescriptions =
827 new LinkedHashMap<JSTypeExpression, String>();
828 }
829
830 if (!documentation.throwsDescriptions.containsKey(type)) {
831 documentation.throwsDescriptions.put(type, throwsDescription);
832 return true;
833 }
834
835 return false;
836 }
837
838
839 /**
840 * Documents a parameter. Parameters are described using the {@code @param}
841 * annotation.
842 *
843 * @param parameter the parameter's name
844 * @param description the parameter's description
845 */
846 boolean documentParam(String parameter, String description) {
847 if (!lazyInitDocumentation()) {
848 return true;
849 }
850
851 if (documentation.parameters == null) {
852 documentation.parameters = new LinkedHashMap<String, String>();
853 }
854
855 if (!documentation.parameters.containsKey(parameter)) {
856 documentation.parameters.put(parameter, description);
857 return true;
858 } else {
859 return false;
860 }
861 }
862
863 /**
864 * Documents the block-level comment/description.
865 *
866 * @param description the description
867 */
868 boolean documentBlock(String description) {
869 if (!lazyInitDocumentation()) {
870 return true;
871 }
872
873 if (documentation.blockDescription != null) {
874 return false;
875 }
876
877 documentation.blockDescription = description;
878 return true;
879 }
880
881 /**
882 * Documents the fileoverview comment/description.
883 *
884 * @param description the description
885 */
886 boolean documentFileOverview(String description) {
887 setFlag(true, MASK_FILEOVERVIEW);
888 if (!lazyInitDocumentation()) {
889 return true;
890 }
891
892 if (documentation.fileOverview != null) {
893 return false;
894 }
895
896 documentation.fileOverview = description;
897 return true;
898 }
899
900 /**
901 * Documents the return value. Return value is described using the
902 * {@code @return} annotation.
903 *
904 * @param description the return value's description
905 */
906 boolean documentReturn(String description) {
907 if (!lazyInitDocumentation()) {
908 return true;
909 }
910
911 if (documentation.returnDescription != null) {
912 return false;
913 }
914
915 documentation.returnDescription = description;
916 return true;
917 }
918
919 /**
920 * Declares a parameter. Parameters are described using the {@code @param}
921 * annotation.
922 *
923 * @param jsType the parameter's type, it may be {@code null} when the
924 * {@code @param} annotation did not specify a type.
925 * @param parameter the parameter's name
926 */
927 boolean declareParam(JSTypeExpression jsType, String parameter) {
928 lazyInitInfo();
929 if (info.parameters == null) {
930 info.parameters = new LinkedHashMap<String, JSTypeExpression>();
931 }
932 if (!info.parameters.containsKey(parameter)) {
933 info.parameters.put(parameter, jsType);
934 return true;
935 } else {
936 return false;
937 }
938 }
939
940 /**
941 * Declares a template type name. Template type names are described using the
942 * {@code @template} annotation.
943 *
944 * @param templateTypeNames the template type name.
945 */
946 boolean declareTemplateTypeNames(List<String> templateTypeNames) {
947 lazyInitInfo();
948
949 if (info.templateTypeNames != null) {
950 return false;
951 }
952
953 info.templateTypeNames = ImmutableList.copyOf(templateTypeNames);
954 return true;
955 }
956
957 /**
958 * Declares a template type name. Template type names are described using the
959 * {@code @template} annotation.
960 *
961 * @param templateTypeNames the template type name.
962 */
963 boolean declareClassTemplateTypeNames(List<String> templateTypeNames) {
964 lazyInitInfo();
965
966 if (info.classTemplateTypeNames != null) {
967 return false;
968 }
969
970 info.classTemplateTypeNames = ImmutableList.copyOf(templateTypeNames);
971 return true;
972 }
973
974 /**
975 * Declares that the method throws a given type.
976 *
977 * @param jsType The type that can be thrown by the method.
978 */
979 boolean declareThrows(JSTypeExpression jsType) {
980 lazyInitInfo();
981
982 if (info.thrownTypes == null) {
983 info.thrownTypes = Lists.newArrayList();
984 }
985
986 info.thrownTypes.add(jsType);
987 return true;
988 }
989
990 /**
991 * Gets the visibility specified by {@code @private}, {@code @protected} or
992 * {@code @public} annotation. If no visibility is specified, visibility
993 * is inherited from the base class.
994 */
995 public Visibility getVisibility() {
996 return visibility;
997 }
998
999 /**
1000 * Gets the parameter type.
1001 * @param parameter the parameter's name
1002 * @return the parameter's type or {@code null} if this parameter is not
1003 * defined or has a {@code null} type
1004 */
1005 public JSTypeExpression getParameterType(String parameter) {
1006 if (info == null || info.parameters == null) {
1007 return null;
1008 }
1009 return info.parameters.get(parameter);
1010 }
1011
1012 /**
1013 * Returns whether the parameter is defined.
1014 */
1015 public boolean hasParameter(String parameter) {
1016 if (info == null || info.parameters == null) {
1017 return false;
1018 }
1019 return info.parameters.containsKey(parameter);
1020 }
1021
1022 /**
1023 * Returns whether the parameter has an attached type.
1024 *
1025 * @return {@code true} if the parameter has an attached type, {@code false}
1026 * if the parameter has no attached type or does not exist.
1027 */
1028 public boolean hasParameterType(String parameter) {
1029 return getParameterType(parameter) != null;
1030 }
1031
1032 /**
1033 * Returns the set of names of the defined parameters. The iteration order
1034 * of the returned set is not the order in which parameters are defined.
1035 *
1036 * @return the set of names of the defined parameters. The returned set is
1037 * immutable.
1038 */
1039 public Set<String> getParameterNames() {
1040 if (info == null || info.parameters == null) {
1041 return ImmutableSet.of();
1042 }
1043 return ImmutableSet.copyOf(info.parameters.keySet());
1044 }
1045
1046 /**
1047 * Gets the number of parameters defined.
1048 */
1049 public int getParameterCount() {
1050 if (info == null || info.parameters == null) {
1051 return 0;
1052 }
1053 return info.parameters.size();
1054 }
1055
1056 void setType(JSTypeExpression type) {
1057 setType(type, TYPEFIELD_TYPE);
1058 }
1059
1060 void setReturnType(JSTypeExpression type) {
1061 setType(type, TYPEFIELD_RETURN);
1062 }
1063
1064 void setEnumParameterType(JSTypeExpression type) {
1065 setType(type, TYPEFIELD_ENUM);
1066 }
1067
1068 void setTypedefType(JSTypeExpression type) {
1069 setType(type, TYPEFIELD_TYPEDEF);
1070 }
1071
1072 private void setType(JSTypeExpression type, int mask) {
1073 if ((bitset & MASK_TYPEFIELD) != 0) {
1074 throw new IllegalStateException(
1075 "API tried to add two incompatible type tags. " +
1076 "This should have been blocked and emitted a warning.");
1077 }
1078 this.bitset = (bitset & MASK_FLAGS) | mask;
1079 this.type = type;
1080 }
1081
1082 /**
1083 * Returns the list of thrown types.
1084 */
1085 public List<JSTypeExpression> getThrownTypes() {
1086 if (info == null || info.thrownTypes == null) {
1087 return ImmutableList.of();
1088 }
1089 return Collections.unmodifiableList(info.thrownTypes);
1090 }
1091
1092 /**
1093 * Returns whether a type, specified using the {@code @type} annotation, is
1094 * present on this JSDoc.
1095 */
1096 public boolean hasType() {
1097 return hasType(TYPEFIELD_TYPE);
1098 }
1099
1100 /**
1101 * Returns whether an enum parameter type, specified using the {@code @enum}
1102 * annotation, is present on this JSDoc.
1103 */
1104 public boolean hasEnumParameterType() {
1105 return hasType(TYPEFIELD_ENUM);
1106 }
1107
1108 /**
1109 * Returns whether a typedef parameter type, specified using the
1110 * {@code @typedef} annotation, is present on this JSDoc.
1111 */
1112 public boolean hasTypedefType() {
1113 return hasType(TYPEFIELD_TYPEDEF);
1114 }
1115
1116 /**
1117 * Returns whether this {@link JSDocInfo} contains a type for {@code @return}
1118 * annotation.
1119 */
1120 public boolean hasReturnType() {
1121 return hasType(TYPEFIELD_RETURN);
1122 }
1123
1124 private boolean hasType(int mask) {
1125 return (bitset & MASK_TYPEFIELD) == mask;
1126 }
1127
1128 /**
1129 * Gets the type specified by the {@code @type} annotation.
1130 */
1131 public JSTypeExpression getType() {
1132 return getType(TYPEFIELD_TYPE);
1133 }
1134
1135 /**
1136 * Gets the return type specified by the {@code @return} annotation.
1137 */
1138 public JSTypeExpression getReturnType() {
1139 return getType(TYPEFIELD_RETURN);
1140 }
1141
1142 /**
1143 * Gets the enum parameter type specified by the {@code @enum} annotation.
1144 */
1145 public JSTypeExpression getEnumParameterType() {
1146 return getType(TYPEFIELD_ENUM);
1147 }
1148
1149 /**
1150 * Gets the typedef type specified by the {@code @type} annotation.
1151 */
1152 public JSTypeExpression getTypedefType() {
1153 return getType(TYPEFIELD_TYPEDEF);
1154 }
1155
1156 private JSTypeExpression getType(int typefield) {
1157 if ((MASK_TYPEFIELD & bitset) == typefield) {
1158 return type;
1159 } else {
1160 return null;
1161 }
1162 }
1163
1164 /**
1165 * Gets the type specified by the {@code @this} annotation.
1166 */
1167 public JSTypeExpression getThisType() {
1168 return thisType;
1169 }
1170
1171 /**
1172 * Sets the type specified by the {@code @this} annotation.
1173 */
1174 void setThisType(JSTypeExpression type) {
1175 this.thisType = type;
1176 }
1177
1178 /**
1179 * Returns whether this {@link JSDocInfo} contains a type for {@code @this}
1180 * annotation.
1181 */
1182 public boolean hasThisType() {
1183 return thisType != null;
1184 }
1185
1186 void setBaseType(JSTypeExpression type) {
1187 lazyInitInfo();
1188 info.baseType = type;
1189 }
1190
1191 /**
1192 * Gets the base type specified by the {@code @extends} annotation.
1193 */
1194 public JSTypeExpression getBaseType() {
1195 return (info == null) ? null : info.baseType;
1196 }
1197
1198 /**
1199 * Gets the description specified by the {@code @desc} annotation.
1200 */
1201 public String getDescription() {
1202 return (info == null) ? null : info.description;
1203 }
1204
1205 void setDescription(String desc) {
1206 lazyInitInfo();
1207 info.description = desc;
1208 }
1209
1210 /**
1211 * Gets the meaning specified by the {@code @meaning} annotation.
1212 *
1213 * In localization systems, two messages with the same content but
1214 * different "meanings" may be translated differently. By default, we
1215 * use the name of the variable that the message is initialized to as
1216 * the "meaning" of the message.
1217 *
1218 * But some code generators (like Closure Templates) inject their own
1219 * meaning with the jsdoc {@code @meaning} annotation.
1220 */
1221 public String getMeaning() {
1222 return (info == null) ? null : info.meaning;
1223 }
1224
1225 void setMeaning(String meaning) {
1226 lazyInitInfo();
1227 info.meaning = meaning;
1228 }
1229
1230 /**
1231 * Gets the name we're lending to in a {@code @lends} annotation.
1232 *
1233 * In many reflection APIs, you pass an anonymous object to a function,
1234 * and that function mixes the anonymous object into another object.
1235 * The {@code @lends} annotation allows the type system to track
1236 * those property assignments.
1237 */
1238 public String getLendsName() {
1239 return (info == null) ? null : info.lendsName;
1240 }
1241
1242 void setLendsName(String name) {
1243 lazyInitInfo();
1244 info.lendsName = name;
1245 }
1246
1247 /**
1248 * Returns whether JSDoc is annotated with {@code @ngInject} annotation.
1249 */
1250 public boolean isNgInject() {
1251 return (info == null) ? false : info.ngInject;
1252 }
1253
1254 void setNgInject(boolean ngInject) {
1255 lazyInitInfo();
1256 info.ngInject = ngInject;
1257 }
1258
1259 /**
1260 * Gets the description specified by the {@code @license} annotation.
1261 */
1262 public String getLicense() {
1263 return (info == null) ? null : info.license;
1264 }
1265
1266 /** License directives can appear in multiple comments, and always
1267 * apply to the entire file. Break protection and allow outsiders to
1268 * update the license string so that we can attach the license text even
1269 * when the JSDocInfo has been created and tagged with other information.
1270 * @param license String containing new license text.
1271 */
1272
1273 public void setLicense(String license) {
1274 lazyInitInfo();
1275 info.license = license;
1276 }
1277
1278 @Override
1279 public String toString() {
1280 return "JSDocInfo";
1281 }
1282
1283 /**
1284 * Returns whether this {@link JSDocInfo} contains a type for {@code @extends}
1285 * annotation.
1286 */
1287 public boolean hasBaseType() {
1288 return getBaseType() != null;
1289 }
1290
1291 /**
1292 * Adds an implemented interface. Returns whether the interface was added. If
1293 * the interface was already present in the list, it won't get added again.
1294 */
1295 boolean addImplementedInterface(JSTypeExpression interfaceName) {
1296 lazyInitInfo();
1297 if (info.implementedInterfaces == null) {
1298 info.implementedInterfaces = Lists.newArrayListWithCapacity(2);
1299 }
1300 if (info.implementedInterfaces.contains(interfaceName)) {
1301 return false;
1302 }
1303 info.implementedInterfaces.add(interfaceName);
1304 return true;
1305 }
1306
1307 /**
1308 * Returns the types specified by the {@code @implements} annotation.
1309 *
1310 * @return An immutable list of JSTypeExpression objects that can
1311 * be resolved to types.
1312 */
1313 public List<JSTypeExpression> getImplementedInterfaces() {
1314 if (info == null || info.implementedInterfaces == null) {
1315 return ImmutableList.of();
1316 }
1317 return Collections.unmodifiableList(info.implementedInterfaces);
1318 }
1319
1320 /**
1321 * Gets the number of interfaces specified by the {@code @implements}
1322 * annotation.
1323 */
1324 public int getImplementedInterfaceCount() {
1325 if (info == null || info.implementedInterfaces == null) {
1326 return 0;
1327 }
1328 return info.implementedInterfaces.size();
1329 }
1330
1331 /**
1332 * Adds an extended interface (for interface only).
1333 * Returns whether the type was added.
1334 * if the type was already present in the list, it won't get added again.
1335 */
1336 boolean addExtendedInterface(JSTypeExpression type) {
1337 lazyInitInfo();
1338 if (info.extendedInterfaces == null) {
1339 info.extendedInterfaces = Lists.newArrayListWithCapacity(2);
1340 }
1341 if (info.extendedInterfaces.contains(type)) {
1342 return false;
1343 }
1344 info.extendedInterfaces.add(type);
1345 return true;
1346 }
1347
1348 /**
1349 * Returns the interfaces extended by an interface
1350 *
1351 * @return An immutable list of JSTypeExpression objects that can
1352 * be resolved to types.
1353 */
1354 public List<JSTypeExpression> getExtendedInterfaces() {
1355 if (info == null || info.extendedInterfaces == null) {
1356 return ImmutableList.of();
1357 }
1358 return Collections.unmodifiableList(info.extendedInterfaces);
1359 }
1360
1361 /**
1362 * Gets the number of extended interfaces specified
1363 */
1364 public int getExtendedInterfacesCount() {
1365 if (info == null || info.extendedInterfaces == null) {
1366 return 0;
1367 }
1368 return info.extendedInterfaces.size();
1369 }
1370
1371 /**
1372 * Returns the deprecation reason or null if none specified.
1373 */
1374 public String getDeprecationReason() {
1375 return info == null ? null : info.deprecated;
1376 }
1377
1378 /**
1379 * Returns the set of suppressed warnings.
1380 */
1381 public Set<String> getSuppressions() {
1382 Set<String> suppressions = info == null ? null : info.suppressions;
1383 return suppressions == null ? Collections.<String>emptySet() : suppressions;
1384 }
1385
1386 /**
1387 * Returns the set of sideeffect notations.
1388 */
1389 public Set<String> getModifies() {
1390 Set<String> modifies = info == null ? null : info.modifies;
1391 return modifies == null ? Collections.<String>emptySet() : modifies;
1392 }
1393
1394 /**
1395 * Returns whether a description exists for the parameter with the specified
1396 * name.
1397 */
1398 public boolean hasDescriptionForParameter(String name) {
1399 if (documentation == null || documentation.parameters == null) {
1400 return false;
1401 }
1402
1403 return documentation.parameters.containsKey(name);
1404 }
1405
1406 /**
1407 * Returns the description for the parameter with the given name, if its
1408 * exists.
1409 */
1410 public String getDescriptionForParameter(String name) {
1411 if (documentation == null || documentation.parameters == null) {
1412 return null;
1413 }
1414
1415 return documentation.parameters.get(name);
1416 }
1417
1418 /**
1419 * Returns the list of authors or null if none.
1420 */
1421 public Collection<String> getAuthors() {
1422 return documentation == null ? null : documentation.authors;
1423 }
1424
1425 /**
1426 * Returns the list of references or null if none.
1427 */
1428 public Collection<String> getReferences() {
1429 return documentation == null ? null : documentation.sees;
1430 }
1431
1432 /**
1433 * Returns the version or null if none.
1434 */
1435 public String getVersion() {
1436 return documentation == null ? null : documentation.version;
1437 }
1438
1439 /**
1440 * Returns the description of the returned object or null if none specified.
1441 */
1442 public String getReturnDescription() {
1443 return documentation == null ? null : documentation.returnDescription;
1444 }
1445
1446 /**
1447 * Returns the block-level description or null if none specified.
1448 */
1449 public String getBlockDescription() {
1450 return documentation == null ? null : documentation.blockDescription;
1451 }
1452
1453 /**
1454 * Returns whether this has a fileoverview flag.
1455 */
1456 public boolean hasFileOverview() {
1457 return getFlag(MASK_FILEOVERVIEW);
1458 }
1459
1460 /**
1461 * Returns the file overview or null if none specified.
1462 */
1463 public String getFileOverview() {
1464 return documentation == null ? null : documentation.fileOverview;
1465 }
1466
1467 public Node getAssociatedNode() {
1468 return this.associatedNode;
1469 }
1470
1471 /**
1472 * Sets the node associated with this JSDoc.
1473 * Notice that many nodes may have pointer to the same JSDocInfo
1474 * object (because we propagate it across the type graph). But there
1475 * is only one canonical "owner" node of the JSDocInfo, which corresponds
1476 * to its original place in the syntax tree.
1477 */
1478 public void setAssociatedNode(Node node) {
1479 this.associatedNode = node;
1480 }
1481
1482 /** Gets the name of the source file that contains this JSDoc. */
1483 public String getSourceName() {
1484 return this.associatedNode != null
1485 ? this.associatedNode.getSourceFileName() : null;
1486 }
1487
1488 /** Gets the list of all markers for the documentation in this JSDoc. */
1489 public Collection<Marker> getMarkers() {
1490 return (documentation == null || documentation.markers == null)
1491 ? ImmutableList.<Marker>of() : documentation.markers;
1492 }
1493
1494 /** Gets the template type name. */
1495 public ImmutableList<String> getTemplateTypeNames() {
1496 if (info == null || info.templateTypeNames == null) {
1497 return ImmutableList.of();
1498 }
1499 return info.templateTypeNames;
1500 }
1501
1502 public ImmutableList<String> getClassTemplateTypeNames() {
1503 if (info == null || info.classTemplateTypeNames == null) {
1504 return ImmutableList.of();
1505 }
1506 return info.classTemplateTypeNames;
1507 }
1508
1509 /**
1510 * Returns a collection of all type nodes that are a part of this JSDocInfo.
1511 * This includes @type, @this, @extends, @implements, @param, @throws,
1512 * and @return. Any future type specific JSDoc should make sure to add the
1513 * appropriate nodes here.
1514 * @return collection of all type nodes
1515 */
1516 public Collection<Node> getTypeNodes() {
1517 List<Node> nodes = Lists.newArrayList();
1518
1519 if (type != null) {
1520 nodes.add(type.getRoot());
1521 }
1522
1523 if (thisType != null) {
1524 nodes.add(thisType.getRoot());
1525 }
1526
1527 if (info != null) {
1528 if (info.baseType != null) {
1529 nodes.add(info.baseType.getRoot());
1530 }
1531
1532 if (info.extendedInterfaces != null) {
1533 for (JSTypeExpression interfaceType : info.extendedInterfaces) {
1534 nodes.add(interfaceType.getRoot());
1535 }
1536 }
1537
1538 if (info.implementedInterfaces != null) {
1539 for (JSTypeExpression interfaceType : info.implementedInterfaces) {
1540 nodes.add(interfaceType.getRoot());
1541 }
1542 }
1543
1544 if (info.parameters != null) {
1545 for (JSTypeExpression parameterType : info.parameters.values()) {
1546 if (parameterType != null) {
1547 nodes.add(parameterType.getRoot());
1548 }
1549 }
1550 }
1551
1552 if (info.thrownTypes != null) {
1553 for (JSTypeExpression thrownType : info.thrownTypes) {
1554 if (thrownType != null) {
1555 nodes.add(thrownType.getRoot());
1556 }
1557 }
1558 }
1559 }
1560
1561 return nodes;
1562 }
1563
1564 public boolean hasModifies() {
1565 return info != null && info.modifies != null;
1566 }
1567
1568 /**
1569 * Returns the original JSDoc comment string. Returns null unless
1570 * parseJsDocDocumentation is enabled via the ParserConfig.
1571 */
1572 public String getOriginalCommentString() {
1573 return documentation == null ? null : documentation.sourceComment;
1574 }
1575
1576 void setOriginalCommentString(String sourceComment) {
1577 if (!lazyInitDocumentation()) {
1578 return;
1579 }
1580 documentation.sourceComment = sourceComment;
1581 }
1582 }