JSR-308: Nonnull Refinement, ParameterAreNonnullByDefault and Eclipse Kepler Detection Method

I decided to change my question, seeing that a year after that I changed the way I work with zeros:

  • I do not use Eclipse builtin null check, because I found it quite primitive (and maybe a little complicated to understand)
  • I use @Nullableto report that the value may be null. In the end, there should be less than zero values ​​than non-zero values.
  • I am using Java 8, and I prefer to use Optionalthat allows you to: Optional.ofNullable(value).orElseGet(() -> 1);. It does not break operators ?:and ?.Groovy, but does not necessarily provide a good tool, for example map, filteretc.

And as for my code:

  • constructors check null values ​​with Objects.requireNonNull, for example:

    public Foobar (String a) {this.a = Objects.requireNonNull (a, "a"); }

  • methods check values ​​using Preconditions.checkNotNullGuava shell whenever I use it in my projects or Objects.requireNonNull:

    public void foobar (String a) {Preconditions.checkNotNull (a, "a"); }

The use of one or the other depends on whether I will reuse the value.

I do not check the parameters of the method every time, but more often in the methods public. The idea is not to replace the default validation, which makes it NullPointerExceptionmore efficient than I can.


I am currently using annotation @Nonnulleither @Nullablefor all my parameters, fields, method result (return), but I am wondering what is really better:

  • , ? (@ParameterAreNonnullByDefault ). , ( , , findbugs)
  • com.foobar @ParameterAreNonnullByDefault, com.foobar.example?
  • ( ) @Nonnull?

, Eclipse 3.8 . "" :

@ParameterAreNonnullByDefault
class Foobar<E extends Throwable> {
  @Nullable private Constructor<E> one;
  @Nullable private Constructor<E> two;

  public Foobar(Constructor<E> one, @Nullable Constructor<E> two) {
    this.one = Objects.requireNonNull(one, "one");
    this.two = two;
  }

  // don't care about exceptions.
  public E getInstance(String msg, Throwable t) { 
    if (null == two) {
      return (E)one.newInstance(msg).initCause(t);
    } 
    return two.newInstance(msg, t);
  }
}

, ?

+3
3

two getInstance, , , . , :

public E getInstance(String msg, Throwable t) { 
  final Constructor<E> localTwo = two;
  if (null == localTwo) {
    return (E)one.newInstance(msg).initCause(t);
  }
  return localTwo.newInstance(msg, t);
}

Preferences > Java > Compiler > Errors/Warnings > Null analysis Enable syntactic null analysis for fields, :

if (two != null) {
  return two.newInstance(msg, t);
}

.

+1

, Eclipse:

Eclipse Kepler, @NonNullByDefault . Luna, Java 8 , .

: Eclipse CI-, . JDT, IntelliJ Eclipse.

Java . , . , , .

, , Eclipse / , . : " @NonNullByDefault" ".

@NonNull , . , , , , , .

, final @NonNull, , .

Eclipse

Eclipse . , , concurrency, NPE, . " " () , , , . Eclipse .

+1

, , Checker Framework Nullness Checker. Eclipse , Eclipse FindBugs. , Eclipse, IDE , Eclipse. , , .

, ?

. .

com.foobar @ParameterAreNonnullByDefault, com.foobar.example?

( , - @Nullable ).

( ) @Nonnull?

, , nullness unsound — , null mind > . Eclipse FindBugs . Checker . , .

, Eclipse 3.8 . "" :

Checker Framework - , .

, Checker Framework , .

, -AconcurrentSemantics. Checker Framework , reset two . , , , , . , , . , ; , .

0
source

All Articles