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.bytecode.visitor;
021    
022    import org.sonar.java.bytecode.asm.AsmClass;
023    import org.sonar.java.bytecode.asm.AsmEdge;
024    import org.sonar.squid.api.SourceClass;
025    import org.sonar.squid.api.SourceCode;
026    import org.sonar.squid.api.SourceCodeEdge;
027    import org.sonar.squid.api.SourceCodeEdgeUsage;
028    import org.sonar.squid.api.SourceFile;
029    import org.sonar.squid.api.SourcePackage;
030    import org.sonar.graph.DirectedGraph;
031    import org.sonar.squid.indexer.SquidIndex;
032    import org.sonar.squid.measures.Metric;
033    
034    public class DependenciesVisitor extends BytecodeVisitor {
035    
036      private SourceClass fromSourceClass;
037      private final DirectedGraph<SourceCode, SourceCodeEdge> graph;
038    
039      public DependenciesVisitor(SquidIndex index, DirectedGraph<SourceCode, SourceCodeEdge> graph) {
040        super(index);
041        this.graph = graph;
042      }
043    
044      public void visitClass(AsmClass asmClass) {
045        this.fromSourceClass = getSourceClass(asmClass);
046      }
047    
048      public void visitEdge(AsmEdge edge) {
049        AsmClass toAsmClass = edge.getTargetAsmClass();
050        SourceClass toSourceClass = getSourceClass(toAsmClass);
051        switch (edge.getUsage()) {
052          case EXTENDS:
053            link(fromSourceClass, toSourceClass, SourceCodeEdgeUsage.EXTENDS);
054            break;
055          case IMPLEMENTS:
056            link(fromSourceClass, toSourceClass, SourceCodeEdgeUsage.IMPLEMENTS);
057            break;
058          default:
059            link(fromSourceClass, toSourceClass, SourceCodeEdgeUsage.USES);
060            break;
061        }
062      }
063    
064      private void link(SourceClass from, SourceClass to, SourceCodeEdgeUsage link) {
065        if (canWeLinkNodes(from, to) && graph.getEdge(from, to) == null) {
066          SourceCodeEdge edge = new SourceCodeEdge(from, to, link);
067          graph.addEdge(edge);
068          from.add(Metric.CE, 1);
069          to.add(Metric.CA, 1);
070          SourceCodeEdge fileEdge = createEdgeBetweenParents(SourceFile.class, from, to, edge);
071          createEdgeBetweenParents(SourcePackage.class, from, to, fileEdge);
072        }
073      }
074    
075      private SourceCodeEdge createEdgeBetweenParents(Class<? extends SourceCode> type, SourceClass from, SourceClass to, SourceCodeEdge rootEdge) {
076        SourceCode fromParent = from.getParent(type);
077        SourceCode toParent = to.getParent(type);
078        SourceCodeEdge parentEdge = null;
079        if (canWeLinkNodes(fromParent, toParent) && rootEdge!=null) {
080          if (graph.getEdge(fromParent, toParent) == null) {
081            parentEdge = new SourceCodeEdge(fromParent, toParent, SourceCodeEdgeUsage.USES);
082            parentEdge.addRootEdge(rootEdge);
083            graph.addEdge(parentEdge);
084            fromParent.add(Metric.CE, 1);
085            toParent.add(Metric.CA, 1);
086          } else {
087            parentEdge = graph.getEdge(fromParent, toParent);
088            if (!parentEdge.hasAnEdgeFromRootNode(rootEdge.getFrom())) {
089              toParent.add(Metric.CA, 1);
090            }
091            if (!parentEdge.hasAnEdgeToRootNode(rootEdge.getTo())) {
092              fromParent.add(Metric.CE, 1);
093            }
094            parentEdge.addRootEdge(rootEdge);
095          }
096        }
097        return parentEdge;
098      }
099    
100      private boolean canWeLinkNodes(SourceCode from, SourceCode to) {
101        return from != null && to != null && !from.equals(to);
102      }
103    
104    }