Add condition argument to Rule annotation (#998)
This commit is contained in:
1 parent
7421e56670
commit
9e86407323
3 files changed
+28
-1
No files matched your search
@@ -0,0 +1,5 @@
|
|||||||
|
package carpet.settings;
|
||||||
|
|
||||||
|
public interface Condition {
|
||||||
|
boolean isTrue();
|
||||||
|
}
|
||||||
@@ -64,4 +64,9 @@ public @interface Rule
|
|||||||
* The class of the validator checked when the rule is changed.
|
* The class of the validator checked when the rule is changed.
|
||||||
*/
|
*/
|
||||||
Class<? extends Validator>[] validate() default {};
|
Class<? extends Validator>[] validate() default {};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The class of the condition checked when the rule is parsed.
|
||||||
|
*/
|
||||||
|
Class<? extends Condition>[] condition() default {};
|
||||||
}
|
}
|
||||||
@@ -30,6 +30,7 @@ import java.io.BufferedReader;
|
|||||||
import java.io.BufferedWriter;
|
import java.io.BufferedWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.NoSuchFileException;
|
import java.nio.file.NoSuchFileException;
|
||||||
@@ -143,7 +144,23 @@ public class SettingsManager
|
|||||||
Rule rule = f.getAnnotation(Rule.class);
|
Rule rule = f.getAnnotation(Rule.class);
|
||||||
if (rule == null) continue;
|
if (rule == null) continue;
|
||||||
ParsedRule parsed = new ParsedRule(f, rule, this);
|
ParsedRule parsed = new ParsedRule(f, rule, this);
|
||||||
rules.put(parsed.name, parsed);
|
|
||||||
|
List<Boolean> conditions = new ArrayList<>();
|
||||||
|
for (Class<? extends Condition> condition : rule.condition()) {
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Constructor<?> constr = condition.getDeclaredConstructor();
|
||||||
|
constr.setAccessible(true);
|
||||||
|
conditions.add(((Condition) constr.newInstance()).isTrue());
|
||||||
|
}
|
||||||
|
catch (ReflectiveOperationException e)
|
||||||
|
{
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rule.condition().length == 0 || conditions.stream().allMatch(Boolean::valueOf))
|
||||||
|
rules.put(parsed.name, parsed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user