[FEATURE] Получение модуля по интерфейсу, который он реализовывает

This commit is contained in:
Gravit 2019-08-29 18:33:27 +07:00
parent 08a371f327
commit 04f147e368
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
8 changed files with 65 additions and 2 deletions

View file

@ -7,12 +7,14 @@ public class LauncherModuleInfo {
public final Version version; public final Version version;
public final int priority; public final int priority;
public final String[] dependencies; public final String[] dependencies;
public final String[] providers;
public LauncherModuleInfo(String name, Version version) { public LauncherModuleInfo(String name, Version version) {
this.name = name; this.name = name;
this.version = version; this.version = version;
this.priority = 0; this.priority = 0;
this.dependencies = new String[]{}; this.dependencies = new String[]{};
providers = new String[0];
} }
public LauncherModuleInfo(String name) { public LauncherModuleInfo(String name) {
@ -20,6 +22,7 @@ public LauncherModuleInfo(String name) {
this.version = new Version(1,0,0); this.version = new Version(1,0,0);
this.priority = 0; this.priority = 0;
this.dependencies = new String[]{}; this.dependencies = new String[]{};
providers = new String[0];
} }
public LauncherModuleInfo(String name, Version version, String[] dependencies) { public LauncherModuleInfo(String name, Version version, String[] dependencies) {
@ -27,6 +30,7 @@ public LauncherModuleInfo(String name, Version version, String[] dependencies) {
this.version = version; this.version = version;
this.priority = 0; this.priority = 0;
this.dependencies = dependencies; this.dependencies = dependencies;
providers = new String[0];
} }
public LauncherModuleInfo(String name, Version version, int priority, String[] dependencies) { public LauncherModuleInfo(String name, Version version, int priority, String[] dependencies) {
@ -34,5 +38,14 @@ public LauncherModuleInfo(String name, Version version, int priority, String[] d
this.version = version; this.version = version;
this.priority = priority; this.priority = priority;
this.dependencies = dependencies; this.dependencies = dependencies;
providers = new String[0];
}
public LauncherModuleInfo(String name, Version version, int priority, String[] dependencies, String[] providers) {
this.name = name;
this.version = version;
this.priority = priority;
this.dependencies = dependencies;
this.providers = providers;
} }
} }

View file

@ -4,6 +4,7 @@
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.List;
import java.util.function.Predicate; import java.util.function.Predicate;
public interface LauncherModulesManager { public interface LauncherModulesManager {
@ -23,6 +24,8 @@ default <T extends LauncherModule> boolean containsModule(Class<? extends T> cl
} }
ClassLoader getModuleClassLoader(); ClassLoader getModuleClassLoader();
<T extends LauncherModule> T getModule(Class<? extends T> clazz); <T extends LauncherModule> T getModule(Class<? extends T> clazz);
<T> T getModuleByInterface(Class<T> clazz);
<T> List<T> getModulesByInterface(Class<T> clazz);
<T extends LauncherModule> T findModule(Class<? extends T> clazz, Predicate<Version> versionPredicate); <T extends LauncherModule> T findModule(Class<? extends T> clazz, Predicate<Version> versionPredicate);
/** /**

View file

@ -153,7 +153,7 @@ public LauncherModule getModule(String name) {
for(LauncherModule module : modules) for(LauncherModule module : modules)
{ {
LauncherModuleInfo info = module.getModuleInfo(); LauncherModuleInfo info = module.getModuleInfo();
if(info.name.equals(name)) return module; if(info.name.equals(name) || ( info.providers.length > 0 && Arrays.asList(info.providers).contains(name))) return module;
} }
return null; return null;
} }
@ -178,6 +178,27 @@ public <T extends LauncherModule> T getModule(Class<? extends T> clazz) {
return null; return null;
} }
@Override
@SuppressWarnings("unchecked")
public <T> T getModuleByInterface(Class<T> clazz) {
for(LauncherModule module : modules)
{
if(clazz.isAssignableFrom(module.getClass())) return (T) module;
}
return null;
}
@Override
@SuppressWarnings("unchecked")
public <T> List<T> getModulesByInterface(Class<T> clazz) {
List<T> list = new ArrayList<>();
for(LauncherModule module : modules)
{
if(clazz.isAssignableFrom(module.getClass())) list.add((T) module);
}
return list;
}
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T extends LauncherModule> T findModule(Class<? extends T> clazz, Predicate<Version> versionPredicate) { public <T extends LauncherModule> T findModule(Class<? extends T> clazz, Predicate<Version> versionPredicate) {

View file

@ -53,6 +53,7 @@ public void dependenciesTest()
Assertions.assertEquals(moduleManager.getModule("depend2").getInitStatus(), LauncherModule.InitStatus.FINISH); Assertions.assertEquals(moduleManager.getModule("depend2").getInitStatus(), LauncherModule.InitStatus.FINISH);
Assertions.assertEquals(moduleManager.getModule("depend3").getInitStatus(), LauncherModule.InitStatus.FINISH); Assertions.assertEquals(moduleManager.getModule("depend3").getInitStatus(), LauncherModule.InitStatus.FINISH);
Assertions.assertEquals(moduleManager.getModule("internal").getInitStatus(), LauncherModule.InitStatus.FINISH); Assertions.assertEquals(moduleManager.getModule("internal").getInitStatus(), LauncherModule.InitStatus.FINISH);
Assertions.assertEquals(moduleManager.getModule("virtual").getInitStatus(), LauncherModule.InitStatus.FINISH);
Assertions.assertEquals(moduleManager.getModule("main").getInitStatus(), LauncherModule.InitStatus.FINISH); Assertions.assertEquals(moduleManager.getModule("main").getInitStatus(), LauncherModule.InitStatus.FINISH);
} }
@Test @Test

View file

@ -10,7 +10,7 @@ public class Depend1Module extends LauncherModule {
public Depend1Module() { public Depend1Module() {
super(new LauncherModuleInfo("depend1", new Version(1,0,0), super(new LauncherModuleInfo("depend1", new Version(1,0,0),
0, 0,
new String[]{"depend3", "internal"})); new String[]{"depend3", "internal", "virtual"}));
} }
@Override @Override
@ -19,5 +19,7 @@ public void init(LauncherInitContext initContext) {
Assertions.assertEquals(module.getInitStatus(), InitStatus.FINISH); Assertions.assertEquals(module.getInitStatus(), InitStatus.FINISH);
Depend3Module module1 = modulesManager.getModule(Depend3Module.class); Depend3Module module1 = modulesManager.getModule(Depend3Module.class);
Assertions.assertEquals(module1.getInitStatus(), InitStatus.FINISH); Assertions.assertEquals(module1.getInitStatus(), InitStatus.FINISH);
VirtualInterface virtualInterface = modulesManager.getModuleByInterface(VirtualInterface.class);
Assertions.assertEquals(((LauncherModule) virtualInterface).getInitStatus(), InitStatus.FINISH);
} }
} }

View file

@ -12,6 +12,7 @@ public Depend2Module() {
@Override @Override
public void preInitAction() { public void preInitAction() {
modulesManager.loadModule(new InternalModule()); modulesManager.loadModule(new InternalModule());
modulesManager.loadModule(new ProvidedModule());
} }
@Override @Override

View file

@ -0,0 +1,18 @@
package pro.gravit.launcher.impl;
import pro.gravit.launcher.modules.LauncherInitContext;
import pro.gravit.launcher.modules.LauncherModule;
import pro.gravit.launcher.modules.LauncherModuleInfo;
import pro.gravit.utils.Version;
public class ProvidedModule extends LauncherModule implements VirtualInterface {
public ProvidedModule() {
super(new LauncherModuleInfo("provided", new Version(1,0,0),
0, new String[]{}, new String[]{"virtual"}));
}
@Override
public void init(LauncherInitContext initContext) {
}
}

View file

@ -0,0 +1,4 @@
package pro.gravit.launcher.impl;
public interface VirtualInterface {
}