use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.
the class TccAttributeAccessCollector method getVariableName.
private String getVariableName(ASTPrimaryExpression node) {
ASTPrimaryPrefix prefix = node.getFirstDescendantOfType(ASTPrimaryPrefix.class);
if (prefix.usesThisModifier()) {
List<ASTPrimarySuffix> suffixes = node.findChildrenOfType(ASTPrimarySuffix.class);
if (suffixes.size() > 1) {
if (!suffixes.get(1).isArguments()) {
// not a method call
return suffixes.get(0).getImage();
}
}
}
ASTName name = prefix.getFirstDescendantOfType(ASTName.class);
String variableName = null;
if (name != null) {
int dotIndex = name.getImage().indexOf(".");
if (dotIndex == -1) {
variableName = name.getImage();
} else {
variableName = name.getImage().substring(0, dotIndex);
}
}
return variableName;
}
use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.
the class CheckResultSetRule method visit.
@Override
public Object visit(ASTLocalVariableDeclaration node, Object data) {
ASTClassOrInterfaceType type = node.getFirstChildOfType(ASTType.class).getFirstDescendantOfType(ASTClassOrInterfaceType.class);
if (type != null && (type.getType() != null && "java.sql.ResultSet".equals(type.getType().getName()) || "ResultSet".equals(type.getImage()))) {
ASTVariableDeclarator declarator = node.getFirstChildOfType(ASTVariableDeclarator.class);
if (declarator != null) {
ASTName name = declarator.getFirstDescendantOfType(ASTName.class);
if (type.getType() != null || name != null && name.getImage().endsWith("executeQuery")) {
ASTVariableDeclaratorId id = declarator.getFirstChildOfType(ASTVariableDeclaratorId.class);
resultSetVariables.put(id.getImage(), node);
}
}
}
return super.visit(node, data);
}
use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.
the class ForLoopCanBeForeachRule method getIterableDeclOfIteratorLoop.
private Entry<VariableNameDeclaration, List<NameOccurrence>> getIterableDeclOfIteratorLoop(VariableNameDeclaration indexDecl, Scope scope) {
Node initializer = indexDecl.getNode().getFirstParentOfType(ASTVariableDeclarator.class).getFirstChildOfType(ASTVariableInitializer.class);
if (initializer == null) {
return null;
}
ASTName nameNode = initializer.getFirstDescendantOfType(ASTName.class);
if (nameNode == null) {
// TODO : This can happen if we are calling a local / statically imported method that returns the iterable - currently unhandled
return null;
}
String name = nameNode.getImage();
int dotIndex = name.indexOf('.');
if (dotIndex > 0) {
name = name.substring(0, dotIndex);
}
return findDeclaration(name, scope);
}
use of net.sourceforge.pmd.lang.java.ast.ASTName 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.ASTName in project pmd by pmd.
the class AbstractJUnitRule method doesNodeContainJUnitAnnotation.
private boolean doesNodeContainJUnitAnnotation(Node node, String annotationTypeClassName) {
List<ASTAnnotation> annotations = node.findDescendantsOfType(ASTAnnotation.class);
for (ASTAnnotation annotation : annotations) {
Node annotationTypeNode = annotation.jjtGetChild(0);
TypeNode annotationType = (TypeNode) annotationTypeNode;
if (annotationType.getType() == null) {
ASTName name = annotationTypeNode.getFirstChildOfType(ASTName.class);
if (name != null && (name.hasImageEqualTo("Test") || name.hasImageEqualTo(annotationTypeClassName))) {
return true;
}
} else if (TypeHelper.isA(annotationType, annotationTypeClassName)) {
return true;
}
}
return false;
}
Aggregations