亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 Spoon 從 java 類中刪除注釋?

如何使用 Spoon 從 java 類中刪除注釋?

人到中年有點甜 2023-06-28 16:17:00
我正在使用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);

它的作用是,它設置setCommentEnabledfalse并且注釋被完全忽略。所以,你根本不需要擔心它們。

查看完整回答
反對 回復 2023-06-28
  • 1 回答
  • 0 關注
  • 192 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號