Skip to content

Commit 6d27452

Browse files
author
thibault.faure
committed
BAEL-6206 code for the JetBrains @contract annotation article
1 parent 9a26f2c commit 6d27452

File tree

5 files changed

+171
-2
lines changed

5 files changed

+171
-2
lines changed

jetbrains/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Jetbrains
2+
3+
This module contains articles about Jetbrains' libraries.
4+
5+
### Relevant articles:

jetbrains/pom.xml

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>jetbrains</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<name>jetbrains</name>
9+
<packaging>jar</packaging>
10+
<url>http://maven.apache.org</url>
11+
12+
<parent>
13+
<groupId>com.baeldung</groupId>
14+
<artifactId>parent-java</artifactId>
15+
<version>0.0.1-SNAPSHOT</version>
16+
<relativePath>../parent-java</relativePath>
17+
</parent>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.apache.commons</groupId>
22+
<artifactId>commons-lang3</artifactId>
23+
<version>${apache.commons.version}</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.jetbrains</groupId>
27+
<artifactId>annotations</artifactId>
28+
<version>${jetbrains.annotations.version}</version>
29+
</dependency>
30+
</dependencies>
31+
32+
<properties>
33+
<apache.commons.version>3.12.0</apache.commons.version>
34+
<jetbrains.annotations.version>24.0.1</jetbrains.annotations.version>
35+
</properties>
36+
37+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.annotations;
2+
3+
import org.jetbrains.annotations.Contract;
4+
5+
public class Person {
6+
7+
String name;
8+
9+
@Contract("_ -> this")
10+
Person withName(String name) {
11+
this.name = name;
12+
return this;
13+
}
14+
15+
}

pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@
352352
<module>java-jdi</module>
353353
<module>java-websocket</module>
354354

355+
<module>jetbrains</module>
355356
<module>jhipster-5</module>
356357

357358
<module>jmh</module>
@@ -730,7 +731,7 @@
730731
<module>spring-boot-modules/spring-boot-react</module>
731732
<module>spring-ejb-modules/ejb-beans</module>
732733
<module>vaadin</module>
733-
<module>vavr-modules</module>
734+
<module>vavr-modules</module>
734735
</modules>
735736

736737
</profile>
@@ -933,7 +934,6 @@
933934
<module>asm</module>
934935
<module>atomikos</module>
935936
<module>atomix</module>
936-
<!-- <module>axon</module>--><!-- JAVA-18408 -->
937937

938938
<module>bazel</module>
939939
<module>code-generation</module>

0 commit comments

Comments
 (0)