001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.java.ast.check;
021
022 import com.puppycrawl.tools.checkstyle.api.DetailAST;
023 import org.apache.commons.lang.StringUtils;
024 import org.sonar.api.utils.WildcardPattern;
025 import org.sonar.check.Priority;
026 import org.sonar.check.Rule;
027 import org.sonar.check.RuleProperty;
028 import org.sonar.java.PatternUtils;
029 import org.sonar.java.ast.visitor.JavaAstVisitor;
030 import org.sonar.java.ast.visitor.PublicApiVisitor;
031 import org.sonar.squid.api.*;
032
033 import java.util.List;
034
035 @Rule(key = "UndocumentedApi", priority = Priority.MAJOR)
036 public class UndocumentedApiCheck extends JavaAstVisitor {
037
038 @RuleProperty
039 private String forClasses = "";
040
041 private WildcardPattern[] patterns;
042
043 @Override
044 public List<Integer> getWantedTokens() {
045 return PublicApiVisitor.TOKENS;
046 }
047
048 @Override
049 public void visitToken(DetailAST ast) {
050 SourceCode currentResource = peekSourceCode();
051 SourceClass sourceClass = peekParentClass();
052 if (WildcardPattern.match(getPatterns(), sourceClass.getKey())) {
053 if (currentResource instanceof SourceMethod && ((SourceMethod) currentResource).isAccessor()) {
054 return;
055 }
056
057 if (PublicApiVisitor.isPublicApi(ast) && !PublicApiVisitor.isDocumentedApi(ast, getFileContents())) {
058 SourceFile sourceFile = currentResource.getParent(SourceFile.class);
059 CheckMessage message = new CheckMessage(this, "Avoid undocumented API.");
060 message.setLine(ast.getLineNo());
061 sourceFile.log(message);
062 }
063 }
064 }
065
066 private WildcardPattern[] getPatterns() {
067 if (patterns == null) {
068 patterns = PatternUtils.createPatterns(StringUtils.defaultIfEmpty(forClasses, "**"));
069 }
070 return patterns;
071 }
072
073 public String getForClasses() {
074 return forClasses;
075 }
076
077 public void setForClasses(String forClasses) {
078 this.forClasses = forClasses;
079 }
080
081 }