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 */
020 package org.sonar.java.bytecode;
021
022 import org.sonar.java.bytecode.asm.AsmClass;
023 import org.sonar.java.bytecode.asm.AsmEdge;
024 import org.sonar.java.bytecode.asm.AsmField;
025 import org.sonar.java.bytecode.asm.AsmMethod;
026 import org.sonar.java.bytecode.asm.AsmResource;
027 import org.sonar.java.bytecode.visitor.BytecodeVisitor;
028 import org.sonar.squid.indexer.SquidIndex;
029
030 public class BytecodeVisitorNotifier {
031
032 private final AsmClass asmClass;
033 private final BytecodeVisitor[] bytecodeVisitors;
034
035 public BytecodeVisitorNotifier(AsmClass asmClass, BytecodeVisitor[] bytecodeVisitors) {
036 this.asmClass = asmClass;
037 this.bytecodeVisitors = new BytecodeVisitor[bytecodeVisitors.length];
038 System.arraycopy(bytecodeVisitors, 0, this.bytecodeVisitors, 0, bytecodeVisitors.length);
039 }
040
041 public void notifyVisitors(SquidIndex indexer) {
042 for (BytecodeVisitor visitor : bytecodeVisitors) {
043 visitor.setSquidIndex(indexer);
044 }
045 callVisitClass();
046 callVisitMethodAndFieldAndEdge();
047 callLeaveClass();
048 }
049
050 private void callVisitMethodAndFieldAndEdge() {
051 callVisitEdgeForSpecificAsmResource(asmClass);
052 for (AsmMethod method : asmClass.getMethods()) {
053 callVisitMethod(method);
054 callVisitEdgeForSpecificAsmResource(method);
055 }
056 for (AsmField field : asmClass.getFields()) {
057 callVisitField(field);
058 callVisitEdgeForSpecificAsmResource(field);
059 }
060 }
061
062 private void callVisitEdgeForSpecificAsmResource(AsmResource resource) {
063 for (AsmEdge edge : resource.getOutgoingEdges()) {
064 for (BytecodeVisitor visitor : bytecodeVisitors) {
065 visitor.visitEdge(edge);
066 }
067 }
068 }
069
070 private void callVisitMethod(AsmMethod asmMethod) {
071 for (BytecodeVisitor visitor : bytecodeVisitors) {
072 visitor.visitMethod(asmMethod);
073 }
074 }
075
076 private void callVisitField(AsmField asmField) {
077 for (BytecodeVisitor visitor : bytecodeVisitors) {
078 visitor.visitField(asmField);
079 }
080 }
081
082 private void callVisitClass() {
083 for (BytecodeVisitor visitor : bytecodeVisitors) {
084 visitor.visitClass(asmClass);
085 }
086 }
087
088 private void callLeaveClass() {
089 for (BytecodeVisitor visitor : bytecodeVisitors) {
090 visitor.leaveClass(asmClass);
091 }
092 }
093 }