View Javadoc

1   package net.sourceforge.pmd.lang.java.rule.comments;
2   
3   import net.sourceforge.pmd.PropertySource;
4   import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
5   import net.sourceforge.pmd.lang.rule.properties.EnumeratedProperty;
6   /**
7    *
8    * @author Brian Remedios
9    */
10  public class CommentRequiredRule extends AbstractCommentRule {
11  
12  	enum CommentRequirement {
13  		Required("Required"),
14  		Ignored("Ignored"),
15  		Unwanted("Unwanted");
16  
17  		private final String label;
18  
19  		CommentRequirement(String theLabel) {
20  			label = theLabel;
21  		}
22  
23  		public static String[] labels() {
24  			String[] labels = new String[values().length];
25  			int i=0;
26  			for (CommentRequirement requirement : values()) {
27  				labels[i++] = requirement.label;
28  			}
29  			return labels;
30  		}
31  	}
32  
33      public static final EnumeratedProperty<CommentRequirement> HEADER_CMT_REQUIREMENT_DESCRIPTOR = new EnumeratedProperty<CommentRequirement>(
34      	    "headerCommentRequirement",
35      	    "Header comments",
36      	    CommentRequirement.labels(),
37      	    CommentRequirement.values(),
38      	    0, 1.0f
39      	    );
40  
41      public static final EnumeratedProperty<CommentRequirement> FIELD_CMT_REQUIREMENT_DESCRIPTOR = new EnumeratedProperty<CommentRequirement>(
42      	    "fieldCommentRequirement",
43      	    "Field comments",
44      	    CommentRequirement.labels(),
45      	    CommentRequirement.values(),
46      	    0, 2.0f
47      	    );
48  
49      public static final EnumeratedProperty<CommentRequirement> PUB_METHOD_CMT_REQUIREMENT_DESCRIPTOR = new EnumeratedProperty<CommentRequirement>(
50      	    "publicMethodCommentRequirement",
51      	    "Public method comments",
52      	    CommentRequirement.labels(),
53      	    CommentRequirement.values(),
54      	    0, 3.0f
55      	    );
56  
57      public static final EnumeratedProperty<CommentRequirement> PROT_METHOD_CMT_REQUIREMENT_DESCRIPTOR = new EnumeratedProperty<CommentRequirement>(
58      	    "protectedMethodCommentRequirement",
59      	    "Protected method comments",
60      	    CommentRequirement.labels(),
61      	    CommentRequirement.values(),
62      	    0, 4.0f
63      	    );
64  
65  	public CommentRequiredRule() {
66  		definePropertyDescriptor(HEADER_CMT_REQUIREMENT_DESCRIPTOR);
67  		definePropertyDescriptor(FIELD_CMT_REQUIREMENT_DESCRIPTOR);
68  		definePropertyDescriptor(PUB_METHOD_CMT_REQUIREMENT_DESCRIPTOR);
69  		definePropertyDescriptor(PROT_METHOD_CMT_REQUIREMENT_DESCRIPTOR);
70  	}
71  
72  	@Override
73      public Object visit(ASTCompilationUnit cUnit, Object data) {
74  
75  //		SortedMap<Integer, Object> itemsByLineNumber = orderedCommentsAndDeclarations(cUnit);
76  
77          return super.visit(cUnit, data);
78      }
79  
80  	public boolean allCommentsAreIgnored() {
81  
82  		return getProperty(HEADER_CMT_REQUIREMENT_DESCRIPTOR) == CommentRequirement.Ignored &&
83  			getProperty(FIELD_CMT_REQUIREMENT_DESCRIPTOR) == CommentRequirement.Ignored &&
84  			getProperty(PUB_METHOD_CMT_REQUIREMENT_DESCRIPTOR) == CommentRequirement.Ignored &&
85  			getProperty(PROT_METHOD_CMT_REQUIREMENT_DESCRIPTOR) == CommentRequirement.Ignored ;
86  	}
87  
88  	/**
89  	 * @see PropertySource#dysfunctionReason()
90  	 */
91  	@Override
92  	public String dysfunctionReason() {
93  		return allCommentsAreIgnored() ?
94  				"All comment types are ignored" :
95  				null;
96  	}
97  }