Win a copy of Java Challengers this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

How to force compile time error, if I annotate a method which has void as return type

 
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I created a custom annotation that can be applied on every method at this time. My intention is to limit the use of this annotation and not allow annotation on methods that have void as the return type. In other words, if a method has a return type as void, then I cannot apply my custom annotation on that method. If an attempt is made to apply custom annotation on that method (method that has void as the return type), then raise a compile time error (red X in the IDE).

For example, the name of the custom annotation is MyCustomAnnotation.

In a Java class, I have a method as:
public void myMethod(){....}

If an attempt is made to annotate myMethod, then show a compile time error (red X in the IDE).

How can I force a compile time error, if particular types of methods (all the methods that retun void) are annotated with MyCustomAnnotation?
 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy, Sam!

Well champion, you won't be able to do that. Because this isn't really the target of annotations, which are a simple form of metadata. What you want is something like the @Override, @Deprecated and @SuppressWarnings annotations, which are annotations known by the compiler. I guess that, in order to achieve what you want, you would have to change the compiler so your annotation would be known by it.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic