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.bytecode.visitor; 021 022import org.sonar.graph.DirectedGraph; 023import org.sonar.java.bytecode.asm.AsmClass; 024import org.sonar.java.bytecode.asm.AsmEdge; 025import org.sonar.squid.api.SourceClass; 026import org.sonar.squid.api.SourceCode; 027import org.sonar.squid.api.SourceCodeEdge; 028import org.sonar.squid.api.SourceCodeEdgeUsage; 029import org.sonar.squid.api.SourceFile; 030import org.sonar.squid.api.SourcePackage; 031import org.sonar.squid.measures.Metric; 032 033public class DependenciesVisitor extends BytecodeVisitor { 034 035 private SourceClass fromSourceClass; 036 private final DirectedGraph<SourceCode, SourceCodeEdge> graph; 037 038 public DependenciesVisitor(DirectedGraph<SourceCode, SourceCodeEdge> graph) { 039 this.graph = graph; 040 } 041 042 @Override 043 public void visitClass(AsmClass asmClass) { 044 this.fromSourceClass = getSourceClass(asmClass); 045 } 046 047 @Override 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, 076 SourceCodeEdge rootEdge) { 077 SourceCode fromParent = from.getParent(type); 078 SourceCode toParent = to.getParent(type); 079 SourceCodeEdge parentEdge = null; 080 if (canWeLinkNodes(fromParent, toParent) && rootEdge != null) { 081 if (graph.getEdge(fromParent, toParent) == null) { 082 parentEdge = new SourceCodeEdge(fromParent, toParent, SourceCodeEdgeUsage.USES); 083 parentEdge.addRootEdge(rootEdge); 084 graph.addEdge(parentEdge); 085 fromParent.add(Metric.CE, 1); 086 toParent.add(Metric.CA, 1); 087 } else { 088 parentEdge = graph.getEdge(fromParent, toParent); 089 if ( !parentEdge.hasAnEdgeFromRootNode(rootEdge.getFrom())) { 090 toParent.add(Metric.CA, 1); 091 } 092 if ( !parentEdge.hasAnEdgeToRootNode(rootEdge.getTo())) { 093 fromParent.add(Metric.CE, 1); 094 } 095 parentEdge.addRootEdge(rootEdge); 096 } 097 } 098 return parentEdge; 099 } 100 101 private boolean canWeLinkNodes(SourceCode from, SourceCode to) { 102 return from != null && to != null && !from.equals(to); 103 } 104 105}