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

Benjamin Gould edited comment on THRIFT-3446 at 12/9/15 12:37 AM:
------------------------------------------------------------------

I'm not sure about other languages, but I understand the request.  

The general use case here is a somewhat common idiom in Java... C# too.   Even 
in PHP etc they use this pattern sometimes (for example, 
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/working-with-indexed-associations.html)

That said, I can't really think of an elegant way to make this work in the 
Thrift compiler; you would somehow need to specify the fully qualified class 
name of the Java annotation to add, and also Java annotations can have required 
and optional attributes.  Even if you came up with a way to add a Thrift 
annotation to a struct that could cause a Java annotation to be emitted ... the 
syntax would be quite ugly in the IDL, and also would probably need to be 
different for every language.

That said, it WOULD be useful I think if the name/value pairs in Thrift 
annotations could be read via reflection at runtime... for example, in Java 
maybe if you had something like this:

{code}
struct Foo {
  1: i32 bar (nonnegative = "true")
}
{code}

It could add annotations to the generated code like this:

{code}
@IDLAnnotations({ @IDLAnnotation(key="nonnegative", val="true") })
{code}

Then at least user code could inspect the annotations at runtime.  I'm not sure 
what an analogy would look like in languages that don't have annotations, but I 
imagine a similar scheme could be used in languages that do such as C#.

As it happens, for the original use case that sparked this thread... this sort 
of thing is much easier in Java if you use Facebook's Swift tool 
(https://github.com/facebook/swift), which is totally Java-centric as opposed 
to Thrift itself.


was (Author: bcg):
I'm not sure about other languages, but I understand the request.  

The general use case here is a somewhat common idiom in Java... C# too.   Even 
in PHP etc they use this pattern sometimes (for example, 
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/working-with-indexed-associations.html)

That said, I can't really think of an elegant way to make this work in the 
Thrift compiler; you would somehow need to specify the fully qualified class 
name of the Java annotation to add, and also Java annotations can have required 
and optional attributes.  Even if you came up with a way to add a Thrift 
annotation to a struct that could cause a Java annotation to be emitted ... the 
syntax would be quite ugly in the IDL, and also would probably need to be 
different for every language.

That said, it WOULD be useful I think if the name/value pairs in Thrift 
annotations could be read via reflection at runtime... for example, in Java 
maybe if you had something like this:

{code}
struct Foo {
  1: i32 bar (nonnegative = "true")
}
{code}

It could add annotations to the generated code like this:

{code}
@IDLAnnotations({ @IDLAnnotation(key="nonnegative", val="true") })
{code}

Then at least user code could inspect the annotations at runtime.  I'm not sure 
what an analogy would look like in languages that don't have annotations, but I 
imagine a similar scheme could be used in languages that do such as C#.

As it happens, for the original use case that sparked this thread... this sort 
of thing is much easier in Java if you use Facebook's Swift tool 
(https://github.com/facebook/swift), which totally Java-centric as opposed to 
Thrift itself.

> Add support for annotations
> ---------------------------
>
>                 Key: THRIFT-3446
>                 URL: https://issues.apache.org/jira/browse/THRIFT-3446
>             Project: Thrift
>          Issue Type: Wish
>          Components: C# - Compiler, Java - Compiler
>            Reporter: Siddharth Jain
>            Priority: Minor
>
> I understand currently it is not possible to write a thrift file which when 
> compiled will generate java code that can have annotations in it. I am 
> working on a project in which I need the fields in generated java classes to 
> have annotations on them e.g., in below I have defined a annotation named 
> Column  that is used on fields in MyClass:
> @Retention(value= RetentionPolicy.RUNTIME)
> @interface Column {
>     int value() default -1;
> }
> public class MyClass {
>     @Column(0)
>     private String stateFP;
>     
>     @Column(1)
>     private String countyFP;
>     
>     @Column(2)
>     private String countyNS;
>     
>     @Column(3)
>     private String geoid; 



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

Reply via email to