[Spoon-discuss] generate classes from scratch
Lea Hänsenberger
lhaensenberger at students.unibe.ch
Thu Jan 15 10:49:20 CET 2009
Hi,
I just realized that spoon creates the java file for me. I'm working
with eclipse and didn't refresh the project after running it :-). And
it also makes the needed import statements automatically. Thanks for
your help, anyway.
Cheers,
Lea
On Jan 14, 2009, at 19:14 , Renaud Pawlak wrote:
> Hello,
>
> By doing this, you will eventually run into issues because you
> reference in your new class the members of another class.
>
> It is better to loop on the methods, constructors, and fields and use
> the corresponding factories to copy them one by one in the target
> type. For instance :
>
> http://spoon.gforge.inria.fr/mvnsites/spoon-core/apidocs/spoon/reflect/factory/MethodFactory.html#create(spoon.reflect.declaration.CtType,%20spoon.reflect.declaration.CtMethod,%20boolean)
>
> for(CtMethod m:clazz.getMethods()) {
> getFactory().Method().create(newClass,m,true);
> }
>
> // same things for other members
>
> You should also add you new class to the package:
>
> class.getPackage().getTypes().add(newClass);
> newClass.setParent( clazz.getPackage() ); // this was right
>
> If I remember well, compilation units are not mandatory for top
> level classes.
>
> Then Spoon should automatically print out the class. If not, let me
> know, it means that I forgot something...
>
> Cheers,
> /Renaud
>
> On Wed, Jan 14, 2009 at 5:21 PM, Lea Hänsenberger
> <lhaensenberger at students.unibe.ch> wrote:
>> Hi all,
>> I'd like to automatically rewrite resp. copy an existing JUnit
>> TestCase and
>> write it to a new Java-file. So far, I've written a processor that
>> processes
>> the CtClass of the TestCase and adds all the methods and fields to
>> a new
>> CtClass. The code looks like this:
>> public void process( CtClass<?> clazz ) {
>> CtClass<?> newClass = getFactory().Class().create(
>> clazz.getSimpleName() + "Copy" );
>> newClass.getMethods().addAll( clazz.getMethods() );
>> newClass.getFields().addAll( clazz.getFields() );
>> newClass.setParent( clazz.getPackage() );
>> System.out.println( newClass );
>> }
>> Since I couldn't really find an example doing something similar,
>> I'm not
>> sure at all if this is the correct way to do it. Furthermore, I
>> don't know
>> how to write a java file from a CtClass. In the API I have found the
>> interface CompilationUnit which would seem to be helpful since it
>> knows its
>> file and source code, but I don't know how to get the
>> CompilationUnit for an
>> existing file or create one for a non existing one.
>> Can someone point me to a helpful example or some hints on how to
>> do that?
>> Thank you.
>> Cheers,
>> Lea
>>
>> _______________________________________________
>> Spoon-discuss mailing list
>> Spoon-discuss at lists.gforge.inria.fr
>> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/spoon-discuss
>>
>
>
>
> --
> Renaud Pawlak, Professor
> ISEP - 21 rue d'Assas 75006 Paris, France
> Head of the Computer Engineering Department
> Head of ISEP Research and Consulting for Computer Engineering
> Phone: +33 149 54 52 78
> Cell #1: +33 6 37 29 12 15
> Cell #2: +33 6 36 47 06 01
> Fax: +33 1 49 54 52 51
> http://renaudpawlak.wikidot.com
>
> _______________________________________________
> Spoon-discuss mailing list
> Spoon-discuss at lists.gforge.inria.fr
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/spoon-discuss
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.gforge.inria.fr/pipermail/spoon-discuss/attachments/20090115/fdb747e3/attachment-0002.htm
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 194 bytes
Desc: This is a digitally signed message part
Url : http://lists.gforge.inria.fr/pipermail/spoon-discuss/attachments/20090115/fdb747e3/attachment-0002.pgp
More information about the Spoon-discuss
mailing list