Esempio n. 1
0
  @Override
  public void enterMethodBody(MethodBodyContext ctx) {
    patternMatchFlagsForMethod = new HashMap<String, Boolean>();
    patternMatchedInMethod = false;

    lastMatchingIfForPattern = new HashMap<String, CodePart>();
    allMatchingIfStmtsForPattern = new HashMap<String, List<CodePart>>();
    nearestMatchingIfsForPattern = new HashMap<String, List<CodePart>>();

    if (searchPatterns != null) {
      String methodText =
          rewriter.getText().substring(ctx.start.getStartIndex(), ctx.stop.getStopIndex() + 1);

      if (methodText.toLowerCase().indexOf("processfacade") != -1
          || methodText.toLowerCase().indexOf("processdelegate") != -1) {
        conditionText.append(
            "\nProcessCalls References are found. Class : ["
                + className
                + "] Method :["
                + actMethodName
                + "]\n");
        patternText.append(
            "\nProcessCalls References are found. Class : ["
                + className
                + "] Method :["
                + actMethodName
                + "]\n");
        conditionText.append("Line Number==>" + ctx.start.getLine() + "\n");
        patternText.append("Line Number==>" + ctx.start.getLine() + "\n");
      }

      for (String searchKeyword : searchPatterns) {
        String actualPropertyValue = "";
        if (messageMap != null && messageMap.get(searchKeyword) != null) {
          actualPropertyValue = " [" + messageMap.get(searchKeyword) + "] ";
        }

        if (searchKeyword != null) {
          String keywordWOQuote = Pattern.quote(searchKeyword);
          pattern = Pattern.compile(keywordWOQuote, Pattern.CASE_INSENSITIVE);

          if (pattern.matcher(methodText).find()) {
            Matcher match = pattern.matcher(methodText);
            match.find();
            patternMatchedInMethod = true;
            patternMatchFlagsForMethod.put(searchKeyword, true);

            if (actClassName != null) {
              patternText.append("\nClass name==>" + actClassName + "\n");
              actClassName = null;
            }
            if (actMethodName != null) {
              patternText.append("Method name==>" + actMethodName + "\n");
              actMethodName = null;
            }
            patternText.append(
                "Pattern Matched for searchPattern==>"
                    + searchKeyword
                    + actualPropertyValue
                    + "\n");
          }
        }
      }
    }
  }
Esempio n. 2
0
  @Override
  public void enterStatement(StatementContext ctx) {
    if (patternMatchedInMethod) {
      boolean isIfStmt = (ctx.getText().indexOf("if") == 0);

      for (String searchKeyword : searchPatterns) {

        if (patternMatchFlagsForMethod.get(searchKeyword) != null) {

          if (patternMatchFlagsForMethod.get(searchKeyword).booleanValue()) {

            if (isIfStmt) {

              conditionText.append("From statement handler" + "\n");
              conditionText.append("Line no==>" + ctx.start.getLine() + "\n");
              conditionText.append(
                  rewriter
                          .getText()
                          .substring(ctx.start.getStartIndex(), ctx.stop.getStopIndex() + 1)
                      + "\n");

              String actualPropertyValue = "";
              if (messageMap != null && messageMap.get(searchKeyword) != null) {
                actualPropertyValue = " [" + messageMap.get(searchKeyword) + "] ";
              }

              CodePart lastMatchingCodePart = lastMatchingIfForPattern.get(searchKeyword);

              if (lastMatchingCodePart != null) {

                if (lastMatchingCodePart.getBlockText().indexOf(ctx.getText()) == -1) {
                  if (lastMatchingCodePart.isElseIf()) {
                    List<CodePart> allMatchingIfStmts =
                        allMatchingIfStmtsForPattern.get(searchKeyword);
                    int length = allMatchingIfStmts.size();
                    for (int j = length - 1; j >= 0; j--) {
                      if (allMatchingIfStmts
                              .get(j)
                              .getBlockText()
                              .indexOf(lastMatchingCodePart.getBlockText())
                          > 0) {
                        patternText.append("MATCHING IFELSEIF START: " + searchKeyword + "\n");
                        if (actClassName != null && actMethodName != null) {
                          patternText.append("\nClass name==>" + actClassName + "\n");
                          patternText.append("Method name==>" + actMethodName + "\n");
                        }
                        System.out.println("MATCHING IF : " + searchKeyword + "\n");
                        // System.out.println(ifConditionList.get(j));

                        CommonUtils.addValueToTargetMap(
                            ruleID, allMatchingIfStmts.get(j), nearestMatchingIfsForPattern);

                        patternText.append(
                            "Line Number==>"
                                + tokens.get(allMatchingIfStmts.get(j).getStartIndex()).getLine()
                                + "\n");
                        System.out.println(
                            "Line Number==>"
                                + tokens.get(allMatchingIfStmts.get(j).getStartIndex()).getLine()
                                + "\n");

                        patternText.append(
                            tokens.getText(
                                    tokens.get(allMatchingIfStmts.get(j).getStartIndex()),
                                    tokens.get(allMatchingIfStmts.get(j).getEndIndex()))
                                + "\n");
                        System.out.println(
                            tokens.getText(
                                    tokens.get(allMatchingIfStmts.get(j).getStartIndex()),
                                    tokens.get(allMatchingIfStmts.get(j).getEndIndex()))
                                + "\n");
                        patternText.append("MATCHING IF END: " + searchKeyword + "\n");
                        break;
                      }
                    }

                  } else {

                    patternText.append("MATCHING IF START: " + searchKeyword + "\n");

                    CommonUtils.addValueToTargetMap(
                        ruleID, lastMatchingCodePart, nearestMatchingIfsForPattern);
                    if (actClassName != null && actMethodName != null) {
                      patternText.append("\nClass name==>" + actClassName + "\n");
                      patternText.append("Method name==>" + actMethodName + "\n");
                    }
                    System.out.println("MATCHING IF : " + searchKeyword + "\n");

                    patternText.append(
                        "Line Number==>"
                            + tokens.get(lastMatchingCodePart.getStartIndex()).getLine()
                            + "\n");

                    System.out.println(
                        "Line Number==>"
                            + tokens.get(lastMatchingCodePart.getStartIndex()).getLine()
                            + "\n");

                    patternText.append(
                        tokens.getText(
                                tokens.get(lastMatchingCodePart.getStartIndex()),
                                tokens.get(lastMatchingCodePart.getEndIndex()))
                            + "\n");

                    System.out.println(
                        tokens.getText(
                                tokens.get(lastMatchingCodePart.getStartIndex()),
                                tokens.get(lastMatchingCodePart.getEndIndex()))
                            + "\n");

                    patternText.append("MATCHING IF END: " + searchKeyword + "\n");
                  }
                  lastMatchingIfForPattern.put(searchKeyword, null);
                }
              }

              if (searchKeyword != null) {
                String keywordWOQuote = Pattern.quote(searchKeyword);
                pattern = Pattern.compile(keywordWOQuote, Pattern.CASE_INSENSITIVE);
                if (pattern.matcher(ctx.getText()).find()) {

                  CodePart codePart = null;
                  // We need to check for else closer to if
                  // condition
                  // TODO ( Last 3 tokens generally
                  // matches..Need to double check this )
                  StringBuffer strBufferToChkforElseIf = new StringBuffer();
                  for (int i = 3; i >= 1; i--) {
                    strBufferToChkforElseIf.append(
                        tokens.get(ctx.start.getTokenIndex() - i).getText());
                  }
                  String elseIfChkStr = strBufferToChkforElseIf.toString().trim();
                  System.out.println("elseIfChkStr-->" + elseIfChkStr);

                  codePart =
                      new CodePart(
                          ctx.start.getTokenIndex(),
                          ctx.stop.getTokenIndex(),
                          ctx.getText(),
                          false,
                          ctx.start.getLine());
                  codePart.setReadableText(tokens.getText(ctx));

                  if (elseIfChkStr.equals("else")) {

                    codePart.setElseIf(true);
                  } else {
                    CommonUtils.addValueToTargetMap(
                        searchKeyword, codePart, allMatchingIfStmtsForPattern);
                  }

                  lastMatchingIfForPattern.put(searchKeyword, codePart);
                }
              }
            } else {

              if (searchKeyword != null) {
                CodePart lastMatchingCodePart = lastMatchingIfForPattern.get(searchKeyword);

                if (lastMatchingCodePart != null
                    && lastMatchingCodePart.getBlockText().indexOf(ctx.getText()) == -1) {

                  patternText.append("MATCHING LAST IF START: " + searchKeyword + "\n");

                  CommonUtils.addValueToTargetMap(
                      ruleID, lastMatchingCodePart, nearestMatchingIfsForPattern);
                  if (actClassName != null && actMethodName != null) {
                    patternText.append("\nClass name==>" + actClassName + "\n");
                    patternText.append("Method name==>" + actMethodName + "\n");
                  }
                  System.out.println("MATCHING IF : " + searchKeyword + "\n");

                  patternText.append(
                      "Line Number==>"
                          + tokens.get(lastMatchingCodePart.getStartIndex()).getLine()
                          + "\n");

                  System.out.println(
                      "Line Number==>"
                          + tokens.get(lastMatchingCodePart.getStartIndex()).getLine()
                          + "\n");

                  patternText.append(
                      tokens.getText(
                              tokens.get(lastMatchingCodePart.getStartIndex()),
                              tokens.get(lastMatchingCodePart.getEndIndex()))
                          + "\n");

                  System.out.println(
                      tokens.getText(
                              tokens.get(lastMatchingCodePart.getStartIndex()),
                              tokens.get(lastMatchingCodePart.getEndIndex()))
                          + "\n");

                  patternText.append("MATCHING IF END: " + searchKeyword + "\n");

                  lastMatchingIfForPattern.put(searchKeyword, null);
                }
              }
            }
          }
        }
      }
    }
  }
Esempio n. 3
0
  @Override
  public void enterClassDeclaration(ClassDeclarationContext ctx) {

    boolean patternFound = false;
    conditionText = new StringBuffer();
    patternText = new StringBuffer();
    // System.out.println("Class Data ==>"+tokens.getText());
    if (ctx == null || ctx.Identifier() == null) {
      return;
    }
    // System.out.println("Class name==>"+ ctx.Identifier().getText());
    if (LogUtil.isDebugEnabled(LOG_NAME)) {
      LogUtil.debug(CLASS_NAME, "", "Class name==>" + ctx.Identifier().getText(), LOG_NAME);
    }
    // getJavaDocMatch();

    className = ctx.Identifier().getText();

    if (className.toLowerCase().indexOf("processfacade") != -1
        || className.toLowerCase().indexOf("processdelegate") != -1) {
      conditionText.append(
          "\nProcessCalls References are found. Class : " + ctx.Identifier().getText() + "\n");
      patternText.append(
          "\nProcessCalls References are found. Class : " + ctx.Identifier().getText() + "\n");
    }

    conditionText.append("\n Class name-->" + ctx.Identifier().getText() + "\n");

    if (searchPatterns != null) {

      for (String searchKeyword : searchPatterns) {

        String actualPropertyValue = "";
        if (messageMap != null && messageMap.get(searchKeyword) != null) {
          actualPropertyValue = " [" + messageMap.get(searchKeyword) + "] ";
        }
        // System.out.println("SearchKeyword ==> "+searchKeyword+" =
        // "+rewriter.getText().toUpperCase().contains(searchKeyword.toUpperCase()));
        if (searchKeyword != null) {
          try {
            String keywordWOQuote = Pattern.quote(searchKeyword);
            pattern = Pattern.compile(keywordWOQuote, Pattern.CASE_INSENSITIVE);
            System.out.println(pattern.compile(keywordWOQuote));
            // Need to add pattern for finding keyword
            // if(rewriter.getText().indexOf(searchKeyword)!=-1) {
            if (pattern.matcher(rewriter.getText()).find()) {
              conditionText.append(
                  "Pattern Matched for searchPattern-->"
                      + searchKeyword
                      + actualPropertyValue
                      + "\n");
              actClassName = ctx.Identifier().getText();
              patternText.append("\n Class name-->" + className + "\n");
              patternText.append(
                  "Pattern Matched for searchPattern-->"
                      + searchKeyword
                      + actualPropertyValue
                      + "\n");

              // System.out.println("Pattern Matched for
              // searchPattern=="+searchKeyword+actualPropertyValue);
              if (LogUtil.isDebugEnabled(LOG_NAME)) {
                LogUtil.debug(
                    CLASS_NAME,
                    "enterClassDeclaration",
                    "Pattern Matched for searchPattern=" + searchKeyword + actualPropertyValue,
                    LOG_NAME);
              }
              patternFound = true;
            }
          } catch (Exception ex) {
            ex.printStackTrace();
          }
        }
      }
    }
    if (!patternFound) throw new PatternNotFoundException("Keyword not match");
  }