/*
 * FindBugs - Find bugs in Java programs
 * Copyright (C) 2004-2005, Tom Truscott <trt@unx.sas.com>
 *
 * 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 org.apache.bcel.classfile.Code;

public class FindLocalSelfAssignment2 extends BytecodeScanningDetector implements StatelessDetector {

	private BugReporter bugReporter;
	private int previousLoadOf = -1;
	private int previousGotoTarget;
	private int gotoCount;
	public FindLocalSelfAssignment2(BugReporter bugReporter) {
		this.bugReporter = bugReporter;
	}

	@Override
         public Object clone() throws CloneNotSupportedException {
		return super.clone();
	}

	@Override
         public void visit(Code obj) {
		previousLoadOf = -1;
		previousGotoTarget = -1;
		gotoCount = 0;
		super.visit(obj);
	}


	@Override
         public void sawOpcode(int seen) {
		if (seen == GOTO) {
			previousGotoTarget = getBranchTarget();
			gotoCount++;
		} else {
			if (isRegisterLoad()) 
				previousLoadOf = getRegisterOperand();
			else {
				if (isRegisterStore() && previousLoadOf == getRegisterOperand() && gotoCount < 2 && getPC() != previousGotoTarget)
				       bugReporter.reportBug(
					new BugInstance(this, 
							"SA_LOCAL_SELF_ASSIGNMENT", NORMAL_PRIORITY)
	                                        .addClassAndMethod(this)
	                                        .addSourceLine(this));
	
				previousLoadOf = -1;
				gotoCount = 0;
			}
		}
	}
}

@@ -1525,37 +1525,32 @@
 %09%7D%0A%0A%0A%09@Override%0A
-     
     public void 
@@ -1652,16 +1652,78 @@
 ount++;%0A
+%09%09%09if (previousGotoTarget %3C getPC())%0A%09%09%09%09previousLoadOf = -1;%0A
 %09%09%7D else

Fix [ 1459133 ] false positive for local self assign