use of net.sourceforge.pmd.lang.java.ast.ASTBlockStatement in project pmd by pmd.
the class PrematureDeclarationRule method blocksAfter.
/**
* Returns all the blocks found right after the node supplied within the its
* current scope.
*
* @param block
* SimpleJavaNode
* @param node
* SimpleNode
* @return List
*/
private static List<ASTBlockStatement> blocksAfter(AbstractJavaNode block, AbstractJavaNode node) {
int count = block.jjtGetNumChildren();
int start = indexOf(block, node.jjtGetParent()) + 1;
List<ASTBlockStatement> nextBlocks = new ArrayList<>(count);
for (int i = start; i < count; i++) {
Node maybeBlock = block.jjtGetChild(i);
if (maybeBlock instanceof ASTBlockStatement) {
nextBlocks.add((ASTBlockStatement) maybeBlock);
}
}
return nextBlocks;
}
use of net.sourceforge.pmd.lang.java.ast.ASTBlockStatement in project pmd by pmd.
the class CycloBaseVisitor method visit.
@Override
public Object visit(ASTSwitchStatement node, Object data) {
int childCount = node.jjtGetNumChildren();
int lastIndex = childCount - 1;
for (int n = 0; n < lastIndex; n++) {
Node childNode = node.jjtGetChild(n);
if (childNode instanceof ASTSwitchLabel) {
// default is not considered a decision (same as "else")
ASTSwitchLabel sl = (ASTSwitchLabel) childNode;
if (!sl.isDefault()) {
// check the label is not empty
childNode = node.jjtGetChild(n + 1);
if (childNode instanceof ASTBlockStatement) {
((MutableInt) data).increment();
}
}
}
}
super.visit(node, data);
return data;
}
use of net.sourceforge.pmd.lang.java.ast.ASTBlockStatement in project pmd by pmd.
the class JUnitUseExpectedRule method visit.
@Override
public Object visit(ASTMethodDeclaration node, Object data) {
List<ASTTryStatement> catches = node.findDescendantsOfType(ASTTryStatement.class);
List<Node> found = new ArrayList<>();
if (catches.isEmpty()) {
return found;
}
for (ASTTryStatement trySt : catches) {
ASTCatchStatement cStatement = getCatch(trySt);
if (cStatement != null) {
ASTBlock block = (ASTBlock) cStatement.jjtGetChild(1);
if (block.jjtGetNumChildren() != 0) {
continue;
}
List<ASTBlockStatement> blocks = trySt.jjtGetChild(0).findDescendantsOfType(ASTBlockStatement.class);
if (blocks.isEmpty()) {
continue;
}
ASTBlockStatement st = blocks.get(blocks.size() - 1);
ASTName name = st.getFirstDescendantOfType(ASTName.class);
if (name != null && st.equals(name.getNthParent(5)) && "fail".equals(name.getImage())) {
found.add(name);
continue;
}
ASTThrowStatement th = st.getFirstDescendantOfType(ASTThrowStatement.class);
if (th != null && st.equals(th.getNthParent(2))) {
found.add(th);
continue;
}
}
}
return found;
}
use of net.sourceforge.pmd.lang.java.ast.ASTBlockStatement in project pmd by pmd.
the class InefficientStringBufferingRule method visit.
@Override
public Object visit(ASTAdditiveExpression node, Object data) {
ASTBlockStatement bs = node.getFirstParentOfType(ASTBlockStatement.class);
if (bs == null) {
return data;
}
int immediateLiterals = 0;
int immediateStringLiterals = 0;
List<ASTLiteral> nodes = node.findDescendantsOfType(ASTLiteral.class);
for (ASTLiteral literal : nodes) {
if (literal.getNthParent(3) instanceof ASTAdditiveExpression) {
immediateLiterals++;
if (literal.isStringLiteral()) {
immediateStringLiterals++;
}
}
if (literal.isIntLiteral() || literal.isFloatLiteral() || literal.isDoubleLiteral() || literal.isLongLiteral()) {
return data;
}
}
if (immediateLiterals > 1) {
return data;
}
// if literal + public static final, return
List<ASTName> nameNodes = node.findDescendantsOfType(ASTName.class);
for (ASTName name : nameNodes) {
if (name.getNameDeclaration() != null && name.getNameDeclaration() instanceof VariableNameDeclaration) {
VariableNameDeclaration vnd = (VariableNameDeclaration) name.getNameDeclaration();
AccessNode accessNodeParent = vnd.getAccessNodeParent();
if (accessNodeParent.isFinal() && accessNodeParent.isStatic()) {
return data;
}
}
}
// if literal primitive type and not strings variables, then return
boolean stringFound = false;
for (ASTName name : nameNodes) {
if (!isPrimitiveType(name) && isStringType(name)) {
stringFound = true;
break;
}
}
if (!stringFound && immediateStringLiterals == 0) {
return data;
}
if (bs.isAllocation()) {
for (Iterator<ASTName> iterator = nameNodes.iterator(); iterator.hasNext(); ) {
ASTName name = iterator.next();
if (!name.getImage().endsWith("length")) {
break;
} else if (!iterator.hasNext()) {
// All names end with length
return data;
}
}
if (isAllocatedStringBuffer(node)) {
addViolation(data, node);
}
} else if (isInStringBufferOperation(node, 6, "append")) {
addViolation(data, node);
}
return data;
}
use of net.sourceforge.pmd.lang.java.ast.ASTBlockStatement in project pmd by pmd.
the class ConsecutiveAppendsShouldReuseRule method getNextBlockStatementSibling.
private ASTBlockStatement getNextBlockStatementSibling(Node node) {
Node parent = node.jjtGetParent();
int childIndex = -1;
for (int i = 0; i < parent.jjtGetNumChildren(); i++) {
if (parent.jjtGetChild(i) == node) {
childIndex = i;
break;
}
}
if (childIndex + 1 < parent.jjtGetNumChildren()) {
Node nextSibling = parent.jjtGetChild(childIndex + 1);
if (nextSibling instanceof ASTBlockStatement) {
return (ASTBlockStatement) nextSibling;
}
}
return null;
}
Aggregations