title: how to find empty block using regular expression in java?
body: <p>I want to find unused if statements in my projects. So i wrote a sample program for find if statements but it has some issues. Please find below test file and source file for finding unsued if statements</p>
  
  <p>This is sample java file for testing</p>
  
  <pre><code>//test file
  
   public class SearchClass {
  
  private void test() {
  
      if(true) {
          if(true) {
  
          }
      }
  
        if(true) {
           //kavi
            //kamal
            //kamal
        }
  
        if(true) {
            //kavi
            //kamal
        } 
  
        if(true) {
  
          } 
  
        if(true){
              /*fsdsddgd
              ddgdgdfg
              gdfgdgdfg*/
              System.out.println("");
              /*gdfgdfg
              fgdfgfg*/
          }
  
        if(true) {
            /*fsdsddgd
              ddgdgdfg
              gdfgdgdfg*/
        }
  
                          if(true) {
              /*              fsdsddgd
                              ddgdgdfg
                             gdfgdgdfg
                             gdfgdfg
                             fgdfgfg
  
          */              System.out.println("kamal");
           }
  
   }
   }
  </code></pre>
  
  <p>From the above,  i am not able to clean all unused if statements. So please do need full.</p>
  
  <pre><code>       //sample program
               import java.io.IOException;
                import java.io.StringReader;
                import java.nio.charset.Charset;
                import java.nio.file.Files;
                import java.nio.file.Paths;
                import java.util.regex.Matcher;
                import java.util.regex.Pattern;
  
   public class EmptyBlockCleaner {
  
    private static String text;
  
  public static final String EMPTY_IF_BLOCK_REEXP = "((if[\\s]*\\([^\\)]*\\)[\\s]*\\{[\\s]*})(.+?))" ;
  
  public static final String IF_BLOCK_MULTIPLE_LINE = "((if[\\s]*\\([^\\)]*\\)[\\s]*\\{((/\\*(.|[\\r\\n])*?\\*/)|\\s)*\\})(.+?))";
  
  
  
  private String getFileAsString( String file ) throws IOException {
      byte[] encoded = Files.readAllBytes(Paths.get(file));
      return new String(encoded, Charset.defaultCharset());
  
  }
  
  private boolean isEmptyBlockWithoutComments(String file) throws IOException {
      Pattern p = Pattern.compile(IF_BLOCK_MULTIPLE_LINE, Pattern.MULTILINE);
      text = getFileAsString(file);
      Matcher matcher = p.matcher(text); 
       while (matcher.find()) {
          String javaCode = removeComments(matcher.group());
          Pattern innerPattern = Pattern.compile(EMPTY_IF_BLOCK_REEXP, Pattern.MULTILINE);
          Matcher innerMatcher = innerPattern.matcher(javaCode); 
          while (innerMatcher.find()) {
              text = matcher.replaceAll("");
              System.out.println(text);
          }
  
      }
  
      return false;
  }
  
  private String removeComments(String code) {
      StringBuilder newCode = new StringBuilder();
      try (StringReader sr = new StringReader(code)) {
          boolean inBlockComment = false;
          boolean inLineComment = false;
          boolean out = true;
  
          int prev = sr.read();
          int cur;
          for(cur = sr.read(); cur != -1; cur = sr.read()) {
              if(inBlockComment) {
                  if (prev == '*' &amp;&amp; cur == '/') {
                      inBlockComment = false;
                      out = false;
                  }
              } else if (inLineComment) {
                  if (cur == '\r') {
                      sr.mark(1);
                      int next = sr.read();
                      if (next != '\n') {
                          sr.reset();
                      }
                      inLineComment = false;
                      out = false; 
                  } else if (cur == '\n') {
                      inLineComment = false;
                      out = false;
                  }
              } else {
                  if (prev == '/' &amp;&amp; cur == '*') {
                      sr.mark(1); 
                      int next = sr.read();
                      if (next != '*') {
                          inBlockComment = true;
                      }
                      sr.reset(); 
                  } else if (prev == '/' &amp;&amp; cur == '/') {
                      inLineComment = true;
                  } else if (out){
                      newCode.append((char)prev);
                  } else {
                      out = true;
                  }
              }
              prev = cur;
          }
          if (prev != -1 &amp;&amp; out &amp;&amp; !inLineComment) {
              newCode.append((char)prev);
          }
      } catch (IOException e) {
          e.printStackTrace();
      }
  
      return newCode.toString();
  }
  
  public static void main(String[] args) throws Exception {
  
      EmptyBlockCleaner blockCleaner = new EmptyBlockCleaner();
      blockCleaner.isEmptyBlockWithoutComments("path");
  
  }
  </code></pre>
  
  <p>}</p>