Prezentace je o tom co je kontrakt obecně, jak kontrolovat jeho porušení hlavně pomocí
IntelliJ anotací a CoFoJa, a tak předejít běžným chybám jako je NullPointerException.
The presentation is about what is a contract generally and how to check the contract
violation mostly by IntelliJ Annotations or CoFoJa and prevent common errors such a NullPointerException.
Project Based Learning (A.I).pptx detail explanation
Contract in Java
1. Programming Against Interface Contract IntelliJ Annotations CoFoJa
Contract in Java
Jakub Novotn´y
January 19, 2015
Jakub Novotn´y Keyup
Contract in Java
2. Programming Against Interface Contract IntelliJ Annotations CoFoJa
Interface
set of information published by entity (class, function, ...)
signature
contract
Jakub Novotn´y Keyup
Contract in Java
3. Programming Against Interface Contract IntelliJ Annotations CoFoJa
Signature
everything that can be checked by compiler
method/function
name
return type
number of parameters
parameter types
thrown exceptions
synchronized, native, ...
data types
name
ancestor(s)
implemented interface
signatures of all members
Jakub Novotn´y Keyup
Contract in Java
4. Programming Against Interface Contract IntelliJ Annotations CoFoJa
Contract
informations how to use
can not be easily checked by compiler
parameter values
returned values - for example:
o1.equals(o2) => o1.hashCode() == o2.hashCode()
equals must be reflexive, symmetric, transitive
ompareTo must be transitive, antisymmetric and should be
consistent with equals
used algorithms and methods
Jakub Novotn´y Keyup
Contract in Java
5. Programming Against Interface Contract IntelliJ Annotations CoFoJa
General parameters checking
always check - declared exception thrown - OK / undeclared -
fail of implementation
always publish in javadoc
use standardised methods
methods from org.springframework.util.Assert which
throws IllegalArgumentException
isTrue(boolean expression, String message)
hasLength(String text, String message)
...
assert operator for debugging
is applied with -ea switch only
Jakub Novotn´y Keyup
Contract in Java
6. Programming Against Interface Contract IntelliJ Annotations CoFoJa
Example
1 /∗∗
2 ∗ Square root of the given number .
3 ∗
4 ∗ @param n must be >= 0
5 ∗ @return square root of {@code n}
6 ∗ @throws I l l e g a l A r g u m e n t E x c e p t i o n i f {@code n} i s < 0.
7 ∗ @see j a v a . lang . Math#s q r t
8 ∗/
9 p u b l i c double s q r t ( double n ) throws I l l e g a l A r g u m e n t E x c e p t i o n {
10 A s s e r t . i s Tr u e ( n >= 0) ;
11 return Math . s q r t ( n ) ;
12 }
Jakub Novotn´y Keyup
Contract in Java
7. Programming Against Interface Contract IntelliJ Annotations CoFoJa
Selenium IDE - Creating the test
Jakub Novotn´y Keyup
Contract in Java
8. Programming Against Interface Contract IntelliJ Annotations CoFoJa
IntelliJ Annotations
contract checking by annotations
internal annotations for IntelliJ IDE
Apache license
pending: include in the standard JDK
maven repository
Jakub Novotn´y Keyup
Contract in Java
9. Programming Against Interface Contract IntelliJ Annotations CoFoJa
@Nullable
method - can return null
parameter - accepts null
variable - can hold null
producing warnings - configurable in Inspection/Probable bugs
Jakub Novotn´y Keyup
Contract in Java
10. Programming Against Interface Contract IntelliJ Annotations CoFoJa
@Nullable - producing warning
1 public class N u l l a b l e 1 {
2 @Nullable
3 public S t r i n g a = n u l l ;
4
5 public int aLength () {
6 return a . length () ;
7 }
8 }
Jakub Novotn´y Keyup
Contract in Java
11. Programming Against Interface Contract IntelliJ Annotations CoFoJa
@Nullable - producing warning
1
2 public class N u l l a b l e 2 {
3 private S t r i n g a ;
4
5 @Nullable
6 public S t r i n g getA () {
7 return a ;
8 }
9
10 public int aLength () {
11 return getA () . length () ;
12 }
13 }
Jakub Novotn´y Keyup
Contract in Java
12. Programming Against Interface Contract IntelliJ Annotations CoFoJa
@NotNull
method - can not return null
parameter - does not accept null
variable - can not hold null
Jakub Novotn´y Keyup
Contract in Java
13. Programming Against Interface Contract IntelliJ Annotations CoFoJa
@NotNull - producing error
1 public class NotNullError {
2 @NotNull
3 private S t r i n g s t r ;
4
5 }
Jakub Novotn´y Keyup
Contract in Java
14. Programming Against Interface Contract IntelliJ Annotations CoFoJa
@NotNull - producing warning
1 public class NotNull1 {
2
3 private S t r i n g s t r ;
4
5 public S t r i n g t ( @NotNull S t r i n g a ) {
6 return a ;
7 }
8
9 public S t r i n g t2 ( @Nullable S t r i n g a )
{
10 return t e s t ( a ) ;
11 }
12 }
Jakub Novotn´y Keyup
Contract in Java
15. Programming Against Interface Contract IntelliJ Annotations CoFoJa
@Contract
value - for constraints
pure - true = does not change the object
Jakub Novotn´y Keyup
Contract in Java
16. Programming Against Interface Contract IntelliJ Annotations CoFoJa
@Contract - example
1 public class Contract1 {
2
3 @Contract ( ” n u l l −> n u l l ” )
4 public S t r i n g t e s t ( S t r i n g a ) {
5 return a ;
6 }
7
8 }
Jakub Novotn´y Keyup
Contract in Java
17. Programming Against Interface Contract IntelliJ Annotations CoFoJa
CoFoJa
Contract For Java
contract checking by annotations
maven repository
plugin for eclipse only
Jakub Novotn´y Keyup
Contract in Java
18. Programming Against Interface Contract IntelliJ Annotations CoFoJa
CoFoJa - example
1 @ In v a ri a n t ( ” s i z e () >= 0” )
2 interface Stack<T> {
3 public int s i z e () ;
4
5 @Requires ( ” s i z e () >= 1” )
6 public T peek () ;
7
8 @Requires ( ” s i z e () >= 1” )
9 @Ensures ({
10 ” s i z e () == old ( s i z e () ) − 1” ,
11 ” r e s u l t == old ( peek () ) ”
12 })
13 public T pop () ;
14
15 @Ensures ({Jakub Novotn´y Keyup
Contract in Java
19. Programming Against Interface Contract IntelliJ Annotations CoFoJa
That’s all folks
Jakub Novotn´y Keyup
Contract in Java