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

The following examples show how to use com.sonar.sslr.api.AstNode#getFirstChild() . 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: 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 2
Source File: FlowCommentLinesVisitor.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) {

  if (astNode.getParent().getType().equals(FlowGrammar.MAP)) {
    AstNode map = astNode.getParent();
    if (map.hasParent(FlowGrammar.INVOKE) || map.hasParent(FlowGrammar.MAPINVOKE)) {
      return;
    }
  } else {
    if (astNode.hasDirectChildren(FlowTypes.ELEMENT_VALUE)) {
      AstNode value = astNode.getFirstChild(FlowTypes.ELEMENT_VALUE);
      if (!value.getTokenOriginalValue().isEmpty()) {
        getContext().peekSourceCode().add(metric, 1);
      }
    }
  }
}
 
Example 3
Source File: ExitCheck.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) {
  AstNode exitNode = astNode.getFirstChild(FlowGrammar.ATTRIBUTES);
  if (exitNode != null) {
    logger.debug("++ Exit interface element found. ++");
    String exitFrom = getExitFrom(exitNode);
    if (exitFrom == null || exitFrom.trim().equals("")) {
      logger.debug("++ \"Exit from\" property found to be empty! ++");
      getContext().createLineViolation(this,
          "The \"Exit from\" " + "property must be defined for the interface element 'EXIT'",
          exitNode);
    }
    if (hasSignalSetToFailure(exitNode)) {
      String exitFailureMessage = getExitFailureMessage(exitNode);
      if (exitFailureMessage == null || exitFailureMessage.trim().equals("")) {
        logger.debug("++ Failure message has not been set even though"
            + " the signal status has been set to failure! ++");
        getContext().createLineViolation(this,
            "Create a Failure message" + " for the interface element 'EXIT'.", exitNode);
      }
    }
  }
}
 
Example 4
Source File: ExitCheck.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
private Boolean hasSignalSetToFailure(AstNode exitNode) {
  if (exitNode != null) {
    AstNode signalAtt = exitNode.getFirstChild(FlowAttTypes.SIGNAL);
    if (signalAtt != null) {
      logger.debug("++ Signal found ++");
      String signalValue = signalAtt.getToken().getOriginalValue();
      if (signalValue != null) {
        if (signalValue.equalsIgnoreCase("FAILURE")) {
          logger.debug("++ Signal is set to FAILURE! ++");
          return true;
        }
      }
    }
  }
  return false;
}
 
Example 5
Source File: BranchPropertiesCheck.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) {
  AstNode attributesNode = astNode.getFirstChild(FlowGrammar.ATTRIBUTES);

  AstNode switchAttribute = attributesNode.getFirstChild(FlowAttTypes.SWITCH);
  Boolean switchDefined = (switchAttribute == null
      || "".equals(switchAttribute.getTokenOriginalValue())) ? false : true;

  AstNode labelExpressions = attributesNode.getFirstChild(FlowAttTypes.LABELEXPRESSIONS);
  Boolean evaluateLabelsDefined = (labelExpressions == null
      || "false".equals(labelExpressions.getTokenOriginalValue())
      || "".equals(labelExpressions.getTokenOriginalValue())) ? false : true;

  if (switchDefined && evaluateLabelsDefined) {
    getContext().createLineViolation(this,
        "Both switch and evaluate labels are defined in properties of BRANCH", astNode);
  }

  if (!switchDefined && !evaluateLabelsDefined) {
    getContext().createLineViolation(this,
        "Evaluate labels must be true when no switch parameter is defined in BRANCH", astNode);
  }
}
 
Example 6
Source File: AssertMessageCheck.java    From enforce-sonarqube-plugin with MIT License 5 votes vote down vote up
/**
 * It is responsible for verifying whether the rule is met in the rule base.
 * In the event that the rule is not correct, create message error.
 *
 * @param astNode It is the node that stores all the rules.
 */
@Override
public void visitNode(AstNode astNode) {
    try {
        if (astNode.hasDescendant(ApexGrammarRuleKey.PRIMARY_SUFFIX)) {
            List<AstNode> suffixes = astNode.getDescendants(ApexGrammarRuleKey.PRIMARY_SUFFIX);
            for (AstNode suffix : suffixes) {
                AstNode primaryExpression = suffix.getParent();
                AstNode name = primaryExpression.getFirstDescendant(ApexGrammarRuleKey.NAME);
                if (name != null) {
                    AstNode first = name.getFirstChild(ApexGrammarRuleKey.METHOD_IDENTIFIER);
                    AstNode last = name.getLastChild(ApexGrammarRuleKey.METHOD_IDENTIFIER);
                    if (first != null && last != null
                            && first.getTokenValue().equals(SYSTEM)
                            && last.getTokenValue().matches(ASSERT_REGEX)) {
                        List<AstNode> arguments = primaryExpression.getFirstDescendant(ApexGrammarRuleKey.ARGUMENTS_LIST).getChildren();
                        if (arguments.size() <= VALUE_OF_ONE || !hasMessage(arguments)) {
                            getContext().createLineViolation(this,
                                    MESSAGE, primaryExpression, astNode.getFirstChild(ApexGrammarRuleKey.METHOD_IDENTIFIER).getTokenOriginalValue());
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        ChecksLogger.logCheckError(this.toString(), "visitNode", e.toString());
    }
}
 
Example 7
Source File: AssertLiteralBooleanCheck.java    From enforce-sonarqube-plugin with MIT License 5 votes vote down vote up
/**
 * It is responsible for verifying whether the rule is met in the rule base.
 * In the event that the rule is not correct, create message error.
 *
 * @param astNode It is the node that stores all the rules.
 */
@Override
public void visitNode(AstNode astNode) {
    try {
        if (astNode.hasDescendant(ApexGrammarRuleKey.PRIMARY_SUFFIX)) {
            List<AstNode> suffixes = astNode.getDescendants(ApexGrammarRuleKey.PRIMARY_SUFFIX);
            for (AstNode suffix : suffixes) {
                AstNode primaryExpression = suffix.getParent();
                AstNode name = primaryExpression.getFirstDescendant(ApexGrammarRuleKey.NAME);
                if (name != null) {
                    AstNode first = name.getFirstChild(ApexGrammarRuleKey.METHOD_IDENTIFIER);
                    AstNode last = name.getLastChild(ApexGrammarRuleKey.METHOD_IDENTIFIER);
                    if (first != null && last != null
                            && first.getTokenValue().equals(SYSTEM)
                            && last.getTokenValue().equals(ASSERT)) {
                        List<AstNode> arguments = primaryExpression.getFirstDescendant(ApexGrammarRuleKey.ARGUMENTS_LIST).getChildren();
                        for (AstNode argument : arguments) {
                            if (argument.getTokenValue().equals(TRUE_LITERAL)
                                    || argument.getTokenValue().equals(FALSE_LITERAL)) {
                                getContext().createLineViolation(this,
                                        MESSAGE, suffix, astNode.getFirstChild(ApexGrammarRuleKey.METHOD_IDENTIFIER).getTokenOriginalValue());
                            }
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        ChecksLogger.logCheckError(this.toString(), "visitNode", e.toString());
    }
}
 
Example 8
Source File: StartAndStopCheck.java    From enforce-sonarqube-plugin with MIT License 5 votes vote down vote up
/**
 * Verifies if the expression is a sequence of test.startTest or
 * test.stopTest
 *
 * @param expression
 * @param keyword
 * @return true if the expression is the right sequence
 */
private boolean isTestMethodCall(AstNode expression, String keyword) {
    AstNode name = expression.getFirstDescendant(ApexGrammarRuleKey.NAME);
    if (name != null) {
        AstNode first = name.getFirstChild(ApexGrammarRuleKey.METHOD_IDENTIFIER);
        AstNode last = name.getLastChild(ApexGrammarRuleKey.METHOD_IDENTIFIER);
        return first != null && last != null
                && first.getTokenValue().equals(TEST)
                && last.getTokenValue().matches(keyword);
    }
    return false;
}
 
Example 9
Source File: SoqlLimitCheck.java    From enforce-sonarqube-plugin with MIT License 5 votes vote down vote up
/**
 * It is responsible for verifying whether the rule is met in the rule base.
 * In the event that the rule is not correct, create message error.
 *
 * @param astNode It is the node that stores all the rules.
 */
@Override
public void visitNode(AstNode astNode) {
    try {
    	AstNode nodeToCheck = astNode;
        if(SoqlParser.isStringQuery(astNode)){
            AstNode parsedQuery = SoqlParser.parseQuery(astNode);
            if(parsedQuery != null) {
                nodeToCheck = parsedQuery;
            }
        }

        if (nodeToCheck.is(ApexGrammarRuleKey.QUERY_EXPRESSION) && !nodeToCheck.hasDescendant(ApexGrammarRuleKey.LIMIT_SENTENCE)) {
        	AstNode selectNode = nodeToCheck.getFirstChild(ApexGrammarRuleKey.SELECT_SENTENCE);
        	if(selectNode.hasDescendant(ApexGrammarRuleKey.AGGREGATE_EXPR)){ 
    			if(nodeToCheck.hasDescendant(ApexGrammarRuleKey.GROUP_BY_SENTENCE)){
    				getContext().createLineViolation(this, MESSAGE, astNode);
    			}
        	}else{
        		getContext().createLineViolation(this, MESSAGE, astNode);
        	}
            
        }
    } catch (Exception e) {
        ChecksLogger.logCheckError(this.toString(), "visitNode", e.toString());
    }
}
 
Example 10
Source File: SoqlInLoopCheck.java    From enforce-sonarqube-plugin with MIT License 5 votes vote down vote up
private AstNode getStatementAstNode(AstNode astNode){
		AstNode statementAstNode = astNode.getFirstChild(ApexGrammarRuleKey.STATEMENT);
 	if(statementAstNode == null){
 		statementAstNode = astNode.getLastChild(); // For this Rule, Last Child will be FOR_LOOP or FOR_EACH_LOOP
 		statementAstNode = getStatementAstNode(statementAstNode);
 	}
 	return statementAstNode;
}
 
Example 11
Source File: AnnotationMethodCheck.java    From enforce-sonarqube-plugin with MIT License 5 votes vote down vote up
/**
 * Analyzes if a node is annotation class or method.
 *
 * @param astNode to be analyzed.
 * @param keyword to be found.
 * @return the analysis result.
 */
protected boolean isAnnotation(AstNode astNode, String keyword) {
    boolean result = Boolean.FALSE;
    if (astNode.hasDirectChildren(ApexGrammarRuleKey.ANNOTATION)) {
        AstNode annotation = astNode.getFirstChild(ApexGrammarRuleKey.ANNOTATION);
        result = annotation.getFirstChild(ApexGrammarRuleKey.NAME).getTokenOriginalValue().equals(keyword);
    }
    return result;
}
 
Example 12
Source File: TryCatchCheck.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
private String getSequenceType(AstNode sequenceNode) {
  if (sequenceNode != null) {
    AstNode attributes = sequenceNode.getFirstChild(FlowGrammar.ATTRIBUTES);
    if (attributes != null) {
      AstNode exitOn = attributes.getFirstChild(FlowAttTypes.EXITON);
      if (exitOn != null) {
        return exitOn.getTokenValue();
      }
    }
  }
  return null;
}
 
Example 13
Source File: ExitCheck.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
private String getExitFrom(AstNode exitNode) {
  if (exitNode != null) {
    AstNode fromAtt = exitNode.getFirstChild(FlowAttTypes.FROM);
    if (fromAtt != null) {
      String fromType = fromAtt.getToken().getOriginalValue();
      logger.debug("++ From field found! ++");
      if (fromType != null) {
        return fromType;
      }
    }
  }
  return null;
}
 
Example 14
Source File: ExitCheck.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
private String getExitFailureMessage(AstNode exitNode) {
  if (exitNode != null) {
    AstNode failureMessageAtt = exitNode.getFirstChild(FlowAttTypes.FAILUREMESSAGE);
    if (failureMessageAtt != null) {
      logger.debug("++ Failure message field found! ++");
      return failureMessageAtt.getToken().getOriginalValue();
    }
  }
  return null;
}
 
Example 15
Source File: TryCatchCheck.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 4 votes vote down vote up
private AstNode getContent(AstNode sequenceNode) {
  if (sequenceNode != null) {
    return sequenceNode.getFirstChild(FlowGrammar.CONTENT);
  }
  return null;
}