use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.
the class ForLoopCanBeForeachRule method isReplaceableIteratorLoop.
private boolean isReplaceableIteratorLoop(Entry<VariableNameDeclaration, List<NameOccurrence>> indexInfo, ASTExpression guardCondition, Entry<VariableNameDeclaration, List<NameOccurrence>> iterableInfo, ASTForStatement stmt) {
if (isIterableModifiedInsideLoop(iterableInfo, stmt)) {
return false;
}
String indexName = indexInfo.getKey().getName();
if (indexName == null) {
return false;
}
if (!guardCondition.hasDescendantMatchingXPath("./PrimaryExpression/PrimaryPrefix/Name[@Image='" + indexName + ".hasNext']")) {
return false;
}
List<NameOccurrence> occurrences = indexInfo.getValue();
if (occurrences.size() > 2) {
return false;
}
for (NameOccurrence occ : indexInfo.getValue()) {
ScopedNode location = occ.getLocation();
boolean isCallingNext = location instanceof ASTName && (location.hasImageEqualTo(indexName + ".hasNext") || location.hasImageEqualTo(indexName + ".next"));
if (!isCallingNext) {
return false;
}
}
return true;
}
use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.
the class GuardLogStatementRule method getLogLevelName.
/**
* Determines the log level, that is used. It is either the called method name
* itself or - if the method has a first argument a primary prefix - the first argument.
*
* @param node the method call
* @param methodCallName the called method name previously determined
* @return the log level
*/
private String getLogLevelName(Node node, String methodCallName) {
String logLevel = methodCallName;
ASTPrimarySuffix suffix = node.getFirstDescendantOfType(ASTPrimarySuffix.class);
if (suffix != null) {
ASTArgumentList argumentList = suffix.getFirstDescendantOfType(ASTArgumentList.class);
if (argumentList != null && argumentList.hasDescendantOfType(ASTName.class)) {
ASTName name = argumentList.getFirstDescendantOfType(ASTName.class);
String lastPart = getLastPartOfName(name.getImage());
lastPart = lastPart.toLowerCase(Locale.ROOT);
if (!lastPart.isEmpty()) {
logLevel = lastPart;
}
}
}
return logLevel;
}
use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.
the class JUnitUseExpectedRule method visit.
@Override
public Object visit(ASTClassOrInterfaceBodyDeclaration node, Object data) {
boolean inAnnotation = false;
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
Node child = node.jjtGetChild(i);
if (child instanceof ASTAnnotation) {
ASTName annotationName = child.getFirstDescendantOfType(ASTName.class);
if ("Test".equals(annotationName.getImage())) {
inAnnotation = true;
continue;
}
}
if (child instanceof ASTMethodDeclaration) {
boolean isJUnitMethod = isJUnitMethod((ASTMethodDeclaration) child, data);
if (inAnnotation || isJUnitMethod) {
List<Node> found = new ArrayList<>();
found.addAll((List<Node>) visit((ASTMethodDeclaration) child, data));
for (Node name : found) {
addViolation(data, name);
}
}
}
inAnnotation = false;
}
return super.visit(node, data);
}
use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.
the class PreserveStackTraceRule method checkForTargetUsage.
/**
* Checks whether the given target is in the argument list. If this is the
* case, then the target (root exception) is used as the cause.
*
* @param target
* @param baseNode
*/
private boolean checkForTargetUsage(String target, Node baseNode) {
boolean match = false;
if (target != null && baseNode != null) {
// TODO : use Node.findDescendantsOfType(ASTName.class, true) on 7.0.0
List<ASTName> nameNodes = new ArrayList<>();
baseNode.findDescendantsOfType(ASTName.class, nameNodes, true);
for (ASTName nameNode : nameNodes) {
if (target.equals(nameNode.getImage())) {
boolean isPartOfStringConcatenation = isStringConcat(nameNode, baseNode);
if (!isPartOfStringConcatenation) {
match = true;
break;
}
}
}
}
return match;
}
use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.
the class UnusedFormalParameterRule method hasOverrideAnnotation.
private boolean hasOverrideAnnotation(ASTMethodDeclaration node) {
int childIndex = node.jjtGetChildIndex();
for (int i = 0; i < childIndex; i++) {
Node previousSibling = node.jjtGetParent().jjtGetChild(i);
List<ASTMarkerAnnotation> annotations = previousSibling.findDescendantsOfType(ASTMarkerAnnotation.class);
for (ASTMarkerAnnotation annotation : annotations) {
ASTName name = annotation.getFirstChildOfType(ASTName.class);
if (name != null && (name.hasImageEqualTo("Override") || name.hasImageEqualTo("java.lang.Override"))) {
return true;
}
}
}
return false;
}
Aggregations