Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » public static <T> @NonNull T @NonNull [] arrayElementsAreNonNull( @Nullable T @Nullable[] val
public static <T> @NonNull T @NonNull [] arrayElementsAreNonNull( @Nullable T @Nullable[] val [message #1748308] Tue, 22 November 2016 01:21 Go to next message
Frank Benoit is currently offline Frank BenoitFriend
Messages: 179
Registered: July 2009
Senior Member
How to implement a function that converts (after checks) a non-null array with all elements non-null

	public static <T> @NonNull T @NonNull [] arrayElementsAreNonNull( @Nullable T @Nullable[] value) {


?
Re: public static <T> @NonNull T @NonNull [] arrayElementsAreNonNull( @Nullable T @Nullable[] [message #1748316 is a reply to message #1748308] Tue, 22 November 2016 07:32 Go to previous messageGo to next message
Frank Benoit is currently offline Frank BenoitFriend
Messages: 179
Registered: July 2009
Senior Member
Now i have it like this, using a cast with suppressed warning.
I thougth it might be possible without this.

	
public static <T> T[] arrayIsNonNull( @Nullable T @Nullable[] value) {
	@Nullable T[] notNull = Assert.isNotNull(value);
	for( int i = 0; i < notNull.length; i++ ){
		Assert.isNotNull(notNull[i]);
	}
	@SuppressWarnings("null")
	T[] res = (@NonNull T @NonNull [])notNull;
	return res;
}


Re: public static <T> @NonNull T @NonNull [] arrayElementsAreNonNull( @Nullable T @Nullable[] [message #1748409 is a reply to message #1748316] Tue, 22 November 2016 20:42 Go to previous message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
Your approach looks good, and I don't see a better alternative, because flow analysis cannot see that all array cells have been checked. After manual checks, a cast is the correct idiom for telling the compiler.

To be perfectly explicit, you may want to specify the method return type as "@NonNull T @NonNull[]", too.

BTW, feel free to open an RFE suggesting this method as an addition to our utility class org.eclipse.jdt.annotation.Checks, which already has a number of similar methods.

Stephan
Previous Topic:HELP
Next Topic:What formatter option can I change to stop my arrays being formatted onto one line
Goto Forum:
  


Current Time: Fri Oct 04 21:38:13 GMT 2024

Powered by FUDForum. Page generated in 0.03235 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top