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 {
}

View File

@@ -0,0 +1,330 @@
package net.woggioni.wdi.api;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Arrays;
import java.util.List;
public class BeanContainerTest {
public static class Test1 {
@RequiredArgsConstructor
public static class Foo {
public final String s;
}
public static class Configuration implements BeanFactory {
@Override
public List<Class<?>> getClasses() {
return Arrays.asList(Foo.class);
}
@Bean
public String bar() {
return "bar";
}
}
@Test
public void test() {
BeanContainer container = BeanContainer.with(Configuration.class);
Foo foo = container.getBean(Foo.class);
System.out.println(foo.s);
}
}
public static class Test2 {
public static class Foo {}
@RequiredArgsConstructor
public static class Bar {
public final Foo foo;
}
public static class Configuration implements BeanFactory {
@Override
public List<Class<?>> getClasses() {
return Arrays.asList(Foo.class);
}
@Bean
public Bar bar(Foo foo) {
return new Bar(foo);
}
}
@Test
public void test() {
BeanContainer container = BeanContainer.with(Configuration.class);
Foo foo = container.getBean(Foo.class);
Bar bar = container.getBean(Bar.class);
Assertions.assertSame(foo, bar.foo);
}
}
public static class Test3 {
public static class Foo {}
@RequiredArgsConstructor
public static class Bar {
public final Foo foo;
}
@BeanClasses(Foo.class)
public static class Configuration implements BeanFactory {
@Bean
public Bar bar(Foo foo) {
return new Bar(foo);
}
}
@Test
public void test() {
BeanContainer container = BeanContainer.with(Configuration.class);
Foo foo = container.getBean(Foo.class);
Bar bar = container.getBean(Bar.class);
Assertions.assertSame(foo, bar.foo);
}
}
public static class Test4 {
public static class Foo {}
@RequiredArgsConstructor
public static class Bar {
public final Foo foo;
}
@Import(AnotherConfiguration.class)
public static class Configuration implements BeanFactory {
@Bean
public Bar bar(Foo foo) {
return new Bar(foo);
}
}
@BeanClasses(Foo.class)
public static class AnotherConfiguration implements BeanFactory { }
@Test
public void test() {
BeanContainer container = BeanContainer.with(Configuration.class);
Foo foo = container.getBean(Foo.class);
Bar bar = container.getBean(Bar.class);
Assertions.assertSame(foo, bar.foo);
}
}
public static class Test5 {
@RequiredArgsConstructor
public static class Person {
final String name;
final String surname;
final int age;
}
@RequiredArgsConstructor
public static class People {
final Person bob;
final Person john;
}
public static class People2 {
public People2(@Bean(name = "bob") Person p1, @Bean(name = "john") Person p2) {
this.bob = p1;
this.john = p2;
}
final Person bob;
final Person john;
}
public static class People3 {
public People3(Person bob, Person john) {
this.bob = bob;
this.john = john;
}
final Person bob;
final Person john;
}
@BeanClasses({People.class, People2.class, People3.class})
public static class Configuration implements BeanFactory {
@Bean
public Person bob() {
return new Person("Bob", "Kennedy", 42);
}
@Bean
public Person john() {
return new Person("John", "Kennedy", 46);
}
}
@Test
public void test() {
BeanContainer container = BeanContainer.with(Configuration.class);
Person bob = container.getBean(Person.class, "bob");
Person john = container.getBean(Person.class, "john");
Assertions.assertEquals("Bob", bob.name);
Assertions.assertEquals("John", john.name);
People people = container.getBean(People.class);
Assertions.assertSame(people.bob, bob);
Assertions.assertSame(people.john, john);
People2 people2 = container.getBean(People2.class);
Assertions.assertSame(people2.bob, bob);
Assertions.assertSame(people2.john, john);
People3 people3 = container.getBean(People3.class);
Assertions.assertSame(people3.bob, bob);
Assertions.assertSame(people3.john, john);
}
}
public static class Test6 {
@Qualifier
@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Bob {}
@Qualifier
@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface John {}
@RequiredArgsConstructor
public static class Person {
final String name;
final String surname;
final int age;
}
public static class People {
public People(@Bob Person p1, @John Person p2) {
this.bob = p1;
this.john = p2;
}
final Person bob;
final Person john;
}
@BeanClasses(People.class)
public static class Configuration implements BeanFactory {
@Bean
@Bob
public Person bob() {
return new Person("Bob", "Kennedy", 42);
}
@John
@Bean
public Person john() {
return new Person("John", "Kennedy", 46);
}
}
@Test
@Bob
@John
@SneakyThrows
public void test() {
BeanContainer container = BeanContainer.with(Configuration.class);
Bob b = Configuration.class.getMethod("bob").getAnnotation(Bob.class);
Person bob = container.getBean(Person.class, null, b);
John j = Configuration.class.getMethod("john").getAnnotation(John.class);
Person john = container.getBean(Person.class, null, j);
Assertions.assertEquals("Bob", bob.name);
Assertions.assertEquals("John", john.name);
People people = container.getBean(People.class);
Assertions.assertSame(people.bob, bob);
Assertions.assertSame(people.john, john);
}
}
public static class Test7 {
@Qualifier
@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Named {
String name() default "";
}
@RequiredArgsConstructor
public static class Person {
final String name;
final String surname;
final int age;
}
public static class People {
public People(@Named(name = "bob") Person p1, @Named(name = "john") Person p2) {
this.bob = p1;
this.john = p2;
}
final Person bob;
final Person john;
}
@BeanClasses({People.class})
public static class Configuration implements BeanFactory {
@Bean
@Named(name = "bob")
public Person bob() {
return new Person("Bob", "Kennedy", 42);
}
@Bean
@Named(name = "john")
public Person john() {
return new Person("John", "Kennedy", 46);
}
}
@Test
@SneakyThrows
public void test() {
BeanContainer container = BeanContainer.with(Configuration.class);
Named b = Configuration.class.getMethod("bob").getAnnotation(Named.class);
Named j = Configuration.class.getMethod("john").getAnnotation(Named.class);
Person bob = container.getBean(Person.class, null, b);
Person john = container.getBean(Person.class, null, j);
Assertions.assertEquals("Bob", bob.name);
Assertions.assertEquals("John", john.name);
People people = container.getBean(People.class);
Assertions.assertSame(people.bob, bob);
Assertions.assertSame(people.john, john);
}
}
}

View File

@@ -0,0 +1,30 @@
package net.woggioni.wdi.api;
import net.woggioni.jwo.CollectionUtils;
import net.woggioni.jwo.JWO;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class ParentIteratorTest {
@Test
public void test() {
List<Class<?>> expectedClasses = CollectionUtils.immutableList(
java.util.AbstractList.class,
java.util.AbstractCollection.class,
Object.class,
java.util.Collection.class,
java.lang.Iterable.class,
java.util.List.class,
java.util.RandomAccess.class,
java.lang.Cloneable.class,
java.io.Serializable.class
);
Assertions.assertEquals(expectedClasses,
JWO.iterator2Stream(new ParentIterator(ArrayList.class)).collect(Collectors.toList()));
}
}