Skip to content

Commit

Permalink
check annotation retention in more places
Browse files Browse the repository at this point in the history
  • Loading branch information
Revxrsal committed Sep 21, 2024
1 parent d272003 commit 4fb6ff2
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 1 deletion.
2 changes: 2 additions & 0 deletions common/src/main/java/revxrsal/commands/Lamp.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import java.util.function.Predicate;
import java.util.function.Supplier;

import static revxrsal.commands.util.Classes.checkRetention;
import static revxrsal.commands.util.Classes.wrap;
import static revxrsal.commands.util.Preconditions.notNull;

Expand Down Expand Up @@ -744,6 +745,7 @@ public Builder() {
public <T extends Annotation> Builder<A> annotationReplacer(@NotNull Class<T> annotationType, @NotNull AnnotationReplacer<T> replacer) {
notNull(annotationType, "annotation type");
notNull(replacer, "annotation replacer");
checkRetention(annotationType);
annotationReplacers.computeIfAbsent(annotationType, k -> new HashSet<>()).add(replacer);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import java.util.List;
import java.util.function.Function;

import static revxrsal.commands.util.Classes.checkRetention;

/**
* An interface that supplies completions for the user depending on their input.
*/
Expand Down Expand Up @@ -154,6 +156,7 @@ static <A extends CommandActor> Factory<? super A> forTypeAndSubclasses(@NotNull
* @return The newly created {@link Factory}.
*/
static @NotNull <A extends CommandActor, L extends Annotation> SuggestionProvider.@NotNull Factory<? super A> forAnnotation(@NotNull Class<L> annotationType, @NotNull Function<L, SuggestionProvider<A>> provider) {
checkRetention(annotationType);
return (type, annotations, lamp) -> {
L annotation = annotations.get(annotationType);
if (annotation != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.lang.annotation.Annotation;
import java.util.function.Function;

import static revxrsal.commands.util.Classes.checkRetention;

/**
* Represents a permission that is required in order to execute a
* command.
Expand Down Expand Up @@ -82,6 +84,7 @@ static <A extends CommandActor, T extends Annotation> Factory<A> forAnnotation(
@NotNull Class<T> annotationType,
@NotNull Function<T, @Nullable CommandPermission<A>> permissionCreator
) {
checkRetention(annotationType);
return (annotations, lamp) -> {
T annotation = annotations.get(annotationType);
if (annotation != null)
Expand Down
3 changes: 2 additions & 1 deletion common/src/main/java/revxrsal/commands/util/Classes.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ private static void addPrimitive(
*/
public static void checkRetention(@NotNull Class<? extends Annotation> type) {
if (!type.isAnnotationPresent(Retention.class) || type.getAnnotation(Retention.class).value() != RetentionPolicy.RUNTIME)
throw new IllegalArgumentException("Tried to check for annotation @" + type.getName() + ", but it does not have @Retention(RetentionPolicy.RUNTIME)!");
throw new IllegalArgumentException("Tried to check for annotation @" + type.getName() + ", but it does not have @Retention(RetentionPolicy.RUNTIME)! " +
"As such, it may be present but we cannot see it.");
}
}

0 comments on commit 4fb6ff2

Please sign in to comment.