I'm wanting to know where exactly the comment should go and what keyword I should use as I cant really seem to find an example online, should I for example do this?
/**
* @invariant invariant example
*/
public class Example {
}
A class invariant is simply a property that holds for all instances of a class, always, no matter what other code does.
Writing Javadoc Comments In general, Javadoc comments are any multi-line comments (" /** ... */ ") that are placed before class, field, or method declarations. They must begin with a slash and two stars, and they can include special tags to describe characteristics like method parameters or return values.
To create the Java documentation API, you need to write Javadoc followed by file name. After successful execution of the above command, a number of HTML files will be created, open the file named index to see all the information about classes. provide version of the class, interface or enum.
There are a few possibilites
Some examples
@Contract("_, null -> null") - method returns null if its second argument is null.
@Contract("_, null -> null; _, !null -> !null") - method returns null if its second argument is null, and not-null otherwise.
@Contract("true -> fail") - a typical assertFalse() method which throws an exception if true is passed to it.
See https://www.jetbrains.com/help/idea/2016.2/contract-annotations.html for more details.
You could use them without IntelliJ IDEA. IDEA just has smart support for those annotations. It will check your method code if specified invariants are really met.
This method does not cover all cases. For more complex dependencies between fields you need to describe invariant using english words.
For example, https://docs.oracle.com/javase/7/docs/api/java/util/Map.html#put(K,%20V)
If the map previously contained a mapping for the key, the old value is replaced by the specified value.
Also, exceptions can be used to describe and enforce invariant. For above-mentioned Map.put
method we have following exceptions for invalid arguments (arguments that would break class invariant)
- @throws UnsupportedOperationException if the put operation is not supported by this map
- @throws ClassCastException if the class of the specified key or value prevents it from being stored in this map
- @throws NullPointerException if the specified key or value is null and this map does not permit null keys or values
- @throws IllegalArgumentException if some property of the specified key or value prevents it from being stored in this map
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With