⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exceptiontypechecking.java

📁 检查Java程序漏洞
💻 JAVA
字号:
package net.sourceforge.pmd.rules.strictexception;

import net.sourceforge.pmd.AbstractRule;
import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.ast.ASTCastExpression;
import net.sourceforge.pmd.ast.ASTCatch;
import net.sourceforge.pmd.ast.ASTInstanceOfExpression;
import net.sourceforge.pmd.ast.ASTName;
import net.sourceforge.pmd.ast.ASTPrimaryExpression;
import net.sourceforge.pmd.ast.ASTTryStatement;
import net.sourceforge.pmd.ast.ASTType;
import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;

import java.util.Iterator;
import java.util.List;

/**
 * 
 * <p>
 * @author <a mailto:trond.andersen@nordea.com>Trond Andersen</a>
 * @version 1.0
 * @since 1.1?
 */
public class ExceptionTypeChecking extends AbstractRule {

    public Object visit(ASTTryStatement catchStatment, Object object) {
        if (catchStatment.hasCatch()) {
            for (Iterator iter = catchStatment.getCatchBlocks().iterator(); iter.hasNext();) {
                evaluateCatchClause((ASTCatch)iter.next(), (RuleContext)object);
            }
        }

        return super.visit(catchStatment, object);
    }

    private void evaluateCatchClause(ASTCatch catchStmt, RuleContext ctx) {
        String exceptionParameter = getExceptionParameter(catchStmt);
        // Retrieves all instance of expressions
        List myList = catchStmt.getBlock().findChildrenOfType(ASTInstanceOfExpression.class);

        for (Iterator i = myList.iterator(); i.hasNext();) {
            evaluateInstanceOfExpression((ASTInstanceOfExpression)i.next(), exceptionParameter, ctx);
        }
    }

    private void evaluateInstanceOfExpression(ASTInstanceOfExpression instanceOfExpression,
                                              String exceptionName, RuleContext ctx) {
        if (!hasTypeEvaluation(instanceOfExpression)) {
            return;
        }
        if (exceptionName.equals( getInstanceOfObjectReference(instanceOfExpression)) ) {
            ctx.getReport().addRuleViolation(createRuleViolation(ctx, instanceOfExpression.getBeginLine()));
        }
    }

    private boolean hasTypeEvaluation(ASTInstanceOfExpression instanceOfExpression) {
        List typeList = instanceOfExpression.findChildrenOfType(ASTType.class);
        if (typeList != null && typeList.size() >= 1) {
            ASTType theType = (ASTType)typeList.get(0);
            if (!(theType.jjtGetParent() instanceof ASTCastExpression)) {
                return true;
            }
        }
       return false;
    }

    private String getInstanceOfObjectReference(ASTInstanceOfExpression expression) {
        List primaryList = expression.findChildrenOfType(ASTPrimaryExpression.class);
        String objectReferenceName = null;
        if (primaryList.size() == 1) {
            List someList = ((ASTPrimaryExpression)primaryList.get(0)).findChildrenOfType(ASTName.class);
            if (someList.size() == 1) {
                objectReferenceName = ((ASTName)someList.get(0)).getImage();
            }
        }
        return objectReferenceName;
    }

    private String getExceptionParameter(ASTCatch catchStmt) {
        List declarationList = catchStmt.getFormalParameter().findChildrenOfType(ASTVariableDeclaratorId.class);
        return ((ASTVariableDeclaratorId)declarationList.get(0)).getImage();
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -