Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » How to rename local variable programmatically?(How to rename local variable programmatically?)
icon10.gif  How to rename local variable programmatically? [message #800062] Thu, 16 February 2012 11:35 Go to next message
Eclipse UserFriend
I have given all the needed parameters to the
RenameJavaElementDescriptor
like follows:

		desc.setUpdateReferences(true);
		desc.setProject(project.getName());
		desc.setNewName(newName);
		desc.setJavaElement(lVToRename);


But when I run my plugin I have exceptions in
checkFinalConditions
/
checkInitialConditions


as follows:

A local variable declaration or reference must be selected to activate this refactoring

and with exceptions:

java.lang.NullPointerException
	at org.eclipse.jdt.internal.corext.refactoring.rename.RenameLocalVariableProcessor.createEdits(RenameLocalVariableProcessor.java:292)
	at org.eclipse.jdt.internal.corext.refactoring.rename.RenameLocalVariableProcessor.doCheckFinalConditions(RenameLocalVariableProcessor.java:258)
	at org.eclipse.jdt.internal.corext.refactoring.rename.JavaRenameProcessor.checkFinalConditions(JavaRenameProcessor.java:46)
	at org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring.checkFinalConditions(ProcessorBasedRefactoring.java:224)
	at my.project.refactoring.plugin.LocalVariableRefactoring.makeChangetoLVs(LocalVariableRefactoring.java:120)


Who can give an advice?

[Updated on: Thu, 01 March 2012 03:45] by Moderator

Re: How to rename local variable programmatically? [message #800127 is a reply to message #800062] Thu, 16 February 2012 13:23 Go to previous messageGo to next message
Eclipse UserFriend
I found out that in the RenameLocalVariableProcessor it uses this code snippet:

  private void initAST() {
    if (!this.fIsComposite)
      this.fCompilationUnitNode = RefactoringASTParser.parseWithASTProvider(this.fCu, true, null);
    ISourceRange sourceRange = this.fLocalVariable.getNameRange();
    ASTNode name = NodeFinder.perform(this.fCompilationUnitNode, sourceRange);
    if (name == null)
      return;
    if ((name.getParent() instanceof VariableDeclaration))
      this.fTempDeclarationNode = ((VariableDeclaration)name.getParent());
  }


because I use the following:

for (VariableDeclarationFragment variable : visitor.getVariableFragments()) {
	IVariableBinding binding = variable.resolveBinding();
	if (!binding.isField() && !binding.isEnumConstant() && !binding.isParameter()) {
		ILocalVariable local = (ILocalVariable) binding.getJavaElement();


my localvariable is of type VariableDeclarationFragment where its parent is of type VariableDeclarationStatement and not of type VariableDeclaration, which eclipse needs in order to continue!
How can I change this to make work?
Any help would be appreciated.

[Updated on: Wed, 22 February 2012 04:19] by Moderator

Re: How to rename local variable programmatically? [message #800634 is a reply to message #800062] Fri, 17 February 2012 04:52 Go to previous messageGo to next message
Eclipse UserFriend
Does anybody have an idea about this?

[Updated on: Wed, 22 February 2012 04:20] by Moderator

Re: How to rename local variable programmatically? [message #802700 is a reply to message #800634] Mon, 20 February 2012 05:21 Go to previous messageGo to next message
Eclipse UserFriend
Maybe I need to use another strategy?

[Updated on: Wed, 22 February 2012 04:21] by Moderator

Re: How to rename local variable programmatically? [message #810559 is a reply to message #802700] Thu, 01 March 2012 03:48 Go to previous message
Eclipse UserFriend
I found the way:
When a method holds variables that are already assigned at their declaration time, they are considered VariableStatements and not VariableDeclarations anymore, so JDT will not rename them, as it only renames children of VaraibleDeclaration type.
The solution is to refactor the variable into a simple declaration(with no assignments at the declared time) then to perform the main refactoring here.
But eclipse must change this in the coming releases to include also childeren of VariableStatements, for not having to do this kind of nested operations.

[Updated on: Mon, 05 March 2012 10:42] by Moderator

Previous Topic:Java formatter and linebreak/join issues
Next Topic:How to cast/change IVariableBinding to ILocalVariable
Goto Forum:
  


Current Time: Sat May 17 06:25:20 EDT 2025

Powered by FUDForum. Page generated in 0.05295 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top