Dustin Getz
Dustin Getz

Reputation: 21811

force warning in java

I'd like a mechanism to throw a compile-time warning manually. I'm using it to flag unfinished code so I can't possibly forget about it later. @Deprecated is close but warns at caller site, not at creation site. I'm using eclipse. Something like #Warning in C#.

Upvotes: 10

Views: 2889

Answers (7)

Daniel Hofmann
Daniel Hofmann

Reputation: 21

A lot better than using the deprecated annotation in my humble opinion the suppresswarnings annotation.

@SuppressWarnings("XXX")

can be annotated almost everywhere and as long as XXX is not a valid warning you will get a warning from eclipse about that.

Upvotes: 2

Andi
Andi

Reputation: 3498

When I want to warn users about calling a method, I (mis)use the build-in @Deprecated annotation which generates a warning at the caller side. Actually it has a slightly different meaning, but I don't care.

/** [Additional information...]
 * It is marked as deprecated to warn callers. */
@Deprecated public void methodForWhichIWantToCreateAWarning();

Upvotes: 1

LoudNPossiblyWrong
LoudNPossiblyWrong

Reputation: 3893

I'm not sure if there is anything like that in java

but here is something that may help you:

compile the code below like this:

javac -Xprint verbose.java

public class verbose{

    public @interface SomeMessage {
        String somevalue();
    }


    @SomeMessage(
        somevalue = "WHOOOOHOOOO000000000000000"
    )

    public static void main(){
        System.out.println("Hello");
    }   
}

Upvotes: 0

Jay
Jay

Reputation: 27512

I do this all the time by just putting "*** LEFT OFF HERE ***" or some such. As this is not valid Java or C++ or pretty much any other language syntax, the compiler gives an error message for it.

Of course that gives an error and not a warning. If you want to otherwise successfully compile, I suppose an easy thing to do would be to put a statement that you know will generate a compiler warning with a comment saying "finish this" or whatever.

Upvotes: 1

Mitch Dempsey
Mitch Dempsey

Reputation: 39959

Why not just add a flag in the source code like //TODO: or something? And then just search for all occurances of TODO in the file? You could even have some special flag too like FINISHME or something.

If you have hundreds of TODOs in your project from the team, you can filter by a string e.g. your initials in the Task pane's top-right menus.

Upvotes: 7

Goibniu
Goibniu

Reputation: 2231

You could try to throw an exception if an unfinished method is called (e.g.)

throw new UnsupportedOperationException()

(Or you could create your own Exception). Obviously this will only help at runtime (unless you do a search or something in your code for it - but Todo would be a better solution for that)

And if you are using eclipse you can mark your unfinished code with // TODO and it will show up on your task list (and the right gutter if you have it configured). Obviously things can get very noisy if you have a lot of Todo tags.

Upvotes: 1

Jonathon Faust
Jonathon Faust

Reputation: 12553

Create an annotation that prints out a warning.

Upvotes: 1

Related Questions