Skip to content

Commit

Permalink
Add compliance test for nullability annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment committed Oct 5, 2024
1 parent e1e8410 commit 3517181
Show file tree
Hide file tree
Showing 35 changed files with 149 additions and 5 deletions.
26 changes: 26 additions & 0 deletions src/main/java/net/dv8tion/jda/annotations/UnknownNullability.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.dv8tion.jda.annotations;

import java.lang.annotation.*;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface UnknownNullability
{
}
2 changes: 2 additions & 0 deletions src/main/java/net/dv8tion/jda/api/JDA.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,13 @@ public int getShardTotal()
*
* @return A String representing the information used to build this shard.
*/
@Nonnull
public String getShardString()
{
return "[" + shardId + " / " + shardTotal + "]";
}

@Nonnull
@Override
public String toString()
{
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/net/dv8tion/jda/api/OnlineStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package net.dv8tion.jda.api;

import javax.annotation.Nonnull;

/**
* Represents the online presence of a {@link net.dv8tion.jda.api.entities.Member Member}.
*/
Expand Down Expand Up @@ -63,6 +65,7 @@ public enum OnlineStatus
*
* @see <a href="https://discord.com/developers/docs/topics/gateway#presence-update">PRESENCE_UPDATE</a>
*/
@Nonnull
public String getKey()
{
return key;
Expand All @@ -77,7 +80,8 @@ public String getKey()
*
* @return The matching {@link net.dv8tion.jda.api.OnlineStatus OnlineStatus}. If there is no match, returns {@link net.dv8tion.jda.api.OnlineStatus#UNKNOWN UNKNOWN}.
*/
public static OnlineStatus fromKey(String key)
@Nonnull
public static OnlineStatus fromKey(@Nonnull String key)
{
for (OnlineStatus onlineStatus : values())
{
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/net/dv8tion/jda/api/audit/ActionType.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
import net.dv8tion.jda.api.entities.emoji.RichCustomEmoji;

import javax.annotation.Nonnull;

/**
* ActionTypes for {@link net.dv8tion.jda.api.audit.AuditLogEntry AuditLogEntry} instances
* <br>Found via {@link net.dv8tion.jda.api.audit.AuditLogEntry#getType() AuditLogEntry.getType()}
Expand Down Expand Up @@ -684,11 +686,13 @@ public int getKey()
*
* @return {@link net.dv8tion.jda.api.audit.TargetType TargetType}
*/
@Nonnull
public TargetType getTargetType()
{
return target;
}

@Nonnull
public static ActionType from(int key)
{
for (ActionType type : values())
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/net/dv8tion/jda/api/audit/AuditLogKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import net.dv8tion.jda.api.entities.sticker.GuildSticker;
import net.dv8tion.jda.internal.utils.EntityString;

import javax.annotation.Nonnull;

/**
* Enum of possible/expected keys that can be provided
* to {@link AuditLogEntry#getChangeByKey(AuditLogKey) AuditLogEntry.getChangeByKey(AuditLogEntry.AuditLogKey}.
Expand Down Expand Up @@ -686,11 +688,13 @@ public enum AuditLogKey
this.key = key;
}

@Nonnull
public String getKey()
{
return key;
}

@Nonnull
@Override
public String toString()
{
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/net/dv8tion/jda/api/audit/AuditLogOption.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import net.dv8tion.jda.internal.utils.EntityString;

import javax.annotation.Nonnull;

/**
* Enum constants for possible options
* <br>Providing detailed description of possible occasions and expected types.
Expand Down Expand Up @@ -139,6 +141,7 @@ public enum AuditLogOption
*
* @return Key for this option
*/
@Nonnull
public String getKey()
{
return key;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/net/dv8tion/jda/api/entities/ClientType.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public enum ClientType
*
* @return The raw key
*/
@Nonnull
public String getKey()
{
return key;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ default String getApplicationId()
*
* @return The id of the user that purchased the {@link Entitlement Entitlement}
*/
@Nonnull
default String getUserId()
{
return Long.toUnsignedString(getUserIdLong());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public interface RichPresence extends Activity
* @see ActivityFlag
* @see ActivityFlag#getFlags(int)
*/
@Nonnull
EnumSet<ActivityFlag> getFlagSet();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,7 @@ default PollVotersPaginationAction retrievePollVotersById(long messageId, long a
*
* @return A {@link net.dv8tion.jda.api.entities.MessageHistory MessageHistory} related to this channel.
*/
@Nonnull
default MessageHistory getHistory()
{
return new MessageHistory(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ public int getKey()
*
* @return The layout type or {@link #UNKNOWN}
*/
@Nonnull
public static LayoutType fromKey(int key)
{
for (LayoutType type : values())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public long getIdLong()
* @throws java.lang.UnsupportedOperationException
* The date of creation cannot be calculated.
*/
@Nonnull
@Override
public OffsetDateTime getTimeCreated()
{
Expand Down Expand Up @@ -341,6 +342,7 @@ public long getIdLong()
* @throws java.lang.UnsupportedOperationException
* The date of creation cannot be calculated.
*/
@Nonnull
@Override
public OffsetDateTime getTimeCreated()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public long getIdLong()
* @throws java.lang.UnsupportedOperationException
* The date of creation cannot be calculated.
*/
@Nonnull
@Override
public OffsetDateTime getTimeCreated()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public GuildMemberRoleRemoveEvent(@Nonnull JDA api, long responseNumber, @Nonnul
*
* @return The removed roles
*/
@Nonnull
public List<Role> getRoles()
{
return removedRoles;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public boolean isServerError()
*
* @return Never-null meaning of this error.
*/
@Nonnull
public String getMeaning()
{
return meaning;
Expand All @@ -118,6 +119,7 @@ public int getErrorCode()
*
* @return {@link net.dv8tion.jda.api.requests.ErrorResponse ErrorResponse}
*/
@Nonnull
public ErrorResponse getErrorResponse()
{
return errorResponse;
Expand All @@ -128,6 +130,7 @@ public ErrorResponse getErrorResponse()
*
* @return {@link net.dv8tion.jda.api.requests.Response Response}
*/
@Nonnull
public Response getResponse()
{
return response;
Expand All @@ -145,12 +148,14 @@ public List<SchemaError> getSchemaErrors()
return schemaErrors;
}

public static ErrorResponseException create(String message, ErrorResponseException cause)
@Nonnull
public static ErrorResponseException create(@Nonnull String message, @Nonnull ErrorResponseException cause)
{
return new ErrorResponseException(message, cause);
}

public static ErrorResponseException create(ErrorResponse errorResponse, Response response)
@Nonnull
public static ErrorResponseException create(@Nonnull ErrorResponse errorResponse, @Nonnull Response response)
{
String meaning = errorResponse.getMeaning();
int code = errorResponse.getCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import net.dv8tion.jda.api.requests.Route;
import net.dv8tion.jda.internal.utils.Helpers;

import javax.annotation.Nonnull;

/**
* Indicates that we received a {@code 429: Too Many Requests} response
*/
Expand All @@ -45,6 +47,7 @@ public RateLimitedException(String route, long retryAfter)
*
* @return The corresponding route
*/
@Nonnull
public String getRateLimitedRoute()
{
return rateLimitedRoute;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ public String getName()
*
* @return The Choice instance, for chaining
*/
@Nonnull
public Choice setName(@Nonnull String name)
{
checkName(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package net.dv8tion.jda.api.interactions.commands;

import net.dv8tion.jda.annotations.UnknownNullability;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
Expand All @@ -24,6 +25,7 @@
import net.dv8tion.jda.api.interactions.Interaction;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.internal.utils.Checks;
import org.jetbrains.annotations.Contract;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -410,6 +412,7 @@ default <T> T getOption(@Nonnull String name, @Nonnull Function<? super OptionMa
* @see #getOption(String, Function)
* @see #getOption(String, Supplier, Function)
*/
@Contract("_,null,_->null")
default <T> T getOption(@Nonnull String name,
@Nullable T fallback,
@Nonnull Function<? super OptionMapping, ? extends T> resolver)
Expand Down Expand Up @@ -459,6 +462,7 @@ default <T> T getOption(@Nonnull String name,
* @see #getOption(String, Function)
* @see #getOption(String, Object, Function)
*/
@UnknownNullability
default <T> T getOption(@Nonnull String name,
@Nullable Supplier<? extends T> fallback,
@Nonnull Function<? super OptionMapping, ? extends T> resolver)
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/net/dv8tion/jda/api/requests/RestAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package net.dv8tion.jda.api.requests;

import net.dv8tion.jda.annotations.UnknownNullability;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.exceptions.ContextException;
import net.dv8tion.jda.api.exceptions.RateLimitedException;
Expand Down Expand Up @@ -630,6 +631,7 @@ default void queue(@Nullable Consumer<? super T> success)
* @return The response value
*/
@Blocking
@UnknownNullability
default T complete()
{
try
Expand Down Expand Up @@ -664,6 +666,7 @@ default T complete()
* @return The response value
*/
@Blocking
@UnknownNullability
T complete(boolean shouldQueue) throws RateLimitedException;

/**
Expand Down Expand Up @@ -1361,6 +1364,7 @@ default DelayedCompletableFuture<T> submitAfter(long delay, @Nonnull TimeUnit un
* @return The response value
*/
@Blocking
@UnknownNullability
default T completeAfter(long delay, @Nonnull TimeUnit unit)
{
Checks.notNull(unit, "TimeUnit");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ else if (isStopped)
});
}

@Nonnull
public Queue<Work> getRequests()
{
return requests;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,7 @@ default CompletableFuture<?> forEachRemainingAsync(@Nonnull final Procedure<? su
@Blocking
void forEachRemaining(@Nonnull final Procedure<? super T> action);

@Nonnull
@Override
@Blocking
default Spliterator<T> spliterator()
Expand Down Expand Up @@ -790,7 +791,9 @@ public boolean hasNext()
return false;
}

@Nonnull
@Override
@SuppressWarnings("DataFlowIssue")
public E next()
{
if (!hasNext())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ public int getShardsTotal()
}

@Override
@Nullable
public Guild getGuildById(long id)
{
int shardId = MiscUtil.getShardForGuild(id, getShardsTotal());
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/net/dv8tion/jda/api/utils/Compression.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package net.dv8tion.jda.api.utils;

import javax.annotation.Nonnull;

/**
* Compression algorithms that can be used with JDA.
*
Expand All @@ -41,6 +43,7 @@ public enum Compression
*
* @return The query key
*/
@Nonnull
public String getKey()
{
return key;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/net/dv8tion/jda/api/utils/IOFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package net.dv8tion.jda.api.utils;

import net.dv8tion.jda.annotations.UnknownNullability;

import java.io.IOException;

/**
Expand All @@ -27,5 +29,6 @@
@FunctionalInterface
public interface IOFunction<T, R>
{
@UnknownNullability
R apply(T t) throws IOException;
}
Loading

0 comments on commit 3517181

Please sign in to comment.