tags:

views:

75

answers:

4

hi, 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 that?Help

A: 

select the method name

right click

Refactoring

Move

choose destination class and click OK

Boris Pavlović
not like that.i mean using methods.its a plug in where i want to copy some specific methods to some other place
Steven
+3  A: 

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

VonC
On reading this, I guess this is what the OP most likely wanted. I'll leave my answer up in any case...
Chinmay Kanchi
+1  A: 

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.

Chinmay Kanchi
A: 

Why? Why do you want to duplicate code? This is poor practice.

EJP