All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.umd.cs.findbugs.detect.FindCircularDependencies Maven / Gradle / Ivy

There is a newer version: 4.8.6
Show newest version
/*
 * FindBugs - Find bugs in Java programs
 * Copyright (C) 2005 Dave Brosius 
 * Copyright (C) 2005 University of Maryland
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package edu.umd.cs.findbugs.detect;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import edu.umd.cs.findbugs.util.ClassName;
import org.apache.bcel.Const;
import org.apache.bcel.classfile.JavaClass;

import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.BugReporter;
import edu.umd.cs.findbugs.BytecodeScanningDetector;

public class FindCircularDependencies extends BytecodeScanningDetector {
    private HashMap> dependencyGraph = null;

    private final BugReporter bugReporter;

    private String clsName;

    public FindCircularDependencies(BugReporter bugReporter) {
        this.bugReporter = bugReporter;
        this.dependencyGraph = new HashMap<>();
    }

    @Override
    public void visit(JavaClass obj) {
        clsName = obj.getClassName();
    }

    @Override
    public void sawOpcode(int seen) {
        if ((seen == Const.INVOKESPECIAL) || (seen == Const.INVOKESTATIC) || (seen == Const.INVOKEVIRTUAL)) {
            String refClsName = getClassConstantOperand();
            refClsName = ClassName.toDottedClassName(refClsName);
            if (refClsName.startsWith("java")) {
                return;
            }

            if (clsName.equals(refClsName)) {
                return;
            }

            if (clsName.startsWith(refClsName) && (refClsName.indexOf('$') >= 0)) {
                return;
            }

            if (refClsName.startsWith(clsName) && (clsName.indexOf('$') >= 0)) {
                return;
            }

            Set dependencies = dependencyGraph.computeIfAbsent(clsName, k -> new HashSet<>());

            dependencies.add(refClsName);
        }
    }

    @Override
    public void report() {
        removeDependencyLeaves(dependencyGraph);

        LoopFinder lf = new LoopFinder();

        while (dependencyGraph.size() > 0) {
            String clsName = dependencyGraph.keySet().iterator().next();
            Set loop = lf.findLoop(dependencyGraph, clsName);
            boolean pruneLeaves;
            if (loop != null) {
                BugInstance bug = new BugInstance(this, "CD_CIRCULAR_DEPENDENCY", NORMAL_PRIORITY);
                for (String loopCls : loop) {
                    bug.addClass(loopCls);
                }
                bugReporter.reportBug(bug);
                pruneLeaves = removeLoopLinks(dependencyGraph, loop);
            } else {
                dependencyGraph.remove(clsName);
                pruneLeaves = true;
            }
            if (pruneLeaves) {
                removeDependencyLeaves(dependencyGraph);
            }
        }

        dependencyGraph.clear();
    }

    private void removeDependencyLeaves(Map> dependencyGraph) {
        boolean changed = true;
        while (changed) {
            changed = false;
            Iterator> it = dependencyGraph.values().iterator();
            while (it.hasNext()) {
                Set dependencies = it.next();

                boolean foundClass = false;
                Iterator dit = dependencies.iterator();
                while (dit.hasNext()) {
                    foundClass = dependencyGraph.containsKey(dit.next());
                    if (!foundClass) {
                        dit.remove();
                        changed = true;
                    }
                }
                if (dependencies.size() == 0) {
                    it.remove();
                    changed = true;
                }
            }
        }
    }

    private boolean removeLoopLinks(Map> dependencyGraph, Set loop) {
        Set dependencies = null;
        for (String clsName : loop) {
            if (dependencies != null) {
                dependencies.remove(clsName);
            }
            dependencies = dependencyGraph.get(clsName);
        }
        if (dependencies != null) {
            dependencies.remove(loop.iterator().next());
        }

        boolean removedClass = false;
        Iterator cIt = loop.iterator();
        while (cIt.hasNext()) {
            String clsName = cIt.next();
            dependencies = dependencyGraph.get(clsName);
            if (dependencies.size() == 0) {
                cIt.remove();
                removedClass = true;
            }
        }
        return removedClass;
    }

    static class LoopFinder {
        private Map> dGraph = null;

        private String startClass = null;

        private Set visited = null;

        private Set loop = null;

        public Set findLoop(Map> dependencyGraph, String startCls) {
            dGraph = dependencyGraph;
            startClass = startCls;
            visited = new HashSet<>();
            loop = new LinkedHashSet<>();
            if (findLoop(startClass)) {
                return loop;
            }
            return null;
        }

        private boolean findLoop(String curClass) {
            Set dependencies = dGraph.get(curClass);
            if (dependencies == null) {
                return false;
            }

            visited.add(curClass);
            loop.add(curClass);
            for (String depClass : dependencies) {
                if (depClass.equals(startClass)) {
                    return true;
                }

                if (visited.contains(depClass)) {
                    continue;
                }

                if (findLoop(depClass)) {
                    return true;
                }
            }
            loop.remove(curClass);
            return false;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy