0

With the following Java code:

public class Bean{
  private String value;
  public Bean(@NonNull String value) {
    //Usually fail-fast validation can be added here if it is needed
    this.value = value;
  }
  public String getValue() {return this.value;}

}

Is it possible to check the constructor argument value by means of the annotation, @NonNull at run time other than compile time? Personally I still did not find any checker-framework, which can do validation checking at run time. However, is it possible to implement an Annotation processor to do run time checking?

Rui
  • 3,454
  • 6
  • 37
  • 70

3 Answers3

0

You should take a look at @NotNull from javax.validation.constraints. I use it in my models and it throw a Constraint exception when I try to save a model with a null @NotNull value.

The import is import javax.validation.constraints.NotNull;

If you are using Spring and mongodb, you'll have to configure it so it works, I have found a piece of code somewhere on the Internet (can't remember where), you may use it:

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.mapping.event.ValidatingMongoEventListener;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

@Configuration
public class CustomRepositoryRestConfigurerAdapter {
    @Bean
    public LocalValidatorFactoryBean localValidatorFactoryBean() {
        return new LocalValidatorFactoryBean();
    }

    @Bean
    public ValidatingMongoEventListener validatingMongoEventListener(
            @Qualifier("localValidatorFactoryBean") LocalValidatorFactoryBean lfb
    ) {
        return new ValidatingMongoEventListener(lfb);
    }
}
0

Yes. Lombok's @NonNull is a runtime check which just inserts an if-statement with a throw:

With Lombok

import lombok.NonNull;

public class NonNullExample extends Something {
  private String name;

  public NonNullExample(@NonNull Person person) {
    super("Hello");
    this.name = person.getName();
  }
}

Vanilla Java

import lombok.NonNull;

public class NonNullExample extends Something {
  private String name;

  public NonNullExample(@NonNull Person person) {
    super("Hello");
    if (person == null) {
      throw new NullPointerException("person is marked @NonNull but is null");
    }
    this.name = person.getName();
  }
}
Michael
  • 41,989
  • 11
  • 82
  • 128
0

Misconception at your end: there is no single answer to your question.

Some annotations, when used on source code like this are mainly targeting compile time. Like some static analysis tool that analyses the data flow to tell you that you are violating "annotated" contracts here or there.

But some annotations are also "meant" to be used at runtime, for example to be used with "beans". Such objects might come in as parameter of a HTTP request, and then you have some framework checking if the content received as JSON for example is actually valid, according to the rules specified via annotations. See this tutorial for some examples.

GhostCat
  • 137,827
  • 25
  • 176
  • 248