/** * 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 isSerialVersionUID(ASTFieldDeclaration field) { return "serialVersionUID".equals(field.getVariableName()) && field.isStatic() && field.isFinal() && field.getType() == long.class; }
@Override String kindDisplayName(ASTVariableDeclaratorId node, PropertyDescriptor<Pattern> descriptor) { ASTFieldDeclaration field = (ASTFieldDeclaration) node.getNthParent(2); if (field.isFinal() && field.isStatic()) { return field.isPublic() ? "public constant" : "constant"; } else if (field.isFinal()) { return "final field"; } else if (field.isStatic()) { return "static field"; } else { return "field"; } }
private int checkType() { if (jjtGetNumChildren() == 0 || !(jjtGetChild(0) instanceof ASTType)) { return 0; } return ((ASTType) jjtGetChild(0)).getArrayDepth(); }
@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); } }
@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); } }
/** * Method counts ONLY public class attributes which are not PUBLIC and * static- these usually represent constants.... */ @Override public Object visit(ASTFieldDeclaration node, Object data) { if (node.isFinal() && node.isStatic()) { return NumericConstants.ZERO; } return this.getTallyOnAccessType(node); }
@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 (!checkMembers) { return data; } boolean isStatic = node.isStatic(); boolean isFinal = node.isFinal(); Node type = node.jjtGetParent().jjtGetParent().jjtGetParent(); // Anything from an interface is necessarily static and final // Anything inside an annotation type is also static and final if (type instanceof ASTClassOrInterfaceDeclaration && ((ASTClassOrInterfaceDeclaration) type).isInterface() || type instanceof ASTAnnotationTypeDeclaration) { isStatic = true; isFinal = true; } return checkVariableDeclarators(node.isStatic() ? staticPrefixes : memberPrefixes, isStatic ? staticSuffixes : memberSuffixes, node, isStatic, isFinal, data); }
@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(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); }
@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 (String formatter: THREAD_SAFE_FORMATTER) { if (TypeHelper.isA(var, formatter)) {
final public void FieldDeclaration(int modifiers) throws ParseException { ASTFieldDeclaration jjtn000 = new ASTFieldDeclaration(this, JJTFIELDDECLARATION); boolean jjtc000 = true; jjtree.openNodeScope(jjtn000); jjtn000.jjtSetFirstToken(getToken(1));jjtn000.setModifiers(modifiers); try { Type(); if (jjtc000) { jjtree.closeNodeScope(jjtn000, true); jjtn000.jjtSetLastToken(getToken(0));
boolean disallowNotAssignment = getProperty(DISALLOW_NOT_ASSIGNMENT); if (node.isPrivate() && !node.isStatic() && !hasClassLombokAnnotation() && !hasIgnoredAnnotation(node)) { for (ASTVariableDeclarator declarator : node.findChildrenOfType(ASTVariableDeclarator.class)) { ASTVariableDeclaratorId declaration = (ASTVariableDeclaratorId) declarator.jjtGetChild(0); List<NameOccurrence> usages = declaration.getUsages();
@Override public Object visit(ASTClassOrInterfaceDeclaration node, Object data) { ASTExtendsList extendsList = node.getFirstChildOfType(ASTExtendsList.class); if (extendsList == null) { return super.visit(node, data); } if (!hasThread(extendsList)) { return super.visit(node, data); } List<ASTMethodDeclaration> methodDeclarations = node.findDescendantsOfType(ASTMethodDeclaration.class); if (methodDeclarations == null || methodDeclarations.isEmpty()) { return super.visit(node, data); } checkMathRandom(methodDeclarations, data); List<ASTFieldDeclaration> fieldDeclarations = node.findDescendantsOfType(ASTFieldDeclaration.class); if (fieldDeclarations == null || fieldDeclarations.isEmpty()) { return super.visit(node, data); } for (ASTFieldDeclaration fieldDeclaration : fieldDeclarations) { if (fieldDeclaration.getType() == Random.class && fieldDeclaration.isStatic()) { checkRandom(fieldDeclaration, methodDeclarations, data); } } return super.visit(node, data); }
@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) { ASTClassOrInterfaceDeclaration cl = node.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class); if (cl != null && node.getVariableName().equalsIgnoreCase(cl.getImage())) { addViolation(data, node); } return data; } }
@Override public Object visit(ASTFieldDeclaration fieldDeclaration, Object data) { if (fieldDeclaration.isVolatile()) { for (ASTVariableDeclaratorId declarator : fieldDeclaration .findDescendantsOfType(ASTVariableDeclaratorId.class)) { this.volatileFields.add(declarator.getImage()); } } return super.visit(fieldDeclaration, 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(ASTFieldDeclaration node, Object data) { Node parent = node.jjtGetParent().jjtGetParent(); for (int i = 0; i < parent.jjtGetNumChildren(); i++) { Node child = parent.jjtGetChild(i); continue; if (node.hasDescendantOfType(ASTClassOrInterfaceBodyDeclaration.class) && getProperty(ignoreAnonymousClassDeclarations)) { continue;