我想用正则表达式从Java / JSP源代码中提取完全限定的类名。
已经有一些线索,特别是。 Regular expression matching fully qualified class names
虽然我非常接近解决问题,但我无法摆脱误报。
以下是一些例子。在该行的末尾,我附上了预期值。
Logger l = LoggerFactory.getLogger("test"); // not a FQN, because it starts with an uppercase letter ("LoggerFactory")
if(!com.db.TFSec.isPermitted("test") return; // should return "com.db.TFSecurity"
new java.util.concurrent.BrokenException(); // java.util.concurrent.BrokenException
java.util.Set<Log> ls = new java.util.HashSet<>(); // java.util.Set, java.util.HashSet
java.awt.Component c1d2 = new java.awt.List(); // java.awt.Component, java.awt.List
com.de.tfsecurity.TFUser u; // com.de.tfsecurity.TFUser
我已经尝试了这3个正则表达式:
// Own try. Only one false positive in line1: [oggerFactory.getLogger("test")] ([a-z]\\w*\\.\\w+(\\.\\w+)*)[<\\( ;] // The following two regexes are the "correct" answers from the thread mentioned above. But I get false positives. ([a-zA-Z_$][a-zA-Z\\d_$]*\\.)*[a-zA-Z_$][a-zA-Z\\d_$]* // false positives: [Logger, LoggerFactory.getLogger, test, if, return, new, c1d2] etc. ([a-z][a-z_0-9]*\\.)*[A-Z_]($[A-Z_]|[\\w_])* // false positives: the same as in the previous example
这是我的源代码:
public class FileUsageScanner {
// This is my own try. Works for most of the time, but we have false positives with LoggerFactory.getLogger, which is not a FQN
private final Pattern fqnPatternOwnTry = Pattern.compile("([a-z]\\w*\\.\\w+(\\.\\w+)*)[<\\( ;]");
// Solutions from https://stackoverflow.com/questions/5205339/regular-expression-matching-fully-qualified-java-classes
// Lots of false positives like: [Logger, LoggerFactory.getLogger, test, if, return, new, c1d2] etc.
private final Pattern fqnPatternThr = Pattern.compile("([a-zA-Z_$][a-zA-Z\\d_$]*\\.)*[a-zA-Z_$][a-zA-Z\\d_$]*");
private final Pattern fqnPatternThr2 = Pattern.compile("([a-z][a-z_0-9]*\\.)*[A-Z_]($[A-Z_]|[\\w_])*");
public static void main(String[] args) throws IOException {
FileUsageScanner scan = new FileUsageScanner();
scan.getFQClassname("Logger logger = LoggerFactory.getLogger(\"test\");)"); // not a FQN
scan.getFQClassname("if(!com.db.TFSec.isPermitted(\"test\") return;"); // com.db.TFSec
scan.getFQClassname("new java.util.concurrent.BrokenException();"); // java.util.concurrent.BrokenException
scan.getFQClassname("java.util.Set<Log> loggers = new java.util.HashSet<>();"); // java.util.Set, java.util.HashSet
scan.getFQClassname("java.awt.Component c1d2 = new java.awt.List();"); // java.awt.Component, java.awt.List
scan.getFQClassname("com.de.tfsecurity.TFUser u;"); //com.de.tfsecurity.TFUser
}
private List<String> getFQClassname(String line) {
if (line != null && !line.isEmpty() && line.contains(".")) {
Matcher matcher = fqnPatternThr2.matcher(line);
List<String> l = null;
while (matcher.find()) {
if (l == null) {
l = new ArrayList<String>();
}
l.add(matcher.group());
}
if (l != null)
System.out.println("Found FQN in " + line + " -> " + l);
return l;
}
return null;
}
}
我如何摆脱误报?
感谢您提出任何意见,
哈德
答案 0 :(得分:0)
我倾向于使用Character
:
循环遍历全名的每个部分(用点分隔)的字符。