use of net.sourceforge.pmd.lang.java.ast.AccessNode in project pmd by pmd.
the class ImmutableFieldRule method visit.
@Override
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
Object result = super.visit(node, data);
Map<VariableNameDeclaration, List<NameOccurrence>> vars = node.getScope().getDeclarations(VariableNameDeclaration.class);
List<ASTConstructorDeclaration> constructors = findAllConstructors(node);
for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : vars.entrySet()) {
VariableNameDeclaration field = entry.getKey();
AccessNode accessNodeParent = field.getAccessNodeParent();
if (accessNodeParent.isStatic() || !accessNodeParent.isPrivate() || accessNodeParent.isFinal() || accessNodeParent.isVolatile() || hasClassLombokAnnotation()) {
continue;
}
FieldImmutabilityType type = initializedInConstructor(entry.getValue(), new HashSet<>(constructors));
if (type == FieldImmutabilityType.MUTABLE) {
continue;
}
if (type == FieldImmutabilityType.IMMUTABLE || type == FieldImmutabilityType.CHECKDECL && initializedWhenDeclared(field)) {
addViolation(data, field.getNode(), field.getImage());
}
}
return result;
}
use of net.sourceforge.pmd.lang.java.ast.AccessNode in project pmd by pmd.
the class UnusedPrivateFieldRule method visit.
@Override
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
boolean classHasLombok = hasLombokAnnotation(node);
Map<VariableNameDeclaration, List<NameOccurrence>> vars = node.getScope().getDeclarations(VariableNameDeclaration.class);
for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : vars.entrySet()) {
VariableNameDeclaration decl = entry.getKey();
AccessNode accessNodeParent = decl.getAccessNodeParent();
if (!accessNodeParent.isPrivate() || isOK(decl.getImage()) || classHasLombok || hasIgnoredAnnotation((Annotatable) accessNodeParent)) {
continue;
}
if (!actuallyUsed(entry.getValue())) {
if (!usedInOuterClass(node, decl) && !usedInOuterEnum(node, decl)) {
addViolation(data, decl.getNode(), decl.getImage());
}
}
}
return super.visit(node, data);
}
use of net.sourceforge.pmd.lang.java.ast.AccessNode in project pmd by pmd.
the class MethodArgumentCouldBeFinalRule method lookForViolation.
private void lookForViolation(Scope scope, Object data) {
Map<VariableNameDeclaration, List<NameOccurrence>> decls = scope.getDeclarations(VariableNameDeclaration.class);
for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : decls.entrySet()) {
VariableNameDeclaration var = entry.getKey();
AccessNode node = var.getAccessNodeParent();
if (!node.isFinal() && node instanceof ASTFormalParameter && !assigned(entry.getValue())) {
addViolation(data, node, var.getImage());
}
}
}
use of net.sourceforge.pmd.lang.java.ast.AccessNode 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.AccessNode in project pmd by pmd.
the class BeanMembersShouldSerializeRule method visit.
@Override
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
if (node.isInterface()) {
return data;
}
Map<MethodNameDeclaration, List<NameOccurrence>> methods = node.getScope().getEnclosingScope(ClassScope.class).getMethodDeclarations();
List<ASTMethodDeclarator> getSetMethList = new ArrayList<>(methods.size());
for (MethodNameDeclaration d : methods.keySet()) {
ASTMethodDeclarator mnd = d.getMethodNameDeclaratorNode();
if (isBeanAccessor(mnd)) {
getSetMethList.add(mnd);
}
}
String[] methNameArray = imagesOf(getSetMethList);
Arrays.sort(methNameArray);
Map<VariableNameDeclaration, List<NameOccurrence>> vars = node.getScope().getDeclarations(VariableNameDeclaration.class);
for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : vars.entrySet()) {
VariableNameDeclaration decl = entry.getKey();
AccessNode accessNodeParent = decl.getAccessNodeParent();
if (entry.getValue().isEmpty() || accessNodeParent.isTransient() || accessNodeParent.isStatic()) {
continue;
}
String varName = trimIfPrefix(decl.getImage());
varName = varName.substring(0, 1).toUpperCase(Locale.ROOT) + varName.substring(1, varName.length());
boolean hasGetMethod = Arrays.binarySearch(methNameArray, "get" + varName) >= 0 || Arrays.binarySearch(methNameArray, "is" + varName) >= 0;
boolean hasSetMethod = Arrays.binarySearch(methNameArray, "set" + varName) >= 0;
// variable...
if (!hasGetMethod || !accessNodeParent.isFinal() && !hasSetMethod) {
addViolation(data, decl.getNode(), decl.getImage());
}
}
return super.visit(node, data);
}
Aggregations