com.github.leeonky.jfactory.SpecClassFactory Maven / Gradle / Ivy
package com.github.leeonky.jfactory;
import com.github.leeonky.util.BeanClass;
import com.github.leeonky.util.Converter;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Stream;
import static com.github.leeonky.util.Classes.newInstance;
import static com.github.leeonky.util.Suppressor.run;
class SpecClassFactory extends ObjectFactory {
private final Class extends Spec> specClass;
private final Supplier> base;
public SpecClassFactory(Class extends Spec> specClass, FactorySet factorySet, boolean globalSpec) {
super(BeanClass.create(newInstance(specClass).getType()), factorySet);
this.specClass = specClass;
base = guessBaseFactory(factorySet, globalSpec);
registerTraits();
constructor(getBase()::create);
}
private Supplier> guessBaseFactory(FactorySet factorySet, boolean globalSpec) {
if (!globalSpec)
return () -> factorySet.queryObjectFactory(getType());
ObjectFactory typeBaseFactory = factorySet.queryObjectFactory(getType()); // DO NOT INLINE
return () -> typeBaseFactory;
}
@Override
protected Spec createSpec() {
return newInstance(specClass);
}
private void registerTraits() {
Stream.of(specClass.getMethods())
.filter(this::isTraitMethod)
.forEach(method -> spec(getTraitName(method), instance -> run(() ->
method.invoke(instance.spec(), convertParams(method, instance.traitParams())))));
}
private Object[] convertParams(Method method, Object[] traitParams) {
return new ArrayList
© 2015 - 2025 Weber Informatics LLC | Privacy Policy