Skip to content

Commit

Permalink
Fixed configvalue annotation, Inteface "support", fixed issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ablax committed Feb 22, 2020
1 parent 08bbb0e commit 662228c
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 59 deletions.
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.ablax.decode</groupId>
<artifactId>AdvancedPluginApi</artifactId>
<version>1.3.0</version>
<version>1.4.0</version>

<build>
<plugins>
Expand Down Expand Up @@ -84,6 +84,7 @@
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
<scope>compile</scope>
</dependency>
</dependencies>

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/me/ablax/decode/ApiManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ void register(JavaPlugin javaPlugin) {

if (!classesList.isEmpty()) {
managersController.registerAllComponents(classesList);
managersController.registerAllListeners(javaPlugin, classesList);
managersController.registerAllCommands(javaPlugin, classesList);
managersController.registerAllListeners(classesList);
managersController.registerAllCommands(classesList);
managersController.populateInjectors(classesList);
managersController.populateValues(javaPlugin, classesList);
managersController.populateValues(classesList);
} else {
Bukkit.getLogger().severe("I could't find any classes belonging to plugin: " + javaPlugin.getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Value {
@Target({ElementType.FIELD, ElementType.PARAMETER})
public @interface ConfigValue {

String name();
String value();

}
4 changes: 2 additions & 2 deletions src/main/java/me/ablax/decode/managers/CommandsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ class CommandsManager {
this.componentsManager = componentsManager;
}

void registerAllCommands(JavaPlugin javaPlugin, List<? extends Class<?>> classesList) {
void registerAllCommands(List<? extends Class<?>> classesList) {
try {
for (Class<?> aClass : classesList) {
if (aClass.isAnnotationPresent(RegisterCommand.class)) {
registerCommand(javaPlugin, aClass);
registerCommand(JavaPlugin.getProvidingPlugin(aClass), aClass);
}
}
} catch (Exception e) {
Expand Down
39 changes: 27 additions & 12 deletions src/main/java/me/ablax/decode/managers/ComponentsManager.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package me.ablax.decode.managers;

import me.ablax.decode.annotation.Component;
import me.ablax.decode.annotation.ConfigValue;
import org.bukkit.Bukkit;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -15,9 +17,11 @@
class ComponentsManager {

private final Map<String, Object> components;
private final ConfigValuesManager configValuesManager;

ComponentsManager(Map<String, Object> components) {
ComponentsManager(final Map<String, Object> components, final ConfigValuesManager configValuesManager) {
this.components = components;
this.configValuesManager = configValuesManager;
}

boolean containsClass(Class<?> clazz) {
Expand All @@ -27,15 +31,23 @@ boolean containsClass(Class<?> clazz) {
void registerAllComponents(List<? extends Class<?>> allClasses) {
for (Class<?> aClass : allClasses) {
if (aClass.isInterface()) {
boolean registered = false;
for (Class<?> tempImp : allClasses) {
if (tempImp.isInstance(aClass) && tempImp.isAnnotationPresent(Component.class)) {
registerComponent(tempImp);
components.put(aClass.getCanonicalName(), components.get(tempImp.getCanonicalName()));
if (!registered && !tempImp.isInterface()) {
if (aClass.isAssignableFrom(tempImp) && tempImp.isAnnotationPresent(Component.class)) {
registerComponent(tempImp);
components.put(aClass.getCanonicalName(), components.get(tempImp.getCanonicalName()));
registered = true;
}
}
}
}
if (aClass.isAnnotationPresent(Component.class)) {
registerComponent(aClass);
}
for (Class<?> aClass : allClasses) {
if (!aClass.isInterface()) {
if (aClass.isAnnotationPresent(Component.class)) {
registerComponent(aClass);
}
}
}
}
Expand All @@ -50,11 +62,11 @@ private void loadClass(Class<?> aClass) {
final List<Constructor<?>> constructors = getConstructors(aClass);
for (Constructor<?> constructor : constructors) {
try {
Class<?>[] parameterTypes = constructor.getParameterTypes();
Object[] vars = new Object[parameterTypes.length];
for (int i = 0; i < parameterTypes.length; i++) {
Class<?> parameterType = parameterTypes[i];
final Object resolved = resolveParameter(parameterType);
Parameter[] parameters = constructor.getParameters();
Object[] vars = new Object[parameters.length];
for (int i = 0; i < parameters.length; i++) {
Class<?> parameterType = parameters[i].getType();
final Object resolved = resolveParameter(aClass, parameters[i]);
if (resolved == null) {
throw new InstantiationException("I don't know what " + parameterType.getSimpleName() + " is to pass it on " + aClass.getSimpleName());
}
Expand All @@ -76,13 +88,16 @@ private void loadClass(Class<?> aClass) {
Bukkit.getLogger().severe("After trying all we can, we weren't able to instantiate " + aClass.getSimpleName() + " are you sure you have a nice friendly constructor?");
}

private Object resolveParameter(Class<?> parameterType) {
private Object resolveParameter(Class<?> parent, Parameter parameter) {
final Class<?> parameterType = parameter.getType();
if (components.containsKey(parameterType.getCanonicalName())) {
return components.get(parameterType.getCanonicalName());
} else {
if (parameterType.isAnnotationPresent(Component.class)) {
registerComponent(parameterType);
return components.get(parameterType.getCanonicalName());
} else if (parameter.isAnnotationPresent(ConfigValue.class)) {
return configValuesManager.getValue(parent, parameter);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package me.ablax.decode.managers;

import me.ablax.decode.annotation.Value;
import me.ablax.decode.annotation.ConfigValue;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.util.Map;
import java.util.logging.Level;

class ValuesManager {
class ConfigValuesManager {

private final Map<String, Object> components;
private sun.misc.Unsafe unsafe;
private boolean useUnsafe = true;

ValuesManager(Map<String, Object> components) {
ConfigValuesManager(Map<String, Object> components) {
try {
Field field = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
Expand All @@ -30,30 +30,36 @@ class ValuesManager {
this.components = components;
}

void populateValues(Object klass) {
for (Field field : klass.getClass().getDeclaredFields()) {
if (field.isAnnotationPresent(Value.class)) {
Class<?> type = field.getType();
final Value annotation = type.getAnnotation(Value.class);
if (klass instanceof Plugin) {
final Object object = ((Plugin) klass).getConfig().get(annotation.name());
populateField(klass, field, object);
}
void populateValues(JavaPlugin plugin) {
for (Field field : plugin.getClass().getDeclaredFields()) {
if (field.isAnnotationPresent(ConfigValue.class)) {
final ConfigValue annotation = field.getAnnotation(ConfigValue.class);
final Object object = plugin.getConfig().get(annotation.value());
populateField(plugin, field, object);
}
}
}

public void populateValues(JavaPlugin javaPlugin, Class<?> clazz) {
public void populateValues(Class<?> clazz) {
final JavaPlugin plugin = JavaPlugin.getProvidingPlugin(clazz);
for (Field field : clazz.getDeclaredFields()) {
if (field.isAnnotationPresent(Value.class)) {
Class<?> type = field.getType();
final Value annotation = type.getAnnotation(Value.class);
final Object object = javaPlugin.getConfig().get(annotation.name());
if (field.isAnnotationPresent(ConfigValue.class)) {
final ConfigValue annotation = field.getAnnotation(ConfigValue.class);
final Object object = plugin.getConfig().get(annotation.value());
populateField(components.get(clazz.getCanonicalName()), field, object);
}
}
}

public Object getValue(Class<?> parent, Parameter parameter) {
final JavaPlugin plugin = JavaPlugin.getProvidingPlugin(parent);
if (parameter.isAnnotationPresent(ConfigValue.class)) {
final ConfigValue annotation = parameter.getAnnotation(ConfigValue.class);
return plugin.getConfig().get(annotation.value());
}
return null;
}

private void populateField(Object klass, Field field, Object injectInstance) {
final boolean accessible = field.isAccessible();
if (!useUnsafe) {
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/me/ablax/decode/managers/ListenersManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ class ListenersManager {
this.componentsManager = componentsManager;
}

void registerAllListeners(JavaPlugin javaPlugin, List<? extends Class<?>> classesList) {
void registerAllListeners(List<? extends Class<?>> classesList) {
for (Class<?> aClass : classesList) {
if (aClass.isAnnotationPresent(RegisterListener.class)) {
registerListener(javaPlugin, aClass);
registerListener(JavaPlugin.getProvidingPlugin(aClass), aClass);
}
}
}

private void registerListener(JavaPlugin claz, Class<?> aClass) {
private void registerListener(JavaPlugin plugin, Class<?> aClass) {
if (components.containsKey(aClass.getCanonicalName())) {
Bukkit.getPluginManager().registerEvents((Listener) components.get(aClass.getCanonicalName()), claz);
Bukkit.getPluginManager().registerEvents((Listener) components.get(aClass.getCanonicalName()), plugin);
} else {
componentsManager.registerComponent(aClass);
if (components.containsKey(aClass.getCanonicalName())) {
Bukkit.getPluginManager().registerEvents((Listener) components.get(aClass.getCanonicalName()), claz);
Bukkit.getPluginManager().registerEvents((Listener) components.get(aClass.getCanonicalName()), plugin);
}
}
}
Expand Down
28 changes: 14 additions & 14 deletions src/main/java/me/ablax/decode/managers/ManagersController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@

public class ManagersController {

private static ManagersController instance;
private static Object isInstantiated;

private final ComponentsManager componentsManager;
private final InjectorsManager injectorsManager;
private final ValuesManager valuesManager;
private final ConfigValuesManager configValuesManager;
private final ListenersManager listenersManager;
private final CommandsManager commandsManager;

public ManagersController(Map<String, Object> components) {
if (instance != null) {
if (isInstantiated != null) {
Bukkit.getServer().shutdown();
throw new SecurityException("This move is illegal!");
}
instance = this;
this.componentsManager = new ComponentsManager(components);
isInstantiated = new Object();
this.configValuesManager = new ConfigValuesManager(components);
this.componentsManager = new ComponentsManager(components, configValuesManager);
this.injectorsManager = new InjectorsManager(components, componentsManager);
this.valuesManager = new ValuesManager(components);
this.listenersManager = new ListenersManager(components, componentsManager);
this.commandsManager = new CommandsManager(components, componentsManager);
}
Expand All @@ -33,12 +33,12 @@ public void registerComponent(Class<?> componentToRegister) {
this.componentsManager.registerComponent(componentToRegister);
}

public void registerAllCommands(JavaPlugin javaPlugin, List<? extends Class<?>> classesList) {
this.commandsManager.registerAllCommands(javaPlugin, classesList);
public void registerAllCommands(List<? extends Class<?>> classesList) {
this.commandsManager.registerAllCommands(classesList);
}

public void registerAllListeners(JavaPlugin javaPlugin, List<? extends Class<?>> classesList) {
listenersManager.registerAllListeners(javaPlugin, classesList);
public void registerAllListeners(List<? extends Class<?>> classesList) {
listenersManager.registerAllListeners(classesList);
}

public void registerAllComponents(List<? extends Class<?>> classesList) {
Expand All @@ -52,19 +52,19 @@ public void populateInjectors(JavaPlugin javaPlugin) {
public void populateInjectors(List<? extends Class<?>> classesList) {
for (Class<?> clazz : classesList) {
if (componentsManager.containsClass(clazz)) {
this.injectorsManager.populateInjectors(componentsManager);
this.injectorsManager.populateInjectors(clazz);
}
}
}

public void populateValues(JavaPlugin javaPlugin) {
this.valuesManager.populateValues(javaPlugin);
this.configValuesManager.populateValues(javaPlugin);
}

public void populateValues(JavaPlugin javaPlugin, List<? extends Class<?>> classesList) {
public void populateValues(List<? extends Class<?>> classesList) {
for (Class<?> clazz : classesList) {
if (componentsManager.containsClass(clazz)) {
this.valuesManager.populateValues(javaPlugin, clazz);
this.configValuesManager.populateValues(clazz);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
name: AdvancedPluginApi
main: me.ablax.decode.AdvancedPlugin
version: 1.2.0
version: 1.4.0

0 comments on commit 662228c

Please sign in to comment.