Update Injector and create injectable object factory
- Using e.g. Factory<Converter> instead of the injector directly makes its purpose more specific and disallows any future abuse of the injector's functions
This commit is contained in:
@@ -14,6 +14,7 @@ import fr.xephi.authme.initialization.OnShutdownPlayerSaver;
|
||||
import fr.xephi.authme.initialization.OnStartupTasks;
|
||||
import fr.xephi.authme.initialization.SettingsProvider;
|
||||
import fr.xephi.authme.initialization.TaskCloser;
|
||||
import fr.xephi.authme.initialization.factory.FactoryDependencyHandler;
|
||||
import fr.xephi.authme.listener.BlockListener;
|
||||
import fr.xephi.authme.listener.EntityListener;
|
||||
import fr.xephi.authme.listener.PlayerListener;
|
||||
@@ -196,7 +197,10 @@ public class AuthMe extends JavaPlugin {
|
||||
getDataFolder().mkdir();
|
||||
|
||||
// Create injector, provide elements from the Bukkit environment and register providers
|
||||
injector = new InjectorBuilder().addDefaultHandlers("fr.xephi.authme").create();
|
||||
injector = new InjectorBuilder()
|
||||
.addHandlers(new FactoryDependencyHandler())
|
||||
.addDefaultHandlers("fr.xephi.authme")
|
||||
.create();
|
||||
injector.register(AuthMe.class, this);
|
||||
injector.register(Server.class, getServer());
|
||||
injector.register(PluginManager.class, getServer().getPluginManager());
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package fr.xephi.authme.command;
|
||||
|
||||
import ch.jalu.injector.Injector;
|
||||
import fr.xephi.authme.AuthMe;
|
||||
import fr.xephi.authme.command.help.HelpProvider;
|
||||
import fr.xephi.authme.initialization.factory.Factory;
|
||||
import fr.xephi.authme.message.MessageKey;
|
||||
import fr.xephi.authme.message.Messages;
|
||||
import fr.xephi.authme.permission.PermissionsManager;
|
||||
@@ -40,13 +40,13 @@ public class CommandHandler {
|
||||
private Map<Class<? extends ExecutableCommand>, ExecutableCommand> commands = new HashMap<>();
|
||||
|
||||
@Inject
|
||||
CommandHandler(Injector injector, CommandMapper commandMapper, PermissionsManager permissionsManager,
|
||||
Messages messages, HelpProvider helpProvider) {
|
||||
CommandHandler(Factory<ExecutableCommand> commandFactory, CommandMapper commandMapper,
|
||||
PermissionsManager permissionsManager, Messages messages, HelpProvider helpProvider) {
|
||||
this.commandMapper = commandMapper;
|
||||
this.permissionsManager = permissionsManager;
|
||||
this.messages = messages;
|
||||
this.helpProvider = helpProvider;
|
||||
initializeCommands(injector, commandMapper.getCommandClasses());
|
||||
initializeCommands(commandFactory, commandMapper.getCommandClasses());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -94,13 +94,13 @@ public class CommandHandler {
|
||||
/**
|
||||
* Initialize all required ExecutableCommand objects.
|
||||
*
|
||||
* @param injector the injector
|
||||
* @param commandFactory factory to create command objects
|
||||
* @param commandClasses the classes to instantiate
|
||||
*/
|
||||
private void initializeCommands(Injector injector,
|
||||
private void initializeCommands(Factory<ExecutableCommand> commandFactory,
|
||||
Set<Class<? extends ExecutableCommand>> commandClasses) {
|
||||
for (Class<? extends ExecutableCommand> clazz : commandClasses) {
|
||||
commands.put(clazz, injector.newInstance(clazz));
|
||||
commands.put(clazz, commandFactory.newInstance(clazz));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package fr.xephi.authme.command.executable.authme;
|
||||
|
||||
import ch.jalu.injector.Injector;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import fr.xephi.authme.ConsoleLogger;
|
||||
@@ -13,6 +12,7 @@ import fr.xephi.authme.datasource.converter.RoyalAuthConverter;
|
||||
import fr.xephi.authme.datasource.converter.SqliteToSql;
|
||||
import fr.xephi.authme.datasource.converter.vAuthConverter;
|
||||
import fr.xephi.authme.datasource.converter.xAuthConverter;
|
||||
import fr.xephi.authme.initialization.factory.Factory;
|
||||
import fr.xephi.authme.message.MessageKey;
|
||||
import fr.xephi.authme.service.BukkitService;
|
||||
import fr.xephi.authme.service.CommonService;
|
||||
@@ -37,7 +37,7 @@ public class ConverterCommand implements ExecutableCommand {
|
||||
private BukkitService bukkitService;
|
||||
|
||||
@Inject
|
||||
private Injector injector;
|
||||
private Factory<Converter> converterFactory;
|
||||
|
||||
@Override
|
||||
public void executeCommand(final CommandSender sender, List<String> arguments) {
|
||||
@@ -52,7 +52,7 @@ public class ConverterCommand implements ExecutableCommand {
|
||||
}
|
||||
|
||||
// Get the proper converter instance
|
||||
final Converter converter = injector.newInstance(converterClass);
|
||||
final Converter converter = converterFactory.newInstance(converterClass);
|
||||
|
||||
// Run the convert job
|
||||
bukkitService.runTaskAsynchronously(new Runnable() {
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package fr.xephi.authme.initialization.factory;
|
||||
|
||||
/**
|
||||
* Injectable factory that creates new instances of a certain type.
|
||||
*
|
||||
* @param <P> the parent type to which the factory is limited to
|
||||
*/
|
||||
public interface Factory<P> {
|
||||
|
||||
/**
|
||||
* Creates an instance of the given class.
|
||||
*
|
||||
* @param clazz the class to instantiate
|
||||
* @param <C> the class type
|
||||
* @return new instance of the class
|
||||
*/
|
||||
<C extends P> C newInstance(Class<C> clazz);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package fr.xephi.authme.initialization.factory;
|
||||
|
||||
import ch.jalu.injector.Injector;
|
||||
import ch.jalu.injector.context.ResolvedInstantiationContext;
|
||||
import ch.jalu.injector.handlers.dependency.DependencyHandler;
|
||||
import ch.jalu.injector.handlers.instantiation.DependencyDescription;
|
||||
import ch.jalu.injector.utils.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Dependency handler that builds {@link Factory} objects.
|
||||
*/
|
||||
public class FactoryDependencyHandler implements DependencyHandler {
|
||||
|
||||
@Override
|
||||
public Object resolveValue(ResolvedInstantiationContext<?> context, DependencyDescription dependencyDescription) {
|
||||
if (dependencyDescription.getType() == Factory.class) {
|
||||
Class<?> genericType = ReflectionUtils.getGenericType(dependencyDescription.getGenericType());
|
||||
if (genericType == null) {
|
||||
throw new IllegalStateException("Factory fields must have concrete generic type. " +
|
||||
"Cannot get generic type for field in '" + context.getMappedClass() + "'");
|
||||
}
|
||||
|
||||
return new FactoryImpl<>(genericType, context.getInjector());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static final class FactoryImpl<P> implements Factory<P> {
|
||||
|
||||
private final Injector injector;
|
||||
private final Class<P> parentClass;
|
||||
|
||||
FactoryImpl(Class<P> parentClass, Injector injector) {
|
||||
this.parentClass = parentClass;
|
||||
this.injector = injector;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <C extends P> C newInstance(Class<C> clazz) {
|
||||
if (parentClass.isAssignableFrom(clazz)) {
|
||||
return injector.newInstance(clazz);
|
||||
}
|
||||
throw new IllegalArgumentException(clazz + " not child of " + parentClass);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
package fr.xephi.authme.security;
|
||||
|
||||
import ch.jalu.injector.Injector;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.events.PasswordEncryptionEvent;
|
||||
import fr.xephi.authme.initialization.Reloadable;
|
||||
import fr.xephi.authme.initialization.factory.Factory;
|
||||
import fr.xephi.authme.security.crypts.EncryptionMethod;
|
||||
import fr.xephi.authme.security.crypts.HashedPassword;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
@@ -29,7 +29,7 @@ public class PasswordSecurity implements Reloadable {
|
||||
private PluginManager pluginManager;
|
||||
|
||||
@Inject
|
||||
private Injector injector;
|
||||
private Factory<EncryptionMethod> hashAlgorithmFactory;
|
||||
|
||||
private HashAlgorithm algorithm;
|
||||
private Collection<HashAlgorithm> legacyAlgorithms;
|
||||
@@ -154,7 +154,7 @@ public class PasswordSecurity implements Reloadable {
|
||||
if (HashAlgorithm.CUSTOM.equals(algorithm) || HashAlgorithm.PLAINTEXT.equals(algorithm)) {
|
||||
return null;
|
||||
}
|
||||
return injector.newInstance(algorithm.getClazz());
|
||||
return hashAlgorithmFactory.newInstance(algorithm.getClazz());
|
||||
}
|
||||
|
||||
private void hashPasswordForNewAlgorithm(String password, String playerName) {
|
||||
|
||||
Reference in New Issue
Block a user