users@codemodel.java.net

Re: Nested Annotations in Codemodel

From: Jonas von Malottki <yoshtec_at_gmx.net>
Date: Thu, 08 May 2008 17:06:14 +0200

Hi,
I found the solution:

@Test
public void testAnnotations() throws Exception{
        JCodeModel jmod = new JCodeModel();
        
        // adding a test package
        JPackage pack = jmod._package("test");
        
        // building an interface
        JDefinedClass interface1 = pack._interface("Interface1");

        // adding annotations
        JDefinedClass annotation1 = pack._annotationTypeDeclaration("Annot1");
        JDefinedClass annotation2 = pack._annotationTypeDeclaration("Annot2");
        
        // adding a method for annotation2
        annotation1.method(JMod.NONE, String.class, "value");
        
        //adding a method which has an annotation as type to annotation1
        annotation2.method(JMod.NONE, annotation1.array(), "value");

        // add an annotation to the Interface
        JAnnotationArrayMember paramarray =
interface1.annotate(annotation2).paramArray("value");
        paramarray.annotate(annotation1).param("value", "a");
        paramarray.annotate(annotation1).param("value", "b");
        paramarray.annotate(annotation1).param("value", "c");
        
        jmod.build(new SingleStreamCodeWriter(System.out));

}

Thanks anyways,
Jonas

Jonas von Malottki wrote:
> Hello List,
>
> is it possible to generate Nested Annotations with CodeModel like in the
> following small example:
>
> //---
> @interface Annot1{
> String value();
> }
>
> @interface Annot2{
> Annot1[] value();
> }
>
> //--- annotated class
>
> @Annot2( {_at_Annot1("hello"),_at_Annot1("World")} )
> public class MyClass{
>
> }
>
> Thanks for the thoughts,
> Greetings
> Jonas von Malottki
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_codemodel.dev.java.net
> For additional commands, e-mail: users-help_at_codemodel.dev.java.net