/*
 * FindBugs - Find bugs in Java programs
 * Copyright (C) 2003, 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 edu.umd.cs.findbugs.*;
import java.util.*;
import java.io.PrintStream;
import org.apache.bcel.classfile.*;
import java.util.zip.*;
import java.io.*;
import edu.umd.cs.pugh.visitclass.Constants2;

public class InitializationChain extends BytecodeScanningDetector implements   Constants2 {
    Set<String> requires = new TreeSet<String>();
    Map<String, Set<String>> classRequires = new TreeMap<String, Set<String>>();
    private BugReporter bugReporter;

    private static final boolean DEBUG = Boolean.getBoolean("ic.debug");

    public InitializationChain(BugReporter bugReporter) {
	this.bugReporter = bugReporter;
    }

    public void visit(Code obj) {
        if (!methodName.equals("<clinit>")) return;
        super.visit(obj);
	requires.remove(betterClassName);
	if (betterClassName.equals("java.lang.System")) {
		requires.add("java.io.FileInputStream");
		requires.add("java.io.FileOutputStream");
		requires.add("java.io.BufferedInputStream");
		requires.add("java.io.BufferedOutputStream");
		requires.add("java.io.PrintStream");
		}
	if (!requires.isEmpty()) {
	  classRequires.put(betterClassName,requires);
	  requires = new TreeSet<String>();
	  }
    }


    public void sawOpcode(int seen) {
	if (PC + 6 >= codeBytes.length) return;

        if (seen == PUTSTATIC || seen == GETSTATIC || seen == INVOKESTATIC
			|| seen == NEW)  
		requires.add(betterClassConstant);
	}

    public void compute() {
	Set<String> allClasses = classRequires.keySet();
	Set<String> emptyClasses = new TreeSet<String>();
	for(Iterator i = allClasses.iterator(); i.hasNext(); ) {
		String c = (String) i.next();
		Set<String> needs = classRequires.get(c);
		needs.retainAll(allClasses);
		Set<String> extra = new TreeSet<String>();
		for(Iterator j = needs.iterator(); j.hasNext(); ) 
			extra.addAll(classRequires.get(j.next()));
		needs.addAll(extra);
		needs.retainAll(allClasses);
		classRequires.put(c,needs);
		if (needs.isEmpty()) emptyClasses.add(c);
		}
	for(Iterator i = emptyClasses.iterator(); i.hasNext(); ) {
		String c = (String) i.next();
		classRequires.remove(c);
		}
	}

    public void report() {

	if (DEBUG) System.out.println("Finishing computation");
        compute();
        compute();
        compute();
        compute();
        compute();
        compute();
        compute();
        compute();
	Set<String> allClasses = classRequires.keySet();

	for(Iterator<String> i = allClasses.iterator(); i.hasNext(); ) {
		String c = i.next();
        	if (DEBUG) System.out.println("Class " + c + " requires:");
		for(Iterator<String> j = (classRequires.get(c)).iterator(); 
				j.hasNext(); ) {
		    String needs = j.next();
                    if (DEBUG) System.out.println("  " + needs);
		    Set<String> s = classRequires.get(needs);
		    if (s != null && s.contains(c) && c.compareTo(needs) < 0)
			bugReporter.reportBug(new BugInstance("IC_INIT_CIRCULARITY", NORMAL_PRIORITY)
				.addClass(c)
				.addClass(needs));
		    }
		}
	}

    }

@@ -1328,16 +1328,102 @@
 eporter;
+%0A    private boolean instanceCreated;%0A    private boolean instanceCreatedWarningGiven;
 %0A%0A    pr
@@ -1622,16 +1622,80 @@
  obj) %7B%0A
+%09instanceCreated = false;%0A%09instanceCreatedWarningGiven = false;%0A
         
@@ -2243,49 +2243,615 @@
 ) %7B%0A
+%0A%0A
 %09if (
-PC + 6 %3E= codeBytes.length) return;%0A
+seen == PUTSTATIC)%0A%09%09System.out.println(%22Saw putstatic %22 + nameConstant);%0A        if (seen == PUTSTATIC && classConstant.equals(className))  %7B%0A%09%09if (instanceCreated && !instanceCreatedWarningGiven)  %7B%0A%09%09%09String okSig = %22L%22 + className + %22;%22;%0A%09%09%09if (!okSig.equals(sigConstant)) %7B%0A%09%09%09  System.out.println(%22Instance created in static initializer before static field %22 + nameConstant + %22 assigned%22);%0A%09%09%09  System.out.println(%22Class is %22 + className);%0A%09%09%09  instanceCreatedWarningGiven = true;%0A%09%09%09  %7D%0A%09%09%09%7D%0A%09%09%7D%0A        else if (seen == NEW && classConstant.equals(className))  %7B%0A%09%09instanceCreated = true;%0A%09%09%7D
 %0A   
@@ -2847,32 +2847,37 @@
 ue;%0A%09%09%7D%0A        
+else 
 if (seen == PUTS
@@ -2948,16 +2948,51 @@
  NEW)  %0A
+%09%09if (PC + 6 %3C codeBytes.length) %0A%09
 %09%09requir

Check for assignment to static field after creating an instance.