@Override public Object visit(ASTFieldDeclaration node, Object data) { if (checkNonStaticFields || node.isStatic()) { fieldDecls.put(node.getVariableName(), node); } return super.visit(node, data); }
@Override public Object visit(ASTFieldDeclaration node, Object data) { stack.peek().addField(node.getVariableName(), node.getSignature()); return data; // end recursion }
private boolean isSerialVersionUID(ASTFieldDeclaration field) { return "serialVersionUID".equals(field.getVariableName()) && field.isStatic() && field.isFinal() && field.getType() == long.class; }
@Override public Object visit(ASTFieldDeclaration node, Object data) { ASTClassOrInterfaceDeclaration cl = node.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class); if (cl != null && node.getVariableName().equalsIgnoreCase(cl.getImage())) { addViolation(data, node); } return data; } }
private static boolean isGetterOrSetter(ASTMethodDeclaration node) { ClassScope scope = node.getScope().getEnclosingScope(ClassScope.class); // fields names mapped to their types Map<String, String> fieldNames = new HashMap<>(); for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> decl : scope.getVariableDeclarations().entrySet()) { ASTFieldDeclaration field = decl.getKey() .getNode() .getFirstParentOfType(ASTFieldDeclaration.class); Matcher matcher = FIELD_NAME_PATTERN.matcher(field.getVariableName()); String varName = matcher.find() ? matcher.group(1) : field.getVariableName(); fieldNames.put(varName, field.getFirstChildOfType(ASTType.class).getTypeImage()); } return isGetter(node, fieldNames) || isSetter(node, fieldNames); }
@Override public Object visit(ASTClassOrInterfaceBody node, Object data) { int n = node.jjtGetNumChildren(); List<ASTFieldDeclaration> fields = new ArrayList<>(); Set<String> methodNames = new HashSet<>(); for (int i = 0; i < n; i++) { Node child = node.jjtGetChild(i); if (child.jjtGetNumChildren() == 0) { continue; } child = child.jjtGetChild(child.jjtGetNumChildren() - 1); if (child instanceof ASTFieldDeclaration) { fields.add((ASTFieldDeclaration) child); } else if (child instanceof ASTMethodDeclaration) { methodNames.add(((ASTMethodDeclaration) child).getMethodName().toLowerCase(Locale.ROOT)); } } for (ASTFieldDeclaration field : fields) { String varName = field.getVariableName().toLowerCase(Locale.ROOT); if (methodNames.contains(varName)) { addViolation(data, field, field.getVariableName()); } } return super.visit(node, data); }
private String getNodeName(Node node) { // constructors are differentiated by their parameters, while we only use method name for methods if (node instanceof ASTMethodDeclaration) { return ((ASTMethodDeclaration) node).getMethodName(); } else if (node instanceof ASTMethodOrConstructorDeclaration) { // constructors are differentiated by their parameters, while we only use method name for methods return ((ASTConstructorDeclaration) node).getQualifiedName().getOperation(); } else if (node instanceof ASTFieldDeclaration) { return ((ASTFieldDeclaration) node).getVariableName(); } else if (node instanceof ASTResource) { return ((ASTResource) node).getVariableDeclaratorId().getImage(); } else { return node.getImage(); } }
@Override public String make() { return I18nResources.getMessage(MESSAGE_KEY_PREFIX + ".field", decl.getVariableName()); } });
private Set<String> getSqlMapFields(List<Node> fieldDeclarations) { if (fieldDeclarations == null || fieldDeclarations.isEmpty()) { return Collections.emptySet(); } Set<String> set = new HashSet<>(); for (Node node : fieldDeclarations) { ASTFieldDeclaration fieldDeclaration = (ASTFieldDeclaration)node; if (sqlMapClientField(fieldDeclaration)) { set.add(fieldDeclaration.getVariableName()); } } return set; }
private void checkThreadLocal(ASTFieldDeclaration fieldDeclaration, ASTCompilationUnit node, Object data) { try { List<Node> nodes = node.findChildNodesWithXPath(String.format(XPATH_TPL, fieldDeclaration.getVariableName())); if (nodes == null || nodes.isEmpty()) { ViolationUtils.addViolationWithPrecisePosition(this, fieldDeclaration, data, I18nResources.getMessage("java.concurrent.ThreadLocalShouldRemoveRule.violation.msg", fieldDeclaration.getVariableName())); } } catch (JaxenException ignore) { } } }
@Override public Object visit(ASTFieldDeclaration node, Object data) { stack.peek().addField(node.getVariableName(), node.getSignature()); return data; // end recursion }
@Override public Object visit(ASTFieldDeclaration node, Object data) { if (checkNonStaticFields || node.isStatic()) { fieldDecls.put(node.getVariableName(), node); } return super.visit(node, data); }
private boolean isSerialVersionUID(ASTFieldDeclaration field) { return "serialVersionUID".equals(field.getVariableName()) && field.isStatic() && field.isFinal() && field.getType() == long.class; }
@Override public Object visit(ASTFieldDeclaration node, Object data) { ASTClassOrInterfaceDeclaration cl = node.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class); if (cl != null && node.getVariableName().equalsIgnoreCase(cl.getImage())) { addViolation(data, node); } return data; } }
private static boolean isGetterOrSetter(ASTMethodDeclaration node) { ClassScope scope = node.getScope().getEnclosingScope(ClassScope.class); // fields names mapped to their types Map<String, String> fieldNames = new HashMap<>(); for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> decl : scope.getVariableDeclarations().entrySet()) { ASTFieldDeclaration field = decl.getKey() .getNode() .getFirstParentOfType(ASTFieldDeclaration.class); Matcher matcher = FIELD_NAME_PATTERN.matcher(field.getVariableName()); String varName = matcher.find() ? matcher.group(1) : field.getVariableName(); fieldNames.put(varName, field.getFirstChildOfType(ASTType.class).getTypeImage()); } return isGetter(node, fieldNames) || isSetter(node, fieldNames); }
private void checkRandom(ASTFieldDeclaration fieldDeclaration, List<ASTMethodDeclaration> methodDeclarations, Object data) { for (ASTMethodDeclaration methodDeclaration : methodDeclarations) { try { List<Node> nodes = methodDeclaration.findChildNodesWithXPath(String.format(XPATH_TPL, fieldDeclaration.getVariableName())); if (nodes == null || nodes.isEmpty()) { continue; } for (Node rvNode : nodes) { addViolationWithMessage(data, rvNode, MESSAGE_KEY_PREFIX + ".violation.msg.random", new Object[] {rvNode.getImage()}); } } catch (JaxenException ignore) { } } }
@Override public Object visit(ASTClassOrInterfaceBody node, Object data) { int n = node.jjtGetNumChildren(); List<ASTFieldDeclaration> fields = new ArrayList<>(); Set<String> methodNames = new HashSet<>(); for (int i = 0; i < n; i++) { Node child = node.jjtGetChild(i); if (child.jjtGetNumChildren() == 0) { continue; } child = child.jjtGetChild(child.jjtGetNumChildren() - 1); if (child instanceof ASTFieldDeclaration) { fields.add((ASTFieldDeclaration) child); } else if (child instanceof ASTMethodDeclaration) { methodNames.add(((ASTMethodDeclaration) child).getMethodName().toLowerCase(Locale.ROOT)); } } for (ASTFieldDeclaration field : fields) { String varName = field.getVariableName().toLowerCase(Locale.ROOT); if (methodNames.contains(varName)) { addViolation(data, field, field.getVariableName()); } } return super.visit(node, data); }
private String getNodeName(Node node) { // constructors are differentiated by their parameters, while we only use method name for methods if (node instanceof ASTMethodDeclaration) { return ((ASTMethodDeclaration) node).getMethodName(); } else if (node instanceof ASTMethodOrConstructorDeclaration) { // constructors are differentiated by their parameters, while we only use method name for methods return ((ASTConstructorDeclaration) node).getQualifiedName().getOperation(); } else if (node instanceof ASTFieldDeclaration) { return ((ASTFieldDeclaration) node).getVariableName(); } else if (node instanceof ASTResource) { return ((ASTResource) node).getVariableDeclaratorId().getImage(); } else { return node.getImage(); } }
@Override public Object visit(ASTClassOrInterfaceDeclaration node, Object data) { if (!isPojo(node)) { return super.visit(node, data); } try { List<Node> fields = node.findChildNodesWithXPath( "ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/FieldDeclaration"); for (Node fieldNode : fields) { ASTFieldDeclaration field = (ASTFieldDeclaration)fieldNode; boolean shouldProcess = !field.isPublic() && !field.isStatic() && !field.isTransient(); if (!shouldProcess) { continue; } Class type = field.getType(); // TODO works only in current compilation file, by crossing files will be null if (type != null && type.isPrimitive()) { addViolationWithMessage(data, field.getFirstDescendantOfType(ASTType.class), "java.oop.PojoMustUsePrimitiveFieldRule.violation.msg", new Object[] {field.getVariableName()}); } } } catch (JaxenException e) { throw new RuntimeException(e.getMessage(), e); } return super.visit(node, data); } }
@Override public Object visit(ASTClassOrInterfaceDeclaration node, Object data) { if (!isPojo(node)) { return super.visit(node, data); } try { List<Node> fields = node.findChildNodesWithXPath( "ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/FieldDeclaration"); for (Node fieldNode : fields) { ASTFieldDeclaration field = (ASTFieldDeclaration)fieldNode; boolean shouldProcess = !field.isPublic() && !field.isFinal() && !field.isStatic() && !field .isVolatile() && field.hasDescendantOfType(ASTVariableInitializer.class); if (!shouldProcess) { continue; } ViolationUtils.addViolationWithPrecisePosition(this, field, data, I18nResources.getMessage("java.oop.PojoNoDefaultValueRule.violation.msg", field.getVariableName())); } } catch (JaxenException e) { throw new RuntimeException(e.getMessage(), e); } return super.visit(node, data); } }