Minor fixes as reported by CodeClimate

This commit is contained in:
ljacqu 2018-09-01 09:17:42 +02:00
parent 58e04556ee
commit f79c364f84
14 changed files with 60 additions and 18 deletions

View File

@ -182,7 +182,11 @@ public class SQLite extends AbstractSqlDataSource {
ConsoleLogger.info("SQLite Setup finished"); ConsoleLogger.info("SQLite Setup finished");
} }
protected void migrateIfNeeded() throws SQLException { /**
* Migrates the database if necessary. See {@link SqLiteMigrater} for details.
*/
@VisibleForTesting
void migrateIfNeeded() throws SQLException {
DatabaseMetaData metaData = con.getMetaData(); DatabaseMetaData metaData = con.getMetaData();
if (SqLiteMigrater.isMigrationRequired(metaData, tableName, col)) { if (SqLiteMigrater.isMigrationRequired(metaData, tableName, col)) {
new SqLiteMigrater(settings, dataFolder).performMigration(this); new SqLiteMigrater(settings, dataFolder).performMigration(this);

View File

@ -35,7 +35,14 @@ class XfBcryptExtension extends MySqlExtension {
updateXenforoTablesOnSave(auth, authId.getAsInt(), con); updateXenforoTablesOnSave(auth, authId.getAsInt(), con);
} }
} }
/**
* Updates the xenforo tables after a player auth has been saved.
*
* @param auth the player auth which was saved
* @param id the account id
* @param con connection to the database
*/
private void updateXenforoTablesOnSave(PlayerAuth auth, int id, Connection con) throws SQLException { private void updateXenforoTablesOnSave(PlayerAuth auth, int id, Connection con) throws SQLException {
// Insert player password, salt in xf_user_authenticate // Insert player password, salt in xf_user_authenticate
String sql = "INSERT INTO " + xfPrefix + "user_authenticate (user_id, scheme_class, data) VALUES (?,?,?)"; String sql = "INSERT INTO " + xfPrefix + "user_authenticate (user_id, scheme_class, data) VALUES (?,?,?)";

View File

@ -79,6 +79,9 @@ public class ProtocolLibService implements SettingsDependent {
this.isEnabled = true; this.isEnabled = true;
} }
/**
* Stops all features based on ProtocolLib.
*/
public void disable() { public void disable() {
isEnabled = false; isEnabled = false;

View File

@ -10,6 +10,7 @@ import static fr.xephi.authme.security.HashUtils.md5;
@Recommendation(Usage.ACCEPTABLE) @Recommendation(Usage.ACCEPTABLE)
@HasSalt(value = SaltType.TEXT, length = 8) @HasSalt(value = SaltType.TEXT, length = 8)
@SuppressWarnings({"checkstyle:AbbreviationAsWordInName"})
public class MyBB extends SeparateSaltMethod { public class MyBB extends SeparateSaltMethod {
@Override @Override

View File

@ -97,6 +97,11 @@ public class BackupService {
return false; return false;
} }
/**
* Performs a backup for the MySQL data source.
*
* @return true if successful, false otherwise
*/
private boolean performMySqlBackup() { private boolean performMySqlBackup() {
FileUtils.createDirectory(backupFolder); FileUtils.createDirectory(backupFolder);
File sqlBackupFile = constructBackupFile("sql"); File sqlBackupFile = constructBackupFile("sql");

View File

@ -226,8 +226,8 @@ public class GeoIpService {
HashCode actualHash = function.hashBytes(Files.readAllBytes(file)); HashCode actualHash = function.hashBytes(Files.readAllBytes(file));
HashCode expectedHash = HashCode.fromString(expectedChecksum); HashCode expectedHash = HashCode.fromString(expectedChecksum);
if (!Objects.equals(actualHash, expectedHash)) { if (!Objects.equals(actualHash, expectedHash)) {
throw new IOException("GEO IP Checksum verification failed. " + throw new IOException("GEO IP Checksum verification failed. "
"Expected: " + expectedChecksum + "Actual:" + actualHash); + "Expected: " + expectedChecksum + "Actual:" + actualHash);
} }
} }
@ -267,10 +267,10 @@ public class GeoIpService {
* *
* @param ip textual IP address to lookup. * @param ip textual IP address to lookup.
* @return two-character ISO 3166-1 alpha code for the country, "LOCALHOST" for local addresses * @return two-character ISO 3166-1 alpha code for the country, "LOCALHOST" for local addresses
* or "--" if it cannot be fetched. * or "--" if it cannot be fetched.
*/ */
public String getCountryCode(String ip) { public String getCountryCode(String ip) {
if(InternetProtocolUtils.isLocalAddress(ip)) { if (InternetProtocolUtils.isLocalAddress(ip)) {
return "LOCALHOST"; return "LOCALHOST";
} }
return getCountry(ip).map(Country::getIsoCode).orElse("--"); return getCountry(ip).map(Country::getIsoCode).orElse("--");
@ -283,7 +283,7 @@ public class GeoIpService {
* @return The name of the country, "LocalHost" for local addresses, or "N/A" if it cannot be fetched. * @return The name of the country, "LocalHost" for local addresses, or "N/A" if it cannot be fetched.
*/ */
public String getCountryName(String ip) { public String getCountryName(String ip) {
if(InternetProtocolUtils.isLocalAddress(ip)) { if (InternetProtocolUtils.isLocalAddress(ip)) {
return "LocalHost"; return "LocalHost";
} }
return getCountry(ip).map(Country::getName).orElse("N/A"); return getCountry(ip).map(Country::getName).orElse("N/A");

View File

@ -69,7 +69,7 @@ public class PluginHookService {
*/ */
public File getCmiDataFolder() { public File getCmiDataFolder() {
Plugin plugin = pluginManager.getPlugin("CMI"); Plugin plugin = pluginManager.getPlugin("CMI");
if(plugin == null) { if (plugin == null) {
return null; return null;
} }
return plugin.getDataFolder(); return plugin.getDataFolder();

View File

@ -45,6 +45,13 @@ public final class IsEqualByReflectionMatcher<T> extends TypeSafeMatcher<T> {
description.appendText("parameters " + expected); description.appendText("parameters " + expected);
} }
/**
* Checks that all fields of the given {@code item} are equal to the {@link #expected} object. Both objects must
* be exactly of the same type.
*
* @param item the object to check
* @return true if all fields are equal, false otherwise
*/
private boolean assertAreFieldsEqual(T item) { private boolean assertAreFieldsEqual(T item) {
if (expected.getClass() != item.getClass()) { if (expected.getClass() != item.getClass()) {
fail("Classes don't match, got " + expected.getClass().getSimpleName() fail("Classes don't match, got " + expected.getClass().getSimpleName()

View File

@ -25,13 +25,14 @@ public final class ReflectionTestUtils {
* @param instance the instance to modify (pass null for static fields) * @param instance the instance to modify (pass null for static fields)
* @param fieldName the field name * @param fieldName the field name
* @param value the value to set the field to * @param value the value to set the field to
* @param <T> the instance type
*/ */
public static <T> void setField(Class<? super T> clazz, T instance, String fieldName, Object value) { public static <T> void setField(Class<? super T> clazz, T instance, String fieldName, Object value) {
try { try {
Field field = getField(clazz, fieldName); Field field = getField(clazz, fieldName);
field.set(instance, value); field.set(instance, value);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
throw new UnsupportedOperationException( throw new IllegalStateException(
format("Could not set value to field '%s' for instance '%s' of class '%s'", format("Could not set value to field '%s' for instance '%s' of class '%s'",
fieldName, instance, clazz.getName()), e); fieldName, instance, clazz.getName()), e);
} }
@ -55,7 +56,7 @@ public final class ReflectionTestUtils {
field.setAccessible(true); field.setAccessible(true);
return field; return field;
} catch (NoSuchFieldException e) { } catch (NoSuchFieldException e) {
throw new UnsupportedOperationException(format("Could not get field '%s' from class '%s'", throw new IllegalStateException(format("Could not get field '%s' from class '%s'",
fieldName, clazz.getName()), e); fieldName, clazz.getName()), e);
} }
} }
@ -66,13 +67,21 @@ public final class ReflectionTestUtils {
return getFieldValue(field, instance); return getFieldValue(field, instance);
} }
/**
* Returns the value of the field on the given instance. Wraps exceptions into a runtime exception.
*
* @param field the field to read
* @param instance the instance to get the value from, null if field is static
* @param <V> type of the field
* @return value of the field
*/
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <V> V getFieldValue(Field field, Object instance) { public static <V> V getFieldValue(Field field, Object instance) {
field.setAccessible(true); field.setAccessible(true);
try { try {
return (V) field.get(instance); return (V) field.get(instance);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
throw new UnsupportedOperationException("Could not get value of field '" + field.getName() + "'", e); throw new IllegalStateException("Could not get value of field '" + field.getName() + "'", e);
} }
} }
@ -90,7 +99,7 @@ public final class ReflectionTestUtils {
method.setAccessible(true); method.setAccessible(true);
return method; return method;
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException e) {
throw new UnsupportedOperationException("Could not retrieve method '" + methodName + "' from class '" throw new IllegalStateException("Could not retrieve method '" + methodName + "' from class '"
+ clazz.getName() + "'"); + clazz.getName() + "'");
} }
} }
@ -110,7 +119,7 @@ public final class ReflectionTestUtils {
try { try {
return (V) method.invoke(instance, parameters); return (V) method.invoke(instance, parameters);
} catch (InvocationTargetException | IllegalAccessException e) { } catch (InvocationTargetException | IllegalAccessException e) {
throw new UnsupportedOperationException("Could not invoke method '" + method + "'", e); throw new IllegalStateException("Could not invoke method '" + method + "'", e);
} }
} }
@ -138,7 +147,7 @@ public final class ReflectionTestUtils {
constructor.setAccessible(true); constructor.setAccessible(true);
return constructor.newInstance(); return constructor.newInstance();
} catch (ReflectiveOperationException e) { } catch (ReflectiveOperationException e) {
throw new UnsupportedOperationException("Could not invoke no-args constructor of class " + clazz, e); throw new IllegalStateException("Could not invoke no-args constructor of class " + clazz, e);
} }
} }
} }

View File

@ -84,7 +84,8 @@ public final class TestCommandsUtil {
/* Shortcut command to initialize a new test command. */ /* Shortcut command to initialize a new test command. */
private static CommandDescription createCommand(PermissionNode permission, CommandDescription parent, private static CommandDescription createCommand(PermissionNode permission, CommandDescription parent,
List<String> labels, Class<? extends ExecutableCommand> commandClass, List<String> labels,
Class<? extends ExecutableCommand> commandClass,
CommandArgumentDescription... arguments) { CommandArgumentDescription... arguments) {
CommandDescription.CommandBuilder command = CommandDescription.builder() CommandDescription.CommandBuilder command = CommandDescription.builder()
.labels(labels) .labels(labels)

View File

@ -20,7 +20,8 @@ import static com.google.common.base.MoreObjects.firstNonNull;
public class TranslationPageGenerator implements AutoToolTask { public class TranslationPageGenerator implements AutoToolTask {
private static final String DOCS_PAGE = ToolsConstants.DOCS_FOLDER + "translations.md"; private static final String DOCS_PAGE = ToolsConstants.DOCS_FOLDER + "translations.md";
private static final String TEMPLATE_FILE = ToolsConstants.TOOLS_SOURCE_ROOT + "docs/translations/translations.tpl.md"; private static final String TEMPLATE_FILE =
ToolsConstants.TOOLS_SOURCE_ROOT + "docs/translations/translations.tpl.md";
private static final Map<String, String> LANGUAGE_NAMES = buildLanguageNames(); private static final Map<String, String> LANGUAGE_NAMES = buildLanguageNames();
// Color configuration for the bars shown next to translation percentage // Color configuration for the bars shown next to translation percentage

View File

@ -70,6 +70,10 @@ public class MessageFileVerifier {
return !missingKeys.isEmpty() || !missingTags.isEmpty() || !unknownKeys.isEmpty(); return !missingKeys.isEmpty() || !missingTags.isEmpty() || !unknownKeys.isEmpty();
} }
/**
* Performs the actual verification for the given file and collects all found issues into this
* instance's fields.
*/
private void verifyKeys() { private void verifyKeys() {
FileConfiguration configuration = YamlConfiguration.loadConfiguration(messagesFile); FileConfiguration configuration = YamlConfiguration.loadConfiguration(messagesFile);

View File

@ -18,7 +18,7 @@ import java.util.regex.Pattern;
* version (in case the page is viewed on a fork)</li> * version (in case the page is viewed on a fork)</li>
* </ul> * </ul>
*/ */
public class TagReplacer { public final class TagReplacer {
private TagReplacer() { private TagReplacer() {
} }

View File

@ -6,7 +6,7 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class TagValueHolder { public final class TagValueHolder {
private Map<String, TagValue<?>> values; private Map<String, TagValue<?>> values;