Java Code Examples for com.sonar.sslr.api.AstNode#getChildren()

The following examples show how to use com.sonar.sslr.api.AstNode#getChildren() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DeeplyNestedIfStmtsCheck.java    From enforce-sonarqube-plugin with MIT License 6 votes vote down vote up
/**
 * Start at the top of each method declaration.
 * Inspect each top level if-then statement.
 *
 * @param node starts us at the very top of the method declaration.
 */
@Override
public void visitNode(AstNode node) {
	try {
		List<AstNode> blockChildren = node.getChildren(ApexGrammarRuleKey.BLOCK);
		List<AstNode> blockStatementChildren;
		List<AstNode> statementChildren;
		
		for(AstNode bChild : blockChildren){
			blockStatementChildren = bChild.getChildren(ApexGrammarRuleKey.BLOCK_STATEMENT);
			for(AstNode bsChild : blockStatementChildren){
				statementChildren = bsChild.getChildren(ApexGrammarRuleKey.STATEMENT);
				for(AstNode sChild : statementChildren){
					counter = 0;
					recursiveStatementMethod(sChild);
				}
			}
		}
	} catch (Exception e) {
		ChecksLogger.logCheckError(this.toString(), "visitNode", e.toString());
	}
}
 
Example 2
Source File: AsyncMethodsCheck.java    From enforce-sonarqube-plugin with MIT License 6 votes vote down vote up
/**
 * Determines if a method is Async by looking for the "@future" amongst it's
 * annotations.
 *
 * @param astNode the node of the loop statement.
 * @param methodName the name of the method.
 * @return True if the method has the "@future" annotation.
 */
private boolean methodIsAsync(AstNode astNode, String methodName) {
    AstNode firstAncestor = astNode.getFirstAncestor(ApexGrammarRuleKey.TYPE_DECLARATION);
    List<AstNode> methods = firstAncestor.getDescendants(ApexGrammarRuleKey.METHOD_DECLARATION);
    if (!methods.isEmpty()) {
        for (AstNode method : methods) {
            String name = method.getFirstChild(ApexGrammarRuleKey.METHOD_IDENTIFIER).getTokenValue();
            if (name.equals(methodName)) {
                AstNode member = method.getFirstAncestor(ApexGrammarRuleKey.CLASS_OR_INTERFACE_MEMBER);
                AstNode modifiers = member.getFirstChild(ApexGrammarRuleKey.MODIFIERS);
                if (modifiers.hasDescendant(ApexGrammarRuleKey.ANNOTATION)) {
                    List<AstNode> annotations = modifiers.getChildren(ApexGrammarRuleKey.ANNOTATION);
                    for (AstNode annotation : annotations) {
                        String annotationValue = annotation.getFirstChild(ApexGrammarRuleKey.NAME).getTokenValue();
                        if (annotationValue.equalsIgnoreCase(FUTURE)) {
                            return true;
                        }
                    }
                }
            }
        }
    }
    return false;
}
 
Example 3
Source File: EmptyFlowCheck.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void visitNode(AstNode astNode) {
  List<AstNode> flowChildren = astNode.getChildren();
  for (AstNode child : flowChildren) {
    if (child.getName().equalsIgnoreCase("CONTENT")) {
      List<AstNode> contentChildren = child.getChildren();
      int numberOfSteps = contentChildren.size();
      if (numberOfSteps == 1) {
        logger.debug("The service contains {} flow step.", numberOfSteps);
      } else if (numberOfSteps == 0) {
        getContext().createLineViolation(this,
            "Service doesn't contain any flow steps. Remove service or add flow steps.", astNode);
        logger.debug("The service contains {} flow steps. Remove this service or add flow steps.",
            numberOfSteps);
      } else if (numberOfSteps > 1) {
        logger.debug("The service contains {} flow steps.", numberOfSteps);
      }
    }
  }
}
 
Example 4
Source File: InterfaceCommentsCheck.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void visitNode(AstNode astNode) {
  for (AstNode record : astNode.getChildren(NodeGrammar.RECORD)) {
    for (AstNode value : record.getChildren(NodeGrammar.VALUE)) {
      for (AstNode attr : value.getChildren(NodeGrammar.ATTRIBUTES)) {
        for (AstNode name : attr.getChildren(FlowAttTypes.NAME)) {
          if (name.getTokenValue().equals("NODE_COMMENT")) {
            if (attr.getParent().getChildren(FlowTypes.ELEMENT_VALUE).size() <= 0) {
              logger.debug("++ Comment VIOLATION found: " + value.getTokenLine() + " ++");
              getContext().createLineViolation(this, "Add comment", value);
            }
          }
        }
      }
    }
  }
}
 
Example 5
Source File: DeeplyNestedIfStmtsCheck.java    From enforce-sonarqube-plugin with MIT License 5 votes vote down vote up
private void traverseBlockStatement(AstNode blockNode){
	for(AstNode blockStatementNode : blockNode.getChildren(ApexGrammarRuleKey.BLOCK_STATEMENT)){
		for(AstNode statementNode : blockStatementNode.getChildren(ApexGrammarRuleKey.STATEMENT)){
			recursiveStatementMethod(statementNode);
		}
	}
}
 
Example 6
Source File: MethodChecksUtils.java    From enforce-sonarqube-plugin with MIT License 5 votes vote down vote up
/**
 * Method that searches for testMethod keyword in a method given a node.
 *
 * @param astNode parent node for method.
 * @return a boolean value, returns true if there is a testMethod keyword or
 * false otherwise.
 */
public static boolean hasTestMethodKeyword(AstNode astNode) {
    boolean hasAnnotation = false;
    List<AstNode> modifiersChildren = astNode.getChildren(MODIFIERS);
    for (AstNode modifier : modifiersChildren) {
        for (AstNode modifierChild : modifier.getChildren()) {
            if ((modifierChild.getTokenOriginalValue().matches(TEST_METHOD_PATTERN))) {
                hasAnnotation = true;
                break;
            }
        }
    }
    return hasAnnotation;
}
 
Example 7
Source File: EmptyMapCheck.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void visitNode(AstNode astNode) {
  String mode = astNode.getFirstChild(FlowGrammar.ATTRIBUTES).getFirstChild(FlowAttTypes.MODE)
      .getToken().getOriginalValue();
  if (mode.equalsIgnoreCase("STANDALONE")) {
    logger.debug("++ Map step found in the flow ++");
    boolean isEmptyMap = true;
    List<AstNode> children = astNode.getChildren();
    for (AstNode child : children) {
      if (child.getName().equalsIgnoreCase("MAPPING")) {
        for (AstNode child2 : child.getChildren()) {
          if (child2.getTokenOriginalValue().equalsIgnoreCase("MAPINVOKE")
              || child2.getTokenOriginalValue().equalsIgnoreCase("MAPDELETE")
              || child2.getTokenOriginalValue().equalsIgnoreCase("MAPCOPY")
              || child2.getTokenOriginalValue().equalsIgnoreCase("MAPSET")) {
            logger.debug("++ This is not an empty map ++");
            isEmptyMap = false;
          }
        }
      }
    }
    if (isEmptyMap) {
      logger.debug("++ This map step in the flow is empty, create content or remove the map. ++");
      getContext().createLineViolation(this,
          "This map step in the flow is empty, " + "create content or remove the map.", astNode);
    }
  }
}