Minor: simplify CheckMessageKeyUsages task and make check more strict

This commit is contained in:
ljacqu 2018-01-22 19:52:39 +01:00
parent 610a699c95
commit b3a191d7e2
2 changed files with 11 additions and 40 deletions

View File

@ -123,29 +123,13 @@ public class ClassesConsistencyTest {
}
/**
* Prints out the field with (most of) its modifiers.
* Prints out the field with its modifiers.
*
* @param field the field to format
* @return description of the field
*/
private static String formatField(Field field) {
String modifiersText = "";
int modifiers = field.getModifiers();
if (Modifier.isPublic(modifiers)) {
modifiersText += "public ";
} else if (Modifier.isProtected(modifiers)) {
modifiersText += "protected ";
} else if (Modifier.isPrivate(modifiers)) {
modifiersText += "private ";
}
if (Modifier.isStatic(modifiers)) {
modifiersText += "static ";
}
if (Modifier.isFinal(modifiers)) {
modifiersText += "final ";
}
String modifiersText = Modifier.toString(field.getModifiers());
return String.format("[%s] %s %s %s", field.getDeclaringClass().getSimpleName(), modifiersText.trim(),
field.getType().getSimpleName(), field.getName());
}

View File

@ -2,24 +2,22 @@ package tools.messages;
import com.google.common.collect.Lists;
import fr.xephi.authme.message.MessageKey;
import tools.utils.AutoToolTask;
import tools.utils.FileIoUtils;
import tools.utils.ToolTask;
import tools.utils.ToolsConstants;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import static java.util.Arrays.asList;
/**
* Task which checks for {@link MessageKey} usages.
*/
public class CheckMessageKeyUsages implements ToolTask {
public class CheckMessageKeyUsages implements AutoToolTask {
private static final Predicate<File> SHOULD_CHECK_FILE =
file -> file.getName().endsWith(".java") && !file.getName().endsWith("MessageKey.java");
@ -30,12 +28,7 @@ public class CheckMessageKeyUsages implements ToolTask {
}
@Override
public void execute(Scanner scanner) {
System.out.println("Enter a message key to find the files where it is used");
System.out.println("Enter empty line to search for all unused message keys");
String key = scanner.nextLine();
if (key.trim().isEmpty()) {
public void executeDefault() {
List<MessageKey> unusedKeys = findUnusedKeys();
if (unusedKeys.isEmpty()) {
System.out.println("No unused MessageKey entries found :)");
@ -43,12 +36,6 @@ public class CheckMessageKeyUsages implements ToolTask {
System.out.println("Did not find usages for keys:\n- " +
String.join("\n- ", Lists.transform(unusedKeys, MessageKey::name)));
}
} else {
MessageKey messageKey = MessageKey.valueOf(key);
List<File> filesUsingKey = findUsagesOfKey(messageKey);
System.out.println("The following files use '" + key + "':\n- "
+ filesUsingKey.stream().map(File::getName).collect(Collectors.joining("\n- ")));
}
}
private List<MessageKey> findUnusedKeys() {
@ -57,7 +44,7 @@ public class CheckMessageKeyUsages implements ToolTask {
Consumer<File> fileProcessor = file -> {
String source = FileIoUtils.readFromFile(file.toPath());
keys.removeIf(key -> source.contains(key.name()));
keys.removeIf(key -> source.contains("MessageKey." + key.name()));
};
walkJavaFileTree(sourceFolder, fileProcessor);