From dd9312f581e83c509f74a2c619a48358a7f7f89e Mon Sep 17 00:00:00 2001 From: ljacqu Date: Sat, 8 Oct 2016 00:38:51 +0200 Subject: [PATCH] Minor improvements in tool tasks - Use java 8 types in UpdateDocsTask - Simplify permissions page writing task to write to the file without any options - Various minor simplifications (Java 8) to avoid casting etc. --- src/test/java/tools/docs/UpdateDocsTask.java | 40 ++++++------------- .../EncryptionMethodInfoGatherer.java | 5 +-- .../permissions/PermissionNodesGatherer.java | 34 ++++------------ .../permissions/PermissionsListWriter.java | 35 +--------------- src/test/java/tools/utils/TagValue.java | 15 ++++++- 5 files changed, 36 insertions(+), 93 deletions(-) diff --git a/src/test/java/tools/docs/UpdateDocsTask.java b/src/test/java/tools/docs/UpdateDocsTask.java index 48624afe0..df051bd61 100644 --- a/src/test/java/tools/docs/UpdateDocsTask.java +++ b/src/test/java/tools/docs/UpdateDocsTask.java @@ -9,14 +9,15 @@ import tools.utils.ToolTask; import java.util.Scanner; import java.util.Set; +import java.util.function.Consumer; /** * Task that runs all tasks which update files in the docs folder. */ public class UpdateDocsTask implements AutoToolTask { - private static final Set> TASKS = ImmutableSet.>of( - CommandPageCreater.class, HashAlgorithmsDescriptionTask.class, PermissionsListWriter.class); + private static final Set> TASKS = ImmutableSet + .of(CommandPageCreater.class, HashAlgorithmsDescriptionTask.class, PermissionsListWriter.class); @Override public String getTaskName() { @@ -25,22 +26,14 @@ public class UpdateDocsTask implements AutoToolTask { @Override public void execute(final Scanner scanner) { - executeTasks(new TaskRunner() { - @Override - public void execute(ToolTask task) { - task.execute(scanner); - } - }); + executeTasks(task -> task.execute(scanner)); } @Override public void executeDefault() { - executeTasks(new TaskRunner() { - @Override - public void execute(ToolTask task) { - if (task instanceof AutoToolTask) { - ((AutoToolTask) task).executeDefault(); - } + executeTasks(task -> { + if (task instanceof AutoToolTask) { + ((AutoToolTask) task).executeDefault(); } }); } @@ -49,24 +42,15 @@ public class UpdateDocsTask implements AutoToolTask { try { return clazz.newInstance(); } catch (IllegalAccessException | InstantiationException e) { - throw new UnsupportedOperationException(e); + throw new UnsupportedOperationException("Could not instantiate task class '" + clazz + "'", e); } } - private static void executeTasks(TaskRunner runner) { + private static void executeTasks(Consumer taskRunner) { for (Class taskClass : TASKS) { - try { - ToolTask task = instantiateTask(taskClass); - System.out.println("\nRunning " + task.getTaskName() + "\n-------------------"); - runner.execute(task); - } catch (UnsupportedOperationException e) { - System.err.println("Error running task of class '" + taskClass + "'"); - e.printStackTrace(); - } + ToolTask task = instantiateTask(taskClass); + System.out.println("\nRunning " + task.getTaskName() + "\n-------------------"); + taskRunner.accept(task); } } - - private interface TaskRunner { - void execute(ToolTask task); - } } diff --git a/src/test/java/tools/hashmethods/EncryptionMethodInfoGatherer.java b/src/test/java/tools/hashmethods/EncryptionMethodInfoGatherer.java index 057f14bbb..bcc48c7cf 100644 --- a/src/test/java/tools/hashmethods/EncryptionMethodInfoGatherer.java +++ b/src/test/java/tools/hashmethods/EncryptionMethodInfoGatherer.java @@ -3,6 +3,7 @@ package tools.hashmethods; import ch.jalu.injector.Injector; import ch.jalu.injector.InjectorBuilder; import com.github.authme.configme.properties.Property; +import com.google.common.collect.ImmutableSet; import fr.xephi.authme.security.HashAlgorithm; import fr.xephi.authme.security.crypts.EncryptionMethod; import fr.xephi.authme.security.crypts.HexSaltedMethod; @@ -19,7 +20,6 @@ import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; -import static com.google.common.collect.Sets.newHashSet; import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -30,9 +30,8 @@ import static org.mockito.Mockito.when; */ public class EncryptionMethodInfoGatherer { - @SuppressWarnings("unchecked") private final static Set> RELEVANT_ANNOTATIONS = - newHashSet(HasSalt.class, Recommendation.class, AsciiRestricted.class); + ImmutableSet.of(HasSalt.class, Recommendation.class, AsciiRestricted.class); private static Injector injector = createInitializer(); diff --git a/src/test/java/tools/permissions/PermissionNodesGatherer.java b/src/test/java/tools/permissions/PermissionNodesGatherer.java index 58b5df4e3..d5f7e6e9d 100644 --- a/src/test/java/tools/permissions/PermissionNodesGatherer.java +++ b/src/test/java/tools/permissions/PermissionNodesGatherer.java @@ -1,18 +1,14 @@ package tools.permissions; -import fr.xephi.authme.permission.AdminPermission; +import fr.xephi.authme.ClassCollector; import fr.xephi.authme.permission.PermissionNode; -import fr.xephi.authme.permission.PlayerPermission; -import fr.xephi.authme.permission.PlayerStatePermission; import tools.utils.FileUtils; import tools.utils.ToolsConstants; import java.util.EnumSet; import java.util.HashMap; import java.util.Map; -import java.util.Set; import java.util.TreeMap; -import java.util.TreeSet; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -31,36 +27,22 @@ public class PermissionNodesGatherer { + "(.*?)\\s+\\*/" // Capture everything until we encounter '*/' + "\\s+([A-Z_]+)\\("); // Match the enum name (e.g. 'LOGIN'), until before the first '(' - /** - * Return a sorted collection of all permission nodes. - * - * @return AuthMe permission nodes sorted alphabetically - */ - public Set gatherNodes() { - Set nodes = new TreeSet<>(); - for (PermissionNode perm : PlayerPermission.values()) { - nodes.add(perm.getNode()); - } - for (PermissionNode perm : AdminPermission.values()) { - nodes.add(perm.getNode()); - } - return nodes; - } - /** * Return a sorted collection of all permission nodes, including its JavaDoc description. * * @return Ordered map whose keys are the permission nodes and the values the associated JavaDoc */ - public Map gatherNodesWithJavaDoc() { + public & PermissionNode> Map gatherNodesWithJavaDoc() { Map result = new TreeMap<>(); result.put("authme.admin.*", "Give access to all admin commands."); result.put("authme.player.*", "Permission to use all player (non-admin) commands."); - // TODO ljacqu 20160109: Add authme.player.email manual description? + result.put("authme.player.email", "Grants all email permissions."); - addDescriptionsForClass(PlayerPermission.class, result); - addDescriptionsForClass(AdminPermission.class, result); - addDescriptionsForClass(PlayerStatePermission.class, result); + new ClassCollector(ToolsConstants.MAIN_SOURCE_ROOT, "") + .collectClasses(PermissionNode.class) + .stream() + .filter(Class::isEnum) + .forEach(clz -> addDescriptionsForClass((Class) clz, result)); return result; } diff --git a/src/test/java/tools/permissions/PermissionsListWriter.java b/src/test/java/tools/permissions/PermissionsListWriter.java index e0c43b227..43d3b4530 100644 --- a/src/test/java/tools/permissions/PermissionsListWriter.java +++ b/src/test/java/tools/permissions/PermissionsListWriter.java @@ -8,7 +8,6 @@ import tools.utils.ToolsConstants; import java.util.Map; import java.util.Scanner; -import java.util.Set; /** * Task responsible for formatting a permissions node list and @@ -25,23 +24,7 @@ public class PermissionsListWriter implements AutoToolTask { @Override public void execute(Scanner scanner) { - // Ask if result should be written to file - System.out.println("Include description? [Enter 'n' for no]"); - boolean includeDescription = !matches("n", scanner); - - boolean writeToFile = false; - if (includeDescription) { - System.out.println("Write to file? [Enter 'n' for no]"); - writeToFile = !matches("n", scanner); - } - - if (!includeDescription) { - outputSimpleList(); - } else if (writeToFile) { - generateAndWriteFile(); - } else { - System.out.println(generatePermissionsList()); - } + generateAndWriteFile(); } @Override @@ -70,20 +53,4 @@ public class PermissionsListWriter implements AutoToolTask { } return permissionTags; } - - private static void outputSimpleList() { - PermissionNodesGatherer gatherer = new PermissionNodesGatherer(); - Set nodes = gatherer.gatherNodes(); - for (String node : nodes) { - System.out.println(node); - } - System.out.println(); - System.out.println("Total: " + nodes.size()); - } - - private static boolean matches(String answer, Scanner sc) { - String userInput = sc.nextLine(); - return answer.equalsIgnoreCase(userInput); - } - } diff --git a/src/test/java/tools/utils/TagValue.java b/src/test/java/tools/utils/TagValue.java index ad73a816d..3941ee5c2 100644 --- a/src/test/java/tools/utils/TagValue.java +++ b/src/test/java/tools/utils/TagValue.java @@ -3,7 +3,12 @@ package tools.utils; import java.util.ArrayList; import java.util.List; - +/** + * Value of a tag. + * + * @param the tag value type + * @see TagReplacer + */ public abstract class TagValue { private final T value; @@ -18,6 +23,9 @@ public abstract class TagValue { public abstract boolean isEmpty(); + /** + * Text value. + */ public static final class TextTagValue extends TagValue { public TextTagValue(String value) { super(value); @@ -29,9 +37,12 @@ public abstract class TagValue { } } + /** + * List value (iterable). + */ public static final class NestedTagValue extends TagValue> { public NestedTagValue() { - super(new ArrayList()); + super(new ArrayList<>()); } @Override