/*
 * fb-contrib - Auxiliary detectors for Java programs
 * Copyright (C) 2005-2016 Dave Brosius
 *
 * 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 com.mebigfatguy.fbcontrib.detect;

import java.util.HashSet;
import java.util.Set;

import org.apache.bcel.Repository;
import org.apache.bcel.classfile.Code;
import org.apache.bcel.classfile.ConstantString;
import org.apache.bcel.classfile.JavaClass;
import org.apache.bcel.classfile.Method;

import com.mebigfatguy.fbcontrib.utils.BugType;
import com.mebigfatguy.fbcontrib.utils.QMethod;
import com.mebigfatguy.fbcontrib.utils.Values;

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

/**
 * finds methods of abstract classes that do nothing, or just throw exceptions. Since this is an abstract class, it may be more correct to just leave the method
 * abstract.
 */
public class AbstractClassEmptyMethods extends BytecodeScanningDetector {
    enum State {
        SAW_NOTHING, SAW_NEW, SAW_DUP, SAW_LDC, SAW_INVOKESPECIAL, SAW_DONE
    }

    private static JavaClass EXCEPTION_CLASS;

    static {
        try {
            EXCEPTION_CLASS = Repository.lookupClass(Values.SLASHED_JAVA_LANG_EXCEPTION);
        } catch (ClassNotFoundException cnfe) {
            // ignore
        }
    }

    private final BugReporter bugReporter;
    private Set<QMethod> interfaceMethods;
    private String methodName;
    private State state;

    /**
     * constructs a ACEM detector given the reporter to report bugs on
     *
     * @param bugReporter
     *            the sync of bug reports
     */
    public AbstractClassEmptyMethods(BugReporter bugReporter) {
        this.bugReporter = bugReporter;
    }

    /**
     * overrides the visitor to check for abstract classes.
     *
     * @param classContext
     *            the context object that holds the JavaClass being parsed
     */
    @Override
    public void visitClassContext(ClassContext classContext) {
        try {
            JavaClass cls = classContext.getJavaClass();
            if (cls.isAbstract()) {
                interfaceMethods = collectInterfaceMethods(cls);
                super.visitClassContext(classContext);
            }
        } catch (ClassNotFoundException cnfe) {
            bugReporter.reportMissingClass(cnfe);
        } finally {
            interfaceMethods = null;
        }
    }

    /**
     * overrides the visitor to grab the method name and reset the state.
     *
     * @param obj
     *            the method being parsed
     */
    @Override
    public void visitMethod(Method obj) {
        methodName = obj.getName();
        state = State.SAW_NOTHING;
    }

    /**
     * overrides the visitor to filter out constructors.
     *
     * @param obj
     *            the code to parse
     */
    @Override
    public void visitCode(Code obj) {
        if (Values.CONSTRUCTOR.equals(methodName) || Values.STATIC_INITIALIZER.equals(methodName)) {
            return;
        }

        if (!interfaceMethods.contains(new QMethod(methodName, getMethod().getSignature()))) {
            super.visitCode(obj);
        }
    }

    /**
     * overrides the visitor to look for empty methods or simple exception throwers.
     *
     * @param seen
     *            the opcode currently being parsed
     */
    @Override
    public void sawOpcode(int seen) {
        try {
            switch (state) {
                case SAW_NOTHING:
                    if (seen == RETURN) {
                        bugReporter.reportBug(new BugInstance(this, BugType.ACEM_ABSTRACT_CLASS_EMPTY_METHODS.name(), NORMAL_PRIORITY).addClass(this)
                                .addMethod(this).addSourceLine(this));
                        state = State.SAW_DONE;
                    } else if (seen == NEW) {
                        String newClass = getClassConstantOperand();
                        JavaClass exCls = Repository.lookupClass(newClass);
                        if ((EXCEPTION_CLASS != null) && exCls.instanceOf(EXCEPTION_CLASS)) {
                            state = State.SAW_NEW;
                        } else {
                            state = State.SAW_DONE;
                        }
                    } else {
                        state = State.SAW_DONE;
                    }
                break;

                case SAW_NEW:
                    if (seen == DUP) {
                        state = State.SAW_DUP;
                    } else {
                        state = State.SAW_DONE;
                    }
                break;

                case SAW_DUP:
                    if (((seen == LDC) || (seen == LDC_W)) && (getConstantRefOperand() instanceof ConstantString)) {
                        state = State.SAW_LDC;
                    } else {
                        state = State.SAW_DONE;
                    }
                break;

                case SAW_LDC:
                    if ((seen == INVOKESPECIAL) && Values.CONSTRUCTOR.equals(getNameConstantOperand())) {
                        state = State.SAW_INVOKESPECIAL;
                    } else {
                        state = State.SAW_DONE;
                    }
                break;

                case SAW_INVOKESPECIAL:
                    if (seen == ATHROW) {
                        bugReporter.reportBug(new BugInstance(this, BugType.ACEM_ABSTRACT_CLASS_EMPTY_METHODS.name(), NORMAL_PRIORITY).addClass(this)
                                .addMethod(this).addSourceLine(this));
                    }
                    state = State.SAW_DONE;
                break;

                case SAW_DONE:
                break;
            }
        } catch (ClassNotFoundException cnfe) {
            bugReporter.reportMissingClass(cnfe);
            state = State.SAW_DONE;
        }
    }

    private Set<QMethod> collectInterfaceMethods(JavaClass cls) throws ClassNotFoundException {
        Set<QMethod> methods = new HashSet<QMethod>();
        for (JavaClass inf : cls.getAllInterfaces()) {
            for (Method m : inf.getMethods()) {
                methods.add(new QMethod(m.getName(), m.getSignature()));
            }
        }

        return methods;
    }
}

@@ -1828,288 +1828,75 @@
 ate 
-static JavaClass EXCEPTION_CLASS;%0A%0A    static %7B%0A        try %7B%0A            EXCEPTION_CLASS = Repository.lookupClass(Values.SLASHED_JAVA_LANG_EXCEPTION);%0A        %7D catch (ClassNotFoundException cnfe) %7B%0A            // ignore%0A        %7D%0A    %7D%0A%0A    private final BugReporter bugReporter
+final BugReporter bugReporter;%0A    private JavaClass exceptionClass
 ;%0A  
@@ -2254,24 +2254,230 @@
 bugReporter;
+%0A%0A        try %7B%0A            exceptionClass = Repository.lookupClass(Values.SLASHED_JAVA_LANG_EXCEPTION);%0A        %7D catch (ClassNotFoundException e) %7B%0A            bugReporter.reportMissingClass(e);%0A        %7D
 %0A    %7D%0A%0A    
@@ -4741,31 +4741,30 @@
    if ((
-EXCEPTION_CLASS
+exceptionClass
  != null
@@ -4789,23 +4789,22 @@
 eOf(
-EXCEPTION_CLASS
+exceptionClass
 )) %7B
@@ -6669,16 +6669,16 @@
 ption %7B%0A
+
         
@@ -6712,23 +6712,16 @@
 HashSet%3C
-QMethod
 %3E();%0A   

move static JavaClass fields to instance, so you can bugReport them