From 6d2745231ceea6e21e28ac6c9d2c44d2a023658f Mon Sep 17 00:00:00 2001 From: "thibault.faure" Date: Thu, 9 Mar 2023 23:08:06 +0100 Subject: [PATCH] BAEL-6206 code for the JetBrains @Contract annotation article --- jetbrains/README.md | 5 + jetbrains/pom.xml | 37 ++++++ .../java/com/baeldung/annotations/Demo.java | 112 ++++++++++++++++++ .../java/com/baeldung/annotations/Person.java | 15 +++ pom.xml | 4 +- 5 files changed, 171 insertions(+), 2 deletions(-) create mode 100644 jetbrains/README.md create mode 100644 jetbrains/pom.xml create mode 100644 jetbrains/src/main/java/com/baeldung/annotations/Demo.java create mode 100644 jetbrains/src/main/java/com/baeldung/annotations/Person.java diff --git a/jetbrains/README.md b/jetbrains/README.md new file mode 100644 index 0000000000..a63d4dffba --- /dev/null +++ b/jetbrains/README.md @@ -0,0 +1,5 @@ +## Jetbrains + +This module contains articles about Jetbrains' libraries. + +### Relevant articles: diff --git a/jetbrains/pom.xml b/jetbrains/pom.xml new file mode 100644 index 0000000000..a10fd3b913 --- /dev/null +++ b/jetbrains/pom.xml @@ -0,0 +1,37 @@ + + + 4.0.0 + jetbrains + 1.0-SNAPSHOT + jetbrains + jar + http://maven.apache.org + + + com.baeldung + parent-java + 0.0.1-SNAPSHOT + ../parent-java + + + + + org.apache.commons + commons-lang3 + ${apache.commons.version} + + + org.jetbrains + annotations + ${jetbrains.annotations.version} + + + + + 3.12.0 + 24.0.1 + + + \ No newline at end of file diff --git a/jetbrains/src/main/java/com/baeldung/annotations/Demo.java b/jetbrains/src/main/java/com/baeldung/annotations/Demo.java new file mode 100644 index 0000000000..3638d13581 --- /dev/null +++ b/jetbrains/src/main/java/com/baeldung/annotations/Demo.java @@ -0,0 +1,112 @@ +package com.baeldung.annotations; + +import java.util.List; + +import org.apache.commons.lang3.StringUtils; +import org.jetbrains.annotations.Contract; + +public class Demo { + + @Contract("_ -> new") + Person fromName(String name) { + return new Person().withName(name); + } + + @Contract(" -> fail") + void alwaysFail() { + throw new RuntimeException(); + } + + @Contract(" -> fail") + void doNothingWithWrongContract() { + + } + + @Contract("_, null -> null; null, _ -> param2; _, !null -> !null") + String concatenateOnlyIfSecondArgumentIsNotNull(String head, String tail) { + if (tail == null) { + return null; + } + if (head == null) { + return tail; + } + return head + tail; + } + + void uselessNullCheck() { + String head = "1234"; + String tail = "5678"; + String concatenation = concatenateOnlyIfSecondArgumentIsNotNull(head, tail); + if (concatenation != null) { + System.out.println(concatenation); + } + } + + void uselessNullCheckOnInferredAnnotation() { + if (StringUtils.isEmpty(null)) { + System.out.println("baeldung"); + } + } + + @Contract(pure = true) + String replace(String string, char oldChar, char newChar) { + return string.replace(oldChar, newChar); + } + + @Contract(value = "true -> false; false -> true", pure = true) + boolean not(boolean input) { + return !input; + } + + @Contract("true -> new") + void contractExpectsWrongParameterType(List integers) { + + } + + @Contract("_, _ -> new") + void contractExpectsMoreParametersThanMethodHas(String s) { + + } + + @Contract("_ -> _; null -> !null") + String secondContractClauseNotReachable(String s) { + return ""; + } + + @Contract("_ -> true") + void contractExpectsWrongReturnType(String s) { + + } + + // NB: the following examples demonstrate how to use the mutates attribute of the annotation + // This attribute is currently experimental and could be changed or removed in the future + @Contract(mutates = "param") + void incrementArrayFirstElement(Integer[] integers) { + if (integers.length > 0) { + integers[0] = integers[0] + 1; + } + } + + @Contract(pure = true, mutates = "param") + void impossibleToMutateParamInPureFunction(List strings) { + if (strings != null) { + strings.forEach(System.out::println); + } + } + + @Contract(mutates = "param3") + void impossibleToMutateThirdParamWhenMethodHasOnlyTwoParams(int a, int b) { + + } + + @Contract(mutates = "param") + void impossibleToMutableImmutableType(String s) { + + } + + @Contract(mutates = "this") + static void impossibleToMutateThisInStaticMethod() { + + } + +} diff --git a/jetbrains/src/main/java/com/baeldung/annotations/Person.java b/jetbrains/src/main/java/com/baeldung/annotations/Person.java new file mode 100644 index 0000000000..086b73b47f --- /dev/null +++ b/jetbrains/src/main/java/com/baeldung/annotations/Person.java @@ -0,0 +1,15 @@ +package com.baeldung.annotations; + +import org.jetbrains.annotations.Contract; + +public class Person { + + String name; + + @Contract("_ -> this") + Person withName(String name) { + this.name = name; + return this; + } + +} diff --git a/pom.xml b/pom.xml index 6ef6abd805..67353d9839 100644 --- a/pom.xml +++ b/pom.xml @@ -352,6 +352,7 @@ java-jdi java-websocket + jetbrains jhipster-5 jmh @@ -730,7 +731,7 @@ spring-boot-modules/spring-boot-react spring-ejb-modules/ejb-beans vaadin - vavr-modules + vavr-modules @@ -933,7 +934,6 @@ asm atomikos atomix - bazel code-generation