java z注释过滤_如何编写一个java程序来过滤所有注释行并只打印java编码行?
使用
javaparser,您可以解決此問題,如此PoC中所示.
RemoveAllComments
import japa.parser.JavaParser;
import japa.parser.ParseException;
import japa.parser.ast.CompilationUnit;
import japa.parser.ast.Node;
import java.io.File;
import java.io.IOException;
public class RemoveAllComments {
static void removeComments(Node node) {
for (Node child : node.getChildrenNodes()) {
child.setComment(null);
removeComments(child);
}
}
public static void main(String[] args) throws ParseException, IOException {
File sourceFile = new File("Test.java");
CompilationUnit cu = JavaParser.parse(sourceFile);
removeComments(cu);
System.out.println(cu.toString());
}
}
TestClass.java用作示例輸入源
/**
* javadoc comment
*/
class TestClass {
/*
* block comment
*/
static class Cafebabe {
}
// line comment
static interface Commentable {
}
public static void main(String[] args) {
}
}
輸出到stdout(將其存儲在文件中取決于你)
class TestClass {
static class Cafebabe {
}
static interface Commentable {
}
public static void main(String[] args) {
}
}
總結
以上是生活随笔為你收集整理的java z注释过滤_如何编写一个java程序来过滤所有注释行并只打印java编码行?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java图书凭租_如何通过java一步实
- 下一篇: java用集合类求数组交并集_java