/*
 * 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.List;
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.UnmodifiableList;
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 static final List<String> KNOWN_EXCEPTIONS = UnmodifiableList.create(
    // @formatter:off
        "uk/org/lidalia/"
    // @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) {
                    if (!isKnownException(invokedCls)) {
                        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));
                        }
                    }
                }
            }
        }
    }

    /**
     * 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
     * structure
     *
     * @param clsName
     *            the classname to check
     * @return whether the classname is an exception
     */
    private boolean isKnownException(String clsName) {
        for (String exceptionCls : KNOWN_EXCEPTIONS) {
            if (clsName.startsWith(exceptionCls)) {
                return true;
            }
        }

        return false;
    }
}

@@ -1395,24 +1395,113 @@
 Detector %7B%0A%0A
+    private static final String SSCU_EXCEPTION_PACKAGES = %22fb-contrib.sscu.exceptions%22;%0A%0A
     private 
@@ -1560,32 +1560,32 @@
 ableSet.create(%0A
-
     // @formatte
@@ -1677,31 +1677,24 @@
     private 
-static 
 final List%3CS
@@ -1700,32 +1700,31 @@
 String%3E 
-KNOWN_EXCEPTIONS
+knownExceptions
  = Unmod
@@ -1917,24 +1917,24 @@
 Reporter) %7B%0A
-
         this
@@ -1960,16 +1960,267 @@
 eporter;
+%0A%0A        String exceptions = System.getProperty(SSCU_EXCEPTION_PACKAGES, %22%22);%0A        for (String ex : exceptions.split(%22%5C%5Cs,%5C%5Cs*%22)) %7B%0A            if (!ex.isEmpty()) %7B%0A                knownExceptions.add(ex.replace('.', '/'));%0A            %7D%0A        %7D
 %0A    %7D%0A%0A
@@ -3595,24 +3595,23 @@
 s : 
-KNOWN_EXCEPTIONS
+knownExceptions
 ) %7B%0A

add system property 'fb-contrib.sscu.exceptions' for user control