public boolean isVarargs() { ASTVariableDeclaratorId astVariableDeclaratorId = (ASTVariableDeclaratorId) node; ASTFormalParameter parameter = astVariableDeclaratorId.getFirstParentOfType(ASTFormalParameter.class); return parameter != null && parameter.isVarargs(); }
/** * Returns the name of the variable. */ public String getVariableName() { return getImage(); }
@Override public Object visit(ASTLocalVariableDeclaration decl, Object data) { for (int i = 0; i < decl.jjtGetNumChildren(); i++) { if (!(decl.jjtGetChild(i) instanceof ASTVariableDeclarator)) { continue; } ASTVariableDeclaratorId node = (ASTVariableDeclaratorId) decl.jjtGetChild(i).jjtGetChild(0); // TODO this isArray() check misses some cases // need to add DFAish code to determine if an array // is initialized locally or gotten from somewhere else if (!node.getNameDeclaration().isArray() && !actuallyUsed(node.getUsages())) { addViolation(data, node, node.getNameDeclaration().getImage()); } } return data; }
/** * Returns true if this node declares a formal parameter for * a lambda expression. In that case, the type of this parameter * is not necessarily inferred, see {@link #isTypeInferred()}. */ public boolean isLambdaParameter() { return isLambdaParamWithNoType() || jjtGetParent() instanceof ASTFormalParameter && getNthParent(3) instanceof ASTLambdaExpression; }
private boolean isLocalVariableTypeInferred() { if (isResourceDeclaration()) { // covers "var" in try-with-resources return jjtGetParent().getFirstChildOfType(ASTType.class) == null; } else if (getNthParent(2) instanceof ASTLocalVariableDeclaration) { // covers "var" as local variables and in for statements return getNthParent(2).getFirstChildOfType(ASTType.class) == null; } return false; }
private boolean isLambdaTypeInferred() { return getNthParent(3) instanceof ASTLambdaExpression && jjtGetParent().getFirstChildOfType(ASTType.class) == null; }
ASTVariableDeclaratorId jjtn000 = new ASTVariableDeclaratorId(this, JJTVARIABLEDECLARATORID); boolean jjtc000 = true; jjtree.openNodeScope(jjtn000); jjtn000.jjtSetFirstToken(getToken(1));Token t; String image; try { jj_consume_token(DOT); jj_consume_token(THIS); checkforBadExplicitReceiverParameter(); jjtn000.setExplicitReceiverParameter(); image=t.image + ".this"; } else { switch (jj_nt.kind) { case THIS: t = jj_consume_token(THIS); checkforBadExplicitReceiverParameter(); jjtn000.setExplicitReceiverParameter(); image = t.image; break; case IDENTIFIER: jjtn000.bumpArrayDepth(); jjtn000.jjtSetLastToken(getToken(0)); checkForBadAssertUsage(image, "a variable name"); checkForBadEnumUsage(image, "a variable name"); checkForBadIdentifier(image); jjtn000.setImage( image ); } finally { if (jjtc000) { jjtree.closeNodeScope(jjtn000, true); jjtn000.jjtSetLastToken(getToken(0));
@Override public Object visit(ASTFieldDeclaration node, Object data) { for (ASTVariableDeclaratorId declaration : node.findDescendantsOfType(ASTVariableDeclaratorId.class)) { for (NameOccurrence no : declaration.getUsages()) { Node location = no.getLocation(); System.out.println(declaration.getImage() + " is used here: " + location.getImage()); } } return data; } }
private void ensureClosed(ASTLocalVariableDeclaration var, ASTVariableDeclaratorId id, Object data) { String variableToClose = id.getImage(); Node n = var; ASTBlockStatement parentBlock = id.getFirstParentOfType(ASTBlockStatement.class); if (t.getBeginLine() > id.getBeginLine() && t.hasFinally()) { ASTBlock f = (ASTBlock) t.getFinally().jjtGetChild(0); List<ASTName> names = f.findDescendantsOfType(ASTName.class);
@Override public Object visit(final ASTVariableDeclaratorId node, Object data) { // Constant named does not apply to this rule ASTTypeDeclaration typeDeclaration = node.getFirstParentOfType(ASTTypeDeclaration.class); Node jjtGetChild = typeDeclaration.jjtGetChild(0); if (jjtGetChild instanceof ASTAnnotationTypeDeclaration) { return super.visit(node, data); } ASTFieldDeclaration astFieldDeclaration = node.getFirstParentOfType(ASTFieldDeclaration.class); boolean isNotCheck = astFieldDeclaration != null && (astFieldDeclaration.isFinal() || astFieldDeclaration .isStatic()); if (isNotCheck) { return super.visit(node, data); } // variable naming violate lowerCamelCase if (!(pattern.matcher(node.getImage()).matches())) { ViolationUtils.addViolationWithPrecisePosition(this, node, data, I18nResources.getMessage(MESSAGE_KEY_PREFIX + ".variable", node.getImage())); } return super.visit(node, data); }
@Override public Object visit(ASTVariableDeclaratorId node, Object data) { if (node == null || node.isTypeInferred()) { return super.visit(node, data); } // Type common to all declarations in the same statement JavaTypeDefinition baseType = node.getTypeNode().getTypeDefinition(); if (baseType != null) { // add the dimensions specific to the declarator id node.setTypeDefinition(baseType.withDimensions(node.getArrayDepth())); } return super.visit(node, data); }
@Override public Object visit(ASTVariableDeclaratorId node, Object data) { Node nameNode = node.getTypeNameNode(); if (nameNode == null || nameNode instanceof ASTPrimitiveType || !appliesToClassName(node.getNameDeclaration().getTypeImage())) { return data; } List<NameOccurrence> declars = node.getUsages(); for (NameOccurrence occ : declars) { JavaNameOccurrence jocc = (JavaNameOccurrence) occ; if (!isTargetMethod(jocc)) { continue; } Node expr = jocc.getLocation().jjtGetParent().jjtGetParent().jjtGetParent(); checkNodeAndReport(data, jocc.getLocation(), expr); } return data; }
@Override public Object visit(ASTVariableDeclaratorId node, Object data) { if (!TypeHelper.isExactlyAny(node.getNameDeclaration(), String.class) && !TypeHelper.isExactlyAny(node.getNameDeclaration(), String[].class)) { return data; } boolean isArray = node.isArray(); for (NameOccurrence occ : node.getUsages()) { JavaNameOccurrence jocc = (JavaNameOccurrence) occ; NameOccurrence qualifier = jocc.getNameForWhichThisIsAQualifier(); if (qualifier != null) { if (!isArray && isNotAMethodReference(qualifier) && qualifier.getImage().indexOf("toString") != -1) { addViolation(data, jocc.getLocation()); } else if (isArray && isNotAName(qualifier) && qualifier.getImage().equals("toString")) { addViolation(data, jocc.getLocation()); } } } return data; }
/** * Returns true if the variable declared by this node is declared final. * Doesn't account for the "effectively-final" nuance. Resource * declarations are implicitly final. */ public boolean isFinal() { if (isResourceDeclaration()) { // this is implicit even if "final" is not explicitly declared. return true; } else if (isLambdaParamWithNoType()) { return false; } if (jjtGetParent() instanceof ASTFormalParameter) { // This accounts for exception parameters too for now return ((ASTFormalParameter) jjtGetParent()).isFinal(); } Node grandpa = getNthParent(2); if (grandpa instanceof ASTLocalVariableDeclaration) { return ((ASTLocalVariableDeclaration) grandpa).isFinal(); } else if (grandpa instanceof ASTFieldDeclaration) { return ((ASTFieldDeclaration) grandpa).isFinal(); } throw new IllegalStateException("All cases should be handled"); }
formalParameter.jjtSetParent(formalParameters); ASTVariableDeclaratorId variableDeclaratorId = new ASTVariableDeclaratorId( JavaParserTreeConstants.JJTVARIABLEDECLARATORID); variableDeclaratorId.setImage("arg" + i); formalParameter.jjtAddChild(variableDeclaratorId, 1); variableDeclaratorId.jjtSetParent(formalParameter);
private static boolean isStringBuilderOrBuffer(ASTVariableDeclaratorId node) { if (node.getType() != null) { return TypeHelper.isEither(node, StringBuffer.class, StringBuilder.class); } Node nn = node.getTypeNameNode(); if (nn == null || nn.jjtGetNumChildren() == 0) { return false; } return TypeHelper.isEither((TypeNode) nn.jjtGetChild(0), StringBuffer.class, StringBuilder.class); } }
@Override public Object visit(ASTVariableDeclaratorId node, Object data) { VariableNameDeclaration decl = new VariableNameDeclaration(node); node.getScope().addDeclaration(decl); node.setNameDeclaration(decl); return super.visit(node, data); }
private List<NameOccurrence> determineUsages(ASTVariableDeclaratorId node) { Map<VariableNameDeclaration, List<NameOccurrence>> decls = node.getScope() .getDeclarations(VariableNameDeclaration.class); for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : decls.entrySet()) { // find the first variable that matches if (node.hasImageEqualTo(entry.getKey().getName())) { return entry.getValue(); } } return Collections.emptyList(); }
@Override public Object visit(ASTVariableDeclaratorId node, Object data) { if (node == null || node.getNameDeclaration() == null) { return super.visit(node, data); } String name = node.getNameDeclaration().getTypeImage(); if (name != null) { populateType(node, name); } return super.visit(node, data); }
private int getArrayDimensionOnDeclaratorId() { return getDecl().getArrayDepth(); }