1

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

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

1 回答 1

1

通过使用 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;
}
于 2012-04-07T11:31:11.077 回答