当我编写代码时,我在每个方法的开头都包含跟踪调用,如下所示:
public void doOperation()
{
Trace tr = new Trace("doOperation");
... method body ...
}
我正在尝试编写一个 Eclipse 插件,这样当我重命名一个方法时,跟踪方法调用中的字符串常量也会被更新。为了实现这一点,我正在实施 RenameParticipant。
我遇到的问题是,我生成的更改仅在方法名称不更改长度时才能正常工作。如果方法名称更改长度,那么我的更改最终会在更改的文件中编辑错误的偏移量。
我究竟做错了什么?我如何解释方法重命名可能会改变我的 Trace 调用文件中的偏移量这一事实?
要计算更改,我使用以下代码:
@Override
public Change createChange(IProgressMonitor pm)
throws CoreException,
OperationCanceledException
{
ICompilationUnit unit = element.getCompilationUnit();
CompilationUnit astCompUnit = parse(unit, pm);
ASTNode astElement = NodeFinder.perform(astCompUnit, element.getNameRange());
MethodDeclaration astMethod = (MethodDeclaration)getParent(astElement, MethodDeclaration.class);
String newName = getArguments().getNewName();
List<TraceFnFixOperation> ops = new ArrayList<TraceFnFixOperation>(1);
TraceFnCtorFinder finder = new TraceFnCtorFinder(newName, ops);
astMethod.accept(finder);
if (ops.size() == 0)
return null;
return new TraceChange("Fix Trace", unit, ops);
}
TraceFnCtorFinder 的主体:
public static class TraceFnCtorFinder extends ASTVisitor
{
private final String methodName;
private final List<TraceFnFixOperation> workingops;
public TraceFnCtorFinder(String methodName, List<TraceFnFixOperation> workingops)
{
this.methodName = methodName;
this.workingops = workingops;
}
@Override
public boolean visit(ClassInstanceCreation ctorClass)
{
Type type = ctorClass.getType();
// Only examine simple types
if (type.isSimpleType())
{
SimpleType simpleType = (SimpleType)type;
String typeName = simpleType.getName().getFullyQualifiedName();
// Check type has correct name
if ("Trace".equals(typeName))
{
List<?> arguments = ctorClass.arguments();
// Only check a single argument
if ((arguments != null) &&
(arguments.size() == 1))
{
Object arg = arguments.get(0);
// Only check a string literal argument
if (arg instanceof StringLiteral)
{
StringLiteral literal = (StringLiteral) arg;
String currentArg = literal.getLiteralValue();
// Check whether argument value is valid
if (!methodName.equals(currentArg))
{
workingops.add(new TraceFnFixOperation(literal.getStartPosition(),
literal.getLength(),
methodName));
}
}
}
}
}
return false;
}
}
TraceChange 的主体:
public static class TraceChange extends CompilationUnitChange
{
public TraceChange(String name,
ICompilationUnit cunit,
List<TraceFnFixOperation> ops)
{
super(name, cunit);
MultiTextEdit multiTextEdit= new MultiTextEdit();
setEdit(multiTextEdit);
for (TraceFnFixOperation op : ops)
{
addEdit(new ReplaceEdit(op.startPosition,
op.length,
"\"" + op.methodName + "\""));
}
}
}