From 0aa957d7fa0418c8888d5b5efe900b6cc070c8d6 Mon Sep 17 00:00:00 2001 From: yugecin Date: Mon, 25 Jun 2018 22:19:53 +0200 Subject: [PATCH] remove unused dependency injection code --- src/yugecin/opsudance/core/inject/Binder.java | 25 ---- src/yugecin/opsudance/core/inject/Inject.java | 24 ---- .../opsudance/core/inject/Injector.java | 129 ------------------ .../core/inject/InstanceContainer.java | 25 ---- .../core/inject/OpsuDanceInjector.java | 69 ---------- 5 files changed, 272 deletions(-) delete mode 100644 src/yugecin/opsudance/core/inject/Binder.java delete mode 100644 src/yugecin/opsudance/core/inject/Inject.java delete mode 100644 src/yugecin/opsudance/core/inject/Injector.java delete mode 100644 src/yugecin/opsudance/core/inject/InstanceContainer.java delete mode 100644 src/yugecin/opsudance/core/inject/OpsuDanceInjector.java diff --git a/src/yugecin/opsudance/core/inject/Binder.java b/src/yugecin/opsudance/core/inject/Binder.java deleted file mode 100644 index 16f33180..00000000 --- a/src/yugecin/opsudance/core/inject/Binder.java +++ /dev/null @@ -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 . - */ -package yugecin.opsudance.core.inject; - -public interface Binder { - - void asEagerSingleton(); - void asLazySingleton(); - void to(Class type); -} diff --git a/src/yugecin/opsudance/core/inject/Inject.java b/src/yugecin/opsudance/core/inject/Inject.java deleted file mode 100644 index 04fc897a..00000000 --- a/src/yugecin/opsudance/core/inject/Inject.java +++ /dev/null @@ -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 . - */ -package yugecin.opsudance.core.inject; - -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; - -@Retention(RetentionPolicy.RUNTIME) public @interface Inject { -} diff --git a/src/yugecin/opsudance/core/inject/Injector.java b/src/yugecin/opsudance/core/inject/Injector.java deleted file mode 100644 index 9ddee01d..00000000 --- a/src/yugecin/opsudance/core/inject/Injector.java +++ /dev/null @@ -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 . - */ -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, Object> instances; - private final LinkedList> 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 provide(Class type) { - Object instance = instances.get(type); - if (instance != null) { - return (T) instance; - } - ListIterator> 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 createInstance(Class 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 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 injectFields(T obj) { - return injectFields(obj, obj.getClass()); - } - - public final Binder bind(Class 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)); - } - -} diff --git a/src/yugecin/opsudance/core/inject/InstanceContainer.java b/src/yugecin/opsudance/core/inject/InstanceContainer.java deleted file mode 100644 index 77ba1be7..00000000 --- a/src/yugecin/opsudance/core/inject/InstanceContainer.java +++ /dev/null @@ -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 . - */ -package yugecin.opsudance.core.inject; - -public interface InstanceContainer { - - T provide(Class type); - T injectFields(T instance); - -} diff --git a/src/yugecin/opsudance/core/inject/OpsuDanceInjector.java b/src/yugecin/opsudance/core/inject/OpsuDanceInjector.java deleted file mode 100644 index ba100f31..00000000 --- a/src/yugecin/opsudance/core/inject/OpsuDanceInjector.java +++ /dev/null @@ -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 . - */ -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(); - } - -}