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

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

The newest version!
/*
 * FindBugs - Find bugs in Java programs
 * Copyright (C) 2004-2006 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 org.apache.bcel.Const;
import org.apache.bcel.classfile.Method;

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

/*
 * This is a very simply written detector. It checks if there is exactly
 * in the bytecode getting a field, DUP, a store, MONITORENTER, get same
 * field, and check if null.
 *
 * Author: Kristin Stephens
 */

public class SynchronizeAndNullCheckField extends BytecodeScanningDetector {

    BugReporter bugReporter;

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

    @Override
    public void visit(Method obj) {
        gottenField = null;
        currState = 0;
        syncField = null;
    }

    FieldAnnotation gottenField;

    FieldAnnotation syncField;

    int currState;

    @Override
    public void sawOpcode(int seen) {
        // System.out.println(getPC() + " " + Const.getOpcodeName(seen) + " " +
        // currState);
        switch (currState) {
        case 0:
            if (seen == Const.GETFIELD || seen == Const.GETSTATIC) {
                syncField = FieldAnnotation.fromReferencedField(this);
                currState = 1;
            }
            break;
        case 1:
            if (seen == Const.DUP) {
                currState = 2;
            } else {
                currState = 0;
            }
            break;
        case 2:
            if (seen == Const.ASTORE || seen == Const.ASTORE_0 || seen == Const.ASTORE_1 || seen == Const.ASTORE_2 || seen == Const.ASTORE_3) {
                currState = 3;
            } else {
                currState = 0;
            }
            break;
        case 3:
            if (seen == Const.MONITORENTER) {
                currState = 4;
            } else {
                currState = 0;
            }
            break;
        case 4:
            if (seen == Const.GETFIELD || seen == Const.GETSTATIC) {
                gottenField = FieldAnnotation.fromReferencedField(this);
                currState = 5;
            } else {
                currState = 0;
            }
            break;
        case 5:
            if ((seen == Const.IFNONNULL || seen == Const.IFNULL) && gottenField.equals(syncField)) {
                BugInstance bug = new BugInstance(this, "NP_SYNC_AND_NULL_CHECK_FIELD", NORMAL_PRIORITY).addClass(this)
                        .addMethod(this).addField(syncField).addSourceLine(this);
                bugReporter.reportBug(bug);
            } else {
                currState = 0;
            }
            break;
        default:
            currState = 0;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy