Skip to content

Commit

Permalink
Apply new conventions to lang package (SkriptLang#6918)
Browse files Browse the repository at this point in the history
* Update lang package to new annotation conventions, new warning conventions, and use try-with-resources for loggers

* Apply suggestions from code review

Co-authored-by: Patrick Miller <[email protected]>

* Requested changes

---------

Co-authored-by: Moderocky <[email protected]>
Co-authored-by: Patrick Miller <[email protected]>
  • Loading branch information
3 people authored and Prorickey committed Aug 3, 2024
1 parent 72b17b2 commit 94f1133
Show file tree
Hide file tree
Showing 45 changed files with 347 additions and 557 deletions.
36 changes: 17 additions & 19 deletions src/main/java/ch/njol/skript/classes/Changer.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@
* isn't overridden.
* <p>
* Some useful Changers can be found in {@link DefaultChangers}
*
* @author Peter Güttinger
*
* @see DefaultChangers
* @see Expression
*/
public interface Changer<T> {

public static enum ChangeMode {
enum ChangeMode {
ADD, SET, REMOVE, REMOVE_ALL, DELETE, RESET;
}

Expand All @@ -45,45 +44,44 @@ public static enum ChangeMode {
* <p>
* Unlike {@link Expression#acceptChange(ChangeMode)} this method must not print errors.
*
* @param mode
* @param mode The {@link ChangeMode} to test.
* @return An array of types that {@link #change(Object[], Object[], ChangeMode)} accepts as its <code>delta</code> parameter (which can be arrays to denote that multiple of
* that type are accepted), or null if the given mode is not supported. For {@link ChangeMode#DELETE} and {@link ChangeMode#RESET} this can return any non-null array to
* mark them as supported.
*/
@Nullable
public abstract Class<?>[] acceptChange(ChangeMode mode);
Class<?> @Nullable [] acceptChange(ChangeMode mode);

/**
* @param what The objects to change
* @param delta An array with one or more instances of one or more of the the classes returned by {@link #acceptChange(ChangeMode)} for the given change mode (null for
* {@link ChangeMode#DELETE} and {@link ChangeMode#RESET}). <b>This can be a Object[], thus casting is not allowed.</b>
* @param mode
* @param mode The {@link ChangeMode} to test.
* @throws UnsupportedOperationException (optional) if this method was called on an unsupported ChangeMode.
*/
public abstract void change(T[] what, @Nullable Object[] delta, ChangeMode mode);
void change(T[] what, Object @Nullable [] delta, ChangeMode mode);

public static abstract class ChangerUtils {
@SuppressWarnings("unchecked")
public static <T, V> void change(final Changer<T> changer, final Object[] what, final @Nullable Object[] delta, final ChangeMode mode) {
abstract class ChangerUtils {

public static <T> void change(Changer<T> changer, Object[] what, Object @Nullable [] delta, ChangeMode mode) {
//noinspection unchecked
changer.change((T[]) what, delta, mode);
}

/**
* Tests whether an expression accepts changes of a certain type. If multiple types are given it test for whether any of the types is accepted.
*
* @param e The expression to test
* @param expression The expression to test
* @param mode The ChangeMode to use in the test
* @param types The types to test for
* @return Whether <tt>e.{@link Expression#change(Event, Object[], ChangeMode) change}(event, type[], mode)</tt> can be used or not.
* @return Whether <tt>expression.{@link Expression#change(Event, Object[], ChangeMode) change}(event, type[], mode)</tt> can be used or not.
*/
public static boolean acceptsChange(final Expression<?> e, final ChangeMode mode, final Class<?>... types) {
final Class<?>[] cs = e.acceptChange(mode);
if (cs == null)
public static boolean acceptsChange(final Expression<?> expression, final ChangeMode mode, final Class<?>... types) {
final Class<?>[] validTypes = expression.acceptChange(mode);
if (validTypes == null)
return false;
for (final Class<?> type : types) {
for (final Class<?> c : cs) {
if (c.isArray() ? c.getComponentType().isAssignableFrom(type) : c.isAssignableFrom(type))
for (final Class<?> validType : validTypes) {
if (validType.isArray() ? validType.getComponentType().isAssignableFrom(type) : validType.isAssignableFrom(type))
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ public World[] executeSimple(Object[][] params) {
}, DefaultClasses.LOCATION, true) {
@Override
@Nullable
public Location[] execute(FunctionEvent<?> e, Object[][] params) {
public Location[] execute(FunctionEvent<?> event, Object[][] params) {
for (int i : new int[] {0, 1, 2, 4, 5}) {
if (params[i] == null || params[i].length == 0 || params[i][0] == null)
return null;
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/ch/njol/skript/lang/Condition.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Checker;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.Nullable;

import java.util.Iterator;

Expand Down Expand Up @@ -67,12 +67,11 @@ public final boolean isNegated() {
return negated;
}

@Nullable
@SuppressWarnings({"rawtypes", "unchecked"})
public static Condition parse(String input, @Nullable String defaultError) {
public static @Nullable Condition parse(String input, @Nullable String defaultError) {
input = input.trim();
while (input.startsWith("(") && SkriptParser.next(input, 0, ParseContext.DEFAULT) == input.length())
input = input.substring(1, input.length() - 1);
//noinspection unchecked,rawtypes
return (Condition) SkriptParser.parse(input, (Iterator) Skript.getConditions().iterator(), defaultError);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ch/njol/skript/lang/Debuggable.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package ch.njol.skript.lang;

import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.Nullable;

/**
* Represents an element that can print details involving an event.
Expand Down
12 changes: 4 additions & 8 deletions src/main/java/ch/njol/skript/lang/Effect.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import ch.njol.skript.log.ParseLogHandler;
import ch.njol.skript.log.SkriptLogger;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.Nullable;

import java.util.Iterator;

Expand All @@ -50,11 +50,8 @@ public final boolean run(Event event) {
return true;
}

@Nullable
@SuppressWarnings({"rawtypes", "unchecked"})
public static Effect parse(String input, @Nullable String defaultError) {
ParseLogHandler log = SkriptLogger.startParseLogHandler();
try {
public static @Nullable Effect parse(String input, @Nullable String defaultError) {
try (ParseLogHandler log = SkriptLogger.startParseLogHandler()) {
EffFunctionCall functionCall = EffFunctionCall.parse(input);
if (functionCall != null) {
log.printLog();
Expand All @@ -72,6 +69,7 @@ public static Effect parse(String input, @Nullable String defaultError) {
}
log.clear();

//noinspection unchecked,rawtypes
Effect effect = (Effect) SkriptParser.parse(input, (Iterator) Skript.getEffects().iterator(), defaultError);
if (effect != null) {
log.printLog();
Expand All @@ -80,8 +78,6 @@ public static Effect parse(String input, @Nullable String defaultError) {

log.printError();
return null;
} finally {
log.stop();
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/main/java/ch/njol/skript/lang/EffectSection.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.parser.ParserInstance;
import ch.njol.util.Kleenean;
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.Nullable;

import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -69,11 +69,10 @@ public abstract boolean init(Expression<?>[] expressions,
/**
* Similar to {@link Section#parse(String, String, SectionNode, List)}, but will only attempt to parse from other {@link EffectSection}s.
*/
@Nullable
@SuppressWarnings({"unchecked", "rawtypes"})
public static EffectSection parse(String input, @Nullable String defaultError, @Nullable SectionNode sectionNode, @Nullable List<TriggerItem> triggerItems) {
public static @Nullable EffectSection parse(String input, @Nullable String defaultError, @Nullable SectionNode sectionNode, @Nullable List<TriggerItem> triggerItems) {
SectionContext sectionContext = ParserInstance.get().getData(SectionContext.class);

//noinspection unchecked,rawtypes
return sectionContext.modify(sectionNode, triggerItems, () ->
(EffectSection) SkriptParser.parse(
input,
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ch/njol/skript/lang/EffectSectionEffect.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.Nullable;

/**
* Represents the Effect aspect of an EffectSection. This allows for the use of EffectSections as effects, rather than just sections.
Expand Down
22 changes: 8 additions & 14 deletions src/main/java/ch/njol/skript/lang/Expression.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
import ch.njol.util.Checker;
import org.bukkit.event.Event;
import org.bukkit.inventory.ItemStack;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.lang.converter.Converter;

import java.util.HashMap;
Expand Down Expand Up @@ -64,8 +63,7 @@ public interface Expression<T> extends SyntaxElement, Debuggable {
* @return The value or null if this expression doesn't have any value for the event
* @throws UnsupportedOperationException (optional) if this was called on a non-single expression
*/
@Nullable
T getSingle(Event event);
@Nullable T getSingle(Event event);

/**
* Get an optional of the single value of this expression.
Expand Down Expand Up @@ -107,7 +105,7 @@ default Optional<T> getOptionalSingle(Event event) {
* @param event The event
* @return A non-null stream of this expression's non-null values
*/
default Stream<@NonNull ? extends T> stream(Event event) {
default Stream<? extends T> stream(Event event) {
Iterator<? extends T> iterator = iterator(event);
if (iterator == null) {
return Stream.empty();
Expand Down Expand Up @@ -178,9 +176,8 @@ default boolean canBeSingle() {
* @see Converter
* @see ConvertedExpression
*/
@Nullable
@SuppressWarnings("unchecked")
<R> Expression<? extends R> getConvertedExpression(Class<R>... to);
<R> @Nullable Expression<? extends R> getConvertedExpression(Class<R>... to);

/**
* Gets the return type of this expression.
Expand Down Expand Up @@ -265,8 +262,7 @@ default boolean canReturn(Class<?> returnType) {
* @param event The event to be used for evaluation
* @return An iterator to iterate over all values of this expression which may be empty and/or null, but must not return null elements.
*/
@Nullable
Iterator<? extends T> iterator(Event event);
@Nullable Iterator<? extends T> iterator(Event event);

/**
* Checks whether the given 'loop-...' expression should match this loop, e.g. loop-block matches any loops that loop through blocks and loop-argument matches an
Expand Down Expand Up @@ -316,8 +312,7 @@ default boolean canReturn(Class<?> returnType) {
* that type are accepted), or null if the given mode is not supported. For {@link ChangeMode#DELETE} and {@link ChangeMode#RESET} this can return any non-null array to
* mark them as supported.
*/
@Nullable
Class<?>[] acceptChange(ChangeMode mode);
Class<?> @Nullable [] acceptChange(ChangeMode mode);

/**
* Tests all accepted change modes, and if so what type it expects the <code>delta</code> to be.
Expand All @@ -343,7 +338,7 @@ default Map<ChangeMode, Class<?>[]> getAcceptedChangeModes() {
* @param mode The {@link ChangeMode} of the attempted change
* @throws UnsupportedOperationException (optional) - If this method was called on an unsupported ChangeMode.
*/
void change(Event event, @Nullable Object[] delta, ChangeMode mode);
void change(Event event, Object @Nullable [] delta, ChangeMode mode);

/**
* This method is called before this expression is set to another one.
Expand All @@ -356,8 +351,7 @@ default Map<ChangeMode, Class<?>[]> getAcceptedChangeModes() {
* @param delta Initial delta array.
* @return Delta array to use for change.
*/
@Nullable
default Object[] beforeChange(Expression<?> changed, @Nullable Object[] delta) {
default Object @Nullable [] beforeChange(Expression<?> changed, Object @Nullable [] delta) {
if (delta == null || delta.length == 0) // Nothing to nothing
return null;

Expand Down
8 changes: 3 additions & 5 deletions src/main/java/ch/njol/skript/lang/ExpressionInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
*/
package ch.njol.skript.lang;

import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.Nullable;

/**
* Represents an expression's information, for use when creating new instances of expressions.
*/
public class ExpressionInfo<E extends Expression<T>, T> extends SyntaxElementInfo<E> {

@Nullable
public ExpressionType expressionType;
public @Nullable ExpressionType expressionType;
public Class<T> returnType;

public ExpressionInfo(String[] patterns, Class<T> returnType, Class<E> expressionClass, String originClassPath) throws IllegalArgumentException {
Expand All @@ -51,8 +50,7 @@ public Class<T> getReturnType() {
* Get the type of this expression.
* @return The type of this Expression
*/
@Nullable
public ExpressionType getExpressionType() {
public @Nullable ExpressionType getExpressionType() {
return expressionType;
}

Expand Down
25 changes: 10 additions & 15 deletions src/main/java/ch/njol/skript/lang/ExpressionList.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import ch.njol.util.Kleenean;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.Array;
import java.util.ArrayList;
Expand All @@ -47,8 +47,7 @@ public class ExpressionList<T> implements Expression<T> {
protected boolean and;
private final boolean single;

@Nullable
private final ExpressionList<?> source;
private final @Nullable ExpressionList<?> source;

public ExpressionList(Expression<? extends T>[] expressions, Class<T> returnType, boolean and) {
this(expressions, returnType, and, null);
Expand Down Expand Up @@ -89,40 +88,38 @@ public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean is
}

@Override
@Nullable
public T getSingle(Event event) {
public @Nullable T getSingle(Event event) {
if (!single)
throw new UnsupportedOperationException();
Expression<? extends T> expression = CollectionUtils.getRandom(expressions);
return expression != null ? expression.getSingle(event) : null;
}

@Override
@SuppressWarnings("unchecked")
public T[] getArray(Event event) {
if (and)
return getAll(event);
Expression<? extends T> expression = CollectionUtils.getRandom(expressions);
//noinspection unchecked
return expression != null ? expression.getArray(event) : (T[]) Array.newInstance(returnType, 0);
}

@Override
@SuppressWarnings("unchecked")
public T[] getAll(Event event) {
List<T> values = new ArrayList<>();
for (Expression<? extends T> expr : expressions)
values.addAll(Arrays.asList(expr.getAll(event)));
//noinspection unchecked
return values.toArray((T[]) Array.newInstance(returnType, values.size()));
}

@Override
@Nullable
public Iterator<? extends T> iterator(Event event) {
public @Nullable Iterator<? extends T> iterator(Event event) {
if (!and) {
Expression<? extends T> expression = CollectionUtils.getRandom(expressions);
return expression != null ? expression.iterator(event) : null;
}
return new Iterator<T>() {
return new Iterator<>() {
private int i = 0;
@Nullable
private Iterator<? extends T> current = null;
Expand Down Expand Up @@ -178,9 +175,8 @@ public boolean check(Event event, Checker<? super T> checker) {
}

@Override
@Nullable
@SuppressWarnings("unchecked")
public <R> Expression<? extends R> getConvertedExpression(Class<R>... to) {
public <R> @Nullable Expression<? extends R> getConvertedExpression(Class<R>... to) {
Expression<? extends R>[] exprs = new Expression[expressions.length];
Class<?>[] returnTypes = new Class[expressions.length];
for (int i = 0; i < exprs.length; i++) {
Expand Down Expand Up @@ -215,8 +211,7 @@ public void invertAnd() {
}

@Override
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
public Class<?> @Nullable [] acceptChange(ChangeMode mode) {
Class<?>[] exprClasses = expressions[0].acceptChange(mode);
if (exprClasses == null)
return null;
Expand All @@ -234,7 +229,7 @@ public Class<?>[] acceptChange(ChangeMode mode) {
}

@Override
public void change(Event event, @Nullable Object[] delta, ChangeMode mode) throws UnsupportedOperationException {
public void change(Event event, Object @Nullable [] delta, ChangeMode mode) throws UnsupportedOperationException {
for (Expression<?> expr : expressions) {
expr.change(event, delta, mode);
}
Expand Down
Loading

0 comments on commit 94f1133

Please sign in to comment.