1、文本式读取被扫描java文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| public class TXTestCheck extends BaseTreeVisitor implements JavaFileScanner { public void scanFile(JavaFileScannerContext context) { scan(context.getTree()); visitFile(context.getFile()); } private void visitFile(File file) { List<String> lines = new ArrayList<String>();; try { BufferedReader reader = new BufferedReader(new FileReader(file)); String line = null; while((line = reader.readLine()) != null){ lines.add(line); } reader.close(); } catch (IOException e) { throw new IllegalStateException(e); } } }
|
2、sonar树扫描节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| public class TXTestCheck extends BaseTreeVisitor implements JavaFileScanner { private JavaFileScannerContext context; @Override public void visitClass(ClassTree tree) { System.out.println(tree.simpleName().name()); tree.symbol().isAbstract(); super.visitClass(tree); } @Override public void visitMethod(MethodTree tree) { System.out.println(tree.simpleName().name()); tree.symbol().isAbstract(); tree.symbol().isPublic(); super.visitMethod(tree); } public void scanFile(JavaFileScannerContext context) { this.context = context; scan(context.getTree()); } }
|
3、指定soanr树扫描节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| public class TXTestCheck extends IssuableSubscriptionVisitor { @Override public List<Tree.Kind> nodesToVisit() { return ImmutableList.of(Tree.Kind.METHOD, Tree.Kind.VARIABLE); } @Override public void scanFile(JavaFileScannerContext context) { super.context = context; super.scanFile(context); } @Override public void visitNode(Tree tree) { if (tree instanceof MethodTree) { MethodTree methodTree = (MethodTree) tree; System.out.println(methodTree.simpleName().name()); } if (tree instanceof VariableTree) { VariableTree variableTree = (VariableTree) tree; System.out.println(variableTree.simpleName().name()); } } }
|
4、获取注释
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
private String getJavadoc(ClassTree tree){ String javadoc = null; for(SyntaxTrivia trivia : tree.modifiers().firstToken().trivias()){ String comment = trivia.comment(); if(comment != null && comment.trim().startsWith("/*")){ System.out.println(comment); } if(comment != null && comment.trim().startsWith("//")){ System.out.println(comment); } } return javadoc; }
private String getJavadoc(MethodTree tree){ String javadoc = null; for(SyntaxTrivia trivia : tree.modifiers().firstToken().trivias()){ String comment = trivia.comment(); if(comment != null && comment.trim().startsWith("/*")){ System.out.println(comment); } if(comment != null && comment.trim().startsWith("//")){ System.out.println(comment); } } return javadoc; }
private String getJavadoc(VariableTree tree){ String javadoc = null; for(SyntaxTrivia trivia : tree.modifiers().firstToken().trivias()){ String comment = trivia.comment(); if(comment != null && comment.trim().startsWith("/*")){ System.out.println(comment); } if(comment != null && comment.trim().startsWith("//")){ System.out.println(comment); } } return javadoc; }
private String getJavadoc(EnumConstantTree tree){ String javadoc = null; for(SyntaxTrivia trivia : tree.initializer().firstToken().trivias()){ String comment = trivia.comment(); if(comment != null && comment.trim().startsWith("/*")){ System.out.println(comment); } if(comment != null && comment.trim().startsWith("//")){ System.out.println(comment); } } return javadoc; }
|
注:sonarqube5.x和6.x版本获取注释的方法有细微不同,此处的代码适用于6.x版本