use of net.sourceforge.pmd.lang.java.ast.ASTFormalParameter in project pmd by pmd.
the class VariableNameDeclarationTest method testExceptionBlkParam.
@Test
public void testExceptionBlkParam() {
ASTVariableDeclaratorId id = new ASTVariableDeclaratorId(3);
id.testingOnlySetBeginLine(10);
id.setImage("foo");
ASTFormalParameter param = new ASTFormalParameter(2);
id.jjtSetParent(param);
param.jjtSetParent(new ASTTryStatement(1));
VariableNameDeclaration decl = new VariableNameDeclaration(id);
assertTrue(decl.isExceptionBlockParameter());
}
use of net.sourceforge.pmd.lang.java.ast.ASTFormalParameter in project pmd by pmd.
the class ClassTypeResolverTest method testInnerClass.
@Test
public void testInnerClass() throws ClassNotFoundException {
ASTCompilationUnit acu = parseAndTypeResolveForClass15(InnerClass.class);
Class<?> theInnerClass = Class.forName("net.sourceforge.pmd.typeresolution.testdata.InnerClass$TheInnerClass");
// Outer class
ASTTypeDeclaration typeDeclaration = acu.getFirstDescendantOfType(ASTTypeDeclaration.class);
assertEquals(InnerClass.class, typeDeclaration.getType());
ASTClassOrInterfaceDeclaration outerClassDeclaration = typeDeclaration.getFirstDescendantOfType(ASTClassOrInterfaceDeclaration.class);
assertEquals(InnerClass.class, outerClassDeclaration.getType());
// Inner class
assertEquals(theInnerClass, outerClassDeclaration.getFirstDescendantOfType(ASTClassOrInterfaceDeclaration.class).getType());
// Method parameter as inner class
ASTFormalParameter formalParameter = typeDeclaration.getFirstDescendantOfType(ASTFormalParameter.class);
assertEquals(theInnerClass, formalParameter.getType());
}
use of net.sourceforge.pmd.lang.java.ast.ASTFormalParameter in project pmd by pmd.
the class JavaRuleViolationTest method testASTFormalParameterVariableName.
/**
* Verifies that {@link JavaRuleViolation} sets the variable name for an
* {@link ASTFormalParameter} node.
*/
@Test
public void testASTFormalParameterVariableName() {
ASTCompilationUnit ast = parse("class Foo { void bar(int x) {} }");
final ASTFormalParameter node = ast.getFirstDescendantOfType(ASTFormalParameter.class);
final RuleContext context = new RuleContext();
final JavaRuleViolation violation = new JavaRuleViolation(null, context, node, null);
assertEquals("x", violation.getVariableName());
}
use of net.sourceforge.pmd.lang.java.ast.ASTFormalParameter in project pmd by pmd.
the class VariableAccessVisitor method markUsages.
private List<VariableAccess> markUsages(DataFlowNode inode) {
// undefinitions was once a field... seems like it works fine as a local
List<VariableAccess> undefinitions = new ArrayList<>();
Set<Map<VariableNameDeclaration, List<NameOccurrence>>> variableDeclarations = collectDeclarations(inode);
for (Map<VariableNameDeclaration, List<NameOccurrence>> declarations : variableDeclarations) {
for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : declarations.entrySet()) {
VariableNameDeclaration vnd = entry.getKey();
if (vnd.getAccessNodeParent() instanceof ASTFormalParameter) {
// no definition/undefinition/references for parameters
continue;
} else if (vnd.getAccessNodeParent().getFirstDescendantOfType(ASTVariableInitializer.class) != null) {
// add definition for initialized variables
addVariableAccess(vnd.getNode(), new VariableAccess(VariableAccess.DEFINITION, vnd.getImage()), inode.getFlow());
}
undefinitions.add(new VariableAccess(VariableAccess.UNDEFINITION, vnd.getImage()));
for (NameOccurrence occurrence : entry.getValue()) {
addAccess((JavaNameOccurrence) occurrence, inode);
}
}
}
return undefinitions;
}
use of net.sourceforge.pmd.lang.java.ast.ASTFormalParameter in project pmd by pmd.
the class ExceptionAsFlowControlRule method visit.
@Override
public Object visit(ASTThrowStatement node, Object data) {
ASTTryStatement parent = node.getFirstParentOfType(ASTTryStatement.class);
if (parent == null) {
return data;
}
for (parent = parent.getFirstParentOfType(ASTTryStatement.class); parent != null; parent = parent.getFirstParentOfType(ASTTryStatement.class)) {
List<ASTCatchStatement> list = parent.findDescendantsOfType(ASTCatchStatement.class);
for (ASTCatchStatement catchStmt : list) {
ASTFormalParameter fp = (ASTFormalParameter) catchStmt.jjtGetChild(0);
ASTType type = fp.getFirstDescendantOfType(ASTType.class);
ASTClassOrInterfaceType name = type.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
if (node.getFirstClassOrInterfaceTypeImage() != null && node.getFirstClassOrInterfaceTypeImage().equals(name.getImage())) {
addViolation(data, name);
}
}
}
return data;
}
Aggregations