@Override
 public void exitInst(InstContext ctx) {
   String s = getValue(ctx.getChild(0));
   setValue(ctx, s);
   int a = ctx.getStart().getTokenIndex();
   List<Token> tt = tokens.getHiddenTokensToLeft(a, 1);
   List<Token> ttnl = tokens.getHiddenTokensToLeft(a, 2);
   String ttt = "";
   if (ttnl != null && ttnl.size() > 1) ttt = "\n\n";
   if (tt != null) {
     for (Token t : tt) {
       ttt = ttt + t.getText();
       // System.out.println(t.getType() + " - " + t.getChannel());
     }
     // System.out.println(ttt);
   }
   setValue(ctx, ttt + getValue(ctx));
 }
Exemplo n.º 2
0
  private void getJavaDocMatch() {
    // TODO Auto-generated method stub

    try {
      matchedindex = new HashMap<Integer, String>();
      // Need to modify regex Pattern as StackOverFlow Error
      String regex = "/\\*\\*([^\\*]|\\*(?!/))*?.*?\\*/";
      Pattern pattern = Pattern.compile(regex);
      Matcher matcher = pattern.matcher(tokens.getText());

      // System.out.println(matcher.matches());
      if (tokens.getText() != null) {
        while (matcher.find()) { // find the next match

          if (LogUtil.isDebugEnabled(LOG_NAME)) {
            // System.out.println("(matcher.group()==>"+(matcher.group()));
            LogUtil.debug(CLASS_NAME, "Pattern==>", " " + matcher.group(), LOG_NAME);
          }
          for (String searchKeyword : searchPatterns) {
            if (searchKeyword != null) {
              if (matcher.group().contains(searchKeyword)) {
                /*
                 * System.out.println(
                 * "Keyword matched in Javadoc ==>" +
                 * matcher.group() + "\" starting at index " +
                 * matcher.start() + " and ending at index " +
                 * matcher.end());
                 */
                matchedindex.put(matcher.end(), matcher.group());
                break;
              }
            }
          }
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
      System.out.println(ex.toString() + "");
      if (LogUtil.isDebugEnabled(LOG_NAME)) {
        // System.out.println("Javadoc exception==>" + ex.toString());
        LogUtil.debug(CLASS_NAME, "Pattern==>", " " + ex.getStackTrace(), LOG_NAME);
      }
    }
  }
Exemplo n.º 3
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);
                }
              }
            }
          }
        }
      }
    }
  }