我正在使用SequenceR來解析我的 Java 代碼文件。它使用SpoonJava庫,這是一個流行的代碼分析庫。我需要的是,我將向分析器提供一個有錯誤的行號,它將檢測該行并找出它的方法或類,并將刪除該類或方法上的所有注釋。我嘗試了多種方法來實現這一目標。但無法真正得到想要的輸出。這是我的輸入 Java 文件。我在分析器代碼中所做的是這樣的 -? ? public static void generateAbstraction(File buggy_file, int buggy_line, File working_dir) {? ? ? ? Launcher launcher = new Launcher();? ? ? ? launcher.getEnvironment().setAutoImports(true);? ? ? ? launcher.getEnvironment().setNoClasspath(true);? ? ? ? launcher.getEnvironment().setCommentEnabled(true);? ? ? ? launcher.addInputResource(buggy_file.toString());? ? ? ? try {? ? ? ? ? ? launcher.buildModel();? ? ? ? } catch (Exception e) {? ? ? ? }? ? ? ? CtModel model = launcher.getModel();? ? ? ? CtMethod topLevelmethod = null;? ? ? ? CtElement buggy_ctElement = null;? ? ? ? CtElement tmp_ctElement = null;? ? ? ? CtPath buggy_ctElement_ctPath = null;? ? ? ? // This is the main part? ? ? ? for (CtType<?> ctType : model.getAllTypes()) {? ? ? ? ? ? for (Iterator<CtElement> desIter = ctType.descendantIterator(); desIter.hasNext(); ) {? ? ? ? ? ? ? ? tmp_ctElement = desIter.next();? ? ? ? ? ? ? ? try {? ? ? ? ? ? ? ? ? ? // Main problem resides here? ? ? ? ? ? ? ? ? ? if (tmp_ctElement.getPosition().getLine() == buggy_line && !(tmp_ctElement instanceof CtComment)) {? ? ? ? ? ? ? ? ? ? ? ? buggy_ctElement = tmp_ctElement;? ? ? ? ? ? ? ? ? ? ? ? buggy_ctElement_ctPath = tmp_ctElement.getPath();? ? ? ? ? ? ? ? ? ? ? ? topLevelmethod = getTopLevelMethod(buggy_ctElement);? ? ? ? ? ? ? ? ? ? ? ? List<CtComment> comments = topLevelmethod.getElements(? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? new TypeFilter<CtComment>(CtComment.class)? ? ? ? ? ? ? ? ? ? ? ? );? ? ? ? ? ? ? ? ? ? ? ? for(CtComment c: comments){? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?topLevelmethod.removeComment(c);? ? ? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? ? ? ? ? break;? ? ? ? ? ? ? ? ? ? }仔細一看,你會發現只有一條評論被刪除了。那是 -/*?uses?badsource?and?badsink?*/其他評論完好無損。但我需要刪除類或方法中的所有注釋。我究竟做錯了什么?我對勺子完全陌生。任何幫助,將不勝感激。
1 回答

呼啦一陣風
TA貢獻1802條經驗 獲得超6個贊
要刪除文件的所有注釋,您只需關閉一個標志即可。
這條線發揮了魔力——
launcher.getEnvironment().setCommentEnabled(false);
它的作用是,它設置setCommentEnabled
值false
并且注釋被完全忽略。所以,你根本不需要擔心它們。
添加回答
舉報
0/150
提交
取消