我有一个代码可以检测源中方法的调用开始位置和长度,如下所示。
我需要将这些数据存储在 ASTVisitor() 之外,但是使用 final int,我得到了一个错误。
如何在 ASTVisitor() 中存储值?
public void setPositionFinder(String methodName) throws JavaModelException
{
//findMethod(methodName);
IType type = this.javaProject.findType(this.className);
ICompilationUnit unit = type.getCompilationUnit();
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(unit);
parser.setResolveBindings(true);
CompilationUnit cunit = (CompilationUnit) parser.createAST(null);
//ASTNode root = parser.createAST(null);
final String name = this.newMethodName;
final int startPosition = -1;
final int length = -1;
cunit.accept(new ASTVisitor() {
public boolean visit(MethodInvocation methodInvocation)
{
String methodName = methodInvocation.getName().toString();
System.out.println(methodName);
if (methodName.equals(name))
{
// ERROR!
startPosition = methodInvocation.getStartPosition();
length = methodInvocation.getLength();
System.out.printf("startPosition %d - Length %d", startPosition, length);
}
return false;
}
});
}