/*
 * fb-contrib - Auxiliary detectors for Java programs
 * Copyright (C) 2005-2017 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.Set;

import com.mebigfatguy.fbcontrib.utils.BugType;
import com.mebigfatguy.fbcontrib.utils.OpcodeUtils;
import com.mebigfatguy.fbcontrib.utils.SignatureUtils;
import com.mebigfatguy.fbcontrib.utils.UnmodifiableSet;

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

public class SuspiciousShadedClassUse extends BytecodeScanningDetector {

    private static final Set<String> SUSPICIOUS_ROOTS = UnmodifiableSet.create(
    // @formatter:off
        "/org/",
        "/com/",
        "/edu/"
    // @formatter:on
    );

    private BugReporter bugReporter;

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

    @Override
    public void sawOpcode(int seen) {

        if (OpcodeUtils.isStandardInvoke(seen)) {
            String invokedCls = getClassConstantOperand();

            for (String suspiciousRoot : SUSPICIOUS_ROOTS) {
                int rootPos = invokedCls.indexOf(suspiciousRoot);
                if (rootPos >= 0) {
                    String invokedPrefix = invokedCls.substring(0, rootPos);
                    String[] parts = invokedPrefix.split("/");
                    if (!SignatureUtils.similarPackages(invokedCls, getClassName(), Math.min(2, parts.length))) {
                        bugReporter.reportBug(new BugInstance(this, BugType.SSCU_SUSPICIOUS_SHADED_CLASS_USE.name(), NORMAL_PRIORITY).addClass(this)
                                .addMethod(this).addSourceLine(this));
                    }
                }
            }
        }
    }
}

@@ -872,24 +872,47 @@
 ib.detect;%0A%0A
+import java.util.List;%0A
 import java.
@@ -1069,32 +1069,89 @@
 SignatureUtils;%0A
+import com.mebigfatguy.fbcontrib.utils.UnmodifiableList;%0A
 import com.mebig
@@ -1576,24 +1576,183 @@
 :on%0A    );%0A%0A
+    private static final List%3CString%3E KNOWN_EXCEPTIONS = UnmodifiableList.create(%0A    // @formatter:off%0A        %22uk/org/lidalia/%22%0A    // @formatter:on%0A    );%0A%0A
     private 
@@ -2209,24 +2209,85 @@
 Pos %3E= 0) %7B%0A
+                    if (!isKnownException(invokedCls)) %7B%0A    
             
@@ -2367,24 +2367,28 @@
             
+ 
+   
 String%5B%5D par
@@ -2418,16 +2418,20 @@
 t(%22/%22);%0A
+    
         
@@ -2560,16 +2560,20 @@
         
+   
+ 
 bugRepor
@@ -2721,16 +2721,20 @@
         
+    
 .addMeth
@@ -2764,16 +2764,42 @@
 this));%0A
+                        %7D%0A
         
@@ -2859,11 +2859,564 @@
  %7D%0A    %7D
+%0A%0A    /**%0A     * determines if a suspected class is actually one of the known classes that have taken a bad package form by putting a tld later on in the package%0A     * structure%0A     *%0A     * @param clsName%0A     *            the classname to check%0A     * @return whether the classname is an exception%0A     */%0A    private boolean isKnownException(String clsName) %7B%0A        for (String exceptionCls : KNOWN_EXCEPTIONS) %7B%0A            if (clsName.startsWith(exceptionCls)) %7B%0A                return true;%0A            %7D%0A        %7D%0A%0A        return false;%0A    %7D
 %0A%7D%0A

add ability to filter out known exceptions to the shading rule as per #219