Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7772227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:50:28+00:00 2026-06-01T16:50:28+00:00

When I am writing code I include trace calls at the start of every

  • 0

When I am writing code I include trace calls at the start of every method which looks like this:

public void doOperation()
{
  Trace tr = new Trace("doOperation");
  ... method body ...
}

I am trying to write an Eclipse plugin so that when I rename a method the string constant in the trace method call is also updated. To achieve this I am implementing a RenameParticipant.

The issue I have is that the Change I produce only works correctly when the method name doesn’t change length. If the method name changes length then my change ends up editing the wrong offset within the changed file.

What am I doing wrong? How can I account for the fact that the method rename may alter the offset within the file of my Trace call?


To compute the Change I use the following code:

@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);
}

The body of the 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;
  }
}

The body of 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 + "\""));
    }
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:50:29+00:00Added an answer on June 1, 2026 at 4:50 pm

    I was able to get my code working by using createPreChange(…). This allows me to return a change which is executed on the source BEFORE the main refactoring is performed. This means that the changes computed by my code are still accurate at the time when they are actually applied.

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

    EDIT : Using createPreChange(…) was really just a workaround as my change could still conflict with another PreChange. I have come up with a better solution by going back to using createChange(…) and calling getTextChange(…) to get the existing text edits object and adding my edits to this object. This seems to make the offsets work correctly.

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this simple code: public void Run() { var invokerThread = new Thread(new
Recently I've been writing code similar to this: messagehandler.h: #include message.h class MessageHandler {
I'm writing code to download email from various servers, some of which are outside
I'm writing some code for a statistics package, and I start off by reading
Ive been writing the folowing code: #include <iostream> #include <stdlib.h> using namespace std; int
After writing the code below: #include <iostream> using namespace std; typedef struct Node {
I have a problem with this bit of code: #include <boost/multi_array.hpp> #include <boost/array.hpp> #include
I am writing JVMTI code to profile Java programs, which mostly entails obtaining stack
I am writing the code #include<sstream> #include<iostream> using namespace std; int main(){ strstream temp;
While writing code in a file that would comprise of PHP, HTML, CSS &

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.