bt.runtime.ContributionScanner Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bt-core Show documentation
Show all versions of bt-core Show documentation
BitTorrent Client Library (Core)
package bt.runtime;
import bt.BtException;
import bt.module.Contribute;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
class ContributionScanner {
private static final ContributionScanner instance = new ContributionScanner();
public static ContributionScanner scanner() {
return instance;
}
// TODO: check for mutual dependencies
public List> scan(Object object) {
List> contributions = null;
Method[] methods = Objects.requireNonNull(object).getClass().getMethods();
for (Method method : methods) {
Contribute annotation = method.getAnnotation(Contribute.class);
if (annotation != null) {
if (contributions == null) {
contributions = new ArrayList<>();
}
contributions.add(new Contribution<>(annotation.value(),
module -> {
try {
method.invoke(object, module);
} catch (IllegalAccessException | InvocationTargetException e) {
String name = method.getDeclaringClass().getName() + "." + method.getName();
throw new BtException("Failed to apply module contribution: " + name);
}
}));
}
}
return (contributions == null) ? Collections.emptyList() : contributions;
}
}