Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use and recommend org.jspecify.annotations.NonNull instead of a custom one #3046

Merged
merged 19 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
cromoteca marked this conversation as resolved.
Show resolved Hide resolved
public @interface Nonnull {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
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 com.vaadin.hilla.EndpointExposed;
import com.vaadin.hilla.Nullable;

/**
* A browser-callable service that delegates crud operations to a JPA
* repository.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import java.util.List;

import com.vaadin.hilla.Nonnull;
import org.jspecify.annotations.NonNull;
import org.springframework.data.domain.Pageable;

import com.vaadin.hilla.Nullable;
import com.vaadin.hilla.crud.filter.Filter;
import org.springframework.data.domain.Pageable;

/**
* 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 "";
}
taefi marked this conversation as resolved.
Show resolved Hide resolved
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
4 changes: 2 additions & 2 deletions packages/java/engine-runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,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
Expand Up @@ -15,7 +15,7 @@
*/
package com.vaadin.hilla.internal;

import javax.annotation.Nonnull;
import org.jspecify.annotations.NonNull;
import java.io.File;
import java.net.URL;
import java.util.Objects;
Expand Down Expand Up @@ -66,7 +66,7 @@ public class TaskGenerateOpenAPIImpl extends AbstractTaskEndpointGenerator
*/
TaskGenerateOpenAPIImpl(File projectDirectory, String buildDirectoryName,
File outputDirectory, Function<String, URL> resourceFinder,
@Nonnull ClassLoader classLoader, boolean isProductionMode) {
@NonNull ClassLoader classLoader, boolean isProductionMode) {
super(projectDirectory, buildDirectoryName, outputDirectory,
resourceFinder);
this.classLoader = Objects.requireNonNull(classLoader,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,14 @@ class SingleModuleTest : AbstractGradleTest() {

import com.vaadin.flow.server.auth.AnonymousAllowed;
import com.vaadin.hilla.Endpoint;
import com.vaadin.hilla.Nonnull;
import org.jspecify.annotations.NonNull;

@Endpoint
@AnonymousAllowed
public class HelloReactEndpoint {

@Nonnull
public String sayHello(@Nonnull String name) {
@NonNull
public String sayHello(@NonNull String name) {
if (name.isEmpty()) {
return "Hello stranger";
} else {
Expand Down
Loading
Loading