in dubbo-error-code-inspector/src/main/java/org/apache/dubbo/errorcode/extractor/JdtBasedInvalidLoggerInvocationLocator.java [63:114]
public List<LoggerMethodInvocation> locateInvalidLoggerInvocation(String classFile) {
String loggerFieldName = getLoggerFieldName(classFile);
Pattern pattern = Pattern.compile(loggerFieldName + "\\.(warn|error)+\\(");
String sourceText = FileUtils.openFileAsString(FileUtils.getSourceFilePathFromClassFilePath(classFile));
Map<String, List<Integer>> lineOfInvocation = getLineOfLoggerInvocations(pattern, sourceText);
ASTParser astParser = ASTParser.newParser(AST.JLS_Latest);
astParser.setSource(sourceText.toCharArray());
astParser.setKind(ASTParser.K_COMPILATION_UNIT);
astParser.setCompilerOptions(
mapOf(JDT_CORE_COMPILER_SOURCE_PROPERTY_KEY, JDT_CORE_COMPILER_SOURCE_VERSION));
CompilationUnit n = (CompilationUnit) astParser.createAST(null);
List<TypeDeclaration> types = n.types();
TypeDeclaration type = types.get(0);
List<LoggerMethodInvocation> invalidInvocations = new ArrayList<>();
type.accept(new ASTVisitor() {
@Override
public boolean visit(MethodInvocation node) {
Expression exp = node.getExpression();
if (exp instanceof SimpleName) {
String fieldName = ((SimpleName) exp).getIdentifier();
String methodName = node.getName().getIdentifier();
if (fieldName.equals(loggerFieldName) &&
(methodName.equals("warn") || methodName.equals("error"))) {
if (node.arguments().size() < 4) {
LoggerMethodInvocation loggerMethodInvocation = new LoggerMethodInvocation();
loggerMethodInvocation.setLoggerMethodInvocationCode(node.toString());
loggerMethodInvocation.setOccurredLines(lineOfInvocation.get(node.toString()));
invalidInvocations.add(loggerMethodInvocation);
}
}
}
return super.visit(node);
}
});
return invalidInvocations.stream().distinct().collect(Collectors.toList());
}