|
1 | | -package com.baeldung.annotations; |
2 | | - |
3 | | -import java.util.List; |
4 | | - |
5 | | -import org.apache.commons.lang3.StringUtils; |
6 | | -import org.jetbrains.annotations.Contract; |
7 | | - |
8 | | -public class Demo { |
9 | | - |
10 | | - @Contract("_ -> new") |
11 | | - Person fromName(String name) { |
12 | | - return new Person().withName(name); |
13 | | - } |
14 | | - |
15 | | - @Contract(" -> fail") |
16 | | - void alwaysFail() { |
17 | | - throw new RuntimeException(); |
18 | | - } |
19 | | - |
20 | | - @Contract(" -> fail") |
21 | | - void doNothingWithWrongContract() { |
22 | | - |
23 | | - } |
24 | | - |
25 | | - @Contract("_, null -> null; null, _ -> param2; _, !null -> !null") |
26 | | - String concatenateOnlyIfSecondArgumentIsNotNull(String head, String tail) { |
27 | | - if (tail == null) { |
28 | | - return null; |
29 | | - } |
30 | | - if (head == null) { |
31 | | - return tail; |
32 | | - } |
33 | | - return head + tail; |
34 | | - } |
35 | | - |
36 | | - void uselessNullCheck() { |
37 | | - String head = "1234"; |
38 | | - String tail = "5678"; |
39 | | - String concatenation = concatenateOnlyIfSecondArgumentIsNotNull(head, tail); |
40 | | - if (concatenation != null) { |
41 | | - System.out.println(concatenation); |
42 | | - } |
43 | | - } |
44 | | - |
45 | | - void uselessNullCheckOnInferredAnnotation() { |
46 | | - if (StringUtils.isEmpty(null)) { |
47 | | - System.out.println("baeldung"); |
48 | | - } |
49 | | - } |
50 | | - |
51 | | - @Contract(pure = true) |
52 | | - String replace(String string, char oldChar, char newChar) { |
53 | | - return string.replace(oldChar, newChar); |
54 | | - } |
55 | | - |
56 | | - @Contract(value = "true -> false; false -> true", pure = true) |
57 | | - boolean not(boolean input) { |
58 | | - return !input; |
59 | | - } |
60 | | - |
61 | | - @Contract("true -> new") |
62 | | - void contractExpectsWrongParameterType(List<Integer> integers) { |
63 | | - |
64 | | - } |
65 | | - |
66 | | - @Contract("_, _ -> new") |
67 | | - void contractExpectsMoreParametersThanMethodHas(String s) { |
68 | | - |
69 | | - } |
70 | | - |
71 | | - @Contract("_ -> _; null -> !null") |
72 | | - String secondContractClauseNotReachable(String s) { |
73 | | - return ""; |
74 | | - } |
75 | | - |
76 | | - @Contract("_ -> true") |
77 | | - void contractExpectsWrongReturnType(String s) { |
78 | | - |
79 | | - } |
80 | | - |
81 | | - // NB: the following examples demonstrate how to use the mutates attribute of the annotation |
82 | | - // This attribute is currently experimental and could be changed or removed in the future |
83 | | - @Contract(mutates = "param") |
84 | | - void incrementArrayFirstElement(Integer[] integers) { |
85 | | - if (integers.length > 0) { |
86 | | - integers[0] = integers[0] + 1; |
87 | | - } |
88 | | - } |
89 | | - |
90 | | - @Contract(pure = true, mutates = "param") |
91 | | - void impossibleToMutateParamInPureFunction(List<String> strings) { |
92 | | - if (strings != null) { |
93 | | - strings.forEach(System.out::println); |
94 | | - } |
95 | | - } |
96 | | - |
97 | | - @Contract(mutates = "param3") |
98 | | - void impossibleToMutateThirdParamWhenMethodHasOnlyTwoParams(int a, int b) { |
99 | | - |
100 | | - } |
101 | | - |
102 | | - @Contract(mutates = "param") |
103 | | - void impossibleToMutableImmutableType(String s) { |
104 | | - |
105 | | - } |
106 | | - |
107 | | - @Contract(mutates = "this") |
108 | | - static void impossibleToMutateThisInStaticMethod() { |
109 | | - |
110 | | - } |
111 | | - |
112 | | -} |
| 1 | +package com.baeldung.annotations; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import org.apache.commons.lang3.StringUtils; |
| 6 | +import org.jetbrains.annotations.Contract; |
| 7 | + |
| 8 | +public class Demo { |
| 9 | + |
| 10 | + @Contract("_ -> new") |
| 11 | + Person fromName(String name) { |
| 12 | + return new Person().withName(name); |
| 13 | + } |
| 14 | + |
| 15 | + @Contract(" -> fail") |
| 16 | + void alwaysFail() { |
| 17 | + throw new RuntimeException(); |
| 18 | + } |
| 19 | + |
| 20 | + @Contract(" -> fail") |
| 21 | + void doNothingWithWrongContract() { |
| 22 | + |
| 23 | + } |
| 24 | + |
| 25 | + @Contract("_, null -> null; null, _ -> param2; _, !null -> !null") |
| 26 | + String concatenateOnlyIfSecondArgumentIsNotNull(String head, String tail) { |
| 27 | + if (tail == null) { |
| 28 | + return null; |
| 29 | + } |
| 30 | + if (head == null) { |
| 31 | + return tail; |
| 32 | + } |
| 33 | + return head + tail; |
| 34 | + } |
| 35 | + |
| 36 | + void uselessNullCheck() { |
| 37 | + String head = "1234"; |
| 38 | + String tail = "5678"; |
| 39 | + String concatenation = concatenateOnlyIfSecondArgumentIsNotNull(head, tail); |
| 40 | + if (concatenation != null) { |
| 41 | + System.out.println(concatenation); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + void uselessNullCheckOnInferredAnnotation() { |
| 46 | + if (StringUtils.isEmpty(null)) { |
| 47 | + System.out.println("baeldung"); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + @Contract(pure = true) |
| 52 | + String replace(String string, char oldChar, char newChar) { |
| 53 | + return string.replace(oldChar, newChar); |
| 54 | + } |
| 55 | + |
| 56 | + @Contract(value = "true -> false; false -> true", pure = true) |
| 57 | + boolean not(boolean input) { |
| 58 | + return !input; |
| 59 | + } |
| 60 | + |
| 61 | + @Contract("true -> new") |
| 62 | + void contractExpectsWrongParameterType(List<Integer> integers) { |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + @Contract("_, _ -> new") |
| 67 | + void contractExpectsMoreParametersThanMethodHas(String s) { |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + @Contract("_ -> _; null -> !null") |
| 72 | + String secondContractClauseNotReachable(String s) { |
| 73 | + return ""; |
| 74 | + } |
| 75 | + |
| 76 | + @Contract("_ -> true") |
| 77 | + void contractExpectsWrongReturnType(String s) { |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + // NB: the following examples demonstrate how to use the mutates attribute of the annotation |
| 82 | + // This attribute is currently experimental and could be changed or removed in the future |
| 83 | + @Contract(mutates = "param") |
| 84 | + void incrementArrayFirstElement(Integer[] integers) { |
| 85 | + if (integers.length > 0) { |
| 86 | + integers[0] = integers[0] + 1; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @Contract(pure = true, mutates = "param") |
| 91 | + void impossibleToMutateParamInPureFunction(List<String> strings) { |
| 92 | + if (strings != null) { |
| 93 | + strings.forEach(System.out::println); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @Contract(mutates = "param3") |
| 98 | + void impossibleToMutateThirdParamWhenMethodHasOnlyTwoParams(int a, int b) { |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | + @Contract(mutates = "param") |
| 103 | + void impossibleToMutableImmutableType(String s) { |
| 104 | + |
| 105 | + } |
| 106 | + |
| 107 | + @Contract(mutates = "this") |
| 108 | + static void impossibleToMutateThisInStaticMethod() { |
| 109 | + |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments