gpt4 book ai didi

java - Eclipse 重命名参与者偏移量处理

转载 作者:太空宇宙 更新时间:2023-11-04 08:18:16 25 4
gpt4 key购买 nike

当我编写代码时,我在每个方法的开头都包含跟踪调用,如下所示:

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 + "\""));
}
}
}

最佳答案

我能够通过使用 createPreChange(...) 让我的代码正常工作。这允许我返回在执行主要重构之前在源上执行的更改。这意味着我的代码计算的更改在实际应用时仍然准确。

http://help.eclipse.org/helios/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fltk%2Fcore%2Frefactoring%2Fparticipants%2FRefactoringParticipant.html

编辑:使用 createPreChange(...) 实际上只是一种解决方法,因为我的更改仍然可能与另一个 PreChange 冲突。我想出了一个更好的解决方案,通过返回使用 createChange(...) 并调用 getTextChange(...) 来获取现有的文本编辑对象并将我的编辑添加到该对象。这似乎使偏移量正常工作。

public Change createChange(IProgressMonitor pm)
throws CoreException,
OperationCanceledException
{
ICompilationUnit unit = element.getCompilationUnit();
TextChange change = getTextChange(unit);

// Failed to find existing change to add our changes to
if (change == null)
return null;

// Find the AST version of the method being changed
CompilationUnit astCompUnit = parse(unit, pm);
ASTNode astElement = NodeFinder.perform(astCompUnit, element.getNameRange());
MethodDeclaration astMethod = (MethodDeclaration)getParent(astElement, MethodDeclaration.class);

// Visit the contents of the method to find changes to make
String newName = getArguments().getNewName();
List<TraceFnFixOperation> ops = new ArrayList<TraceFnFixOperation>(1);
TraceFnCtorFinder finder = new TraceFnCtorFinder(newName, ops);
astMethod.accept(finder);

// Add identified edits to the overall change
for (TraceFnFixOperation op : ops)
{
change.addEdit(new ReplaceEdit(op.startPosition,
op.length,
"\"" + op.methodName + "\""));
}

// Don't return a dedicated change
return null;
}

关于java - Eclipse 重命名参与者偏移量处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10050545/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com