[ 
https://issues.apache.org/jira/browse/AVRO-1186?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15101978#comment-15101978
 ] 

Christopher Ng commented on AVRO-1186:
--------------------------------------

FYI to make it work in IDL you have to be careful about where you place the 
annotation.  Naively you would probably lay it out like this:

{code}
@namespace("org.apache.avro.ipc.specific")
protocol JavaAnnotation {
  @javaAnnotation("org.apache.avro.TestAnnotation")
  record AnnotatedRecord {
    @javaAnnotation(["org.apache.avro.TestAnnotation"]) 
    string name;
  }
}
{code}

but this won't work, it will end up with the annotation applied to the type, 
which will then be ignored:

{code}
{
    "protocol" : "JavaAnnotation",
    "namespace" : "org.apache.avro.ipc.specific",
    "types" : [ {
        "type" : "record",
        "name" : "AnnotatedRecord",
        "fields" : [ {
            "name" : "name",
            "type" : {
                "type" : "string",
                "javaAnnotation" : [ "org.apache.avro.TestAnnotation" ]
            }
        } ],
        "javaAnnotation" : "org.apache.avro.TestAnnotation"
    } ],
    "messages" : { }
{code}

Instead you have to make sure the annotation is applied to the field name:

{code}
@namespace("org.apache.avro.ipc.specific")
protocol JavaAnnotation {
  @javaAnnotation("org.apache.avro.TestAnnotation")
  record AnnotatedRecord {
    string @javaAnnotation(["org.apache.avro.TestAnnotation"]) name;
  }
}
{code}

which results in:

{code}
{
    "protocol" : "JavaAnnotation",
    "namespace" : "org.apache.avro.ipc.specific",
    "types" : [ {
        "type" : "record",
        "name" : "AnnotatedRecord",
        "fields" : [ {
            "name" : "name",
            "type" : "string",
            "javaAnnotation" : [ "org.apache.avro.TestAnnotation" ]
        } ],
        "javaAnnotation" : "org.apache.avro.TestAnnotation"
    } ],
    "messages" : { }
}
{code}

and the annotation applied correctly to the field.

It would be nice if you could apply annotations to the getter/setters, though, 
they only apply to the fields.

> Java Annotations via IDL
> ------------------------
>
>                 Key: AVRO-1186
>                 URL: https://issues.apache.org/jira/browse/AVRO-1186
>             Project: Avro
>          Issue Type: New Feature
>          Components: java
>            Reporter: Sharmarke Aden
>            Assignee: Doug Cutting
>             Fix For: 1.7.3
>
>         Attachments: AVRO-1186.patch, AVRO-1186.patch
>
>
> We would like to have the ability to add Java annotations to fields in the 
> Avro IDL (i.e. JSR 303 Bean Validation annotations). From a protocol stand 
> point these annotations may not mean much (or could they?) but from the 
> application stand point having this feature would allow developers to use 
> Avro generated beans application wide. From view layer all the way to the 
> data layer. To support this feature an IDL file could look something like 
> this:
> {code} 
> @namespace("test.annotations")
> protocol TestProto {
>   record User {
>      //declare the annotations that are imported in the generated record class
>      @header{
>        import org.hibernate.validator.constraints.NotBlank;
>        import org.hibernate.validator.constraints.Email;
>      }  
>  
>      //for each field one can specify an annotation they would like to use.
>      @{@NotBlank} string username;
>      string password;
>      @{@Email} string email;
>   }
> }
> {code} 
> The above IDL would generate a Java class that looks something like this:
> {code} 
> /**
>  * Autogenerated by Avro
>  * 
>  * DO NOT EDIT DIRECTLY
>  */
> package com.simvia.mode.avro; 
> import org.hibernate.validator.constraints.NotBlank;
> import org.hibernate.validator.constraints.Email;
> @SuppressWarnings("all")
> public class User extends org.apache.avro.specific.SpecificRecordBase 
> implements org.apache.avro.specific.SpecificRecord {
>   ...
>   @Deprecated @NotBlank public java.lang.CharSequence username;
>   @Deprecated public java.lang.CharSequence password;
>   @Deprecated @Email public java.lang.CharSequence email;
>  ...
> }
> {code} 
> Perhaps this is too language specific but IMO the benefits of this feature in 
> the Java world is worth the effort and the exception.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to