edu.umd.cs.findbugs.detect.LpPreCompileRegCheck.java Source code

Java tutorial

Introduction

Here is the source code for edu.umd.cs.findbugs.detect.LpPreCompileRegCheck.java

Source

/**
 * Project: findbugs
 * 
 * File Created at 2012-5-308:25:16
 * $Id$
 * 
 * Copyright 1999-2012 Alibaba.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */
package edu.umd.cs.findbugs.detect;

import org.apache.bcel.classfile.Code;
import org.apache.commons.lang.StringUtils;

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

/**
 * Pattern.compile(String)Pattern.matches(String,
 * CharSequence)
 * 
 * @author fucai.zhangfc 2012-5-308:25:16
 */
public class LpPreCompileRegCheck extends LpAbstractBytecodeScanningDetector {

    BugReporter bugReporter;

    private boolean isMethod = Boolean.FALSE;

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

    @Override
    public void visit(Code obj) {
        String methodName = getMethodName();
        // this.get
        // (springinitset)
        if (StringUtils.isNotBlank(methodName) && (!"<clinit>".equals(methodName) && !"<init>".equals(methodName)
                && !methodName.startsWith("init") && !methodName.startsWith("set"))) {
            isMethod = Boolean.TRUE;
            super.visit(obj);
        } else {
            isMethod = Boolean.FALSE;
            return;
        }
    }

    @Override
    public void sawOpcode(int seen) {
        // 
        if (seen == INVOKESTATIC && isMethod) {
            // 
            if ("java/util/regex/Pattern".equals(getClassConstantOperand())) {

                if ("compile".equals(getNameConstantOperand()) // compile
                        || ("matches".equals(getNameConstantOperand())
                                && "(Ljava/lang/String;Ljava/lang/CharSequence;)Z".equals(getSigConstantOperand() // String,CharSequencematches
                                ))) {
                    reportBugThis(LpTypeEnum.LP_CHECK_REG_PRE_COMPILE);
                }
            }
        }
    }

    private void reportBugThis(String type) {
        BugInstance bug = new BugInstance(this, type, HIGH_PRIORITY).addClassAndMethod(this).addSourceLine(this,
                getPC());
        bug.addInt(getPC());
        bugReporter.reportBug(bug);
    }
}