Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » How to 'copy' MethodDeclaration from one CompilationUnit to another
How to 'copy' MethodDeclaration from one CompilationUnit to another [message #247768] Sat, 15 September 2007 22:40 Go to next message
Werner Janjic is currently offline Werner JanjicFriend
Messages: 15
Registered: July 2009
Junior Member
Hello!

I've got a problem in codeing a plugin. I've got a CompilationUnit
created from Javacode in a file on the filesystem and can wonderfully
traverse through it. Now I want to copy one method from this
CompilationUnit to another CompilationUnit that I've got from the actual
editor.

So I did - naively - the following:

[...]
MethodDeclaration orgMethod = methods[0];
((TypeDeclaration)editorCompUnit.types().get(0)).bodyDeclara tions().add(orgMethod);
[...]

Unfortunately this returns an IllegalArgumentException and seeing the
inner structure in the debugger I assume that this is due to the fact
that orgMethod references its own AST and/or CPU and so the editor CPU
can't handle that.

Now I'd like to know if there is a possibility to copy a method or type
declaration from one CompilationUnit to another one, without haveing to
code one million lines of code examining all of the original one and
then storing it in the destination CPU. That seems pretty hard to me as
I'm not so deep in AST/CPU programming. Is there some clone or copy
option implemented?

Thank you in advance and I'd very much appreciate if you could provide
me some code snippet with your answer. I've been trying it myself for
days now and also searching the newsgroup and googleing didn't give me
any idea.

Werner
Re: How to 'copy' MethodDeclaration from one CompilationUnit to another [message #247770 is a reply to message #247768] Mon, 17 September 2007 09:17 Go to previous messageGo to next message
Eddie Man is currently offline Eddie ManFriend
Messages: 102
Registered: July 2009
Senior Member
Copying ast node from one AST to other:

MethodDeclaration method;
CompilationUnit other;
....
TypeDeclaration targetType = (TypeDeclaration) other.types.get(0);
targetType.bodyDeclarations().add(ASTNode.copySubtree(other. getAST(),
method));

Werner Janjic wrote:
> Hello!
>
> I've got a problem in codeing a plugin. I've got a CompilationUnit
> created from Javacode in a file on the filesystem and can wonderfully
> traverse through it. Now I want to copy one method from this
> CompilationUnit to another CompilationUnit that I've got from the actual
> editor.
>
> So I did - naively - the following:
>
> [...]
> MethodDeclaration orgMethod = methods[0];
> ((TypeDeclaration)editorCompUnit.types().get(0)).bodyDeclara tions().add(orgMethod);
>
> [...]
>
> Unfortunately this returns an IllegalArgumentException and seeing the
> inner structure in the debugger I assume that this is due to the fact
> that orgMethod references its own AST and/or CPU and so the editor CPU
> can't handle that.
>
> Now I'd like to know if there is a possibility to copy a method or type
> declaration from one CompilationUnit to another one, without haveing to
> code one million lines of code examining all of the original one and
> then storing it in the destination CPU. That seems pretty hard to me as
> I'm not so deep in AST/CPU programming. Is there some clone or copy
> option implemented?
>
> Thank you in advance and I'd very much appreciate if you could provide
> me some code snippet with your answer. I've been trying it myself for
> days now and also searching the newsgroup and googleing didn't give me
> any idea.
>
> Werner
Re: How to 'copy' MethodDeclaration from one CompilationUnit to another [message #248323 is a reply to message #247770] Tue, 02 October 2007 12:43 Go to previous message
Werner Janjic is currently offline Werner JanjicFriend
Messages: 15
Registered: July 2009
Junior Member
Thanks a lot. That helped me!

Eddie Man schrieb:
> Copying ast node from one AST to other:
>
> MethodDeclaration method;
> CompilationUnit other;
> ...
> TypeDeclaration targetType = (TypeDeclaration) other.types.get(0);
> targetType.bodyDeclarations().add(ASTNode.copySubtree(other. getAST(),
> method));
>
> Werner Janjic wrote:
>> Hello!
>>
>> I've got a problem in codeing a plugin. I've got a CompilationUnit
>> created from Javacode in a file on the filesystem and can wonderfully
>> traverse through it. Now I want to copy one method from this
>> CompilationUnit to another CompilationUnit that I've got from the
>> actual editor.
>>
>> So I did - naively - the following:
>>
>> [...]
>> MethodDeclaration orgMethod = methods[0];
>> ((TypeDeclaration)editorCompUnit.types().get(0)).bodyDeclara tions().add(orgMethod);
>>
>> [...]
>>
>> Unfortunately this returns an IllegalArgumentException and seeing the
>> inner structure in the debugger I assume that this is due to the fact
>> that orgMethod references its own AST and/or CPU and so the editor CPU
>> can't handle that.
>>
>> Now I'd like to know if there is a possibility to copy a method or
>> type declaration from one CompilationUnit to another one, without
>> haveing to code one million lines of code examining all of the
>> original one and then storing it in the destination CPU. That seems
>> pretty hard to me as I'm not so deep in AST/CPU programming. Is there
>> some clone or copy option implemented?
>>
>> Thank you in advance and I'd very much appreciate if you could provide
>> me some code snippet with your answer. I've been trying it myself for
>> days now and also searching the newsgroup and googleing didn't give me
>> any idea.
>>
>> Werner
Previous Topic:Types not found after install Europa Fall Maintenance
Next Topic:Java version mismatch in org.eclipse.jdt.compiler.tool bundle?
Goto Forum:
  


Current Time: Sat Aug 10 10:31:12 GMT 2024

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

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

Back to the top