[ANY] Небольшой рефакторинг в конфигах.

This commit is contained in:
Zaxar163 2019-12-17 06:56:37 +01:00
parent 9cbdd6300c
commit 53c8a548f0
No known key found for this signature in database
GPG key ID: 1FE4F2E1F053831B
2 changed files with 12 additions and 6 deletions

View file

@ -1,5 +1,7 @@
package pro.gravit.launcher.config; package pro.gravit.launcher.config;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.nio.file.Path; import java.nio.file.Path;
@ -18,11 +20,12 @@ public T getConfig() {
return (T) this; return (T) this;
} }
@Override @SuppressWarnings("unchecked")
@Override
public T getDefaultConfig() { public T getDefaultConfig() {
try { try {
return type.newInstance(); return (T) MethodHandles.publicLookup().findConstructor(type, MethodType.methodType(void.class)).invokeWithArguments();
} catch (InstantiationException | IllegalAccessException e) { } catch (Throwable e) {
return null; return null;
} }
} }

View file

@ -1,5 +1,7 @@
package pro.gravit.launcher.config; package pro.gravit.launcher.config;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.nio.file.Path; import java.nio.file.Path;
public class SimpleConfigurable<T> extends JsonConfigurable<T> { public class SimpleConfigurable<T> extends JsonConfigurable<T> {
@ -16,11 +18,12 @@ public T getConfig() {
return config; return config;
} }
@Override @SuppressWarnings("unchecked")
@Override
public T getDefaultConfig() { public T getDefaultConfig() {
try { try {
return tClass.newInstance(); return (T) MethodHandles.publicLookup().findConstructor(tClass, MethodType.methodType(void.class)).invokeWithArguments();
} catch (InstantiationException | IllegalAccessException e) { } catch (Throwable e) {
return null; return null;
} }
} }