use of io.github.nucleuspowered.nucleus.internal.annotations.RequireExistenceOf in project Nucleus by NucleusPowered.
the class StandardModule method getInstance.
private <T> T getInstance(Class<T> clazz, boolean checkMethods) {
try {
RequireExistenceOf[] v = clazz.getAnnotationsByType(RequireExistenceOf.class);
if (v.length > 0) {
try {
for (RequireExistenceOf r : v) {
String toFind = r.value();
String[] a;
if (toFind.contains("#")) {
a = toFind.split("#", 2);
} else {
a = new String[] { toFind };
}
// Check the class.
Class<?> c = Class.forName(a[0]);
if (a.length == 2) {
// Check the method
Method[] methods = c.getDeclaredMethods();
boolean methodFound = false;
for (Method m : methods) {
if (m.getName().equals(a[1])) {
methodFound = true;
break;
}
}
if (!methodFound) {
if (r.showError()) {
throw new RuntimeException();
}
return null;
}
}
}
} catch (ClassNotFoundException | RuntimeException | NoClassDefFoundError e) {
plugin.getLogger().warn(NucleusPlugin.getNucleus().getMessageProvider().getMessageWithFormat("startup.injectablenotloaded", clazz.getName()));
return null;
}
}
if (checkMethods) {
// This checks all the methods to ensure the classes in question exist.
clazz.getDeclaredMethods();
}
return clazz.newInstance();
// I can't believe I have to do this...
} catch (IllegalAccessException | InstantiationException | RuntimeException | NoClassDefFoundError e) {
if (clazz.isAnnotationPresent(SkipOnError.class)) {
plugin.getLogger().warn(NucleusPlugin.getNucleus().getMessageProvider().getMessageWithFormat("startup.injectablenotloaded", clazz.getName()));
return null;
}
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
} else {
throw new RuntimeException(e);
}
}
}
Aggregations