Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Custom Java Compiler Constraints

 
Kumaravadivel Subramani
Ranch Hand
Posts: 171
Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I'm very eager to know about custom constraints can be added in default java compiler I wanna implement a functionality only when java program has my own annotations, so the compiler has to read my annotations and it should implement the what I wanted. Is it possible? (Like this I wanna to check my own exceptions etc.) Thanks in advance.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will need to write your own compiler (possibly using the source of another compiler to start with) for that to work.
 
Tim Moores
Bartender
Posts: 2687
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you provide more detail about what you intend to do? From your description it's not clear to me that the compiler has to do that, and not the runtime.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure because I've never used it myself, but maybe you can use the apt tool that's included with the JDK to do what you want.

Annotations are not really meant for extending the Java language in the way that you seem to want to do. Using the reflection API, you can find out if classes, methods etc. have annotations on them. Most libraries or frameworks that use annotations use the reflection API to read information about the annotations.
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Annotations are not really meant for extending the Java language in the way that you seem to want to do.

That's true, as far as the original intent of Sun et al is concerned. However, others have indeed used annotations much as the original poster describes. Check out Project Lombok for example. It might be worthwhile to study just how that's been implemented; I suspect it uses apt internally, as Jesper recommends.
 
Kumaravadivel Subramani
Ranch Hand
Posts: 171
Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks All to shared the ideas.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic