views:

104

answers:

5

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#.

+6  A: 

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.

webdestroya
+3  A: 

Create an annotation that prints out a warning.

Jonathon
what, like an eclipse extension? sounds like a huge pain
Dustin Getz
No, not at all like that.
Jonathon
do you mean outputs a warning at runtime?
Dustin Getz
+1  A: 

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.

Rulmeq
no good, this changes the method signature by adding a throws clause all the way up the inheritance hierarchy
Dustin Getz
@Dustin Getz: Just use a subclass of RuntimeException or Error. This doesn't affect the hierarchy or callers.
Mr. Shiny and New
@Dustin Also, UnsupportedOperationException is a RuntimeException
Kathy Van Stone
I do this as well, and searching for the constructor is easy in most IDEs.
Raymond
As already pointed out this is an unchecked exception
Rulmeq
Our variation of the exception approach includes a reference to the related work item in our tracking system.
VoiceOfUnreason
+1  A: 

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.

Jay
A: 

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");
    }   
}
LoudNPossiblyRight