Skip to content

Commit

Permalink
added custom exceptions to api
Browse files Browse the repository at this point in the history
  • Loading branch information
zkingboos committed Jul 15, 2020
1 parent 048bc4b commit 3371bc5
Show file tree
Hide file tree
Showing 16 changed files with 170 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/main/java/io/king/core/api/KingApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ public interface KingApi {
* @return instance of module manager
*/
ModuleManager getModuleManager();
}
}
18 changes: 18 additions & 0 deletions src/main/java/io/king/core/api/exception/InjectableException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.king.core.api.exception;

import java.util.NoSuchElementException;

public final class InjectableException extends NoSuchElementException {

private final static String EXCEPTION_MESSAGE = "Can´t find type of service registration (%s) in injection of constructor of class (%s) has be no found.";

public InjectableException(Class<?> injectionClass, Class<?> typeService) {
super(
String.format(
EXCEPTION_MESSAGE,
injectionClass.getSimpleName(),
typeService.getSimpleName()
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.king.core.api.exception.module;

import java.util.NoSuchElementException;

public final class NoSuchModuleException extends NoSuchElementException {

private final static String EXCEPTION_MESSAGE = "Module (%s) is not loaded.";

public NoSuchModuleException(Class<?> moduleType) {
super(
String.format(
EXCEPTION_MESSAGE,
moduleType.getSimpleName()
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.king.core.api.exception.module;

public final class OverflowSoftDependException extends StackOverflowError {

private final static String EXCEPTION_MESSAGE = "Overflow on softDepend (%s) at module (%s).";

public OverflowSoftDependException(Class<?> module, Class<?> softDepend) {
super(
String.format(
EXCEPTION_MESSAGE,
softDepend.getSimpleName(),
module.getSimpleName()
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.king.core.api.exception.module;

import java.util.NoSuchElementException;

public final class UnknownModuleConfigException extends NoSuchElementException {

private static final String EXCEPTION_MESSAGE = "The config type class (%s) for module (%s) should be moduleConfig module type.";

public UnknownModuleConfigException(Class<?> moduleClass, Class<?> moduleConfig) {
super(
String.format(
EXCEPTION_MESSAGE,
moduleConfig.getSimpleName(),
moduleClass.getSimpleName()
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.king.core.api.exception.service;

import java.util.NoSuchElementException;

public final class NoSuchServiceException extends NoSuchElementException {

private final static String EXCEPTION_MESSAGE = "Service (%s) should be annotated with @Service";

public NoSuchServiceException(Class<?> serviceType) {
super(
String.format(
EXCEPTION_MESSAGE,
serviceType.getSimpleName()
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.king.core.api.exception.service;

import java.util.NoSuchElementException;

public final class NoSuchServiceRegistryException extends NoSuchElementException {

private static final String EXCEPTION_MESSAGE = "No such service (%s) has been found on registry.";

public NoSuchServiceRegistryException(Class<?> service) {
super(
String.format(
EXCEPTION_MESSAGE,
service.getSimpleName()
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.king.core.api.exception.service;

public final class RedundantServiceException extends StackOverflowError {

private final static String REDUNDANT_SERVICE_IMPORT = "Service (%s) and subservice (%s) are the same.";

public RedundantServiceException(Class<?> serviceType, Class<?> importServiceType) {
super(
String.format(
REDUNDANT_SERVICE_IMPORT,
serviceType.getSimpleName(),
importServiceType.getSimpleName()
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.king.core.api.exception.service;

import java.util.NoSuchElementException;

public final class ServiceTypeInvoke extends NoSuchElementException {

public ServiceTypeInvoke() {
super();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ public <T> T getService(Class<?> registration) {

@Override
public File getDataFolder() throws AssertionError {
if (!isModule()) throw new AssertionError("Isn't an module");
if (moduleDataFolder != null) return moduleDataFolder;
if (!isModule())
throw new AssertionError("Assertion error on get data folder of non module object.");

if (moduleDataFolder != null)
return moduleDataFolder;

final ModuleConfig moduleConfig = moduleObject.getModuleConfig();
final String path = String.format("/%s/", moduleConfig.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,25 @@
import io.king.core.api.KingApi;
import io.king.core.api.cycle.CycleLoader;
import io.king.core.api.cycle.strategy.StrategyCycle;
import io.king.core.api.exception.module.UnknownModuleConfigException;
import io.king.core.api.module.Module;
import io.king.core.api.module.ModuleConfig;
import io.king.core.provider.module.ModuleObject;

import java.util.NoSuchElementException;

public final class ConfigCycle implements StrategyCycle {

private final static Class<?> MODULE_CONFIG = ModuleConfig.class;

@Override
public void setup(KingApi kingApi, CycleLoader loader, ModuleObject moduleObject) throws Exception {
final Module module = moduleObject.getModule();
final Class<?> moduleClass = moduleObject.getModuleClass();

final Class<?> moduleConfigClass = module.config();
final Object objectInstance = moduleConfigClass.newInstance();

if (!MODULE_CONFIG.isInstance(objectInstance)) throw new NoSuchElementException(
"The config type should be ModuleConfig's type"
);
if (!MODULE_CONFIG.isInstance(objectInstance))
throw new UnknownModuleConfigException(moduleClass, moduleConfigClass);

final ModuleConfig moduleConfig = (ModuleConfig) objectInstance;
moduleObject.setModuleConfigClass(moduleConfigClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ public void setup(KingApi kingApi, CycleLoader loader, ModuleObject moduleObject

for (Class<? extends Listener> event : module.events()) {
final Object objectInstance = loader.initialize(event);
if (!BUKKIT_LISTENER_CLASS.isInstance(objectInstance)) throw new NoSuchElementException(
"Event type should be instance of Listener"
);
if (!BUKKIT_LISTENER_CLASS.isInstance(objectInstance))
throw new NoSuchElementException("Event type should be instance of Listener");

final LifeCycle lifeCycle = loader.initializeLife(objectInstance);

pluginManager.registerEvents(
(Listener) objectInstance, kingApi.getPlugin()
(Listener) objectInstance,
kingApi.getPlugin()
);

loader.notifyModule(lifeCycle, lifeContext);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import io.king.core.api.cycle.LifeContext;
import io.king.core.api.cycle.LifeCycle;
import io.king.core.api.cycle.strategy.StrategyCycle;
import io.king.core.api.exception.service.NoSuchServiceException;
import io.king.core.api.exception.service.RedundantServiceException;
import io.king.core.api.module.Module;
import io.king.core.api.service.Service;
import io.king.core.api.service.ServiceManager;
import io.king.core.provider.module.ModuleObject;

import java.util.NoSuchElementException;

public final class ServiceCycle implements StrategyCycle {

private final static Class<Service> SERVICE_CLASS = Service.class;
Expand Down Expand Up @@ -47,14 +47,11 @@ public void resolveServiceContext(
) throws Exception {
final Service serviceAnnotation = service.getAnnotation(SERVICE_CLASS);
if (serviceAnnotation == null)
throw new NoSuchElementException("Service should be annotated with @Service");
throw new NoSuchServiceException(service);

for (Class<?> subService : serviceAnnotation.value()) {
if (service == subService) {
throw new StackOverflowError(
"Service and subservice are the same."
);
}
if (service == subService)
throw new RedundantServiceException(service, subService);

resolveServiceContext(
moduleObject,
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/io/king/core/provider/di/InjectionManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import io.king.core.api.di.Inject;
import io.king.core.api.di.InjectionManager;
import io.king.core.api.exception.InjectableException;
import io.king.core.api.exception.service.NoSuchServiceRegistryException;
import io.king.core.api.service.ServiceEntity;
import io.king.core.api.service.ServiceManager;
import lombok.RequiredArgsConstructor;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Parameter;
import java.util.NoSuchElementException;

@RequiredArgsConstructor
public final class InjectionManagerImpl implements InjectionManager {
Expand All @@ -25,14 +26,13 @@ public void injectIntoService(Object objectInstance, Class<?> clazz) throws Ille

final Class<?> type = field.getType();
final ServiceEntity<?> service = serviceManager.getRegistrationService(type);
if (service == null) throw new NoSuchElementException();
if (service == null)
throw new NoSuchServiceRegistryException(type);

final Object serviceObject = service.getService();
final boolean accessible = field.isAccessible();

field.setAccessible(true);
field.set(objectInstance, serviceObject);
field.setAccessible(accessible);
}
}

Expand All @@ -50,11 +50,13 @@ public Object injectIntoClass(Class<?> clazz) throws Exception {
final Parameter type = parameters[i];
if (!type.isAnnotationPresent(INJECT_CLASS)) continue mainLoop;

final Class<?> typeService = type.getType();
final ServiceEntity<?> registration = serviceManager.getRegistrationService(
type.getType()
typeService
);

if (registration == null) throw new NoSuchElementException();
if (registration == null)
throw new InjectableException(clazz, typeService);

objects[i] = registration.getService();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import io.king.core.api.cycle.CycleLoader;
import io.king.core.api.cycle.LifeContext;
import io.king.core.api.exception.module.NoSuchModuleException;
import io.king.core.api.exception.module.OverflowSoftDependException;
import io.king.core.api.module.ModuleManager;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.Comparator;
import java.util.List;
import java.util.NoSuchElementException;

@Getter
@RequiredArgsConstructor
Expand All @@ -31,14 +32,12 @@ public ModuleObject findModuleByType(Class<?> clazz) {
public void tryLoadLife(ModuleObject module) throws Exception {
final Class<?> moduleClass = module.getModuleClass();
for (Class<?> soft : module.getModule().softDepend()) {
if (moduleClass.equals(soft)) throw new StackOverflowError(
"Overflow on softDepend at module " + moduleClass.getSimpleName()
);
if (moduleClass.equals(soft))
throw new OverflowSoftDependException(moduleClass, soft);

final ModuleObject moduleType = findModuleByType(soft);
if (moduleType == null) throw new NoSuchElementException(
"Module not found"
);
if (moduleType == null)
throw new NoSuchModuleException(soft);

tryLoadLife(moduleType);
}
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/io/king/core/provider/module/ModuleModelImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public ModuleManager load() throws Exception {
if (!moduleFolder.exists()) moduleFolder.mkdir();

File[] moduleFiles = moduleFolder.listFiles();
if (moduleFiles == null) throw new NoSuchElementException();
if (moduleFiles == null)
throw new NoSuchElementException("Can´t read files of folder.");

final List<ModuleObject> moduleObjects = new LinkedList<>();
final URLClassLoader urlClassLoader = resolveClasses(moduleFiles);
Expand Down Expand Up @@ -97,7 +98,7 @@ public ModuleObject loadFile(File file, ClassLoader classLoader) throws Exceptio

final Module moduleAnnotation = clazz.getAnnotation(MODULE_CLASS);
if (moduleAnnotation == null) throw new NoSuchElementException(
"Sub-module should be annotated with @Module"
"Module should be annotated with @Module"
);

final long delayedLoad = System.currentTimeMillis() - oldMsTime;
Expand Down Expand Up @@ -141,12 +142,14 @@ public ModuleProps loadJarFile(File file, ClassLoader classLoader) throws Except
jarFile.close();

if (mainClass == null) throw new NoSuchElementException(
"No classes were annotated with @Module in the file."
"No classes were annotated with @Module in this file."
);

final long loadedAtTime = System.currentTimeMillis();
return new ModuleProps(
mainClass, jarFileName, loadedAtTime
mainClass,
jarFileName,
loadedAtTime
);
}
}

0 comments on commit 3371bc5

Please sign in to comment.