问题描述
假设我有这个Java源代码。如何获取extractMethod(amount)调用的startPosition和length? package smcho;
public class Extract {
String _name =;
public int extractedMethod(int amount)
{
....
}
public int getValue(int amount){
if(amount> 10){
int z = extractedMethod(amount);
return z;
}
....
}
我可以使用六位观众查找开始位置为0x1FA,长度为len(extracted(method))== 17,但我想使用JDT以编程方式执行。
一旦我可以获得CompilationUnit,但是我需要知道如何获取CompilationUnit中的调用引用。
IWorkspaceRoot root = ResourcesPlugin.getWorkspace()。getRoot();
IProject orig = root.getProject(this.projectName);
orig.open(pm);
javaProject = JavaCore.create(orig);
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);
root.accept(new ASTVisitor(){
public bool visit(...)
});
这是适用于我的代码。 - 如何在JDT / 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;
cunit.accept(new ASTVisitor(){
public boolean visit(MethodInvocation methodInvocation)
{
String methodName = methodInvocation.getName()。toString();
System.out.println(methodName);
if(methodName.equals(name))
{
startPosition = methodInvocation.getStartPosition();
length = methodInvocation .getLength();
System.out.printf(startPosition%d - Length%d,startPosition,length);
}
return false;
}
});
}
Let's say I have this Java source code. How can I get the startPosition and length of "extractedMethod(amount)" invocation?
package smcho;
public class Extract {
String _name = "";
public int extractedMethod(int amount)
{
....
}
public int getValue(int amount) {
if (amount > 10) {
int z = extractedMethod(amount);
return z;
}
....
}
I could use hexa viewers to find the start position is 0x1FA and the length is len("extracted(method)") == 17, but I'd like to do it programmatically using JDT.
Once I could get the CompilationUnit, but I need to know how to get the invocation reference in that CompilationUnit.
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject orig = root.getProject(this.projectName);
orig.open(pm);
javaProject = JavaCore.create(orig);
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);
root.accept(new ASTVisitor() {
public bool visit(...)
});
This is the code that works for me. - How can I store values inside JDT/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;
cunit.accept(new ASTVisitor() {
public boolean visit(MethodInvocation methodInvocation)
{
String methodName = methodInvocation.getName().toString();
System.out.println(methodName);
if (methodName.equals(name))
{
startPosition = methodInvocation.getStartPosition();
length = methodInvocation.getLength();
System.out.printf("startPosition %d - Length %d", startPosition, length);
}
return false;
}
});
}
这篇关于使用JDT获取startPosition和方法调用的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!