Skip to content

Commit

Permalink
Allow SimpleLiteral to be a vararg (#143)
Browse files Browse the repository at this point in the history
* Allow SimpleLiteral to be a vararg

Allow SimpleLiteral to be a vararg

* refactor: changed SimpleLiteral use-cases to allow varargs in its constructor

* fix: reverted original changes but still left some changes to the SimpleLiteral

Vararg generics are just not the way to go in Java and the code was not working when you'd use it as expected. The original changes were therefore reverted, but there was still a small change left in the SimpleLiteral class.

---------

Co-authored-by: Mwexim <[email protected]>
  • Loading branch information
TheLimeGlass and Mwexim authored Jan 1, 2024
1 parent c360bb8 commit 2c83253
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@
*/
@SuppressWarnings("unchecked")
public class SimpleLiteral<T> implements Literal<T> {
private final T[] values;

private boolean isAndList = true;
private Class<T> returnType;
private final Class<T> returnType;
private final T[] values;

public SimpleLiteral(T[] values) {
this.values = values;
this.returnType = (Class<T>) values.getClass().getComponentType();
}

@SafeVarargs
public SimpleLiteral(Class<T> c, T... values) {
this.values = Arrays.copyOf(values, values.length);
this.returnType = c;
}

@Override
Expand Down Expand Up @@ -60,11 +63,7 @@ public boolean isSingle() {
}

public Class<? extends T> getReturnType() {
if (returnType != null) {
return returnType;
} else {
return returnType = (Class<T>) values.getClass().getComponentType();
}
return returnType;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import io.github.syst3ms.skriptparser.variables.Variables;
import org.intellij.lang.annotations.MagicConstant;

import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -238,7 +237,7 @@ public static Optional<? extends Expression<Boolean>> parseBooleanExpression(Str
var expr = (Optional<? extends Expression<Boolean>>) matchExpressionInfo(s, info, BOOLEAN_PATTERN_TYPE, parserState, logger);
if (expr.isPresent()) {
switch (conditional) {
case 0: // Can't be conditional
case NOT_CONDITIONAL: // Can't be conditional
if (ConditionalExpression.class.isAssignableFrom(expr.get().getClass())) {
logger.error(
"The boolean expression must not be conditional",
Expand All @@ -248,11 +247,11 @@ public static Optional<? extends Expression<Boolean>> parseBooleanExpression(Str
return Optional.empty();
}
break;
case 1: // Can be conditional
case MAYBE_CONDITIONAL: // Can be conditional
if (ConditionalExpression.class.isAssignableFrom(expr.get().getClass())) {
recentConditions.acknowledge((ExpressionInfo<? extends ConditionalExpression, ? extends Boolean>) info);
}
case 2: // Has to be conditional
case CONDITIONAL: // Has to be conditional
if (!ConditionalExpression.class.isAssignableFrom(expr.get().getClass())) {
logger.error(
"The boolean expression must be conditional",
Expand Down Expand Up @@ -533,9 +532,7 @@ public static <T> Optional<? extends Expression<? extends T>> parseLiteral(Strin
if (literalParser.isPresent()) {
var literal = literalParser.map(l -> (T) l.apply(s));
if (literal.isPresent() && expectedClass.isAssignableFrom(c)) {
var one = (T[]) Array.newInstance(literal.get().getClass(), 1);
one[0] = literal.get();
return Optional.of(new SimpleLiteral<>(one));
return Optional.of(new SimpleLiteral<>((Class<T>) literal.get().getClass(), literal.get()));
} else if (literal.isPresent()) {
return new SimpleLiteral<>((Class<T>) c, literal.get()).convertExpression(expectedType.getType().getTypeClass());
}
Expand Down

0 comments on commit 2c83253

Please sign in to comment.