localscopetest.java

来自「检查Java程序漏洞」· Java 代码 · 共 59 行

JAVA
59
字号
/** * BSD-style license; for more info see http://pmd.sourceforge.net/license.html*/package test.net.sourceforge.pmd.symboltable;import junit.framework.TestCase;import net.sourceforge.pmd.ast.ASTName;import net.sourceforge.pmd.ast.ASTPrimaryPrefix;import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;import net.sourceforge.pmd.symboltable.LocalScope;import net.sourceforge.pmd.symboltable.NameOccurrence;import net.sourceforge.pmd.symboltable.VariableNameDeclaration;public class LocalScopeTest extends TestCase {    private class MyASTVariableDeclaratorId extends ASTVariableDeclaratorId {        public MyASTVariableDeclaratorId(int x) {            super(x);        }        public boolean isExceptionBlockParameter() {            return true;        }    }    public void testNameWithThisOrSuperIsNotFlaggedAsUnused() {        LocalScope scope = new LocalScope();        ASTName name = new ASTName(1);        name.setImage("foo");        ASTPrimaryPrefix prefix = new ASTPrimaryPrefix(2);        prefix.setUsesThisModifier();        name.jjtAddChild(prefix, 1);        NameOccurrence occ = new NameOccurrence(name, "foo");        scope.addVariableNameOccurrence(occ);        assertTrue(!scope.getVariableDeclarations(false).keySet().iterator().hasNext());    }    public void testNameWithSuperIsNotFlaggedAsUnused() {        LocalScope scope = new LocalScope();        ASTName name = new ASTName(1);        name.setImage("foo");        ASTPrimaryPrefix prefix = new ASTPrimaryPrefix(2);        prefix.setUsesSuperModifier();        name.jjtAddChild(prefix, 1);        NameOccurrence occ = new NameOccurrence(name, "foo");        scope.addVariableNameOccurrence(occ);        assertTrue(!scope.getVariableDeclarations(false).keySet().iterator().hasNext());    }    public void testExceptionParamNameIsDiscarded() {        ASTVariableDeclaratorId node = new MyASTVariableDeclaratorId(1);        VariableNameDeclaration decl = new VariableNameDeclaration(node);        LocalScope scope = new LocalScope();        scope.addDeclaration(decl);        assertTrue(!scope.getVariableDeclarations(false).keySet().iterator().hasNext());    }}

⌨️ 快捷键说明

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