initial commit

This commit is contained in:
2021-04-07 21:40:57 +02:00
commit 3312341b17
21 changed files with 1138 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
package net.woggioni.wdi.api;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface Bean {
enum Scope {
Singleton, Prototype
}
String name() default "";
Scope scope() default Scope.Singleton;
}

View File

@@ -0,0 +1,13 @@
package net.woggioni.wdi.api;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface BeanClasses {
Class<?>[] value() default {};
}

View File

@@ -0,0 +1,11 @@
package net.woggioni.wdi.api;
public class BeanConfigurationException extends RuntimeException {
public BeanConfigurationException(String msg) {
super(msg);
}
public BeanConfigurationException(String msg, Throwable cause) {
super(msg, cause);
}
}

View File

@@ -0,0 +1,244 @@
package net.woggioni.wdi.api;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import net.woggioni.jwo.CollectionUtils;
import net.woggioni.jwo.JWO;
import net.woggioni.jwo.tuple.Tuple2;
import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import java.util.*;
import java.util.function.BiConsumer;
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class BeanContainer {
private final Map<Executable, Object> singletonCache;
private final NavigableMap<BeanSpec, Executable> bom;
@RequiredArgsConstructor
private static class ConstructorList {
final Iterable<Map.Entry<BeanSpec, Executable>> allBeans;
final Iterable<Class<? extends BeanFactory>> beanFactories;
}
@SneakyThrows
public static BeanContainer with(Class<? extends BeanFactory> beanFactoryClass) {
try {
return with(beanFactoryClass.getConstructor().newInstance());
} catch (NoSuchMethodException | IllegalAccessException ex) {
throw JWO.newThrowable(BeanConfigurationException.class,
"Bean factory class '%s' is required to have a public zero arguments constructor", beanFactoryClass.getName());
}
}
public static BeanContainer with(BeanFactory root) {
Map<Executable, Object> singletonCache = new HashMap<>();
NavigableMap<BeanSpec, Executable> bom = new TreeMap<>(BeanSpec.specComparator);
List<BeanFactory> stack = new ArrayList<>();
stack.add(root);
while(!stack.isEmpty()) {
ConstructorList constructorList = computeBom(JWO.pop(stack));
for(Map.Entry<BeanSpec, Executable> entry : constructorList.allBeans) {
bom.putIfAbsent(entry.getKey(), entry.getValue());
}
for(Class<? extends BeanFactory> beanFactoryClass : constructorList.beanFactories) {
BeanFactory beanFactory = getBean(singletonCache, bom, beanFactoryClass, null, BeanSpec.emptyQualifier);
stack.add(beanFactory);
}
}
return new BeanContainer(singletonCache, Collections.unmodifiableNavigableMap(bom));
}
@SneakyThrows
private static ConstructorList computeBom(BeanFactory beanFactory) {
Class<? extends BeanFactory> beanFactoryClass = beanFactory.getClass();
List<Map.Entry<BeanSpec, Executable>> allBeans = new ArrayList<>();
List<Class<? extends BeanFactory>> beanFactories = new ArrayList<>();
BiConsumer<BeanSpec, Executable> add2ResultList = (BeanSpec beanSpec, Executable executable) -> {
Map.Entry<BeanSpec, Executable> entry = new AbstractMap.SimpleEntry<>(beanSpec, executable);
allBeans.add(entry);
Class<?> cls = beanSpec.getCls();
if(BeanFactory.class.isAssignableFrom(cls) && cls != beanFactoryClass) {
beanFactories.add((Class<? extends BeanFactory>) beanSpec.getCls());
}
Iterator<Class<?>> it = new ParentIterator(cls);
while(it.hasNext()) {
Class<?> superClass = it.next();
entry = new AbstractMap.SimpleEntry<>(
new BeanSpec(superClass, beanSpec.getName(), beanSpec.getQualifiers()), executable);
allBeans.add(entry);
}
};
for(Method method : beanFactoryClass.getMethods()) {
Bean bean = method.getAnnotation(Bean.class);
if(bean != null) {
String name = bean.name();
if(name.isEmpty()) name = JWO.decapitalize(method.getName());
add2ResultList.accept(new BeanSpec(method.getReturnType(), name, extractQualifiers(method)), method);
}
}
List<Class<?>> classes = new ArrayList<>();
classes.add(beanFactoryClass);
for(Class<?> cls : beanFactory.getClasses()) classes.add(cls);
BeanClasses beanClasses = beanFactoryClass.getAnnotation(BeanClasses.class);
if(beanClasses != null) {
for(Class<?> cls : beanClasses.value()) classes.add(cls);
}
Import imports = beanFactoryClass.getAnnotation(Import.class);
if(imports != null) {
for(Class<?> cls : imports.value()) classes.add(cls);
}
for(Class<?> cls : classes) {
Constructor<?>[] constructors = cls.getConstructors();
if(constructors.length == 1) {
BeanSpec spec = new BeanSpec(
cls,
JWO.decapitalize(cls.getSimpleName()),
extractQualifiers(constructors[0])
);
add2ResultList.accept(spec, constructors[0]);
} else {
List<Constructor<?>> annotatedConstructors = new ArrayList<>();
for(Constructor<?> constructor : constructors) {
if(constructor.getAnnotation(Bean.class) != null)
annotatedConstructors.add(constructor);
}
if(annotatedConstructors.isEmpty()) {
throw JWO.newThrowable(BeanConfigurationException.class, "Class '%s' has multiple constructors," +
" but none of them is annotated with %s", cls.getName(), Bean.class.getName());
} else {
for(Constructor<?> annotatedConstructor : annotatedConstructors) {
String name = annotatedConstructor.getAnnotation(Bean.class).name();
if(name.isEmpty()) name = JWO.decapitalize(annotatedConstructor.getName());
BeanSpec spec = new BeanSpec(cls, name, extractQualifiers(annotatedConstructor));
add2ResultList.accept(spec, annotatedConstructor);
}
}
}
}
return new ConstructorList(allBeans, beanFactories);
}
private static NavigableSet<Annotation> extractQualifiers(AnnotatedElement annotatedElement) {
NavigableSet<Annotation> result = new TreeSet<>(BeanSpec.annotationComparator);
for(Annotation methodAnnotation : annotatedElement.getAnnotations()) {
for(Annotation annotationAnnotation : methodAnnotation.annotationType().getAnnotations()) {
if(annotationAnnotation instanceof Qualifier) {
result.add(methodAnnotation);
break;
}
}
}
return Collections.unmodifiableNavigableSet(result);
}
private static Tuple2<BeanSpec, Boolean> analyzeInjectionPoint(Parameter parameter) {
String name;
boolean strictName;
{
String beanName = Optional.ofNullable(parameter.getAnnotation(Bean.class)).map(Bean::name).orElse("");
if(!beanName.isEmpty()) {
name = beanName;
strictName = true;
} else {
if(parameter.isNamePresent()) {
name = parameter.getName();
} else {
name = "";
}
strictName = false;
}
}
return new Tuple2<>(new BeanSpec(parameter.getType(), name, extractQualifiers(parameter)), strictName);
}
@SneakyThrows
private static Object getBean(
Map<Executable, Object> singletonCache,
NavigableMap<BeanSpec, Executable> bom,
BeanSpec spec, boolean strictName) {
List<Map.Entry<BeanSpec, Executable>> candidatesFactoryMethods = new ArrayList<>();
if(bom.containsKey(spec)) candidatesFactoryMethods.add(bom.floorEntry(spec));
else {
BeanSpec head = new BeanSpec(spec.getCls(), "", BeanSpec.emptyQualifier);
for (Map.Entry<BeanSpec, Executable> entry : bom.tailMap(head, true).entrySet()) {
BeanSpec key = entry.getKey();
if (key.getCls() == spec.getCls()) {
key.getQualifiers().containsAll(spec.getQualifiers());
if ((!strictName || Objects.equals(key.getName(), spec.getName())) &&
key.getQualifiers().containsAll(spec.getQualifiers())) {
candidatesFactoryMethods.add(entry);
}
} else break;
}
}
Executable executable;
if(candidatesFactoryMethods.isEmpty())
throw JWO.newThrowable(BeanConfigurationException.class, "No bean found for %s", spec);
else if(candidatesFactoryMethods.size() > 1) {
if(!strictName) {
executable = candidatesFactoryMethods.get(0).getValue();
for(Map.Entry<BeanSpec, Executable> entry : candidatesFactoryMethods) {
if(Objects.equals(entry.getKey().getName(), spec.getName())) {
executable = entry.getValue();
break;
}
}
} else throw JWO.newThrowable(BeanConfigurationException.class, "Multiple candidates found matching %s", spec);
} else {
executable = candidatesFactoryMethods.get(0).getValue();
}
Object result = singletonCache.get(executable);
if(result == null) {
List<Object> executableParameters = new ArrayList<>();
for(Parameter parameter : executable.getParameters()) {
Tuple2<BeanSpec, Boolean> tuple = analyzeInjectionPoint(parameter);
executableParameters.add(getBean(singletonCache, bom, tuple._1, tuple._2));
}
if(executable instanceof Constructor) {
result = ((Constructor<?>) executable).newInstance(executableParameters.toArray());
} else if(executable instanceof Method) {
if(Modifier.isStatic(executable.getModifiers())) {
result = ((Method) executable).invoke(null, executableParameters.toArray());
} else {
Object instance = getBean(singletonCache, bom,
new BeanSpec(
executable.getDeclaringClass(),
"",
BeanSpec.emptyQualifier), false);
result = ((Method) executable).invoke(instance, executableParameters.toArray());
}
}
else throw new IllegalStateException();
singletonCache.put(executable, result);
}
return result;
}
private static <T> T getBean(Map<Executable, Object> singletonCache,
NavigableMap<BeanSpec, Executable> bom,
Class<T> cls,
String name,
NavigableSet<Annotation> qualifiers) {
return (T) getBean(singletonCache, bom, new BeanSpec(cls, name == null ? "" : name, qualifiers), name != null);
}
public <T> T getBean(Class<T> cls, String name, Annotation ...qualifiers) {
return (T) getBean(singletonCache, bom,
new BeanSpec(cls, name == null ? "" : name,
Arrays.stream(qualifiers).collect(CollectionUtils.toUnmodifiableTreeSet(BeanSpec.annotationComparator))),
name != null);
}
public <T> T getBean(Class<T> cls) {
return getBean(cls, null);
}
}

View File

@@ -0,0 +1,9 @@
package net.woggioni.wdi.api;
import java.util.Collections;
public interface BeanFactory {
default Iterable<Class<?>> getClasses() {
return Collections.emptyList();
}
}

View File

@@ -0,0 +1,52 @@
package net.woggioni.wdi.api;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import net.woggioni.jwo.collection.LexicographicIterableComparator;
import java.lang.annotation.Annotation;
import java.util.Collections;
import java.util.Comparator;
import java.util.NavigableSet;
import java.util.Objects;
import java.util.stream.Collectors;
@RequiredArgsConstructor
class BeanSpec {
@Getter
private final Class<?> cls;
@Getter
private final String name;
@Getter
private final NavigableSet<Annotation> qualifiers;
@Override
public String toString() {
return String.format("{class: %s, name: '%s', qualifiers: [%s]}",
cls.getName(), name,
qualifiers.stream()
.map(Annotation::getClass)
.map(Class::getName)
.collect(Collectors.joining(", ")));
}
public static Comparator<Annotation> annotationComparator =
Comparator.<Annotation, String>comparing(it -> it.getClass().getName())
.thenComparing(Annotation::hashCode)
.thenComparing((ann1, ann2) -> {
if(Objects.equals(ann1, ann2)) return 0;
else {
return Integer.compare(System.identityHashCode(ann1), System.identityHashCode(ann2));
}
});
public static Comparator<Class<?>> classComparator = Comparator.comparing(Class::getName);
public static Comparator<BeanSpec> specComparator = Comparator.comparing(BeanSpec::getCls, classComparator)
.thenComparing(BeanSpec::getQualifiers,
new LexicographicIterableComparator<>(annotationComparator))
.thenComparing(BeanSpec::getName);
public static NavigableSet<Annotation> emptyQualifier = Collections.emptyNavigableSet();
}

View File

@@ -0,0 +1,13 @@
package net.woggioni.wdi.api;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Import {
Class<? extends BeanFactory>[] value() default {};
}

View File

@@ -0,0 +1,62 @@
package net.woggioni.wdi.api;
import net.woggioni.jwo.JWO;
import java.util.*;
class ParentIterator implements Iterator<Class<?>> {
static Class<?>[] superClasses(Class<?> cls) {
Class<?>[] interfaces = cls.getInterfaces();
Class<?>[] result;
int i = 0;
Class<?> superclass = cls.getSuperclass();
if(superclass != null) {
result = new Class<?>[interfaces.length + 1];
result[i++] = superclass;
} else {
result = new Class<?>[interfaces.length ];
}
System.arraycopy(interfaces, 0, result, i, interfaces.length);
return result;
}
private final List<Iterator<Class<?>>> stack;
private final Set<Class<?>> resultCache;
private Class<?> nextValue;
ParentIterator(Class<?> cls) {
stack = new ArrayList<>();
stack.add(JWO.iterator(superClasses(cls)));
resultCache = new HashSet<>();
nextValue = computeNext();
}
private Class<?> computeNext() {
while(!stack.isEmpty()) {
Iterator<Class<?>> last = JWO.tail(stack);
if (last.hasNext()) {
Class<?> next = last.next();
if(resultCache.add(next)) {
stack.add(JWO.iterator(superClasses(next)));
return next;
}
} else {
JWO.pop(stack);
}
}
return null;
}
@Override
public boolean hasNext() {
return nextValue != null;
}
@Override
public Class<?> next() {
Class<?> result = nextValue;
nextValue = computeNext();
return result;
}
}

View File

@@ -0,0 +1,12 @@
package net.woggioni.wdi.api;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Qualifier {
}

View File

@@ -0,0 +1,11 @@
package net.woggioni.wdi.api;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Startup {
}