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.java.ast.visitor;
021    
022    import java.util.Arrays;
023    import java.util.List;
024    
025    import org.sonar.squid.api.SourceClass;
026    import org.sonar.squid.api.SourcePackage;
027    import org.sonar.squid.measures.Metric;
028    
029    import com.puppycrawl.tools.checkstyle.api.DetailAST;
030    import com.puppycrawl.tools.checkstyle.api.TokenTypes;
031    
032    public class ClassVisitor extends AstVisitor {
033    
034      private static final List<Integer> wantedTokens = 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 wantedTokens;
040      }
041    
042      @Override
043      public void visitToken(DetailAST ast) {
044        String className = ast.findFirstToken(TokenTypes.IDENT).getText();
045        SourceClass unit;
046        if (peekResource().isType(SourceClass.class)) {
047          unit = createSourceClass((SourceClass)peekResource(), className);
048        } else {
049          unit = createSourceClass(peekParentPackage(), className);
050        }
051        addResource(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      }
061    
062      @Override
063      public void leaveToken(DetailAST ast) {
064        popResource();
065      }
066    
067      private boolean isAbstract(DetailAST ast) {
068        final DetailAST abstractAST = ast.findFirstToken(TokenTypes.MODIFIERS).findFirstToken(TokenTypes.ABSTRACT);
069        return abstractAST != null;
070      }
071    
072      private boolean isInterface(int type) {
073        if (type == TokenTypes.INTERFACE_DEF) {
074          return true;
075        }
076        return false;
077      }
078    
079      static SourceClass createSourceClass(SourcePackage parentPackage, String className) {
080        StringBuilder key = new StringBuilder();
081        if (parentPackage != null && !parentPackage.getKey().equals("")) {
082          key.append(parentPackage.getKey());
083          key.append("/");
084        }
085        key.append(className);
086        return new SourceClass(key.toString(), className);
087      }
088    
089      static SourceClass createSourceClass(SourceClass parentClass, String innerClassName) {
090        return new SourceClass(parentClass.getKey() + "$" + innerClassName, innerClassName);
091      }
092    }