001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2009 SonarSource SA
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.squid.ast.visitor;
021
022 import org.sonar.squid.api.AnalysisException;
023 import org.sonar.squid.api.SourceCode;
024
025 import antlr.collections.AST;
026
027 import com.puppycrawl.tools.checkstyle.api.DetailAST;
028 import com.puppycrawl.tools.checkstyle.api.Scope;
029 import com.puppycrawl.tools.checkstyle.api.ScopeUtils;
030 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
031
032 public class AstUtils {
033
034 public static AST findType(DetailAST ast) {
035 DetailAST typeAst = ast.findFirstToken(TokenTypes.TYPE);
036 if (typeAst != null) {
037 return typeAst.getFirstChild();
038 }
039 return null;
040 }
041
042 public static boolean isClassVariable(DetailAST ast) {
043 return ast.getType() == TokenTypes.VARIABLE_DEF && ast.getParent().getType() == TokenTypes.OBJBLOCK
044 && isClass(ast.getParent().getParent());
045 }
046
047 public static boolean isClass(DetailAST ast) {
048 return ast.getType() == TokenTypes.CLASS_DEF || ast.getType() == TokenTypes.ENUM_DEF || ast.getType() == TokenTypes.ANNOTATION_DEF;
049 }
050
051 public static boolean isInterfaceVariable(DetailAST ast) {
052 return ast.getType() == TokenTypes.VARIABLE_DEF && ast.getParent().getType() == TokenTypes.OBJBLOCK
053 && isInterface(ast.getParent().getParent());
054 }
055
056 public static boolean isInterface(DetailAST ast) {
057 return ast.getType() == TokenTypes.INTERFACE_DEF;
058 }
059
060 public static boolean isFinal(DetailAST detailAst) {
061 return isModifier(detailAst, TokenTypes.FINAL);
062 }
063
064 public static boolean isStatic(DetailAST detailAst) {
065 return isModifier(detailAst, TokenTypes.LITERAL_STATIC);
066 }
067
068 public static boolean isModifier(DetailAST detailAst, int modifierType) {
069 DetailAST modifiers = detailAst.findFirstToken(TokenTypes.MODIFIERS);
070 if (modifiers != null) {
071 boolean isModifierMatching = modifiers.branchContains(modifierType);
072 if (!isModifierMatching && isInterfaceVariable(detailAst)) {
073 // by default if not specified, a var def in an interface is
074 // public final static
075 return (modifierType == TokenTypes.LITERAL_STATIC || modifierType == TokenTypes.FINAL);
076 }
077 return isModifierMatching;
078 }
079 return false;
080 }
081
082 public static Scope getScope(DetailAST ast) {
083 DetailAST modifierAst = ast.findFirstToken(TokenTypes.MODIFIERS);
084 Scope found = modifierAst != null ? ScopeUtils.getScopeFromMods(modifierAst) : Scope.NOTHING;
085 if (found.compareTo(Scope.PACKAGE) == 0 && (ast.getType() == TokenTypes.METHOD_DEF || ast.getType() == TokenTypes.VARIABLE_DEF)) {
086 // check if we found a parent interface declaration
087 // interface methods or var defs are by default public when not
088 // specified
089 found = (isScope(Scope.PACKAGE, found) && findParent(ast, TokenTypes.INTERFACE_DEF) != null) ? Scope.PUBLIC : found;
090 }
091 return found;
092 }
093
094 public static boolean isScope(Scope toCompare, Scope scope) {
095 return scope.compareTo(toCompare) == 0;
096 }
097
098 public static boolean isType(DetailAST ast, int type) {
099 return ast.getType() == type;
100 }
101
102 public static DetailAST findParent(DetailAST ast, int tokenType) {
103 DetailAST parent = ast.getParent();
104 if (parent != null) {
105 return parent.getType() == tokenType ? parent : findParent(parent, tokenType);
106 }
107 return null;
108 }
109
110 public static void ensureResourceType(SourceCode resource, Class<? extends SourceCode> resourceType) {
111 if (!resource.isType(resourceType)) {
112 throw new AnalysisException("Resource " + resource.getKey() + " must be of type " + resourceType.getName());
113 }
114 }
115 }