This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Custom Java Compiler Constraints Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Custom Java Compiler Constraints" Watch "Custom Java Compiler Constraints" New topic
Author

Custom Java Compiler Constraints

Kumaravadivel Subramani
Ranch Hand

Joined: Jul 05, 2008
Posts: 166

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.


No pain, No gain.
OCJP 1.6
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

You will need to write your own compiler (possibly using the source of another compiler to start with) for that to work.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3003
    
    9
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

Joined: Jul 05, 2008
Posts: 166

Thanks All to shared the ideas.
 
 
subject: Custom Java Compiler Constraints
 
Similar Threads
Custom business types??
Don't really Understand what Annotations Are all about
NotNull annotation for Java SE
Doubt in annotations.
Multiple Error messages for Single regex validation