Skip to content

Commit

Permalink
feat: Use and recommend org.jspecify.annotations.NonNull instead of a…
Browse files Browse the repository at this point in the history
… custom one (#3046)
  • Loading branch information
Artur- authored Dec 20, 2024
1 parent aeb9d6a commit 4795fd0
Show file tree
Hide file tree
Showing 124 changed files with 507 additions and 478 deletions.
12 changes: 4 additions & 8 deletions packages/java/endpoint/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
<artifactId>hilla-engine-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jspecify</groupId>
<artifactId>jspecify</artifactId>
</dependency>

<!-- API DEPENDENCIES -->

Expand All @@ -66,14 +70,6 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>annotations</artifactId>
<version>3.0.1</version>
<scope>test</scope>
</dependency>


<!-- Fusion Dependencies -->
<dependency>
<groupId>jakarta.validation</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@
*
* This annotation exists only for convenience because the traditional
* `jakarta.annotation.Nonnull` annotation is not applicable to type parameters.
*
* @deprecated use the standardized {@link org.jspecify.annotations.NonNull}
* instead
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE_USE })
@Deprecated(forRemoval = true)
public @interface Nonnull {
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@
* This annotation exists only for convenience because the traditional
* `jakarta.annotation.Nullable` annotation is not applicable to type
* parameters.
*
* @deprecated use the standardized {@link org.jspecify.annotations.Nullable}
* instead
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE_USE })
@Deprecated(forRemoval = true)
public @interface Nullable {
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.vaadin.hilla.crud;

import com.vaadin.hilla.Nullable;
import com.vaadin.hilla.crud.filter.Filter;

import org.jspecify.annotations.Nullable;

/**
* A browser-callable service that can count the given type of objects with a
* given filter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import java.util.ArrayList;
import java.util.List;

import com.vaadin.hilla.EndpointExposed;
import com.vaadin.hilla.Nullable;
import com.vaadin.hilla.crud.filter.Filter;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.CrudRepository;
import org.jspecify.annotations.Nullable;

import com.vaadin.hilla.EndpointExposed;

/**
* A browser-callable service that delegates crud operations to a JPA
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.vaadin.hilla.crud;

import com.vaadin.hilla.Nullable;
import org.jspecify.annotations.Nullable;

/**
* A browser-callable service that can create, update, and delete a given type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

import com.googlecode.gentyref.GenericTypeReflector;
import com.vaadin.hilla.EndpointExposed;
import com.vaadin.hilla.Nullable;
import com.vaadin.hilla.crud.filter.Filter;

import org.jspecify.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.domain.Pageable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import java.util.List;

import com.vaadin.hilla.Nonnull;
import com.vaadin.hilla.Nullable;
import com.vaadin.hilla.crud.filter.Filter;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;
import org.springframework.data.domain.Pageable;

import com.vaadin.hilla.crud.filter.Filter;

/**
* A browser-callable service that can list the given type of object.
*/
Expand All @@ -21,7 +22,7 @@ public interface ListService<T> {
* the filter to apply or {@code null} to not filter
* @return a list of objects or an empty list if no objects were found
*/
@Nonnull
List<@Nonnull T> list(Pageable pageable, @Nullable Filter filter);
@NonNull
List<@NonNull T> list(Pageable pageable, @Nullable Filter filter);

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import java.util.Map;
import java.util.Optional;

import javax.annotation.Nonnull;
import org.jspecify.annotations.NonNull;

import org.junit.Assert;
import org.junit.Before;
Expand Down Expand Up @@ -406,7 +406,7 @@ public void should_InvokeCheckValueForType_When_AnnotatedNonnull()
.checkValueForAnnotatedElement(notNullValue,
getClass().getMethod("stringNonnull"), false);

Assert.assertNull("Should allow values with @Nonnull", error);
Assert.assertNull("Should allow values with @NonNull", error);

verify(explicitNullableTypeChecker).checkValueForType(notNullValue,
String.class, false);
Expand Down Expand Up @@ -500,7 +500,7 @@ public Long methodWithIdAnnotation() {
/**
* Method for testing
*/
@Nonnull
@javax.annotation.Nonnull
public String stringNonnull() {
return "";
}
Expand All @@ -511,7 +511,7 @@ static private class Bean {
public String ignoreProperty;
public String description;
transient String transientProperty;
@Nonnull
@NonNull
private String title;

public Bean() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import java.util.List;
import java.util.Map;

import org.jspecify.annotations.NonNull;

public class NonnullEntity {
@Nonnull
private final List<@Nonnull String> nonNullableField = new ArrayList<>();
@NonNull
private final List<@NonNull String> nonNullableField = new ArrayList<>();

@Nonnull
@NonNull
public String nonNullableMethod(
@Nonnull Map<String, @Nonnull String> nonNullableParameter) {
@NonNull Map<String, @NonNull String> nonNullableParameter) {
return nonNullableParameter.getOrDefault("test", "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static org.junit.Assert.assertTrue;

public class NonnullParserTest {
private static final String ANNOTATION_NAME = "NonNull";
FieldDeclaration field;
MethodDeclaration method;
com.github.javaparser.ast.body.Parameter parameter;
Expand All @@ -38,32 +39,33 @@ public void init() throws FileNotFoundException {

@Test
public void should_haveNonNullableField() {
assertTrue(field.isAnnotationPresent("Nonnull"));
assertTrue(field.isAnnotationPresent(ANNOTATION_NAME));
}

@Test
public void should_haveFieldWithNonNullableCollectionItem() {
assertTrue(field.getVariables().get(0).getType()
.asClassOrInterfaceType().getTypeArguments().get().get(0)
.getAnnotations().stream().anyMatch(annotation -> "Nonnull"
.equals(annotation.getName().asString())));
assertTrue(
field.getVariables().get(0).getType().asClassOrInterfaceType()
.getTypeArguments().get().get(0).getAnnotations()
.stream().anyMatch(annotation -> ANNOTATION_NAME
.equals(annotation.getName().asString())));
}

@Test
public void should_haveMethodWithNonNullableReturnType() {
assertTrue(method.isAnnotationPresent("Nonnull"));
assertTrue(method.isAnnotationPresent(ANNOTATION_NAME));
}

@Test
public void should_haveMethodWithNonNullableParameter() {
assertTrue(parameter.isAnnotationPresent("Nonnull"));
assertTrue(parameter.isAnnotationPresent(ANNOTATION_NAME));
}

@Test
public void should_haveMethodParameterWithNonNullableCollectionItemType() {
assertTrue(parameter.getType().asClassOrInterfaceType()
.getTypeArguments().get().get(1).getAnnotations().stream()
.anyMatch(annotation -> "Nonnull"
.anyMatch(annotation -> ANNOTATION_NAME
.equals(annotation.getName().asString())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.lang.reflect.Parameter;
import java.util.Map;

import org.jspecify.annotations.NonNull;
import org.junit.Before;
import org.junit.Test;

Expand All @@ -27,33 +28,33 @@ public void init() throws NoSuchFieldException, NoSuchMethodException {

@Test
public void should_haveNonNullableField() {
assertTrue(field.getAnnotatedType().isAnnotationPresent(Nonnull.class));
assertTrue(field.getAnnotatedType().isAnnotationPresent(NonNull.class));
}

@Test
public void should_haveFieldWithNonNullableCollectionItem() {
AnnotatedType listItemType = ((AnnotatedParameterizedType) field
.getAnnotatedType()).getAnnotatedActualTypeArguments()[0];
assertTrue(listItemType.isAnnotationPresent(Nonnull.class));
assertTrue(listItemType.isAnnotationPresent(NonNull.class));
}

@Test
public void should_haveMethodWithNonNullableReturnType() {
assertTrue(method.getAnnotatedReturnType()
.isAnnotationPresent(Nonnull.class));
.isAnnotationPresent(NonNull.class));
}

@Test
public void should_haveMethodWithNonNullableParameter() {
assertTrue(parameter.getAnnotatedType()
.isAnnotationPresent(Nonnull.class));
.isAnnotationPresent(NonNull.class));
}

@Test
public void should_haveMethodParameterWithNonNullableCollectionItemType() {
AnnotatedType mapValueType = ((AnnotatedParameterizedType) parameter
.getAnnotatedType()).getAnnotatedActualTypeArguments()[1];

assertTrue(mapValueType.isAnnotationPresent(Nonnull.class));
assertTrue(mapValueType.isAnnotationPresent(NonNull.class));
}
}
4 changes: 2 additions & 2 deletions packages/java/engine-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<groupId>org.jspecify</groupId>
<artifactId>jspecify</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.vaadin.hilla.engine;

import javax.annotation.Nonnull;
import org.jspecify.annotations.NonNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -81,8 +81,8 @@ public Plugins() {
disableAllDefaults = false;
}

Plugins(@Nonnull Collection<Plugin> use,
@Nonnull Collection<Plugin> disable,
Plugins(@NonNull Collection<Plugin> use,
@NonNull Collection<Plugin> disable,
boolean disableAllDefaults) {
this.use.addAll(use);
this.disable.addAll(disable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.List;
import java.util.Objects;

import javax.annotation.Nonnull;
import org.jspecify.annotations.NonNull;

import com.vaadin.hilla.engine.commandrunner.CommandNotFoundException;
import com.vaadin.hilla.engine.commandrunner.CommandRunnerException;
Expand Down Expand Up @@ -80,7 +80,7 @@ private GeneratorProcessor applyConfiguration(
return this;
}

private void applyPlugins(@Nonnull GeneratorConfiguration.Plugins plugins) {
private void applyPlugins(GeneratorConfiguration.@NonNull Plugins plugins) {
pluginsProcessor.setConfig(plugins);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.Objects;
import java.util.Optional;

import javax.annotation.Nonnull;
import org.jspecify.annotations.NonNull;

import com.vaadin.hilla.parser.core.PluginConfiguration;
import com.vaadin.hilla.parser.plugins.backbone.BackbonePlugin;
Expand Down Expand Up @@ -120,7 +120,7 @@ public PluginConfiguration getConfiguration() {
return configuration;
}

@Nonnull
@NonNull
public String getName() {
return name;
}
Expand All @@ -141,8 +141,8 @@ public Plugins() {
disableAllDefaults = false;
}

Plugins(@Nonnull Collection<Plugin> use,
@Nonnull Collection<Plugin> disable,
Plugins(@NonNull Collection<Plugin> use,
@NonNull Collection<Plugin> disable,
boolean disableAllDefaults) {
this.disable.addAll(disable);
this.use.addAll(use);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.Set;
import java.util.stream.Collectors;

import javax.annotation.Nonnull;
import org.jspecify.annotations.NonNull;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -105,26 +105,26 @@ private void applyConfiguration(ParserConfiguration parserConfiguration) {
}

private void applyEndpointAnnotation(
@Nonnull String endpointAnnotationName) {
@NonNull String endpointAnnotationName) {
this.endpointAnnotationName = Objects
.requireNonNull(endpointAnnotationName);
}

private void applyEndpointExposedAnnotation(
@Nonnull String endpointExposedAnnotationName) {
@NonNull String endpointExposedAnnotationName) {
this.endpointExposedAnnotationName = Objects
.requireNonNull(endpointExposedAnnotationName);
}

private void applyExposedPackages(@Nonnull List<String> exposedPackages) {
private void applyExposedPackages(@NonNull List<String> exposedPackages) {
this.exposedPackages = exposedPackages;
}

private void applyOpenAPIBase(@Nonnull String openAPIBasePath) {
private void applyOpenAPIBase(@NonNull String openAPIBasePath) {
this.openAPIBasePath = openAPIBasePath;
}

private void applyPlugins(@Nonnull ParserConfiguration.Plugins plugins) {
private void applyPlugins(ParserConfiguration.@NonNull Plugins plugins) {
this.pluginsProcessor.setConfig(plugins);
}

Expand Down
Loading

0 comments on commit 4795fd0

Please sign in to comment.