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开发者_C百科 at caller site, not at creation site. I'm using eclipse. Something like #Warning in C#.
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.
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.
Create an annotation that prints out a warning.
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.
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.
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();
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");
}
}
精彩评论