我正在试图找到一个项目中的日志(log)记录不正确的抛出行.正确用法和不正确用法示例:
// Correct
catch (Exception e)
{
Log.error(className, "something went wrong", e);
}
// Incorrect, exception not logged
catch (Throwable t)
{
String errMsg = "something went wrong";
Log.fatal(className, errMsg);
}
// Incorrect, exception name concatenated instead of providing exception to print stacktrace
catch (IOException exception)
{
Log.error(className, "Unable to read file " + exception);
}
还显示了异常类型和异常变量名称的变体.
我目前最接近的表情是catch\s*\(\w+\s+(\w+)\)\s*\{[\s\S]*Log\s*\.\w+\(\s*\w+,[\s\S]*(?!,\b\1\b\);)\);
.它匹配catch
块的开头并捕获用于保存异常的变量名,然后try 找到not以异常变量作为最后一个参数的Log.<level>(
行,即, e);
.它跳过上面的第一个示例并捕获另外两个.
问题:我在这里测试了表达式https://regexr.com/7p58l,但它遗漏了一个重要的例子:在另一个地方,日志(log)记录是正确的,而正则表达式找不到第一个错误的地方.
catch (Exception e)
{
// Regex should capture here, but does not
Log.error(className, "something something exception");
}
void someMethod() {
catch (Exception e)
{
// Because this line hits the negative lookahead
Log.error(className, "something something exception", e);
}
}
我在负面前瞻之后添加字符的所有try 似乎都以某种形式 destruct 了它.需要添加什么才能找到所有不正确的 case ?
这将被放入IntelliJ的全球发现中,所以如果IntelliJ中的工具能更好地工作,将不胜感激