use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.
the class AbstractLombokAwareRule method hasLombokAnnotation.
/**
* @deprecated As of release 6.2.0, replaced by {@link #hasLombokAnnotation(Annotatable)}
* Checks whether the given node is annotated with any lombok annotation.
* The node can be any node, e.g. class declaration or field declaration.
*
* @param node
* the node to check
* @return <code>true</code> if a lombok annotation has been found
*/
@Deprecated
protected boolean hasLombokAnnotation(Node node) {
boolean result = false;
Node parent = node.jjtGetParent();
List<ASTAnnotation> annotations = parent.findChildrenOfType(ASTAnnotation.class);
for (ASTAnnotation annotation : annotations) {
ASTName name = annotation.getFirstDescendantOfType(ASTName.class);
if (name != null) {
String annotationName = name.getImage();
if (lombokImported) {
if (LOMBOK_ANNOTATIONS.contains(annotationName)) {
result = true;
}
} else {
if (annotationName.startsWith(LOMBOK_PACKAGE + ".")) {
String shortName = annotationName.substring(LOMBOK_PACKAGE.length() + 1);
if (LOMBOK_ANNOTATIONS.contains(shortName)) {
result = true;
}
}
}
}
}
return result;
}
use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.
the class AtfdBaseVisitor method getMethodOrAttributeName.
private String getMethodOrAttributeName(ASTPrimaryExpression node) {
ASTPrimaryPrefix prefix = node.getFirstDescendantOfType(ASTPrimaryPrefix.class);
ASTName name = prefix.getFirstDescendantOfType(ASTName.class);
String methodOrAttributeName = null;
if (name != null) {
int dotIndex = name.getImage().indexOf(".");
if (dotIndex > -1) {
methodOrAttributeName = name.getImage().substring(dotIndex + 1);
}
}
return methodOrAttributeName;
}
use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.
the class AtfdBaseVisitor method getNameImage.
private String getNameImage(ASTPrimaryExpression node) {
ASTPrimaryPrefix prefix = node.getFirstDescendantOfType(ASTPrimaryPrefix.class);
ASTName name = prefix.getFirstDescendantOfType(ASTName.class);
String image = null;
if (name != null) {
image = name.getImage();
}
return image;
}
use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.
the class UselessStringValueOfRule method isPrimitive.
private static boolean isPrimitive(Node parent) {
boolean result = false;
if (parent instanceof ASTPrimaryExpression && parent.jjtGetNumChildren() == 1) {
Node child = parent.jjtGetChild(0);
if (child instanceof ASTPrimaryPrefix && child.jjtGetNumChildren() == 1) {
Node gc = child.jjtGetChild(0);
if (gc instanceof ASTName) {
ASTName name = (ASTName) gc;
NameDeclaration nd = name.getNameDeclaration();
if (nd instanceof VariableNameDeclaration && ((VariableNameDeclaration) nd).isPrimitiveType()) {
result = true;
}
} else if (gc instanceof ASTLiteral) {
result = !((ASTLiteral) gc).isStringLiteral();
}
}
}
return result;
}
use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.
the class ClassScope method addNameOccurrence.
public Set<NameDeclaration> addNameOccurrence(NameOccurrence occurrence) {
JavaNameOccurrence javaOccurrence = (JavaNameOccurrence) occurrence;
Set<NameDeclaration> declarations = findVariableHere(javaOccurrence);
if (!declarations.isEmpty() && (javaOccurrence.isMethodOrConstructorInvocation() || javaOccurrence.isMethodReference())) {
for (NameDeclaration decl : declarations) {
List<NameOccurrence> nameOccurrences = getMethodDeclarations().get(decl);
if (nameOccurrences == null) {
// search inner classes
for (ClassNameDeclaration innerClass : getClassDeclarations().keySet()) {
Scope innerClassScope = innerClass.getScope();
if (innerClassScope.contains(javaOccurrence)) {
innerClassScope.addNameOccurrence(javaOccurrence);
}
}
} else {
nameOccurrences.add(javaOccurrence);
Node n = javaOccurrence.getLocation();
if (n instanceof ASTName) {
((ASTName) n).setNameDeclaration(decl);
}
// TODO what to do with PrimarySuffix case?
}
}
} else if (!declarations.isEmpty() && !javaOccurrence.isThisOrSuper()) {
for (NameDeclaration decl : declarations) {
List<NameOccurrence> nameOccurrences = getVariableDeclarations().get(decl);
if (nameOccurrences == null) {
// search inner classes
for (ClassNameDeclaration innerClass : getClassDeclarations().keySet()) {
Scope innerClassScope = innerClass.getScope();
if (innerClassScope.contains(javaOccurrence)) {
innerClassScope.addNameOccurrence(javaOccurrence);
}
}
} else {
nameOccurrences.add(javaOccurrence);
Node n = javaOccurrence.getLocation();
if (n instanceof ASTName) {
((ASTName) n).setNameDeclaration(decl);
}
// TODO what to do with PrimarySuffix case?
}
}
}
return declarations;
}
Aggregations