Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Java doc an invariant for a class?

Tags:

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 {
 } 
like image 651
Moulie415 Avatar asked Nov 08 '16 22:11

Moulie415


People also ask

What is a Java class invariant?

A class invariant is simply a property that holds for all instances of a class, always, no matter what other code does.

How do you write Javadoc comments in Java?

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.

How do you make a Java document?

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.


1 Answers

There are a few possibilites

@Contract annotations

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.

Textual description

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.

Exceptions

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
like image 79
Bartosz Bilicki Avatar answered Sep 23 '22 16:09

Bartosz Bilicki