开发者

How to copy a function or class to another file?

i am developing a plug-in in which i search where a particular method say 'aaa'is called. Then i find out the function in which this particular method 'aaa'is called.I want to copy this particular method or the class in which aaa is called to another file.How can i do 开发者_JAVA技巧that?Help


You could have a look at the org.eclipse.jdt.internal.corext.refactoring.changes package, especially at the CopyCompilationUnitChange class.

It does copy a "compilation unit", which includes a class or method.

getCu().copy(getDestinationPackage(), null, getNewName(), true, pm);

It uses the copy function of org.eclipse.jdt.core.ISourceManipulation


If you mean adding a method to an existing class at runtime, your best bet is to use something like Javassist.

ClassPool pool = ClassPool.getDefault();
CtClass source = pool.get("MySourceClass");
CtMethod sourceMethod = source.getDeclaredMethod("myMethod");
CtClass dest = pool.get("MyDestClass");
dest.addMethod(sourceMethod);
dest.writeFile();

This will require some work to get right, but this should be the general idea. Note that I haven't done any exception handling etc. here. You will need to read, at minimum, the Javassist tutorial and possibly, if you need to do something really arcane, the relevant bits of the JVM spec.


select the method name

right click

Refactoring

Move

choose destination class and click OK

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜