/** * Gets the variable name of this field. This method searches the first * VariableDeclartorId node and returns its image or <code>null</code> if * the child node is not found. * * * @deprecated FieldDeclaration may declare several variables, so this is not exhaustive * Iterate on the {@linkplain ASTVariableDeclaratorId VariableDeclaratorIds} instead * * * @return a String representing the name of the variable */ @Deprecated public String getVariableName() { ASTVariableDeclaratorId decl = getFirstDescendantOfType(ASTVariableDeclaratorId.class); if (decl != null) { return decl.getImage(); } return null; }
@Override public Object visit(final ASTFieldDeclaration decl, final Object data) { if (shouldReport(decl)) { addViolationWithMessage(data, decl, String.format(MESSAGE, decl.getFirstDescendantOfType(ASTVariableDeclaratorId.class).getImage(), "field")); } return super.visit(decl, data); }
/** * Tells if the type declaration has a field with varName. * * @param varName * the name of the field to search * @param typeDeclaration * the type declaration * @return <code>true</code> if there is a field in the type declaration * named varName, <code>false</code> in other case */ protected final boolean isField(String varName, ASTTypeDeclaration typeDeclaration) { final List<ASTFieldDeclaration> fds = typeDeclaration.findDescendantsOfType(ASTFieldDeclaration.class); if (fds != null) { for (ASTFieldDeclaration fd : fds) { final ASTVariableDeclaratorId vid = fd.getFirstDescendantOfType(ASTVariableDeclaratorId.class); if (vid != null && vid.hasImageEqualTo(varName)) { return true; } } } return false; }
private boolean isEmptyArray(String varName, ASTTypeDeclaration typeDeclaration) { final List<ASTFieldDeclaration> fds = typeDeclaration.findDescendantsOfType(ASTFieldDeclaration.class); if (fds != null) { for (ASTFieldDeclaration fd : fds) { final ASTVariableDeclaratorId vid = fd.getFirstDescendantOfType(ASTVariableDeclaratorId.class); if (vid != null && vid.hasImageEqualTo(varName)) { ASTVariableInitializer initializer = fd.getFirstDescendantOfType(ASTVariableInitializer.class); if (initializer != null && initializer.jjtGetNumChildren() == 1) { Node child = initializer.jjtGetChild(0); if (child instanceof ASTArrayInitializer && child.jjtGetNumChildren() == 0) { return true; } else if (child instanceof ASTExpression) { try { List<? extends Node> arrayAllocation = child.findChildNodesWithXPath( "./PrimaryExpression/PrimaryPrefix/AllocationExpression/ArrayDimsAndInits/Expression/PrimaryExpression/PrimaryPrefix/Literal[@IntLiteral=\"true\"][@Image=\"0\"]"); if (arrayAllocation != null && arrayAllocation.size() == 1) { return true; } } catch (JaxenException e) { return false; } } } } } } return false; } }
return data; ASTClassOrInterfaceType cit = node.getFirstDescendantOfType(ASTClassOrInterfaceType.class); if (cit == null || !TypeHelper.isA(cit, formatterClassToCheck)) { return data; ASTVariableDeclaratorId var = node.getFirstDescendantOfType(ASTVariableDeclaratorId.class); for (String formatter: THREAD_SAFE_FORMATTER) { if (TypeHelper.isA(var, formatter)) {
/** * Gets the variable name of this field. This method searches the first * VariableDeclartorId node and returns its image or <code>null</code> if * the child node is not found. * * * @deprecated FieldDeclaration may declare several variables, so this is not exhaustive * Iterate on the {@linkplain ASTVariableDeclaratorId VariableDeclaratorIds} instead * * * @return a String representing the name of the variable */ @Deprecated public String getVariableName() { ASTVariableDeclaratorId decl = getFirstDescendantOfType(ASTVariableDeclaratorId.class); if (decl != null) { return decl.getImage(); } return null; }
private boolean checkThreadLocalWithInitalValue(ASTFieldDeclaration fieldDeclaration) { ASTVariableDeclarator variableDeclarator = fieldDeclaration.getFirstDescendantOfType( ASTVariableDeclarator.class); if (variableDeclarator == null) { return false; } ASTMethodDeclarator initialValueMethod = variableDeclarator.getFirstDescendantOfType(ASTMethodDeclarator.class); if (initialValueMethod != null && METHOD_INITIAL_VALUE.equals(initialValueMethod.getImage())) { return true; } ASTName name = variableDeclarator.getFirstDescendantOfType(ASTName.class); return name != null && WITH_INITIAL.equals(name.getImage()); }
/** * Tells if the type declaration has a field with varName. * * @param varName * the name of the field to search * @param typeDeclaration * the type declaration * @return <code>true</code> if there is a field in the type declaration * named varName, <code>false</code> in other case */ protected final boolean isField(String varName, ASTTypeDeclaration typeDeclaration) { final List<ASTFieldDeclaration> fds = typeDeclaration.findDescendantsOfType(ASTFieldDeclaration.class); if (fds != null) { for (ASTFieldDeclaration fd : fds) { final ASTVariableDeclaratorId vid = fd.getFirstDescendantOfType(ASTVariableDeclaratorId.class); if (vid != null && vid.hasImageEqualTo(varName)) { return true; } } } return false; }
@Override public Object visit(final ASTFieldDeclaration decl, final Object data) { if (shouldReport(decl)) { addViolationWithMessage(data, decl, String.format(MESSAGE, decl.getFirstDescendantOfType(ASTVariableDeclaratorId.class).getImage(), "field")); } return super.visit(decl, data); }
private boolean isEmptyArray(String varName, ASTTypeDeclaration typeDeclaration) { final List<ASTFieldDeclaration> fds = typeDeclaration.findDescendantsOfType(ASTFieldDeclaration.class); if (fds != null) { for (ASTFieldDeclaration fd : fds) { final ASTVariableDeclaratorId vid = fd.getFirstDescendantOfType(ASTVariableDeclaratorId.class); if (vid != null && vid.hasImageEqualTo(varName)) { ASTVariableInitializer initializer = fd.getFirstDescendantOfType(ASTVariableInitializer.class); if (initializer != null && initializer.jjtGetNumChildren() == 1) { Node child = initializer.jjtGetChild(0); if (child instanceof ASTArrayInitializer && child.jjtGetNumChildren() == 0) { return true; } else if (child instanceof ASTExpression) { try { List<? extends Node> arrayAllocation = child.findChildNodesWithXPath( "./PrimaryExpression/PrimaryPrefix/AllocationExpression/ArrayDimsAndInits/Expression/PrimaryExpression/PrimaryPrefix/Literal[@IntLiteral=\"true\"][@Image=\"0\"]"); if (arrayAllocation != null && arrayAllocation.size() == 1) { return true; } } catch (JaxenException e) { return false; } } } } } } return false; } }
@Override public Object visit(ASTFieldDeclaration node, Object data) { if (!(node.isStatic() && node.isFinal())) { return super.visit(node, data); } //If the variable is of type Log or Logger,do not check ASTClassOrInterfaceType classOrInterfaceType = node.getFirstDescendantOfType(ASTClassOrInterfaceType.class); if (classOrInterfaceType != null && LOG_VARIABLE_TYPE_SET.contains(classOrInterfaceType.getImage())) { return super.visit(node, data); } //filter by white list,such as the serialVersionUID String constantName = node.jjtGetChild(1).jjtGetChild(0).getImage(); boolean inWhiteList = StringUtils.isEmpty(constantName) || WHITE_LIST.contains(constantName) || constantName.endsWith(SERVICE_SUFFIX); if (inWhiteList) { return super.visit(node, data); } //Constant should be upper if (!(constantName.equals(constantName.toUpperCase()))) { ViolationUtils.addViolationWithPrecisePosition(this, node, data, I18nResources.getMessage("java.naming.ConstantFieldShouldBeUpperCaseRule.violation.msg", constantName)); } return super.visit(node, data); } }
@Override public Object visit(ASTFieldDeclaration node, Object data) { if (!node.isStatic()) { return data; } ASTClassOrInterfaceType cit = node.getFirstDescendantOfType(ASTClassOrInterfaceType.class); if (cit == null || !TypeHelper.isA(cit, formatterClassToCheck)) { return data; } ASTVariableDeclaratorId var = node.getFirstDescendantOfType(ASTVariableDeclaratorId.class); for (NameOccurrence occ : var.getUsages()) { Node n = occ.getLocation(); if (n.getFirstParentOfType(ASTSynchronizedStatement.class) != null) { continue; } // ignore usages, that don't call a method. if (!n.getImage().contains(".")) { continue; } ASTMethodDeclaration method = n.getFirstParentOfType(ASTMethodDeclaration.class); if (method != null && !method.isSynchronized()) { addViolation(data, n); } } return 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.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); } }