现在想对源程序做处理,之前需要去掉程序中的注释,使后续处理简单化。

解决方案 »

  1.   

    其实我试过了,反编译不保险,现在我知道的比较好用的也就是jad,但jad也不能保证反编译正确,另外它自己也会产生一些注释。
      

  2.   

    刚才尝试自己来写这样的逻辑,发现复杂的注释还不大好理解,不知道哪能找到java关于注释的说明,关于复杂注释,大家可以看:
    1:
        /*
       //DFGSD/*
       SDFSD
       */
    2:
       /*
       //DFGSD/*
       SDFSD
       // */
    3:
      /*
      //DFGSD */ /*
      SDFSD
      // */
    4:
      /*
      //DFGSD */
      SDFSD
      // */大家觉得上面的注释都对吗?其实通过测试,只有第四种不正确,从上面的例子看好像是如果“//”后有“*/”,“*/”是有效的,如果“//”后有“/*”,“/*”是无效的,但这只是自己的测试结果,不知道哪里能查到官方的说法?
      

  3.   

    import java.io.File;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.util.ArrayList;
    import java.io.BufferedOutputStream;
    /**
     * Created by IntelliJ IDEA.
     * User: 骑个破车看夕阳
     * Date: 2005-11-16
     * Time: 13:48:29
     * To change this template use File | Settings | File Templates.
     */
    public class CodeCounter {
        public void scanSrouce(File file) {
            if (file.isDirectory()) {
                File[] sub = file.listFiles();
                for (int i = 0; i < sub.length; i++) {
                    scanSrouce(sub[i]);
                }
            }
            String s1 = file.getName();
            if (!s1.endsWith(".java")) {
                return;
            }
            ArrayList optimized = new ArrayList();
            try {
                System.out.println("Reading " + file.getName() + " ...");
                boolean inCommentblock = false;            BufferedReader bufferedreader = new BufferedReader(new FileReader(file));
                ArrayList arraylist = new ArrayList();
                arraylist.add(new String[]{
                        s1, file.getCanonicalPath()
                });            IntList code = new IntList();
                IntList comment = new IntList();
                IntList blank = new IntList();            int lineNo = 0;
                nextline:
                while (bufferedreader.ready()) {
                    String s2 = bufferedreader.readLine();
                    lineNo++;
                    boolean hasBlockCommented = false;
    //                System.out.println();
    //                System.out.print("=====Line" + lineNo);                //comment like "CCCCCC  /*  CCCCCCCCCCC"
                    // "CCCCCCCCCCC  */  CCCCCCCC"
                    if (inCommentblock) {
                        comment.add(lineNo);
    //                    System.out.print(":Comment==========");
                        if (s2.indexOf("*/") == -1) {
                            continue nextline;
                        } else {
                            hasBlockCommented = true;
                            inCommentblock = false;
                            s2 = s2.substring(s2.indexOf("*/") + 2);
                        }
                    }                if (s2.trim().length() < 1) {
                        blank.add(lineNo);
    //                    System.out.print(":Blank-----------");
                        continue nextline;
                    }                if (findnext(s2, "//") != -1 || findnext(s2, "/*") != -1) {
                        if (!hasBlockCommented) {
                            hasBlockCommented = false;
    //                        System.out.print(":Comment");
                            comment.add(lineNo);
                        }
                    }                while (true) {
                        int cline = findnext(s2, "//");
                        int cblock = findnext(s2, "/*");                    //no comment
                        if (cline == -1 && cblock == -1) {
    //                        set.addAll(getString(s2));
                            if (s2.trim().length() > 0) {
                                code.add(lineNo);
                                optimized.add(s2 + '\n');
    //                            System.out.print(":Code");
                            }                        continue nextline;
                            //comment like "CCCCC  //   CCCC"
                        } else {                        if (cline != -1 && cblock == -1) {
                                s2 = s2.substring(0, cline);
                                //comment like "CCCCC   /*  CCCCCCC"
                            } else if (cline == -1 && cblock != -1) {
                                inCommentblock = true;
                                s1 = s2.substring(0, cblock);
                                String s3 = s2.substring(cblock + 2);
                                //comment like "CCCCC   /*  CCCCCC  */  CCCCCCCC"
                                if (s3.indexOf("*/") != -1) {
                                    inCommentblock = false;
                                    s1 += s3.substring(s3.indexOf("*/") + 2);
                                }
                                s2 = s1;
                                //comment like "CCCCC  // CC  /* CC"
                            } else if (cline < cblock) {
                                s2 = s2.substring(0, cline);
                                //comment like "CCCCC  /* CC  // CC"
                            } else if (cline > cblock) {
                                inCommentblock = true;
                                s1 = s2.substring(0, cblock);
                                String s3 = s2.substring(cblock + 2);
                                //comment like "CCCCC   /*  CCCC  //  CC  */  CCCC"
                                //or "CCCCC   /*  CCCCCC  */  CC  //  CC"
                                if (s3.indexOf("*/") != -1) {
                                    inCommentblock = false;
                                    s1 += s3.substring(s3.indexOf("*/") + 2);
                                }
                                s2 = s1;
                            }
                        }
                    }
                }            System.out.println("==Total Code==" + code.size + "====");
                System.out.println("==Total Comment==" + comment.size + "====");
                System.out.println("==Total Blank==" + blank.size + "====");
                System.out.println("==Total Line==" + lineNo + "====");
                for (int i = 0; i < optimized.size(); i++) {
                    System.out.print(optimized.get(i));   //todo toFile
                }            bufferedreader.close();
                bufferedreader = null;
            }
            catch (Exception exception) {
                exception.printStackTrace();
            }
        }
      

  4.   


        /**
         * 寻找字符s在s1中存在的第一个不在双引号内的位置
         *
         * @param s1
         * @param s
         * @return s在s1中存在的第一个不在双引号内的位置,没有找到-1
         */
        private int findnext(String s1, String s) {
            int p = s1.indexOf(s);
            if (p == -1) {
                return p;
            }
            while (isString(s1, p)) {
                p = s1.indexOf(s, p + s.length());
            }
            return p;
        }    /**
         * 位置p的字符是否是在奇数个双引号后面,“\"”不被计算在内
         *
         * @param s
         * @param end
         * @return
         */
        private boolean isString(String s, int end) {
            boolean inString = false;
            int t = 0;
            for (int i = 0; i < end; i++) {
                if (s.charAt(i) == '\\') {
                    t++;
                } else {
                    if (inString) {
                        if (s.charAt(i) == '"' && (t & 1) == 0) {
                            inString = false;
                        }
                    } else if (s.charAt(i) == '"') {
                        inString = true;
                    }
                    t = 0;
                }
            }
            return inString;
        }
        public void scanTree(File file) {
            if (file.isFile()) {
                scanSrouce(file);
            } else if (file.isDirectory()) {
                File[] files = file.listFiles();
                for (int i = 0; i < files.length; i++) {
                    File file1 = files[i];
                    scanTree(file1);
                }
            }
        }    public static void main(String[] args) {
            CodeCounter cc = new CodeCounter();        File src = new File("F:\\test.java");
            cc.scanTree(src);
        }    class IntList {        private int[] list = new int[10];
            private int size = 0;        public void add(int value) {
                if (size >= list.length) {
                    int newsize = list.length * 2;
                    if (newsize > Integer.MAX_VALUE || newsize < 0) {
                        newsize = Integer.MAX_VALUE;
                    }
                    int[] list1 = new int[newsize];
                    System.arraycopy(list, 0, list1, 0, size);
                    list = list1;
                }            list[size] = value;
                size++;
            }        public void set(int pos, int value) {            if (pos < 0) {
                    throw new ArrayIndexOutOfBoundsException();
                }            if (pos >= list.length) {
                    int newsize = pos * 2;
                    if (newsize > Integer.MAX_VALUE || newsize < 0) {
                        newsize = Integer.MAX_VALUE;
                    }
                    int[] list1 = new int[newsize];
                    System.arraycopy(list, 0, list1, 0, size);
                    list = list1;
                }            list[pos] = value;
            }        public int get(int pos) {
                if (pos < 0 || pos > size) {
                    throw new ArrayIndexOutOfBoundsException();
                }
                return list[pos];
            }        public int[] toArray() {
                int[] ret = new int[size];
                System.arraycopy(list, 0, ret, 0, size);
                return ret;
            }
        }
    }
      

  5.   

    消息发不出去,就给你回在这里了我也没有文档,都是自己总结的我是一行一行扫描的,其实没有必要。全篇扫描更方便,只有四种case,思想也很简单,能省很多事。