Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » Re: my own refactoring
Re: my own refactoring [message #231538] Thu, 15 June 2006 13:46
Eclipse UserFriend
Alexey Kuznetsov wrote:
> Hello, All!
>
> I'm newbie at eclipse platform.
>
> Our team is migrating from Visual Age for Java to Eclipse and we need our own refactor actions to convert our old code. It is very
> hard to do manually because of very large old code base.
>
> So, can any one give me a simple example of such kind plugin? Or any link or any clue...
> It would be great if my own refactor actions could be called from the same menus as build-in.
>
> P.S. sorry for my English
>
> With best regards, Alexey Kuznetsov. E-mail: kuaw26@mail.ru
>

You probably want to ask in eclipse.tools.jdt, since they own the
refactoring code.

There is a simple plugin that you can generate that has a single action
in it (it's the Hello World plugin template). The action could be
modified to work on the current selection, and the resources API could
be used to walk all of the java files below a selected project.

As for refactoring, it gives the basic code in the help in the JDT
plugin section.

While this code isn't robust, here's an example of adding final to the
method parameters in an ICompilationUnit:

public static void updateMethodParameters(ICompilationUnit originalUnit) {
try {
String source = originalUnit.getBuffer().getContents();
Document document = new Document(source);

// creation of DOM/AST from a ICompilationUnit
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(originalUnit);
CompilationUnit astRoot = (CompilationUnit) parser.createAST(null);
// start record of the modifications
astRoot.recordModifications();

boolean modified = false;
TypeDeclaration typeDeclaration = (TypeDeclaration) astRoot.types()
.get(0);
if (typeDeclaration.isInterface()) {
return;
}
MethodDeclaration[] methods = typeDeclaration.getMethods();
for (int i = 0; i < methods.length; i++) {
MethodDeclaration method = methods[i];
Iterator p = method.parameters().iterator();
while (p.hasNext()) {
SingleVariableDeclaration var = (SingleVariableDeclaration) p
.next();

boolean contains = (var.getModifiers() & Modifier.FINAL) ==
Modifier.FINAL;
if (!contains) {
modified = true;
Modifier mf = astRoot.getAST().newModifier(
Modifier.ModifierKeyword.FINAL_KEYWORD);
var.modifiers().add(mf);
}
}
}
if (modified) {
TextEdit edits = astRoot.rewrite(document, originalUnit
.getJavaProject().getOptions(true));

// computation of the new source code
edits.apply(document);
String newSource = document.get();

// update of the compilation unit
originalUnit.getBuffer().setContents(newSource);
originalUnit.save(null, false);
}

} catch (JavaModelException e) {
e.printStackTrace();
} catch (MalformedTreeException e) {
e.printStackTrace();
} catch (BadLocationException e) {
e.printStackTrace();
}
}

Later,
PW
Previous Topic:Quick Fix Question
Next Topic:Classpath Container not working
Goto Forum:
  


Current Time: Sat Jun 07 11:28:47 EDT 2025

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

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

Back to the top