remove unused dependency injection code
This commit is contained in:
parent
ab19b53d63
commit
0aa957d7fa
|
@ -1,25 +0,0 @@
|
||||||
/*
|
|
||||||
* opsu!dance - fork of opsu! with cursordance auto
|
|
||||||
* Copyright (C) 2017 yugecin
|
|
||||||
*
|
|
||||||
* opsu!dance is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* opsu!dance is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with opsu!dance. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
package yugecin.opsudance.core.inject;
|
|
||||||
|
|
||||||
public interface Binder<T> {
|
|
||||||
|
|
||||||
void asEagerSingleton();
|
|
||||||
void asLazySingleton();
|
|
||||||
void to(Class<? extends T> type);
|
|
||||||
}
|
|
|
@ -1,24 +0,0 @@
|
||||||
/*
|
|
||||||
* opsu!dance - fork of opsu! with cursordance auto
|
|
||||||
* Copyright (C) 2017 yugecin
|
|
||||||
*
|
|
||||||
* opsu!dance is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* opsu!dance is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with opsu!dance. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
package yugecin.opsudance.core.inject;
|
|
||||||
|
|
||||||
import java.lang.annotation.Retention;
|
|
||||||
import java.lang.annotation.RetentionPolicy;
|
|
||||||
|
|
||||||
@Retention(RetentionPolicy.RUNTIME) public @interface Inject {
|
|
||||||
}
|
|
|
@ -1,129 +0,0 @@
|
||||||
/*
|
|
||||||
* opsu!dance - fork of opsu! with cursordance auto
|
|
||||||
* Copyright (C) 2017 yugecin
|
|
||||||
*
|
|
||||||
* opsu!dance is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* opsu!dance is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with opsu!dance. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
package yugecin.opsudance.core.inject;
|
|
||||||
|
|
||||||
import java.lang.reflect.Constructor;
|
|
||||||
import java.lang.reflect.Field;
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.ListIterator;
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public abstract class Injector implements InstanceContainer, Binder {
|
|
||||||
|
|
||||||
private final HashMap<Class<?>, Object> instances;
|
|
||||||
private final LinkedList<Class<?>> lazyInstances;
|
|
||||||
|
|
||||||
private Class<?> lastType;
|
|
||||||
|
|
||||||
public Injector() {
|
|
||||||
instances = new HashMap<>();
|
|
||||||
lazyInstances = new LinkedList<>();
|
|
||||||
instances.put(InstanceContainer.class, this);
|
|
||||||
configure();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected abstract void configure();
|
|
||||||
|
|
||||||
public final <T> T provide(Class<T> type) {
|
|
||||||
Object instance = instances.get(type);
|
|
||||||
if (instance != null) {
|
|
||||||
return (T) instance;
|
|
||||||
}
|
|
||||||
ListIterator<Class<?>> iter = lazyInstances.listIterator();
|
|
||||||
while (iter.hasNext()) {
|
|
||||||
Class<?> l = iter.next();
|
|
||||||
if (l == type) {
|
|
||||||
iter.remove();
|
|
||||||
instance = createInstance(type);
|
|
||||||
instances.put(type, instance);
|
|
||||||
return (T) instance;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return createInstance(type);
|
|
||||||
}
|
|
||||||
|
|
||||||
private <T> T createInstance(Class<T> type) {
|
|
||||||
Constructor<?>[] constructors = type.getDeclaredConstructors();
|
|
||||||
if (constructors.length == 0) {
|
|
||||||
throw new RuntimeException("Cannot provide " + type.getSimpleName());
|
|
||||||
}
|
|
||||||
Constructor constructor = constructors[0];
|
|
||||||
Class<?>[] parameterTypes = constructor.getParameterTypes();
|
|
||||||
Object[] params = new Object[parameterTypes.length];
|
|
||||||
for (int i = parameterTypes.length - 1; i >= 0; i--) {
|
|
||||||
params[i] = provide(parameterTypes[i]);
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
return injectFields((T) constructor.newInstance(params), type);
|
|
||||||
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private <T> T injectFields(T object, Class<?> type) {
|
|
||||||
do {
|
|
||||||
for (Field f : type.getDeclaredFields()) {
|
|
||||||
if (f.getDeclaredAnnotation(Inject.class) == null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
boolean accessible = f.isAccessible();
|
|
||||||
if (!accessible) {
|
|
||||||
f.setAccessible(true);
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
f.set(object, provide(f.getType()));
|
|
||||||
} catch (IllegalAccessException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
if (!accessible) {
|
|
||||||
f.setAccessible(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
type = type.getSuperclass();
|
|
||||||
} while (type != null);
|
|
||||||
return object;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <T> T injectFields(T obj) {
|
|
||||||
return injectFields(obj, obj.getClass());
|
|
||||||
}
|
|
||||||
|
|
||||||
public final <T> Binder<T> bind(Class<T> type) {
|
|
||||||
lastType = type;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void asEagerSingleton() {
|
|
||||||
instances.put(lastType, createInstance(lastType));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void asLazySingleton() {
|
|
||||||
lazyInstances.add(lastType);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void to(Class type) {
|
|
||||||
instances.put(lastType, createInstance(type));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,25 +0,0 @@
|
||||||
/*
|
|
||||||
* opsu!dance - fork of opsu! with cursordance auto
|
|
||||||
* Copyright (C) 2017 yugecin
|
|
||||||
*
|
|
||||||
* opsu!dance is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* opsu!dance is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with opsu!dance. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
package yugecin.opsudance.core.inject;
|
|
||||||
|
|
||||||
public interface InstanceContainer {
|
|
||||||
|
|
||||||
<T> T provide(Class<T> type);
|
|
||||||
<T> T injectFields(T instance);
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,69 +0,0 @@
|
||||||
/*
|
|
||||||
* opsu!dance - fork of opsu! with cursordance auto
|
|
||||||
* Copyright (C) 2017 yugecin
|
|
||||||
*
|
|
||||||
* opsu!dance is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* opsu!dance is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
*
|
|
||||||
* along with opsu!dance. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
package yugecin.opsudance.core.inject;
|
|
||||||
|
|
||||||
import itdelatrisu.opsu.beatmap.BeatmapParser;
|
|
||||||
import itdelatrisu.opsu.beatmap.OszUnpacker;
|
|
||||||
import itdelatrisu.opsu.downloads.Updater;
|
|
||||||
import itdelatrisu.opsu.replay.ReplayImporter;
|
|
||||||
import itdelatrisu.opsu.states.*;
|
|
||||||
import yugecin.opsudance.core.DisplayContainer;
|
|
||||||
import yugecin.opsudance.core.state.specialstates.BarNotificationState;
|
|
||||||
import yugecin.opsudance.core.state.specialstates.BubNotifState;
|
|
||||||
import yugecin.opsudance.core.state.specialstates.FpsRenderState;
|
|
||||||
import yugecin.opsudance.core.errorhandling.ErrorHandler;
|
|
||||||
import yugecin.opsudance.options.Configuration;
|
|
||||||
import yugecin.opsudance.options.OptionsService;
|
|
||||||
import yugecin.opsudance.render.GameObjectRenderer;
|
|
||||||
import yugecin.opsudance.skinning.SkinService;
|
|
||||||
|
|
||||||
public class OpsuDanceInjector extends Injector {
|
|
||||||
|
|
||||||
protected void configure() {
|
|
||||||
bind(Configuration.class).asEagerSingleton();
|
|
||||||
|
|
||||||
bind(OptionsService.class).asLazySingleton();
|
|
||||||
bind(ReplayImporter.class).asLazySingleton();
|
|
||||||
bind(OszUnpacker.class).asLazySingleton();
|
|
||||||
bind(BeatmapParser.class).asLazySingleton();
|
|
||||||
bind(Updater.class).asLazySingleton();
|
|
||||||
bind(SkinService.class).asEagerSingleton();
|
|
||||||
|
|
||||||
//bind(PreStartupInitializer.class).asEagerSingleton();
|
|
||||||
bind(DisplayContainer.class).asEagerSingleton();
|
|
||||||
|
|
||||||
bind(ErrorHandler.class).asEagerSingleton();
|
|
||||||
|
|
||||||
bind(FpsRenderState.class).asEagerSingleton();
|
|
||||||
bind(BarNotificationState.class).asEagerSingleton();
|
|
||||||
bind(BubNotifState.class).asEagerSingleton();
|
|
||||||
|
|
||||||
bind(GameObjectRenderer.class).asEagerSingleton();
|
|
||||||
|
|
||||||
bind(Splash.class).asEagerSingleton();
|
|
||||||
bind(MainMenu.class).asEagerSingleton();
|
|
||||||
bind(ButtonMenu.class).asEagerSingleton();
|
|
||||||
bind(SongMenu.class).asEagerSingleton();
|
|
||||||
bind(DownloadsMenu.class).asEagerSingleton();
|
|
||||||
bind(Game.class).asEagerSingleton();
|
|
||||||
bind(GameRanking.class).asEagerSingleton();
|
|
||||||
bind(GamePauseMenu.class).asEagerSingleton();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user