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 */ 020package org.sonar.java.ast.visitor; 021 022import java.util.Arrays; 023import java.util.List; 024 025import org.sonar.squid.api.SourceClass; 026import org.sonar.squid.api.SourcePackage; 027import org.sonar.squid.measures.Metric; 028 029import com.puppycrawl.tools.checkstyle.api.DetailAST; 030import com.puppycrawl.tools.checkstyle.api.TokenTypes; 031 032public class ClassVisitor extends JavaAstVisitor { 033 034 public static final List<Integer> WANTED_TOKENS = Arrays.asList(TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF, TokenTypes.ENUM_DEF, 035 TokenTypes.ANNOTATION_DEF); 036 037 @Override 038 public List<Integer> getWantedTokens() { 039 return WANTED_TOKENS; 040 } 041 042 @Override 043 public void visitToken(DetailAST ast) { 044 String className = ast.findFirstToken(TokenTypes.IDENT).getText(); 045 SourceClass unit; 046 if (peekSourceCode().isType(SourceClass.class)) { 047 unit = createSourceClass((SourceClass) peekSourceCode(), className); 048 } else { 049 unit = createSourceClass(peekParentPackage(), className); 050 } 051 addSourceCode(unit); 052 unit.setStartAtLine(ast.getLineNo()); 053 unit.setMeasure(Metric.CLASSES, 1); 054 if (isInterface(ast.getType())) { 055 unit.setMeasure(Metric.INTERFACES, 1); 056 } 057 if (isAbstract(ast)) { 058 unit.setMeasure(Metric.ABSTRACT_CLASSES, 1); 059 } 060 unit.setSuppressWarnings(SuppressWarningsAnnotationUtils.isSuppressAllWarnings(ast)); 061 } 062 063 @Override 064 public void leaveToken(DetailAST ast) { 065 popSourceCode(); 066 } 067 068 private boolean isAbstract(DetailAST ast) { 069 final DetailAST abstractAST = ast.findFirstToken(TokenTypes.MODIFIERS).findFirstToken(TokenTypes.ABSTRACT); 070 return abstractAST != null; 071 } 072 073 private boolean isInterface(int type) { 074 return type == TokenTypes.INTERFACE_DEF; 075 } 076 077 static SourceClass createSourceClass(SourcePackage parentPackage, String className) { 078 StringBuilder key = new StringBuilder(); 079 if (parentPackage != null && !"".equals(parentPackage.getKey())) { 080 key.append(parentPackage.getKey()); 081 key.append("/"); 082 } 083 key.append(className); 084 return new SourceClass(key.toString(), className); 085 } 086 087 static SourceClass createSourceClass(SourceClass parentClass, String innerClassName) { 088 return new SourceClass(parentClass.getKey() + "$" + innerClassName, innerClassName); 089 } 090 091}